123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- /**
- * This file is part of the ForkBB <https://github.com/forkbb>.
- *
- * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
- * @license The MIT License (MIT)
- */
- declare(strict_types=1);
- namespace ForkBB\Models\Pages\Admin;
- use ForkBB\Core\Container;
- use ForkBB\Models\Page;
- use ForkBB\Models\Forum\Forum;
- use ForkBB\Models\Pages\Admin;
- use function \ForkBB\__;
- class Forums extends Admin
- {
- /**
- * Составление списка категорий/разделов для выбора родителя
- */
- protected function calcList(Forum $forum): void
- {
- $cid = null;
- $categories = $this->c->categories->getList();
- $options = [
- ['', __('Not selected')],
- ];
- $idxs = [];
- $root = $this->c->forums->get(0);
- if ($root instanceof Forum) {
- foreach ($this->c->forums->depthList($root, 0) as $f) {
- if ($cid !== $f->cat_id) {
- $cid = $f->cat_id;
- $options[] = [-$cid, __('Category prefix') . $f->cat_name];
- $idxs[] = -$cid;
- unset($categories[$cid]);
- }
- $indent = \str_repeat(__('Forum indent'), $f->depth);
- if (
- $f->id === $forum->id
- || isset($forum->descendants[$f->id])
- || $f->redirect_url
- ) {
- $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name, true];
- } else {
- $options[] = [$f->id, $indent . __('Forum prefix') . $f->forum_name];
- $idxs[] = $f->id;
- }
- }
- }
- foreach ($categories as $key => $row) {
- $idxs[] = -$key;
- $options[] = [-$key, __('Category prefix') . $row['cat_name']];
- }
- $this->listOfIndexes = $idxs;
- $this->listForOptions = $options;
- }
- /**
- * Вычисление позиции для (нового) раздела
- */
- protected function forumPos(Forum $forum): int
- {
- if (\is_int($forum->disp_position)) {
- return $forum->disp_position;
- }
- $root = $this->c->forums->get(0);
- if (! $root instanceof Forum) {
- return 0;
- }
- $max = 0;
- foreach ($root->descendants as $f) {
- if ($f->disp_position > $max) {
- $max = $f->disp_position;
- }
- }
- return $max + 1;
- }
- /**
- * Просмотр, редактирвоание и добавление разделов
- */
- public function view(array $args, string $method): Page
- {
- $this->c->Lang->load('validator');
- $this->c->Lang->load('admin_forums');
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminForums',
- 'form.*.disp_position' => 'required|integer|min:0|max:9999999999',
- ])->addAliases([
- ])->addArguments([
- ])->addMessages([
- ]);
- if ($v->validation($_POST)) {
- foreach ($v->form as $key => $row) {
- $forum = $this->c->forums->get((int) $key);
- $forum->disp_position = $row['disp_position'];
- $this->c->forums->update($forum);
- }
- $this->c->forums->reset();
- return $this->c->Redirect->page('AdminForums')->message('Forums updated redirect');
- }
- $this->fIswev = $v->getErrors();
- }
- $this->nameTpl = 'admin/form';
- $this->aIndex = 'forums';
- $this->form = $this->formView();
- $this->classForm = ['editforums', 'inline'];
- $this->titleForm = 'Forums';
- return $this;
- }
- /**
- * Подготавливает массив данных для формы
- */
- protected function formView(): array
- {
- $form = [
- 'action' => $this->c->Router->link('AdminForums'),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminForums'),
- ],
- 'sets' => [],
- 'btns' => [
- 'new' => [
- 'type' => 'btn',
- 'value' => __('New forum'),
- 'link' => $this->c->Router->link('AdminForumsNew'),
- ],
- 'update' => [
- 'type' => 'submit',
- 'value' => __('Update positions'),
- ],
- ],
- ];
- $root = $this->c->forums->get(0);
- if ($root instanceof Forum) {
- $list = $this->c->forums->depthList($root, -1);
- $cid = null;
- foreach ($list as $forum) {
- if ($cid !== $forum->cat_id) {
- $form['sets']["category{$forum->cat_id}-info"] = [
- 'info' => [
- [
- 'value' => $forum->cat_name,
- ],
- ],
- ];
- $cid = $forum->cat_id;
- }
- $fields = [];
- $fields["name-btn{$forum->id}"] = [
- 'class' => ['name', 'forum', 'depth' . $forum->depth],
- 'type' => 'btn',
- 'value' => $forum->forum_name,
- 'caption' => 'Forum label',
- 'link' => $this->c->Router->link(
- 'AdminForumsEdit',
- [
- 'id' => $forum->id,
- ]
- ),
- ];
- $fields["form[{$forum->id}][disp_position]"] = [
- 'class' => ['position', 'forum'],
- 'type' => 'number',
- 'min' => '0',
- 'max' => '9999999999',
- 'value' => $forum->disp_position,
- 'caption' => 'Position label',
- ];
- $disabled = (bool) $forum->subforums;
- $fields["delete-btn{$forum->id}"] = [
- 'class' => ['delete', 'forum'],
- 'type' => 'btn',
- 'value' => '❌',
- 'caption' => 'Delete',
- 'title' => __('Delete'),
- 'link' => $disabled
- ? '#'
- : $this->c->Router->link(
- 'AdminForumsDelete',
- [
- 'id' => $forum->id,
- ]
- ),
- 'disabled' => $disabled,
- ];
- $form['sets']["forum{$forum->id}"] = [
- 'class' => ['forum'],
- 'legend' => $forum->cat_name . ' / ' . $forum->forum_name,
- 'fields' => $fields,
- ];
- }
- }
- return $form;
- }
- /**
- * Удаление раздела
- */
- public function delete(array $args, string $method): Page
- {
- $forum = $this->c->forums->get($args['id']);
- if (
- ! $forum instanceof Forum
- || $forum->subforums
- ) {
- return $this->c->Message->message('Bad request');
- }
- $this->c->Lang->load('validator');
- $this->c->Lang->load('admin_forums');
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminForumsDelete',
- 'confirm' => 'checkbox',
- 'delete' => 'required|string',
- ])->addAliases([
- ])->addArguments([
- 'token' => $args,
- ]);
- if (
- ! $v->validation($_POST)
- || '1' !== $v->confirm
- ) {
- return $this->c->Redirect->page('AdminForums')->message('No confirm redirect');
- }
- $this->c->forums->delete($forum);
- $this->c->forums->reset();
- return $this->c->Redirect->page('AdminForums')->message('Forum deleted redirect');
- }
- $this->nameTpl = 'admin/form';
- $this->aIndex = 'forums';
- $this->aCrumbs[] = [
- $this->c->Router->link(
- 'AdminForumsDelete',
- [
- 'id' => $forum->id,
- ]
- ),
- 'Delete forum head',
- ];
- $this->aCrumbs[] = [null, ['"%s"', $forum->forum_name]];
- $this->form = $this->formDelete($args, $forum);
- $this->classForm = ['deleteforum'];
- $this->titleForm = 'Delete forum head';
- return $this;
- }
- /**
- * Подготавливает массив данных для формы
- */
- protected function formDelete(array $args, Forum $forum): array
- {
- return [
- 'action' => $this->c->Router->link('AdminForumsDelete', $args),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminForumsDelete', $args),
- ],
- 'sets' => [
- 'confirm' => [
- 'fields' => [
- 'confirm' => [
- 'caption' => 'Confirm delete',
- 'type' => 'checkbox',
- 'label' => ['I want to delete forum %s', $forum->forum_name],
- 'checked' => false,
- ],
- ],
- ],
- [
- 'info' => [
- [
- 'value' => __('Delete forum warn'),
- 'html' => true,
- ],
- ],
- ],
- ],
- 'btns' => [
- 'delete' => [
- 'type' => 'submit',
- 'value' => __('Delete forum'),
- ],
- 'cancel' => [
- 'type' => 'btn',
- 'value' => __('Cancel'),
- 'link' => $this->c->Router->link('AdminForums'),
- ],
- ],
- ];
- }
- /**
- * Редактирование раздела
- * Создание нового раздела
- */
- public function edit(array $args, string $method): Page
- {
- $this->c->Lang->load('validator');
- $this->c->Lang->load('admin_forums');
- if (empty($args['id'])) {
- $forum = $this->c->forums->create();
- $marker = 'AdminForumsNew';
- $this->aCrumbs[] = [$this->c->Router->link($marker), 'Add forum head'];
- $this->titleForm = 'Add forum head';
- $this->classForm = ['createforum'];
- } else {
- $forum = $this->c->forums->loadTree($args['id']); //?????
- $marker = 'AdminForumsEdit';
- $this->aCrumbs[] = [$this->c->Router->link($marker, $args), 'Edit forum head'];
- $this->aCrumbs[] = [null, ['"%s"', $forum->forum_name]];
- $this->titleForm = 'Edit forum head';
- $this->classForm = ['editforum'];
- }
- if (! $forum instanceof Forum) {
- return $this->c->Message->message('Bad request');
- }
- $this->calcList($forum);
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:' . $marker,
- 'forum_name' => 'required|string:trim|max:80',
- 'forum_desc' => 'exist|string:trim|max:65000 bytes|html',
- 'parent' => 'required|integer|in:' . \implode(',', $this->listOfIndexes),
- 'sort_by' => 'required|integer|in:0,1,2',
- 'redirect_url' => 'string:trim|max:255', //???? это поле может быть отключено в форме
- 'no_sum_mess' => 'required|integer|in:0,1',
- 'perms.*.read_forum' => 'checkbox',
- 'perms.*.post_replies' => 'checkbox',
- 'perms.*.post_topics' => 'checkbox',
- 'submit' => 'string',
- 'reset' => empty($forum->id) ? 'absent' : 'string',
- ])->addAliases([
- ])->addArguments([
- 'token' => $args,
- ]);
- $valid = $v->validation($_POST);
- $forum->forum_name = $v->forum_name;
- $forum->forum_desc = $v->forum_desc;
- $forum->sort_by = $v->sort_by;
- $forum->redirect_url = $v->redirect_url ?? '';
- $forum->no_sum_mess = $v->no_sum_mess;
- if ($v->parent > 0) {
- $forum->parent_forum_id = $v->parent;
- $forum->cat_id = $this->c->forums->get($v->parent)->cat_id;
- } elseif ($v->parent < 0) {
- $forum->cat_id = -$v->parent;
- $forum->parent_forum_id = 0;
- }
- if ($valid) {
- if ($v->reset) {
- $message = 'Perms reverted redirect';
- $this->c->groups->Perm->reset($forum);
- } else {
- if (empty($args['id'])) {
- $message = 'Forum added redirect';
- $forum->disp_position = $this->forumPos($forum);
- $forum->moderators = '';
- $this->c->forums->insert($forum);
- } else {
- $message = 'Forum updated redirect';
- $this->c->forums->update($forum);
- }
- $this->c->groups->Perm->update($forum, $v->perms);
- }
- $this->c->forums->reset();
- return $this->c->Redirect->page('AdminForumsEdit', ['id' => $forum->id])->message($message);
- }
- $this->fIswev = $v->getErrors();
- }
- $this->nameTpl = 'admin/form';
- $this->aIndex = 'forums';
- $this->form = $this->formEdit($args, $forum, $marker);
- return $this;
- }
- /**
- * Подготавливает массив данных для формы
- */
- protected function formEdit(array $args, Forum $forum, string $marker): array
- {
- $form = [
- 'action' => $this->c->Router->link($marker, $args),
- 'hidden' => [
- 'token' => $this->c->Csrf->create($marker, $args),
- ],
- 'sets' => [],
- 'btns' => [],
- ];
- if ($forum->id > 0) {
- $form['btns']['reset'] = [
- 'type' => 'submit',
- 'value' => __('Revert to default'),
- 'class' => ['f-opacity'],
- ];
- }
- $form['btns']['submit'] = [
- 'type' => 'submit',
- 'value' => empty($forum->id) ? __('Add') : __('Update'),
- ];
- $form['sets']['forum'] = [
- 'fields' => [
- 'forum_name' => [
- 'type' => 'text',
- 'maxlength' => '80',
- 'value' => $forum->forum_name,
- 'caption' => 'Forum name label',
- 'required' => true,
- ],
- 'forum_desc' => [
- 'type' => 'textarea',
- 'value' => $forum->forum_desc,
- 'caption' => 'Forum description label',
- ],
- 'parent' => [
- 'type' => 'select',
- 'options' => $this->listForOptions,
- 'value' => $forum->parent_forum_id ? $forum->parent_forum_id : -$forum->cat_id,
- 'caption' => 'Parent label',
- 'help' => 'Parent help',
- 'required' => true,
- ],
- 'sort_by' => [
- 'type' => 'select',
- 'options' => [
- 0 => __('Last post option'),
- 1 => __('Topic start option'),
- 2 => __('Subject option'),
- ],
- 'value' => $forum->sort_by,
- 'caption' => 'Sort by label',
- ],
- 'redirect_url' => [
- 'type' => 'text',
- 'maxlength' => '255',
- 'value' => $forum->redirect_url,
- 'caption' => 'Redirect label',
- 'help' => 'Redirect help',
- 'disabled' => $forum->num_topics || $forum->subforums ? true : null,
- ],
- 'no_sum_mess' => [
- 'type' => 'radio',
- 'value' => $forum->no_sum_mess,
- 'values' => [0 => __('Yes'), 1 => __('No')],
- 'caption' => 'Count messages label',
- 'help' => ['Count messages help', $this->c->Router->link('AdminUsers'), __('Users')],
- ],
- ],
- ];
- $form['sets']['forum-info'] = [
- 'info' => [
- [
- 'value' => __(['Group permissions info', $this->c->Router->link('AdminGroups'), __('User groups')]),
- 'html' => true,
- ],
- ],
- ];
- $aOn = ['cando', 'on'];
- $aOff = ['cando', 'off'];
- foreach ($this->c->groups->Perm->get($forum) as $id => $group) {
- $fields = [];
- $fields["perms[{$id}][read_forum]"] = [
- 'class' => $group->def_read_forum ? $aOn : $aOff,
- 'type' => 'checkbox',
- 'caption' => 'Read forum label',
- 'label' => '<span></span>',
- 'checked' => $group->set_read_forum,
- 'disabled' => $group->dis_read_forum,
- ];
- $fields["perms[{$id}][post_replies]"] = [
- 'class' => $group->def_post_replies ? $aOn : $aOff,
- 'type' => 'checkbox',
- 'caption' => 'Post replies label',
- 'label' => '<span></span>',
- 'checked' => $group->set_post_replies,
- 'disabled' => $group->dis_post_replies,
- ];
- $fields["perms[{$id}][post_topics]"] = [
- 'class' => $group->def_post_topics ? $aOn : $aOff,
- 'type' => 'checkbox',
- 'caption' => 'Post topics label',
- 'label' => '<span></span>',
- 'checked' => $group->set_post_topics,
- 'disabled' => $group->dis_post_topics,
- ];
- $form['sets']["perms{$id}"] = [
- 'class' => ['permission'],
- 'legend' => $group->g_title,
- 'fields' => $fields,
- ];
- }
- return $form;
- }
- }
|