Forums.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. namespace ForkBB\Models\Pages\Admin;
  3. use ForkBB\Core\Container;
  4. use ForkBB\Models\Page;
  5. use ForkBB\Models\Forum\Model as Forum;
  6. use ForkBB\Models\Pages\Admin;
  7. use function \ForkBB\__;
  8. class Forums extends Admin
  9. {
  10. /**
  11. * Составление списка категорий/разделов для выбора родителя
  12. */
  13. protected function calcList(Forum $forum): void
  14. {
  15. $cid = null;
  16. $categories = $this->c->categories->getList();
  17. $options = [
  18. ['', __('Not selected')],
  19. ];
  20. $idxs = [];
  21. $root = $this->c->forums->get(0);
  22. if ($root instanceof Forum) {
  23. foreach ($this->c->forums->depthList($root, 0) as $f) {
  24. if ($cid !== $f->cat_id) {
  25. $cid = $f->cat_id;
  26. $options[] = [-$cid, __('Category prefix') . $f->cat_name];
  27. $idxs[] = -$cid;
  28. unset($categories[$cid]);
  29. }
  30. $indent = \str_repeat(__('Forum indent'), $f->depth);
  31. if (
  32. $f->id === $forum->id
  33. || isset($forum->descendants[$f->id])
  34. || $f->redirect_url
  35. ) {
  36. $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name, true];
  37. } else {
  38. $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name];
  39. $idxs[] = $f->id;
  40. }
  41. }
  42. }
  43. foreach ($categories as $key => $row) {
  44. $idxs[] = -$key;
  45. $options[] = [-$key, __('Category prefix') . $row['cat_name']];
  46. }
  47. $this->listOfIndexes = $idxs;
  48. $this->listForOptions = $options;
  49. }
  50. /**
  51. * Вычисление позиции для (нового) раздела
  52. */
  53. protected function forumPos(Forum $forum): int
  54. {
  55. if (\is_int($forum->disp_position)) {
  56. return $forum->disp_position;
  57. }
  58. $root = $this->c->forums->get(0);
  59. if (! $root instanceof Forum) {
  60. return 0;
  61. }
  62. $max = 0;
  63. foreach ($root->descendants as $f) {
  64. if ($f->disp_position > $max) {
  65. $max = $f->disp_position;
  66. }
  67. }
  68. return $max + 1;
  69. }
  70. /**
  71. * Просмотр, редактирвоание и добавление разделов
  72. */
  73. public function view(array $args, string $method): Page
  74. {
  75. $this->c->Lang->load('validator');
  76. $this->c->Lang->load('admin_forums');
  77. if ('POST' === $method) {
  78. $v = $this->c->Validator->reset()
  79. ->addRules([
  80. 'token' => 'token:AdminForums',
  81. 'form.*.disp_position' => 'required|integer|min:0|max:9999999999',
  82. ])->addAliases([
  83. ])->addArguments([
  84. ])->addMessages([
  85. ]);
  86. if ($v->validation($_POST)) {
  87. foreach ($v->form as $key => $row) {
  88. $forum = $this->c->forums->get((int) $key);
  89. $forum->disp_position = $row['disp_position'];
  90. $this->c->forums->update($forum);
  91. }
  92. $this->c->forums->reset();
  93. return $this->c->Redirect->page('AdminForums')->message('Forums updated redirect');
  94. }
  95. $this->fIswev = $v->getErrors();
  96. }
  97. $this->nameTpl = 'admin/form';
  98. $this->aIndex = 'forums';
  99. $this->form = $this->formView();
  100. $this->classForm = ['editforums', 'inline'];
  101. $this->titleForm = __('Forums');
  102. return $this;
  103. }
  104. /**
  105. * Подготавливает массив данных для формы
  106. */
  107. protected function formView(): array
  108. {
  109. $form = [
  110. 'action' => $this->c->Router->link('AdminForums'),
  111. 'hidden' => [
  112. 'token' => $this->c->Csrf->create('AdminForums'),
  113. ],
  114. 'sets' => [],
  115. 'btns' => [
  116. 'new' => [
  117. 'type' => 'btn',
  118. 'value' => __('New forum'),
  119. 'link' => $this->c->Router->link('AdminForumsNew'),
  120. // 'accesskey' => 'n',
  121. ],
  122. 'update' => [
  123. 'type' => 'submit',
  124. 'value' => __('Update positions'),
  125. // 'accesskey' => 's',
  126. ],
  127. ],
  128. ];
  129. $root = $this->c->forums->get(0);
  130. if ($root instanceof Forum) {
  131. $list = $this->c->forums->depthList($root, -1);
  132. $cid = null;
  133. foreach ($list as $forum) {
  134. if ($cid !== $forum->cat_id) {
  135. $form['sets']["category{$forum->cat_id}-info"] = [
  136. 'info' => [
  137. 'info1' => [
  138. 'type' => '', //????
  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. 'link' => $disabled
  173. ? '#'
  174. : $this->c->Router->link(
  175. 'AdminForumsDelete',
  176. [
  177. 'id' => $forum->id,
  178. ]
  179. ),
  180. 'disabled' => $disabled,
  181. ];
  182. $form['sets']["forum{$forum->id}"] = [
  183. 'class' => 'forum',
  184. 'legend' => $forum->cat_name . ' / ' . $forum->forum_name,
  185. 'fields' => $fields,
  186. ];
  187. }
  188. }
  189. return $form;
  190. }
  191. /**
  192. * Удаление раздела
  193. */
  194. public function delete(array $args, string $method): Page
  195. {
  196. $forum = $this->c->forums->get((int) $args['id']);
  197. if (
  198. ! $forum instanceof Forum
  199. || $forum->subforums
  200. ) {
  201. return $this->c->Message->message('Bad request');
  202. }
  203. $this->c->Lang->load('validator');
  204. $this->c->Lang->load('admin_forums');
  205. if ('POST' === $method) {
  206. $v = $this->c->Validator->reset()
  207. ->addRules([
  208. 'token' => 'token:AdminForumsDelete',
  209. 'confirm' => 'integer', // ????
  210. 'delete' => 'string',
  211. ])->addAliases([
  212. ])->addArguments([
  213. 'token' => $args,
  214. ]);
  215. if (
  216. ! $v->validation($_POST)
  217. || 1 !== $v->confirm
  218. ) {
  219. return $this->c->Redirect->page('AdminForums')->message('No confirm redirect');
  220. }
  221. $this->c->forums->delete($forum);
  222. $this->c->forums->reset();
  223. return $this->c->Redirect->page('AdminForums')->message('Forum deleted redirect');
  224. }
  225. $this->nameTpl = 'admin/form';
  226. $this->aIndex = 'forums';
  227. $this->aCrumbs[] = [
  228. $this->c->Router->link(
  229. 'AdminForumsDelete',
  230. [
  231. 'id' => $forum->id,
  232. ]
  233. ),
  234. __('Delete forum head'),
  235. ];
  236. $this->aCrumbs[] = __('"%s"', $forum->forum_name);
  237. $this->form = $this->formDelete($args, $forum);
  238. $this->classForm = 'deleteforum';
  239. $this->titleForm = __('Delete forum head');
  240. return $this;
  241. }
  242. /**
  243. * Подготавливает массив данных для формы
  244. */
  245. protected function formDelete(array $args, Forum $forum): array
  246. {
  247. return [
  248. 'action' => $this->c->Router->link(
  249. 'AdminForumsDelete',
  250. $args
  251. ),
  252. 'hidden' => [
  253. 'token' => $this->c->Csrf->create(
  254. 'AdminForumsDelete',
  255. $args
  256. ),
  257. ],
  258. 'sets' => [
  259. 'confirm' => [
  260. 'fields' => [
  261. 'confirm' => [
  262. 'caption' => __('Confirm delete'),
  263. 'type' => 'checkbox',
  264. 'label' => __('I want to delete forum %s', $forum->forum_name),
  265. 'value' => '1',
  266. 'checked' => false,
  267. ],
  268. ],
  269. ],
  270. [
  271. 'info' => [
  272. 'info1' => [
  273. 'type' => '', //????
  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. 'type' => '', //????
  469. 'value' => __('Group permissions info', $this->c->Router->link('AdminGroups'), __('User groups')),
  470. 'html' => true,
  471. ],
  472. ],
  473. ];
  474. $aOn = ['cando', 'on'];
  475. $aOff = ['cando', 'off'];
  476. foreach ($this->c->groups->Perm->get($forum) as $id => $group) {
  477. $fields = [];
  478. $fields["perms[{$id}][read_forum]"] = [
  479. 'class' => $group->def_read_forum ? $aOn : $aOff,
  480. 'type' => 'checkbox',
  481. 'value' => '1',
  482. 'caption' => __('Read forum label'),
  483. 'label' => __('<span></span>'),
  484. 'checked' => $group->set_read_forum,
  485. 'disabled' => $group->dis_read_forum,
  486. ];
  487. $fields["perms[{$id}][post_replies]"] = [
  488. 'class' => $group->def_post_replies ? $aOn : $aOff,
  489. 'type' => 'checkbox',
  490. 'value' => '1',
  491. 'caption' => __('Post replies label'),
  492. 'label' => __('<span></span>'),
  493. 'checked' => $group->set_post_replies,
  494. 'disabled' => $group->dis_post_replies,
  495. ];
  496. $fields["perms[{$id}][post_topics]"] = [
  497. 'class' => $group->def_post_topics ? $aOn : $aOff,
  498. 'type' => 'checkbox',
  499. 'value' => '1',
  500. 'caption' => __('Post topics label'),
  501. 'label' => __('<span></span>'),
  502. 'checked' => $group->set_post_topics,
  503. 'disabled' => $group->dis_post_topics,
  504. ];
  505. $form['sets']["perms{$id}"] = [
  506. 'class' => 'permission',
  507. 'legend' => \ForkBB\e($group->g_title),
  508. 'fields' => $fields,
  509. ];
  510. }
  511. return $form;
  512. }
  513. }