View.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace ForkBB\Models\Pages\Admin\Users;
  3. use ForkBB\Core\Validator;
  4. use ForkBB\Models\Page;
  5. use ForkBB\Models\Pages\Admin\Users;
  6. use function \ForkBB\__;
  7. class View extends Users
  8. {
  9. /**
  10. * Генерирует список доступных групп пользователей
  11. *
  12. * @param bool $onlyKeys
  13. *
  14. * @return array
  15. */
  16. protected function groups(bool $onlyKeys = false): array
  17. {
  18. $groups = [
  19. -1 => __('All groups'),
  20. 0 => __('Unverified users'),
  21. ];
  22. foreach ($this->c->groups->getList() as $group) {
  23. if (! $group->groupGuest) {
  24. $groups[$group->g_id] = $group->g_title;
  25. }
  26. }
  27. return $onlyKeys ? \array_keys($groups) : $groups;
  28. }
  29. /**
  30. * Подготавливает данные для шаблона поиска пользователей
  31. *
  32. * @param array $args
  33. * @param string $method
  34. * @param array $data
  35. *
  36. * @return Page
  37. */
  38. public function view(array $args, string $method, array $data = []): Page
  39. {
  40. if ('POST' === $method) {
  41. $v = $this->c->Validator->reset()
  42. ->addRules([
  43. 'token' => 'token:AdminUsers',
  44. 'ip' => 'required',
  45. ]);
  46. if ($v->validation($_POST)) {
  47. $ip = \filter_var($v->ip, \FILTER_VALIDATE_IP);
  48. if (false === $ip) {
  49. $this->fIswev = ['v', __('Bad IP message')];
  50. $data = $v->getData();
  51. } else {
  52. return $this->c->Redirect->page('AdminUsersResult', ['data' => $this->encodeData($ip)]);
  53. }
  54. } else {
  55. $v = $this->c->Validator->reset()
  56. ->addValidators([
  57. ])->addRules([
  58. 'token' => 'token:AdminUsers',
  59. 'username' => 'string|max:25',
  60. 'email' => 'string|max:80',
  61. 'title' => 'string|max:50',
  62. 'realname' => 'string|max:40',
  63. 'gender' => 'integer|in:0,1,2',
  64. 'url' => 'string|max:100',
  65. 'location' => 'string|max:30',
  66. 'signature' => 'string|max:512',
  67. 'admin_note' => 'string|max:30',
  68. 'num_posts_1' => 'integer|min:0|max:9999999999',
  69. 'num_posts_2' => 'integer|min:0|max:9999999999',
  70. 'last_post_1' => 'date',
  71. 'last_post_2' => 'date',
  72. 'last_visit_1' => 'date',
  73. 'last_visit_2' => 'date',
  74. 'registered_1' => 'date',
  75. 'registered_2' => 'date',
  76. 'order_by' => 'required|string|in:username,email,num_posts,last_post,last_visit,registered',
  77. 'direction' => 'required|string|in:ASC,DESC',
  78. 'user_group' => 'required|integer|in:' . \implode(',', $this->groups(true)),
  79. ])->addAliases([
  80. 'username' => 'Username label',
  81. 'email' => 'E-mail address label',
  82. 'title' => 'Title label',
  83. 'realname' => 'Real name label',
  84. 'gender' => 'Gender label',
  85. 'url' => 'Website label',
  86. 'location' => 'Location label',
  87. 'signature' => 'Signature label',
  88. 'admin_note' => 'Admin note label',
  89. 'num_posts_1' => 'Posts label',
  90. 'num_posts_2' => 'Posts label',
  91. 'last_post_1' => 'Last post label',
  92. 'last_post_2' => 'Last post label',
  93. 'last_visit_1' => 'Last visit label',
  94. 'last_visit_2' => 'Last visit label',
  95. 'registered_1' => 'Registered label',
  96. 'registered_2' => 'Registered label',
  97. 'order_by' => 'Order by label',
  98. # 'direction' => ,
  99. 'user_group' => 'User group label',
  100. ])->addArguments([
  101. ])->addMessages([
  102. ]);
  103. if ($v->validation($_POST)) {
  104. return $this->c->Redirect->page('AdminUsersResult', ['data' => $this->encodeData($v->getData())]);
  105. }
  106. $this->fIswev = $v->getErrors();
  107. $data = $v->getData();
  108. }
  109. }
  110. $this->nameTpl = 'admin/users';
  111. $this->formSearch = $this->form($data);
  112. if ($this->c->userRules->viewIP) {
  113. $this->formIP = $this->formIP($data);
  114. }
  115. if ($this->user->isAdmin) {
  116. $this->formNew = $this->formNew();
  117. $this->formRecalculate = $this->formRecalculate();
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Создает массив данных для кнопки добавления пользователя
  123. *
  124. * @return array
  125. */
  126. protected function formNew(): array
  127. {
  128. $form = [
  129. 'action' => $this->c->Router->link('AdminUsers'),
  130. 'hidden' => [],
  131. 'sets' => [
  132. 'new' => [
  133. 'legend' => __('New user'),
  134. 'fields' => [],
  135. ]
  136. ],
  137. 'btns' => [
  138. 'new' => [
  139. 'type' => 'btn',
  140. 'value' => __('Add'),
  141. 'link' => $this->c->Router->link('AdminUsersNew'),
  142. 'accesskey' => 'n',
  143. ],
  144. ],
  145. ];
  146. return $form;
  147. }
  148. /**
  149. * Создает массив данных для формы поиска
  150. *
  151. * @param array $data
  152. *
  153. * @return array
  154. */
  155. protected function form(array $data): array
  156. {
  157. $form = [
  158. 'action' => $this->c->Router->link('AdminUsers'),
  159. 'hidden' => [
  160. 'token' => $this->c->Csrf->create('AdminUsers'),
  161. ],
  162. 'sets' => [],
  163. 'btns' => [
  164. 'search' => [
  165. 'type' => 'submit',
  166. 'value' => __('Submit search'),
  167. 'accesskey' => 's',
  168. ],
  169. ],
  170. ];
  171. $form['sets']['search-info'] = [
  172. 'info' => [
  173. 'info1' => [
  174. 'type' => '', //????
  175. 'value' => __('User search info'),
  176. ],
  177. ],
  178. ];
  179. $fields = [];
  180. $fields['username'] = [
  181. 'type' => 'text',
  182. 'maxlength' => 25,
  183. 'caption' => __('Username label'),
  184. 'value' => $data['username'] ?? null,
  185. ];
  186. $fields['email'] = [
  187. 'type' => 'text',
  188. 'maxlength' => 80,
  189. 'caption' => __('E-mail address label'),
  190. 'value' => $data['email'] ?? null,
  191. ];
  192. $fields['title'] = [
  193. 'type' => 'text',
  194. 'maxlength' => 50,
  195. 'caption' => __('Title label'),
  196. 'value' => $data['title'] ?? null,
  197. ];
  198. $fields['realname'] = [
  199. 'type' => 'text',
  200. 'maxlength' => 40,
  201. 'caption' => __('Real name label'),
  202. 'value' => $data['realname'] ?? null,
  203. ];
  204. $genders = [
  205. 0 => __('Do not display'),
  206. 1 => __('Male'),
  207. 2 => __('Female'),
  208. ];
  209. $fields['gender'] = [
  210. # 'class' => 'block',
  211. 'type' => 'radio',
  212. 'value' => $data['gender'] ?? -1,
  213. 'values' => $genders,
  214. 'caption' => __('Gender label'),
  215. ];
  216. $fields['url'] = [
  217. 'id' => 'website',
  218. 'type' => 'text',
  219. 'maxlength' => 100,
  220. 'caption' => __('Website label'),
  221. 'value' => $data['url'] ?? null,
  222. ];
  223. $fields['location'] = [
  224. 'type' => 'text',
  225. 'maxlength' => 30,
  226. 'caption' => __('Location label'),
  227. 'value' => $data['location'] ?? null,
  228. ];
  229. $fields['signature'] = [
  230. 'type' => 'text',
  231. 'maxlength' => 512,
  232. 'caption' => __('Signature label'),
  233. 'value' => $data['signature'] ?? null,
  234. ];
  235. $fields['admin_note'] = [
  236. 'type' => 'text',
  237. 'maxlength' => 30,
  238. 'caption' => __('Admin note label'),
  239. 'value' => $data['admin_note'] ?? null,
  240. ];
  241. $fields['between1'] = [
  242. 'class' => 'between',
  243. 'type' => 'wrap',
  244. ];
  245. $fields['num_posts_1'] = [
  246. 'type' => 'number',
  247. 'class' => 'bstart',
  248. 'min' => 0,
  249. 'max' => 9999999999,
  250. 'value' => $data['num_posts_1'] ?? null,
  251. 'caption' => __('Posts label'),
  252. ];
  253. $fields['num_posts_2'] = [
  254. 'type' => 'number',
  255. 'class' => 'bend',
  256. 'min' => 0,
  257. 'max' => 9999999999,
  258. 'value' => $data['num_posts_2'] ?? null,
  259. ];
  260. $fields[] = [
  261. 'type' => 'endwrap',
  262. ];
  263. $fields['between2'] = [
  264. 'class' => 'between',
  265. 'type' => 'wrap',
  266. ];
  267. $fields['last_post_1'] = [
  268. 'class' => 'bstart',
  269. 'type' => 'text',
  270. 'maxlength' => 100,
  271. 'value' => $data['last_post_1'] ?? null,
  272. 'caption' => __('Last post label'),
  273. ];
  274. $fields['last_post_2'] = [
  275. 'class' => 'bend',
  276. 'type' => 'text',
  277. 'maxlength' => 100,
  278. 'value' => $data['last_post_2'] ?? null,
  279. ];
  280. $fields[] = [
  281. 'type' => 'endwrap',
  282. ];
  283. $fields['between3'] = [
  284. 'class' => 'between',
  285. 'type' => 'wrap',
  286. ];
  287. $fields['last_visit_1'] = [
  288. 'class' => 'bstart',
  289. 'type' => 'text',
  290. 'maxlength' => 100,
  291. 'value' => $data['last_visit_1'] ?? null,
  292. 'caption' => __('Last visit label'),
  293. ];
  294. $fields['last_visit_2'] = [
  295. 'class' => 'bend',
  296. 'type' => 'text',
  297. 'maxlength' => 100,
  298. 'value' => $data['last_visit_2'] ?? null,
  299. ];
  300. $fields[] = [
  301. 'type' => 'endwrap',
  302. ];
  303. $fields['between4'] = [
  304. 'class' => 'between',
  305. 'type' => 'wrap',
  306. ];
  307. $fields['registered_1'] = [
  308. 'class' => 'bstart',
  309. 'type' => 'text',
  310. 'maxlength' => 100,
  311. 'value' => $data['registered_1'] ?? null,
  312. 'caption' => __('Registered label'),
  313. ];
  314. $fields['registered_2'] = [
  315. 'class' => 'bend',
  316. 'type' => 'text',
  317. 'maxlength' => 100,
  318. 'value' => $data['registered_2'] ?? null,
  319. ];
  320. $fields[] = [
  321. 'type' => 'endwrap',
  322. ];
  323. $form['sets']['filters'] = [
  324. 'legend' => __('User search subhead'),
  325. 'fields' => $fields,
  326. ];
  327. $fields = [];
  328. $fields['between5'] = [
  329. 'class' => 'between',
  330. 'type' => 'wrap',
  331. ];
  332. $fields['order_by'] = [
  333. 'class' => 'bstart',
  334. 'type' => 'select',
  335. 'options' => [
  336. 'username' => __('Order by username'),
  337. 'email' => __('Order by e-mail'),
  338. 'num_posts' => __('Order by posts'),
  339. 'last_post' => __('Order by last post'),
  340. 'last_visit' => __('Order by last visit'),
  341. 'registered' => __('Order by registered'),
  342. ],
  343. 'value' => $data['order_by'] ?? 'registered',
  344. 'caption' => __('Order by label'),
  345. ];
  346. $fields['direction'] = [
  347. 'class' => 'bend',
  348. 'type' => 'select',
  349. 'options' => [
  350. 'ASC' => __('Ascending'),
  351. 'DESC' => __('Descending'),
  352. ],
  353. 'value' => $data['direction'] ?? 'DESC',
  354. ];
  355. $fields[] = [
  356. 'type' => 'endwrap',
  357. ];
  358. $fields['user_group'] = [
  359. 'type' => 'select',
  360. 'options' => $this->groups(),
  361. 'value' => $data['user_group'] ?? -1,
  362. 'caption' => __('User group label'),
  363. ];
  364. $form['sets']['sorting'] = [
  365. 'legend' => __('Search results legend'),
  366. 'fields' => $fields,
  367. ];
  368. return $form;
  369. }
  370. /**
  371. * Создает массив данных для формы поиска по IP
  372. *
  373. * @param array $data
  374. *
  375. * @return array
  376. */
  377. protected function formIP(array $data): array
  378. {
  379. $form = [
  380. 'action' => $this->c->Router->link('AdminUsers'),
  381. 'hidden' => [
  382. 'token' => $this->c->Csrf->create('AdminUsers'),
  383. ],
  384. 'sets' => [],
  385. 'btns' => [
  386. 'find' => [
  387. 'type' => 'submit',
  388. 'value' => __('Find IP address'),
  389. 'accesskey' => 'f',
  390. ],
  391. ],
  392. ];
  393. $fields = [];
  394. $fields['ip'] = [
  395. 'type' => 'text',
  396. 'maxlength' => 49,
  397. 'caption' => __('IP address label'),
  398. 'value' => $data['ip'] ?? null,
  399. 'required' => true,
  400. ];
  401. $form['sets']['ip'] = [
  402. 'legend' => __('IP search subhead'),
  403. 'fields' => $fields,
  404. ];
  405. return $form;
  406. }
  407. /**
  408. * Пересчитывает количество сообщений пользователей
  409. *
  410. * @param array $args
  411. * @param string $method
  412. *
  413. * @return Page
  414. */
  415. public function recalculate(array $args, string $method): Page
  416. {
  417. $v = $this->c->Validator->reset()
  418. ->addValidators([
  419. ])->addRules([
  420. 'token' => 'token:AdminUsersRecalculate',
  421. ])->addAliases([
  422. ])->addArguments([
  423. ])->addMessages([
  424. ]);
  425. if (! $v->validation($_POST)) {
  426. return $this->c->Message->message('Bad token');
  427. }
  428. $this->c->users->updateCountPosts();
  429. return $this->c->Redirect->page('AdminUsers')->message('Updated the number of users posts redirect');
  430. }
  431. /**
  432. * Создает массив данных для формы пересчета количества сообщений
  433. *
  434. * @return array
  435. */
  436. protected function formRecalculate(): array
  437. {
  438. $form = [
  439. 'action' => $this->c->Router->link('AdminUsersRecalculate'),
  440. 'hidden' => [
  441. 'token' => $this->c->Csrf->create('AdminUsersRecalculate'),
  442. ],
  443. 'sets' => [
  444. 'recalculate' => [
  445. 'legend' => __('Number of users posts'),
  446. 'fields' => [],
  447. ]
  448. ],
  449. 'btns' => [
  450. 'recalculate' => [
  451. 'type' => 'submit',
  452. 'value' => __('Recalculate'),
  453. 'accesskey' => 'r',
  454. ],
  455. ],
  456. ];
  457. return $form;
  458. }
  459. }