snippet.getonlineusers.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /** @var array $scriptProperties */
  3. /** @var UsersOnline $UsersOnline */
  4. if (!$UsersOnline = $modx->getService('usersonline', 'UsersOnline', $modx->getOption('usersonline_core_path', null,
  5. $modx->getOption('core_path') . 'components/usersonline/') . 'model/usersonline/', $scriptProperties)
  6. ) {
  7. return 'Could not load UsersOnline class!';
  8. }
  9. if (!$pdo = $modx->getService('pdoTools')) {
  10. return $modx->lexicon('no_pdo');
  11. }
  12. $interval = $modx->getOption('timeInterval', $scriptProperties, -1);
  13. if ($interval == -1) {
  14. $interval = $modx->getOption('usersonline_time_span');
  15. }
  16. $contexts = $modx->getOption('contexts', $scriptProperties, null);
  17. $innerJoin = $modx->getOption('innerJoin', $scriptProperties, '');
  18. $innerJoin = $modx->fromJSON($innerJoin);
  19. $innerJoin['UsersOnline'] = array(
  20. 'class' => 'userOnline',
  21. 'on' => 'modUser.id = UsersOnline.user_id',
  22. );
  23. $select = $modx->getOption('select', $scriptProperties, '');
  24. $select = $modx->fromJSON($select);
  25. $select['UsersOnline'] = '*';
  26. $time = time();
  27. $startTime = $time - $interval;
  28. $where = $modx->getOption('where', $scriptProperties, '');
  29. $where = $modx->fromJSON($where);
  30. $where[] = array(
  31. 'UsersOnline.lastvisit:>=' => $startTime,
  32. 'UsersOnline.lastvisit:<=' => $time,
  33. );
  34. $contextsArray = array();
  35. if($contexts != null){
  36. $contextsArray = explode(',', $contexts);
  37. }
  38. if (!empty($contextsArray)) {
  39. $where[] = array(
  40. 'UsersOnline.context_key:IN' => $contextsArray,
  41. );
  42. }
  43. $scriptProperties['where'] = $modx->toJSON($where);
  44. $scriptProperties['innerJoin'] = $modx->toJSON($innerJoin);
  45. $scriptProperties['select'] = $modx->toJSON($select);
  46. $output = $modx->runSnippet('pdoUsers', $scriptProperties);
  47. return $output;