Userlist.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Forum\Forum;
  13. use InvalidArgumentException;
  14. use function \ForkBB\__;
  15. class Userlist extends Page
  16. {
  17. /**
  18. * Возвращает список доступных групп
  19. */
  20. protected function getgroupList(): array
  21. {
  22. $list = [
  23. 'all' => __('All users'),
  24. ];
  25. foreach ($this->c->groups->getList() as $group) {
  26. if (! $group->groupGuest) {
  27. $list[$group->g_id] = $group->g_title;
  28. }
  29. }
  30. return $list;
  31. }
  32. /**
  33. * Список пользователей
  34. */
  35. public function view(array $args, string $method): Page
  36. {
  37. $this->c->Lang->load('validator');
  38. $this->c->Lang->load('userlist');
  39. $prefix = 'POST' === $method ? 'required|' : '';
  40. $v = $this->c->Validator->reset()
  41. ->addRules([
  42. 'sort' => $prefix . 'string|in:username,registered' . ($this->user->showPostCount ? ',num_posts' : ''),
  43. 'dir' => $prefix . 'string|in:ASC,DESC',
  44. 'group' => $prefix . 'string|in:' . \implode(',', \array_keys($this->groupList)),
  45. 'name' => $prefix . 'string|min:1|max:25' . ($this->user->searchUsers ? '' : '|in:*'),
  46. ]);
  47. $error = true;
  48. if ($v->validation('POST' === $method ? $_POST : $args)) {
  49. $count = (int) (null === $v->sort)
  50. + (int) (null === $v->dir)
  51. + (int) (null === $v->group)
  52. + (int) (null === $v->name);
  53. if (
  54. 0 === $count
  55. || 4 === $count
  56. ) {
  57. $error = false;
  58. }
  59. }
  60. if ($error) {
  61. return $this->c->Message->message('Bad request');
  62. }
  63. if ('POST' === $method) {
  64. return $this->c->Redirect->page('Userlist', $v->getData());
  65. }
  66. $filters = [];
  67. if (\is_numeric($v->group)) {
  68. $filters['group_id'] = ['=', $v->group];
  69. } else {
  70. $filters['group_id'] = ['!=', 0];
  71. }
  72. if (null !== $v->name) {
  73. $filters['username'] = ['LIKE', $v->name];
  74. if (
  75. \preg_match('%[\x80-\xFF]%', $v->name)
  76. && ! $this->c->config->insensitive()
  77. ) {
  78. $this->fIswev = ['i', 'The search may be case sensitive'];
  79. }
  80. }
  81. $order = $v->sort ? [$v->sort => $v->dir] : [];
  82. $ids = $this->c->users->filter($filters, $order);
  83. $number = \count($ids);
  84. $page = $args['page'] ?? 1;
  85. $pages = (int) \ceil(($number ?: 1) / $this->c->config->i_disp_users);
  86. if ($page > $pages) {
  87. return $this->c->Message->message('Not Found', true, 404);
  88. }
  89. if ($number) {
  90. $this->startNum = ($page - 1) * $this->c->config->i_disp_users;
  91. $ids = \array_slice($ids, $this->startNum, $this->c->config->i_disp_users);
  92. $this->userList = $this->c->users->loadByIds($ids);
  93. $links = [];
  94. $vars = ['page' => $page];
  95. if (4 === $count) {
  96. $vars['group'] = 'all';
  97. $vars['name'] = '*';
  98. } else {
  99. $vars['group'] = $v->group;
  100. $vars['name'] = $v->name;
  101. }
  102. $this->activeLink = 0;
  103. foreach (['username', 'num_posts', 'registered'] as $i => $sort) {
  104. $vars['sort'] = $sort;
  105. foreach (['ASC', 'DESC'] as $j => $dir) {
  106. $vars['dir'] = $dir;
  107. $links[$i * 2 + $j] = $this->c->Router->link('Userlist', $vars);
  108. if (
  109. $v->sort === $sort
  110. && $v->dir === $dir
  111. ) {
  112. $this->activeLink = $i * 2 + $j;
  113. }
  114. }
  115. }
  116. $this->links = $links;
  117. } else {
  118. $this->startNum = 0;
  119. $this->userList = null;
  120. $this->links = [null, null, null, null, null, null];
  121. $this->fIswev = ['i', 'No users found'];
  122. }
  123. $this->fIndex = self::FI_USERS;
  124. $this->nameTpl = 'userlist';
  125. $this->onlinePos = 'userlist';
  126. $this->canonical = $this->c->Router->link('Userlist', $args);
  127. $this->robots = 'noindex';
  128. $this->crumbs = $this->crumbs(
  129. [
  130. $this->c->Router->link('Userlist'),
  131. 'User list',
  132. ]
  133. );
  134. $this->pagination = $this->c->Func->paginate($pages, $page, 'Userlist', $args);
  135. $this->form = $this->formUserlist($v);
  136. return $this;
  137. }
  138. /**
  139. * Подготавливает массив данных для формы
  140. */
  141. protected function formUserlist(Validator $v): array
  142. {
  143. $form = [
  144. 'action' => $this->c->Router->link('Userlist'),
  145. 'hidden' => [],
  146. 'sets' => [],
  147. 'btns' => [
  148. 'submit' => [
  149. 'type' => 'submit',
  150. 'value' => __($this->user->searchUsers ? 'Search btn' : 'Submit'),
  151. ],
  152. ],
  153. ];
  154. $fields = [];
  155. if ($this->user->searchUsers) {
  156. $fields['name'] = [
  157. 'class' => ['w0'],
  158. 'type' => 'text',
  159. 'maxlength' => '25',
  160. 'value' => $v->name ?: '*',
  161. 'caption' => 'Username',
  162. 'help' => 'User search info',
  163. 'required' => true,
  164. # 'autofocus' => true,
  165. ];
  166. } else {
  167. $form['hidden']['name'] = '*';
  168. }
  169. $fields['group'] = [
  170. 'class' => ['w4'],
  171. 'type' => 'select',
  172. 'options' => $this->groupList,
  173. 'value' => $v->group,
  174. 'caption' => 'User group',
  175. ];
  176. $fields['sort'] = [
  177. 'class' => ['w4'],
  178. 'type' => 'select',
  179. 'options' => [
  180. ['username', __('Sort by name')],
  181. ['num_posts', __('Sort by number'), $this->user->showPostCount ? null : true],
  182. ['registered', __('Sort by date')],
  183. ],
  184. 'value' => $v->sort,
  185. 'caption' => 'Sort users by',
  186. ];
  187. $fields['dir'] = [
  188. 'class' => ['w4'],
  189. 'type' => 'radio',
  190. 'value' => $v->dir ?: 'ASC',
  191. 'values' => [
  192. 'ASC' => __('Ascending'),
  193. 'DESC' => __('Descending'),
  194. ],
  195. 'caption' => 'User sort order',
  196. ];
  197. $form['sets']['users'] = ['fields' => $fields];
  198. return $form;
  199. }
  200. }