ActiveUsers.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Login
  4. *
  5. * Copyright 2010 by Shaun McCormick <shaun+login@modx.com>
  6. *
  7. * Login is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Login is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package login
  21. */
  22. /**
  23. * Shows a list of active, logged-in users
  24. *
  25. * @package login
  26. * @subpackage controllers
  27. */
  28. class LoginActiveUsersController extends LoginController {
  29. /** @var string $username */
  30. public $username;
  31. /** @var string $password */
  32. public $password;
  33. /** @var modUser $user */
  34. public $user;
  35. public function initialize() {
  36. $this->setDefaultProperties(array(
  37. 'tpl' => 'lgnActiveUser',
  38. 'sortBy' => 'username',
  39. 'sortDir' => 'DESC',
  40. 'limit' => 10,
  41. 'offset' => 0,
  42. 'placeholderPrefix' => 'au.',
  43. 'outputSeparator' => "\n",
  44. 'toPlaceholder' => '',
  45. ));
  46. }
  47. /**
  48. * Run the controller and return the output
  49. * @return string
  50. */
  51. public function process() {
  52. $users = $this->getUsers();
  53. $list = $this->iterate($users);
  54. return $this->output($list);
  55. }
  56. /**
  57. * Get the active user objects
  58. * @return array
  59. */
  60. public function getUsers() {
  61. $placeholderPrefix = rtrim($this->getProperty('placeholderPrefix', 'au.'), '.');
  62. $classKey = $this->getProperty('classKey','modUser');
  63. $sortBy = $this->modx->getOption('sortBy',$_REQUEST,$this->getProperty('sortBy','username'));
  64. $sortDir = $this->modx->getOption('sortDir',$_REQUEST,$this->getProperty('sortDir','DESC'));
  65. $limit = $this->modx->getOption('limit',$_REQUEST,$this->getProperty('limit',10,'isset'));
  66. $offset = $this->modx->getOption('offset',$_REQUEST,$this->getProperty('offset',0));
  67. $c = $this->modx->newQuery($classKey);
  68. $c->innerJoin('modUserProfile','Profile');
  69. $c->innerJoin('modSession','Session','Session.id = Profile.sessionid');
  70. $c->where(array(
  71. 'Profile.blocked' => false,
  72. $classKey.'.active' => true,
  73. ));
  74. $total = $this->modx->getCount($classKey,$c);
  75. $c->select($this->modx->getSelectColumns($classKey,$classKey));
  76. $c->select($this->modx->getSelectColumns('modUserProfile','Profile','',array('id'),true));
  77. if (!empty($limit)) {
  78. $c->limit($limit,$offset);
  79. }
  80. $c->sortby($sortBy,$sortDir);
  81. $users = $this->modx->getCollection($classKey,$c);
  82. $this->modx->toPlaceholders(array(
  83. 'total' => $total,
  84. 'offset' => $offset,
  85. 'limit' => $limit
  86. ), $placeholderPrefix);
  87. return $users;
  88. }
  89. /**
  90. * Iterate and template each user
  91. *
  92. * @param array $users
  93. * @return array
  94. */
  95. public function iterate(array $users = array()) {
  96. $tpl = $this->getProperty('tpl','ActiveUser');
  97. $tplType = $this->getProperty('tplType','modChunk');
  98. $list = array();
  99. $idx = 0;
  100. /** @var modUser $user */
  101. foreach ($users as $user) {
  102. $userArray = $user->toArray();
  103. $userArray['idx'] = $idx;
  104. $userArray['alt'] = $idx % 2;
  105. $list[] = $this->login->getChunk($tpl,$userArray,$tplType);
  106. $idx++;
  107. }
  108. return $list;
  109. }
  110. /**
  111. * Output the data
  112. * @param array $list
  113. * @return string
  114. */
  115. public function output(array $list = array()) {
  116. $outputSeparator = $this->getProperty('outputSeparator',"\n",'isset');
  117. $output = implode($outputSeparator,$list);
  118. $toPlaceholder = $this->getProperty('toPlaceholder','','isset');
  119. if (!empty($toPlaceholder)) {
  120. $this->modx->toPlaceholder($toPlaceholder,$output);
  121. return '';
  122. }
  123. return $output;
  124. }
  125. }
  126. return 'LoginActiveUsersController';