Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Created April 29, 2017 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgi-caesar/153e4b095e2464da164c94c22cac520d to your computer and use it in GitHub Desktop.
Save cgi-caesar/153e4b095e2464da164c94c22cac520d to your computer and use it in GitHub Desktop.
aMember: example of plugin that add new widget to user dashboard
<?php
class Am_Plugin_BlockExpireDate extends Am_Plugin
{
const PLUGIN_STATUS = self::STATUS_PRODUCTION;
const PLUGIN_COMM = self::COMM_COMMERCIAL;
const PLUGIN_REVISION = '@@VERSION@@';
const DEFAULT_TPL = '<div class="am-info">Greetings %user.name_f% &mdash; your subscription expires on %expire|date%</div>';
protected $_configPrefix = 'misc.';
function _initSetupForm(Am_Form_Setup $form)
{
$form->setTitle(___('Expire Date Block'));
$form->addMagicSelect('pids')
->setLabel(___("Products\nuser with specified products will see a block"))
->loadOptions($this->getDi()->productTable->getProductOptions())
->addRule('required');
$form->addHtmlEditor('tmpl', null, array('showInPopup' => true))
->setLabel('Widget Template');
$form->setDefault('tmpl', self::DEFAULT_TPL);
}
function isConfigured()
{
return (bool) $this->getConfig('pids');
}
function hasAccess(User $user)
{
if (!$pids = $this->getConfig('pids')) return false;
return (bool) array_intersect($user->getActiveProductIds(), $this->getDi()->productTable->extractProductIds($pids));
}
function getExpire(User $user)
{
return $user->getExpire($this->getDi()->productTable->extractProductIds($this->getConfig('pids')));
}
function renderBlock(Am_View $view)
{
$user = $this->getDi()->user;
$expire = $this->getExpire($user);
$tpl = new Am_SimpleTemplate;
$tpl->assignStdVars();
$tpl->assign(array(
'user' => $user,
'expire' => $expire
));
return $tpl->render($this->getConfig('tmpl', self::DEFAULT_TPL));
}
function onInitFinished(Am_Event $e)
{
if ($this->getDi()->auth->getUserId() &&
$this->hasAccess($this->getDi()->user)) {
$this->getDi()->blocks->remove('member-main-resources');
$this->getDi()->blocks->add(new Am_Block('member/main/top', null, 'member-expire-date', $this, array($this, 'renderBlock')));
}
}
}
@tarikhagustia
Copy link

Very usefull

@cgi-caesar
Copy link
Author

@tarikhagustia Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment