Forums.php 20 KB

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