123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?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 Categories extends Admin
- {
- /**
- * Просмотр, редактирвоание и добавление категорий
- */
- public function view(array $args, string $method): Page
- {
- $this->c->Lang->load('validator');
- $this->c->Lang->load('admin_categories');
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminCategories',
- 'form.*.cat_name' => 'required|string:trim|max:80',
- 'form.*.disp_position' => 'required|integer|min:0|max:9999999999',
- 'new' => 'exist|string:trim|max:80'
- ])->addAliases([
- ])->addArguments([
- ])->addMessages([
- ]);
- if ($v->validation($_POST)) {
- foreach ($v->form as $key => $row) {
- $this->c->categories->set($key, $row);
- }
- $this->c->categories->update();
- if (\strlen($v->new) > 0) {
- $this->c->categories->insert($v->new);
- }
- $this->c->forums->reset();
- return $this->c->Redirect->page('AdminCategories')->message('Categories updated redirect');
- }
- $this->fIswev = $v->getErrors();
- }
- $this->nameTpl = 'admin/form';
- $this->aIndex = 'categories';
- $this->form = $this->formEdit();
- $this->classForm = ['editcategories'];
- $this->titleForm = 'Categories';
- return $this;
- }
- /**
- * Подготавливает массив данных для формы
- */
- protected function formEdit(): array
- {
- $form = [
- 'action' => $this->c->Router->link('AdminCategories'),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminCategories'),
- ],
- 'sets' => [],
- 'btns' => [
- 'submit' => [
- 'type' => 'submit',
- 'value' => __('Save changes'),
- ],
- ],
- ];
- foreach ($this->c->categories->getList() as $key => $row) {
- $fields = [];
- $fields["form[{$key}][cat_name]"] = [
- 'class' => ['name', 'category'],
- 'type' => 'text',
- 'maxlength' => '80',
- 'value' => $row['cat_name'],
- 'caption' => 'Category name label',
- 'required' => true,
- ];
- $fields["form[{$key}][disp_position]"] = [
- 'class' => ['position', 'category'],
- 'type' => 'number',
- 'min' => '0',
- 'max' => '9999999999',
- 'value' => $row['disp_position'],
- 'caption' => 'Category position label',
- ];
- $fields["delete-btn{$key}"] = [
- 'class' => ['delete', 'category'],
- 'type' => 'btn',
- 'value' => '❌',
- 'caption' => 'Delete',
- 'title' => __('Delete'),
- 'link' => $this->c->Router->link(
- 'AdminCategoriesDelete',
- [
- 'id' => $key,
- ]
- ),
- ];
- $form['sets']["category{$key}"] = [
- 'class' => ['category'],
- 'legend' => $row['cat_name'],
- 'fields' => $fields,
- ];
- }
- $form['sets']['new-cat'] = [
- 'fields' => [
- 'new' => [
- 'class' => ['new'],
- 'type' => 'text',
- 'maxlength' => '80',
- 'caption' => 'Add category label',
- 'help' => ['Add category help', $this->c->Router->link('AdminForums'), __('Forums')],
- ],
- ],
- ];
- return $form;
- }
- /**
- * Удаление категорий
- */
- public function delete(array $args, string $method): Page
- {
- $category = $this->c->categories->get($args['id']);
- if (! $category) {
- return $this->c->Message->message('Bad request');
- }
- $this->c->Lang->load('validator');
- $this->c->Lang->load('admin_categories');
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminCategoriesDelete',
- 'confirm' => 'checkbox',
- 'delete' => 'required|string',
- ])->addAliases([
- ])->addArguments([
- 'token' => $args,
- ]);
- if (
- ! $v->validation($_POST)
- || '1' !== $v->confirm
- ) {
- return $this->c->Redirect->page('AdminCategories')->message('No confirm redirect');
- }
- $this->c->categories->delete($args['id']);
- $this->c->forums->reset();
- return $this->c->Redirect->page('AdminCategories')->message('Category deleted redirect');
- }
- $this->nameTpl = 'admin/form';
- $this->aIndex = 'categories';
- $this->aCrumbs[] = [$this->c->Router->link('AdminCategoriesDelete', $args), 'Delete category head'];
- $this->aCrumbs[] = [null, ['"%s"', $category['cat_name']]];
- $this->form = $this->formDelete($args, $category);
- $this->classForm = ['deletecategory'];
- $this->titleForm = 'Delete category head';
- return $this;
- }
- /**
- * Подготавливает массив данных для формы
- */
- protected function formDelete(array $args, array $category): array
- {
- return [
- 'action' => $this->c->Router->link('AdminCategoriesDelete', $args),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminCategoriesDelete', $args),
- ],
- 'sets' => [
- 'del' => [
- 'fields' => [
- 'confirm' => [
- 'caption' => 'Confirm delete',
- 'type' => 'checkbox',
- 'label' => ['I want to delete the category %s', $category['cat_name']],
- 'checked' => false,
- ],
- ],
- ],
- 'del-info' => [
- 'info' => [
- [
- 'value' => __('Delete category warn'),
- 'html' => true,
- ],
- ],
- ],
- ],
- 'btns' => [
- 'delete' => [
- 'type' => 'submit',
- 'value' => __('Delete category'),
- ],
- 'cancel' => [
- 'type' => 'btn',
- 'value' => __('Cancel'),
- 'link' => $this->c->Router->link('AdminCategories'),
- ],
- ],
- ];
- }
- }
|