Forums.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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\Admin;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Pages\Admin;
  14. use function \ForkBB\__;
  15. class Forums extends Admin
  16. {
  17. /**
  18. * Составление списка категорий/разделов для выбора родителя
  19. */
  20. protected function calcList(Forum $forum): void
  21. {
  22. $cid = null;
  23. $categories = $this->c->categories->getList();
  24. $options = [
  25. ['', __('Not selected')],
  26. ];
  27. $idxs = [];
  28. $root = $this->c->forums->get(0);
  29. if ($root instanceof Forum) {
  30. foreach ($this->c->forums->depthList($root, 0) as $f) {
  31. if ($cid !== $f->cat_id) {
  32. $cid = $f->cat_id;
  33. $options[] = [-$cid, __('Category prefix') . $f->cat_name];
  34. $idxs[] = -$cid;
  35. unset($categories[$cid]);
  36. }
  37. $indent = \str_repeat(__('Forum indent'), $f->depth);
  38. if (
  39. $f->id === $forum->id
  40. || isset($forum->descendants[$f->id])
  41. || $f->redirect_url
  42. ) {
  43. $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name, true];
  44. } else {
  45. $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name];
  46. $idxs[] = $f->id;
  47. }
  48. }
  49. }
  50. foreach ($categories as $key => $row) {
  51. $idxs[] = -$key;
  52. $options[] = [-$key, __('Category prefix') . $row['cat_name']];
  53. }
  54. $this->listOfIndexes = $idxs;
  55. $this->listForOptions = $options;
  56. }
  57. /**
  58. * Вычисление позиции для (нового) раздела
  59. */
  60. protected function forumPos(Forum $forum): int
  61. {
  62. if (\is_int($forum->disp_position)) {
  63. return $forum->disp_position;
  64. }
  65. $root = $this->c->forums->get(0);
  66. if (! $root instanceof Forum) {
  67. return 0;
  68. }
  69. $max = 0;
  70. foreach ($root->descendants as $f) {
  71. if ($f->disp_position > $max) {
  72. $max = $f->disp_position;
  73. }
  74. }
  75. return $max + 1;
  76. }
  77. /**
  78. * Просмотр, редактирвоание и добавление разделов
  79. */
  80. public function view(array $args, string $method): Page
  81. {
  82. $this->c->Lang->load('validator');
  83. $this->c->Lang->load('admin_forums');
  84. if ('POST' === $method) {
  85. $v = $this->c->Validator->reset()
  86. ->addRules([
  87. 'token' => 'token:AdminForums',
  88. 'form.*.disp_position' => 'required|integer|min:0|max:9999999999',
  89. ])->addAliases([
  90. ])->addArguments([
  91. ])->addMessages([
  92. ]);
  93. if ($v->validation($_POST)) {
  94. foreach ($v->form as $key => $row) {
  95. $forum = $this->c->forums->get((int) $key);
  96. $forum->disp_position = $row['disp_position'];
  97. $this->c->forums->update($forum);
  98. }
  99. $this->c->forums->reset();
  100. return $this->c->Redirect->page('AdminForums')->message('Forums updated redirect');
  101. }
  102. $this->fIswev = $v->getErrors();
  103. }
  104. $this->nameTpl = 'admin/form';
  105. $this->aIndex = 'forums';
  106. $this->form = $this->formView();
  107. $this->classForm = ['editforums', 'inline'];
  108. $this->titleForm = 'Forums';
  109. return $this;
  110. }
  111. /**
  112. * Подготавливает массив данных для формы
  113. */
  114. protected function formView(): array
  115. {
  116. $form = [
  117. 'action' => $this->c->Router->link('AdminForums'),
  118. 'hidden' => [
  119. 'token' => $this->c->Csrf->create('AdminForums'),
  120. ],
  121. 'sets' => [],
  122. 'btns' => [
  123. 'new' => [
  124. 'type' => 'btn',
  125. 'value' => __('New forum'),
  126. 'link' => $this->c->Router->link('AdminForumsNew'),
  127. ],
  128. 'update' => [
  129. 'type' => 'submit',
  130. 'value' => __('Update positions'),
  131. ],
  132. ],
  133. ];
  134. $root = $this->c->forums->get(0);
  135. if ($root instanceof Forum) {
  136. $list = $this->c->forums->depthList($root, -1);
  137. $cid = null;
  138. foreach ($list as $forum) {
  139. if ($cid !== $forum->cat_id) {
  140. $form['sets']["category{$forum->cat_id}-info"] = [
  141. 'info' => [
  142. [
  143. 'value' => $forum->cat_name,
  144. ],
  145. ],
  146. ];
  147. $cid = $forum->cat_id;
  148. }
  149. $fields = [];
  150. $fields["name-btn{$forum->id}"] = [
  151. 'class' => ['name', 'forum', 'depth' . $forum->depth],
  152. 'type' => 'btn',
  153. 'value' => $forum->forum_name,
  154. 'caption' => 'Forum label',
  155. 'link' => $this->c->Router->link(
  156. 'AdminForumsEdit',
  157. [
  158. 'id' => $forum->id,
  159. ]
  160. ),
  161. ];
  162. $fields["form[{$forum->id}][disp_position]"] = [
  163. 'class' => ['position', 'forum'],
  164. 'type' => 'number',
  165. 'min' => '0',
  166. 'max' => '9999999999',
  167. 'value' => $forum->disp_position,
  168. 'caption' => 'Position label',
  169. ];
  170. $disabled = (bool) $forum->subforums;
  171. $fields["delete-btn{$forum->id}"] = [
  172. 'class' => ['delete', 'forum'],
  173. 'type' => 'btn',
  174. 'value' => '❌',
  175. 'caption' => 'Delete',
  176. 'title' => __('Delete'),
  177. 'link' => $disabled
  178. ? '#'
  179. : $this->c->Router->link(
  180. 'AdminForumsDelete',
  181. [
  182. 'id' => $forum->id,
  183. ]
  184. ),
  185. 'disabled' => $disabled,
  186. ];
  187. $form['sets']["forum{$forum->id}"] = [
  188. 'class' => ['forum'],
  189. 'legend' => $forum->cat_name . ' / ' . $forum->forum_name,
  190. 'fields' => $fields,
  191. ];
  192. }
  193. }
  194. return $form;
  195. }
  196. /**
  197. * Удаление раздела
  198. */
  199. public function delete(array $args, string $method): Page
  200. {
  201. $forum = $this->c->forums->get($args['id']);
  202. if (
  203. ! $forum instanceof Forum
  204. || $forum->subforums
  205. ) {
  206. return $this->c->Message->message('Bad request');
  207. }
  208. $this->c->Lang->load('validator');
  209. $this->c->Lang->load('admin_forums');
  210. if ('POST' === $method) {
  211. $v = $this->c->Validator->reset()
  212. ->addRules([
  213. 'token' => 'token:AdminForumsDelete',
  214. 'confirm' => 'checkbox',
  215. 'delete' => 'required|string',
  216. ])->addAliases([
  217. ])->addArguments([
  218. 'token' => $args,
  219. ]);
  220. if (
  221. ! $v->validation($_POST)
  222. || '1' !== $v->confirm
  223. ) {
  224. return $this->c->Redirect->page('AdminForums')->message('No confirm redirect');
  225. }
  226. $this->c->forums->delete($forum);
  227. $this->c->forums->reset();
  228. return $this->c->Redirect->page('AdminForums')->message('Forum deleted redirect');
  229. }
  230. $this->nameTpl = 'admin/form';
  231. $this->aIndex = 'forums';
  232. $this->aCrumbs[] = [
  233. $this->c->Router->link(
  234. 'AdminForumsDelete',
  235. [
  236. 'id' => $forum->id,
  237. ]
  238. ),
  239. 'Delete forum head',
  240. ];
  241. $this->aCrumbs[] = [null, ['"%s"', $forum->forum_name]];
  242. $this->form = $this->formDelete($args, $forum);
  243. $this->classForm = ['deleteforum'];
  244. $this->titleForm = 'Delete forum head';
  245. return $this;
  246. }
  247. /**
  248. * Подготавливает массив данных для формы
  249. */
  250. protected function formDelete(array $args, Forum $forum): array
  251. {
  252. return [
  253. 'action' => $this->c->Router->link('AdminForumsDelete', $args),
  254. 'hidden' => [
  255. 'token' => $this->c->Csrf->create('AdminForumsDelete', $args),
  256. ],
  257. 'sets' => [
  258. 'confirm' => [
  259. 'fields' => [
  260. 'confirm' => [
  261. 'caption' => 'Confirm delete',
  262. 'type' => 'checkbox',
  263. 'label' => ['I want to delete forum %s', $forum->forum_name],
  264. 'checked' => false,
  265. ],
  266. ],
  267. ],
  268. [
  269. 'info' => [
  270. [
  271. 'value' => __('Delete forum warn'),
  272. 'html' => true,
  273. ],
  274. ],
  275. ],
  276. ],
  277. 'btns' => [
  278. 'delete' => [
  279. 'type' => 'submit',
  280. 'value' => __('Delete forum'),
  281. ],
  282. 'cancel' => [
  283. 'type' => 'btn',
  284. 'value' => __('Cancel'),
  285. 'link' => $this->c->Router->link('AdminForums'),
  286. ],
  287. ],
  288. ];
  289. }
  290. /**
  291. * Редактирование раздела
  292. * Создание нового раздела
  293. */
  294. public function edit(array $args, string $method): Page
  295. {
  296. $this->c->Lang->load('validator');
  297. $this->c->Lang->load('admin_forums');
  298. if (empty($args['id'])) {
  299. $forum = $this->c->forums->create();
  300. $marker = 'AdminForumsNew';
  301. $this->aCrumbs[] = [$this->c->Router->link($marker), 'Add forum head'];
  302. $this->titleForm = 'Add forum head';
  303. $this->classForm = ['createforum'];
  304. } else {
  305. $forum = $this->c->forums->loadTree($args['id']); //?????
  306. $marker = 'AdminForumsEdit';
  307. $this->aCrumbs[] = [$this->c->Router->link($marker, $args), 'Edit forum head'];
  308. $this->aCrumbs[] = [null, ['"%s"', $forum->forum_name]];
  309. $this->titleForm = 'Edit forum head';
  310. $this->classForm = ['editforum'];
  311. }
  312. if (! $forum instanceof Forum) {
  313. return $this->c->Message->message('Bad request');
  314. }
  315. $this->calcList($forum);
  316. if ('POST' === $method) {
  317. $v = $this->c->Validator->reset()
  318. ->addRules([
  319. 'token' => 'token:' . $marker,
  320. 'forum_name' => 'required|string:trim|max:80',
  321. 'forum_desc' => 'exist|string:trim|max:65000 bytes|html',
  322. 'parent' => 'required|integer|in:' . \implode(',', $this->listOfIndexes),
  323. 'sort_by' => 'required|integer|in:0,1,2',
  324. 'redirect_url' => 'string:trim|max:255', //???? это поле может быть отключено в форме
  325. 'no_sum_mess' => 'required|integer|in:0,1',
  326. 'perms.*.read_forum' => 'checkbox',
  327. 'perms.*.post_replies' => 'checkbox',
  328. 'perms.*.post_topics' => 'checkbox',
  329. 'submit' => 'string',
  330. 'reset' => empty($forum->id) ? 'absent' : 'string',
  331. ])->addAliases([
  332. ])->addArguments([
  333. 'token' => $args,
  334. ]);
  335. $valid = $v->validation($_POST);
  336. $forum->forum_name = $v->forum_name;
  337. $forum->forum_desc = $v->forum_desc;
  338. $forum->sort_by = $v->sort_by;
  339. $forum->redirect_url = $v->redirect_url ?? '';
  340. $forum->no_sum_mess = $v->no_sum_mess;
  341. if ($v->parent > 0) {
  342. $forum->parent_forum_id = $v->parent;
  343. $forum->cat_id = $this->c->forums->get($v->parent)->cat_id;
  344. } elseif ($v->parent < 0) {
  345. $forum->cat_id = -$v->parent;
  346. $forum->parent_forum_id = 0;
  347. }
  348. if ($valid) {
  349. if ($v->reset) {
  350. $message = 'Perms reverted redirect';
  351. $this->c->groups->Perm->reset($forum);
  352. } else {
  353. if (empty($args['id'])) {
  354. $message = 'Forum added redirect';
  355. $forum->disp_position = $this->forumPos($forum);
  356. $forum->moderators = '';
  357. $this->c->forums->insert($forum);
  358. } else {
  359. $message = 'Forum updated redirect';
  360. $this->c->forums->update($forum);
  361. }
  362. $this->c->groups->Perm->update($forum, $v->perms);
  363. }
  364. $this->c->forums->reset();
  365. return $this->c->Redirect->page('AdminForumsEdit', ['id' => $forum->id])->message($message);
  366. }
  367. $this->fIswev = $v->getErrors();
  368. }
  369. $this->nameTpl = 'admin/form';
  370. $this->aIndex = 'forums';
  371. $this->form = $this->formEdit($args, $forum, $marker);
  372. return $this;
  373. }
  374. /**
  375. * Подготавливает массив данных для формы
  376. */
  377. protected function formEdit(array $args, Forum $forum, string $marker): array
  378. {
  379. $form = [
  380. 'action' => $this->c->Router->link($marker, $args),
  381. 'hidden' => [
  382. 'token' => $this->c->Csrf->create($marker, $args),
  383. ],
  384. 'sets' => [],
  385. 'btns' => [],
  386. ];
  387. if ($forum->id > 0) {
  388. $form['btns']['reset'] = [
  389. 'type' => 'submit',
  390. 'value' => __('Revert to default'),
  391. 'class' => ['f-opacity'],
  392. ];
  393. }
  394. $form['btns']['submit'] = [
  395. 'type' => 'submit',
  396. 'value' => empty($forum->id) ? __('Add') : __('Update'),
  397. ];
  398. $form['sets']['forum'] = [
  399. 'fields' => [
  400. 'forum_name' => [
  401. 'type' => 'text',
  402. 'maxlength' => '80',
  403. 'value' => $forum->forum_name,
  404. 'caption' => 'Forum name label',
  405. 'required' => true,
  406. ],
  407. 'forum_desc' => [
  408. 'type' => 'textarea',
  409. 'value' => $forum->forum_desc,
  410. 'caption' => 'Forum description label',
  411. ],
  412. 'parent' => [
  413. 'type' => 'select',
  414. 'options' => $this->listForOptions,
  415. 'value' => $forum->parent_forum_id ? $forum->parent_forum_id : -$forum->cat_id,
  416. 'caption' => 'Parent label',
  417. 'help' => 'Parent help',
  418. 'required' => true,
  419. ],
  420. 'sort_by' => [
  421. 'type' => 'select',
  422. 'options' => [
  423. 0 => __('Last post option'),
  424. 1 => __('Topic start option'),
  425. 2 => __('Subject option'),
  426. ],
  427. 'value' => $forum->sort_by,
  428. 'caption' => 'Sort by label',
  429. ],
  430. 'redirect_url' => [
  431. 'type' => 'text',
  432. 'maxlength' => '255',
  433. 'value' => $forum->redirect_url,
  434. 'caption' => 'Redirect label',
  435. 'help' => 'Redirect help',
  436. 'disabled' => $forum->num_topics || $forum->subforums ? true : null,
  437. ],
  438. 'no_sum_mess' => [
  439. 'type' => 'radio',
  440. 'value' => $forum->no_sum_mess,
  441. 'values' => [0 => __('Yes'), 1 => __('No')],
  442. 'caption' => 'Count messages label',
  443. 'help' => ['Count messages help', $this->c->Router->link('AdminUsers'), __('Users')],
  444. ],
  445. ],
  446. ];
  447. $form['sets']['forum-info'] = [
  448. 'info' => [
  449. [
  450. 'value' => __(['Group permissions info', $this->c->Router->link('AdminGroups'), __('User groups')]),
  451. 'html' => true,
  452. ],
  453. ],
  454. ];
  455. $aOn = ['cando', 'on'];
  456. $aOff = ['cando', 'off'];
  457. foreach ($this->c->groups->Perm->get($forum) as $id => $group) {
  458. $fields = [];
  459. $fields["perms[{$id}][read_forum]"] = [
  460. 'class' => $group->def_read_forum ? $aOn : $aOff,
  461. 'type' => 'checkbox',
  462. 'caption' => 'Read forum label',
  463. 'label' => '<span></span>',
  464. 'checked' => $group->set_read_forum,
  465. 'disabled' => $group->dis_read_forum,
  466. ];
  467. $fields["perms[{$id}][post_replies]"] = [
  468. 'class' => $group->def_post_replies ? $aOn : $aOff,
  469. 'type' => 'checkbox',
  470. 'caption' => 'Post replies label',
  471. 'label' => '<span></span>',
  472. 'checked' => $group->set_post_replies,
  473. 'disabled' => $group->dis_post_replies,
  474. ];
  475. $fields["perms[{$id}][post_topics]"] = [
  476. 'class' => $group->def_post_topics ? $aOn : $aOff,
  477. 'type' => 'checkbox',
  478. 'caption' => 'Post topics label',
  479. 'label' => '<span></span>',
  480. 'checked' => $group->set_post_topics,
  481. 'disabled' => $group->dis_post_topics,
  482. ];
  483. $form['sets']["perms{$id}"] = [
  484. 'class' => ['permission'],
  485. 'legend' => $group->g_title,
  486. 'fields' => $fields,
  487. ];
  488. }
  489. return $form;
  490. }
  491. }