Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Last active January 31, 2020 08:39
Show Gist options
  • Save cgi-caesar/87f51460f972b7999cc82764ef56b2f8 to your computer and use it in GitHub Desktop.
Save cgi-caesar/87f51460f972b7999cc82764ef56b2f8 to your computer and use it in GitHub Desktop.
aMember Plugin: Admin Widget Users of Product
<?php
/**
* @title Admin Widget Users of Product
* @desc widget display list of recent users of product
* @path application/default/plugins/misc/admin-widget-user-product.php
*/
class Am_Plugin_AdminWidgetUserProduct extends Am_Plugin
{
const PLUGIN_STATUS = self::STATUS_PRODUCTION;
const PLUGIN_COMM = self::COMM_COMMERCIAL;
const PLUGIN_REVISION = '@@VERSION@@';
protected $_configPrefix = 'misc.';
function onLoadAdminDashboardWidgets(Am_Event $e)
{
$e->addReturn(new Am_AdminDashboardWidget(
$this->getId(),
___('Users of Product'),
[$this, 'renderWidget'],
Am_AdminDashboardWidget::TARGET_ANY,
[$this, 'createWidgetConfigForm']
));
}
public function createWidgetConfigForm()
{
$form = new Am_Form_Admin();
$form->addInteger('num')
->setLabel(___('Number of Users to display'))
->setValue(5);
$form->addSelect('pid')
->loadOptions($this->getDi()->productTable->getOptions())
->setLabel('Product');
return $form;
}
function renderWidget(Am_View $view, $config = null)
{
$num = is_null($config) ? 5 : $config['num'];
if (empty($config['pid'])) {
return <<<CUT
<div class="widget-nodata">
<h2>Users of Product</h2>
<p>widget is not configured</p>
</div>
CUT;
}
$product = $this->getDi()->productTable->load($config['pid']);
$users = $this->getDi()->userTable->selectObjects(
"SELECT DISTINCT u.* FROM ?_user u LEFT JOIN ?_access a USING (user_id) WHERE a.product_id=? ORDER BY a.begin_date DESC LIMIT ?d;",
$config['pid'],
$num
);
if (!$users) {
return <<<CUT
<div>
<h2>Last {$num} Users of {$product->title}</h2>
<div class="grid-container am-grid-container">
<div style="padding: 1em">There is not active users with product <strong>{$product->title}</strong></div>
</div>
</div>
CUT;
}
$url = $this->getDi()->url("admin-users", "_u_search[-payments-active][product_ids][]={$product->pk()}");
$out = '';
$i = 1;
foreach($users as $user) {
$out .= sprintf('<tr class="grid-row am-grid-row %s"><td><a href="%s" class="link">%s &lt;%s&gt;</a></td></tr>',
$i++%2 ? 'even' : 'odd',
$view->userUrl($user->pk()),
$view->escape($user->getName()),
$view->escape($user->email)
);
}
return sprintf(<<<CUT
<div>
<a href="{$url}" class="link" style="float:right">All Users</a>
<h2>Last {$num} Users of {$product->title}</h2>
<div class="grid-container am-grid-container">
<table class="grid grid-no-highlight am-grid am-grid-no-highlight">
%s
</table>
</div>
</div>
CUT
, $out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment