Forums.php 19 KB

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