Forums.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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\Model as 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' => '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[] = __(['"%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. 'value' => '1',
  265. 'checked' => false,
  266. ],
  267. ],
  268. ],
  269. [
  270. 'info' => [
  271. [
  272. 'value' => __('Delete forum warn'),
  273. 'html' => true,
  274. ],
  275. ],
  276. ],
  277. ],
  278. 'btns' => [
  279. 'delete' => [
  280. 'type' => 'submit',
  281. 'value' => __('Delete forum'),
  282. ],
  283. 'cancel' => [
  284. 'type' => 'btn',
  285. 'value' => __('Cancel'),
  286. 'link' => $this->c->Router->link('AdminForums'),
  287. ],
  288. ],
  289. ];
  290. }
  291. /**
  292. * Редактирование раздела
  293. * Создание нового раздела
  294. */
  295. public function edit(array $args, string $method): Page
  296. {
  297. $this->c->Lang->load('validator');
  298. $this->c->Lang->load('admin_forums');
  299. if (empty($args['id'])) {
  300. $forum = $this->c->forums->create();
  301. $marker = 'AdminForumsNew';
  302. $this->aCrumbs[] = [
  303. $this->c->Router->link($marker),
  304. __('Add forum head'),
  305. ];
  306. $this->titleForm = 'Add forum head';
  307. $this->classForm = ['createforum'];
  308. } else {
  309. $forum = $this->c->forums->loadTree($args['id']); //?????
  310. $marker = 'AdminForumsEdit';
  311. $this->aCrumbs[] = [
  312. $this->c->Router->link($marker, $args),
  313. __('Edit forum head'),
  314. ];
  315. $this->aCrumbs[] = __(['"%s"', $forum->forum_name]);
  316. $this->titleForm = 'Edit forum head';
  317. $this->classForm = ['editforum'];
  318. }
  319. if (! $forum instanceof Forum) {
  320. return $this->c->Message->message('Bad request');
  321. }
  322. $this->calcList($forum);
  323. if ('POST' === $method) {
  324. $v = $this->c->Validator->reset()
  325. ->addRules([
  326. 'token' => 'token:' . $marker,
  327. 'forum_name' => 'required|string:trim|max:80',
  328. 'forum_desc' => 'string:trim|max:65000 bytes|html',
  329. 'parent' => 'required|integer|in:' . implode(',', $this->listOfIndexes),
  330. 'sort_by' => 'required|integer|in:0,1,2',
  331. 'redirect_url' => 'string:trim|max:255', //????
  332. 'no_sum_mess' => 'required|integer|in:0,1',
  333. 'perms.*.read_forum' => 'checkbox',
  334. 'perms.*.post_replies' => 'checkbox',
  335. 'perms.*.post_topics' => 'checkbox',
  336. 'submit' => 'string',
  337. 'reset' => empty($forum->id) ? 'absent' : 'string',
  338. ])->addAliases([
  339. ])->addArguments([
  340. 'token' => $args,
  341. ]);
  342. $valid = $v->validation($_POST);
  343. $forum->forum_name = $v->forum_name;
  344. $forum->forum_desc = $v->forum_desc;
  345. $forum->sort_by = $v->sort_by;
  346. $forum->redirect_url = $v->redirect_url;
  347. $forum->no_sum_mess = $v->no_sum_mess;
  348. if ($v->parent > 0) {
  349. $forum->parent_forum_id = $v->parent;
  350. $forum->cat_id = $this->c->forums->get($v->parent)->cat_id;
  351. } elseif ($v->parent < 0) {
  352. $forum->cat_id = -$v->parent;
  353. $forum->parent_forum_id = 0;
  354. }
  355. if ($valid) {
  356. if ($v->reset) {
  357. $message = 'Perms reverted redirect';
  358. $this->c->groups->Perm->reset($forum);
  359. } else {
  360. if (empty($args['id'])) {
  361. $message = 'Forum added redirect';
  362. $forum->disp_position = $this->forumPos($forum);
  363. $forum->moderators = '';
  364. $this->c->forums->insert($forum);
  365. } else {
  366. $message = 'Forum updated redirect';
  367. $this->c->forums->update($forum);
  368. }
  369. $this->c->groups->Perm->update($forum, $v->perms);
  370. }
  371. $this->c->forums->reset();
  372. return $this->c->Redirect->page('AdminForumsEdit', ['id' => $forum->id])->message($message);
  373. }
  374. $this->fIswev = $v->getErrors();
  375. }
  376. $this->nameTpl = 'admin/form';
  377. $this->aIndex = 'forums';
  378. $this->form = $this->formEdit($args, $forum, $marker);
  379. return $this;
  380. }
  381. /**
  382. * Подготавливает массив данных для формы
  383. */
  384. protected function formEdit(array $args, Forum $forum, string $marker): array
  385. {
  386. $form = [
  387. 'action' => $this->c->Router->link($marker, $args),
  388. 'hidden' => [
  389. 'token' => $this->c->Csrf->create($marker, $args),
  390. ],
  391. 'sets' => [],
  392. 'btns' => [],
  393. ];
  394. if ($forum->id > 0) {
  395. $form['btns']['reset'] = [
  396. 'type' => 'submit',
  397. 'value' => __('Revert to default'),
  398. 'class' => ['f-opacity'],
  399. ];
  400. }
  401. $form['btns']['submit'] = [
  402. 'type' => 'submit',
  403. 'value' => empty($forum->id) ? __('Add') : __('Update'),
  404. ];
  405. $form['sets']['forum'] = [
  406. 'fields' => [
  407. 'forum_name' => [
  408. 'type' => 'text',
  409. 'maxlength' => '80',
  410. 'value' => $forum->forum_name,
  411. 'caption' => 'Forum name label',
  412. 'required' => true,
  413. ],
  414. 'forum_desc' => [
  415. 'type' => 'textarea',
  416. 'value' => $forum->forum_desc,
  417. 'caption' => 'Forum description label',
  418. ],
  419. 'parent' => [
  420. 'type' => 'select',
  421. 'options' => $this->listForOptions,
  422. 'value' => $forum->parent_forum_id ? $forum->parent_forum_id : -$forum->cat_id,
  423. 'caption' => 'Parent label',
  424. 'help' => 'Parent help',
  425. 'required' => true,
  426. ],
  427. 'sort_by' => [
  428. 'type' => 'select',
  429. 'options' => [
  430. 0 => __('Last post option'),
  431. 1 => __('Topic start option'),
  432. 2 => __('Subject option'),
  433. ],
  434. 'value' => $forum->sort_by,
  435. 'caption' => 'Sort by label',
  436. ],
  437. 'redirect_url' => [
  438. 'type' => 'text',
  439. 'maxlength' => '255',
  440. 'value' => $forum->redirect_url,
  441. 'caption' => 'Redirect label',
  442. 'help' => 'Redirect help',
  443. 'disabled' => $forum->num_topics || $forum->subforums ? true : null,
  444. ],
  445. 'no_sum_mess' => [
  446. 'type' => 'radio',
  447. 'value' => $forum->no_sum_mess,
  448. 'values' => [0 => __('Yes'), 1 => __('No')],
  449. 'caption' => 'Count messages label',
  450. 'help' => ['Count messages help', $this->c->Router->link('AdminUsers'), __('Users')],
  451. ],
  452. ],
  453. ];
  454. $form['sets']['forum-info'] = [
  455. 'info' => [
  456. [
  457. 'value' => __(['Group permissions info', $this->c->Router->link('AdminGroups'), __('User groups')]),
  458. 'html' => true,
  459. ],
  460. ],
  461. ];
  462. $aOn = ['cando', 'on'];
  463. $aOff = ['cando', 'off'];
  464. foreach ($this->c->groups->Perm->get($forum) as $id => $group) {
  465. $fields = [];
  466. $fields["perms[{$id}][read_forum]"] = [
  467. 'class' => $group->def_read_forum ? $aOn : $aOff,
  468. 'type' => 'checkbox',
  469. 'value' => '1',
  470. 'caption' => 'Read forum label',
  471. 'label' => __('<span></span>'),
  472. 'checked' => $group->set_read_forum,
  473. 'disabled' => $group->dis_read_forum,
  474. ];
  475. $fields["perms[{$id}][post_replies]"] = [
  476. 'class' => $group->def_post_replies ? $aOn : $aOff,
  477. 'type' => 'checkbox',
  478. 'value' => '1',
  479. 'caption' => 'Post replies label',
  480. 'label' => __('<span></span>'),
  481. 'checked' => $group->set_post_replies,
  482. 'disabled' => $group->dis_post_replies,
  483. ];
  484. $fields["perms[{$id}][post_topics]"] = [
  485. 'class' => $group->def_post_topics ? $aOn : $aOff,
  486. 'type' => 'checkbox',
  487. 'value' => '1',
  488. 'caption' => 'Post topics label',
  489. 'label' => __('<span></span>'),
  490. 'checked' => $group->set_post_topics,
  491. 'disabled' => $group->dis_post_topics,
  492. ];
  493. $form['sets']["perms{$id}"] = [
  494. 'class' => ['permission'],
  495. 'legend' => \ForkBB\e($group->g_title),
  496. 'fields' => $fields,
  497. ];
  498. }
  499. return $form;
  500. }
  501. }