Created
April 29, 2017 07:58
-
-
Save cgi-caesar/153e4b095e2464da164c94c22cac520d to your computer and use it in GitHub Desktop.
aMember: example of plugin that add new widget to user dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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% — 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'))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very usefull