Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A MODX Snippet to show active, logged in users
<li>[[+username]]</li>
<?php
/**
* Shows a list of active, signed-on users
*/
/* snippet options */
$tpl = $modx->getOption('tpl',$scriptProperties,'ActiveUser');
$sortBy = $modx->getOption('sortBy',$_REQUEST,$modx->getOption('sortBy',$scriptProperties,'username'));
$sortDir = $modx->getOption('sortDir',$_REQUEST,$modx->getOption('sortBy',$scriptProperties,'DESC'));
$limit = $modx->getOption('limit',$_REQUEST,$modx->getOption('limit',$scriptProperties,10));
$offset = $modx->getOption('offset',$_REQUEST,$modx->getOption('offset',$scriptProperties,0));
$placeholderPrefix = $modx->getOption('placeholderPrefix',$scriptProperties,'au.');
/* build query */
$c = $modx->newQuery('modUser');
$c->innerJoin('modUserProfile','Profile');
$c->innerJoin('modSession','Session','Session.id = Profile.sessionid');
$c->where(array(
'Profile.blocked' => false,
'modUser.active' => true,
));
$total = $modx->getCount('modUser',$c);
if (!empty($limit)) {
$c->limit($limit,$offset);
}
$c->sortby($sortBy,$sortDir);
$users = $modx->getCollection('modUser',$c);
$list = array();
foreach ($users as $user) {
$list[] = $modx->getChunk($tpl,$user->toArray());
}
$modx->setPlaceholder($placeholderPrefix.'total',$total);
$modx->setPlaceholder($placeholderPrefix.'offset',$offset);
$modx->setPlaceholder($placeholderPrefix.'limit',$limit);
$outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,"\n");
$output = implode($outputSeparator,$list);
$toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,'');
if (!empty($toPlaceholder)) {
$modx->toPlaceholder($toPlaceholder,$output);
return '';
}
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.