Categories.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages\Admin;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Pages\Admin;
  14. use function \ForkBB\__;
  15. class Categories extends Admin
  16. {
  17. /**
  18. * Просмотр, редактирвоание и добавление категорий
  19. */
  20. public function view(array $args, string $method): Page
  21. {
  22. $this->c->Lang->load('validator');
  23. $this->c->Lang->load('admin_categories');
  24. if ('POST' === $method) {
  25. $v = $this->c->Validator->reset()
  26. ->addRules([
  27. 'token' => 'token:AdminCategories',
  28. 'form.*.cat_name' => 'required|string:trim|max:80',
  29. 'form.*.disp_position' => 'required|integer|min:0|max:9999999999',
  30. 'new' => 'exist|string:trim|max:80'
  31. ])->addAliases([
  32. ])->addArguments([
  33. ])->addMessages([
  34. ]);
  35. if ($v->validation($_POST)) {
  36. foreach ($v->form as $key => $row) {
  37. $this->c->categories->set($key, $row);
  38. }
  39. $this->c->categories->update();
  40. if (\strlen($v->new) > 0) {
  41. $this->c->categories->insert($v->new);
  42. }
  43. $this->c->forums->reset();
  44. return $this->c->Redirect->page('AdminCategories')->message('Categories updated redirect');
  45. }
  46. $this->fIswev = $v->getErrors();
  47. }
  48. $this->nameTpl = 'admin/form';
  49. $this->aIndex = 'categories';
  50. $this->form = $this->formEdit();
  51. $this->classForm = ['editcategories'];
  52. $this->titleForm = 'Categories';
  53. return $this;
  54. }
  55. /**
  56. * Подготавливает массив данных для формы
  57. */
  58. protected function formEdit(): array
  59. {
  60. $form = [
  61. 'action' => $this->c->Router->link('AdminCategories'),
  62. 'hidden' => [
  63. 'token' => $this->c->Csrf->create('AdminCategories'),
  64. ],
  65. 'sets' => [],
  66. 'btns' => [
  67. 'submit' => [
  68. 'type' => 'submit',
  69. 'value' => __('Save changes'),
  70. ],
  71. ],
  72. ];
  73. foreach ($this->c->categories->getList() as $key => $row) {
  74. $fields = [];
  75. $fields["form[{$key}][cat_name]"] = [
  76. 'class' => ['name', 'category'],
  77. 'type' => 'text',
  78. 'maxlength' => '80',
  79. 'value' => $row['cat_name'],
  80. 'caption' => 'Category name label',
  81. 'required' => true,
  82. ];
  83. $fields["form[{$key}][disp_position]"] = [
  84. 'class' => ['position', 'category'],
  85. 'type' => 'number',
  86. 'min' => '0',
  87. 'max' => '9999999999',
  88. 'value' => $row['disp_position'],
  89. 'caption' => 'Category position label',
  90. ];
  91. $fields["delete-btn{$key}"] = [
  92. 'class' => ['delete', 'category'],
  93. 'type' => 'btn',
  94. 'value' => '❌',
  95. 'caption' => 'Delete',
  96. 'title' => __('Delete'),
  97. 'link' => $this->c->Router->link(
  98. 'AdminCategoriesDelete',
  99. [
  100. 'id' => $key,
  101. ]
  102. ),
  103. ];
  104. $form['sets']["category{$key}"] = [
  105. 'class' => ['category'],
  106. 'legend' => $row['cat_name'],
  107. 'fields' => $fields,
  108. ];
  109. }
  110. $form['sets']['new-cat'] = [
  111. 'fields' => [
  112. 'new' => [
  113. 'class' => ['new'],
  114. 'type' => 'text',
  115. 'maxlength' => '80',
  116. 'caption' => 'Add category label',
  117. 'help' => ['Add category help', $this->c->Router->link('AdminForums'), __('Forums')],
  118. ],
  119. ],
  120. ];
  121. return $form;
  122. }
  123. /**
  124. * Удаление категорий
  125. */
  126. public function delete(array $args, string $method): Page
  127. {
  128. $category = $this->c->categories->get($args['id']);
  129. if (! $category) {
  130. return $this->c->Message->message('Bad request');
  131. }
  132. $this->c->Lang->load('validator');
  133. $this->c->Lang->load('admin_categories');
  134. if ('POST' === $method) {
  135. $v = $this->c->Validator->reset()
  136. ->addRules([
  137. 'token' => 'token:AdminCategoriesDelete',
  138. 'confirm' => 'checkbox',
  139. 'delete' => 'required|string',
  140. ])->addAliases([
  141. ])->addArguments([
  142. 'token' => $args,
  143. ]);
  144. if (
  145. ! $v->validation($_POST)
  146. || '1' !== $v->confirm
  147. ) {
  148. return $this->c->Redirect->page('AdminCategories')->message('No confirm redirect');
  149. }
  150. $this->c->categories->delete($args['id']);
  151. $this->c->forums->reset();
  152. return $this->c->Redirect->page('AdminCategories')->message('Category deleted redirect');
  153. }
  154. $this->nameTpl = 'admin/form';
  155. $this->aIndex = 'categories';
  156. $this->aCrumbs[] = [$this->c->Router->link('AdminCategoriesDelete', $args), 'Delete category head'];
  157. $this->aCrumbs[] = [null, ['"%s"', $category['cat_name']]];
  158. $this->form = $this->formDelete($args, $category);
  159. $this->classForm = ['deletecategory'];
  160. $this->titleForm = 'Delete category head';
  161. return $this;
  162. }
  163. /**
  164. * Подготавливает массив данных для формы
  165. */
  166. protected function formDelete(array $args, array $category): array
  167. {
  168. return [
  169. 'action' => $this->c->Router->link('AdminCategoriesDelete', $args),
  170. 'hidden' => [
  171. 'token' => $this->c->Csrf->create('AdminCategoriesDelete', $args),
  172. ],
  173. 'sets' => [
  174. 'del' => [
  175. 'fields' => [
  176. 'confirm' => [
  177. 'caption' => 'Confirm delete',
  178. 'type' => 'checkbox',
  179. 'label' => ['I want to delete the category %s', $category['cat_name']],
  180. 'checked' => false,
  181. ],
  182. ],
  183. ],
  184. 'del-info' => [
  185. 'info' => [
  186. [
  187. 'value' => __('Delete category warn'),
  188. 'html' => true,
  189. ],
  190. ],
  191. ],
  192. ],
  193. 'btns' => [
  194. 'delete' => [
  195. 'type' => 'submit',
  196. 'value' => __('Delete category'),
  197. ],
  198. 'cancel' => [
  199. 'type' => 'btn',
  200. 'value' => __('Cancel'),
  201. 'link' => $this->c->Router->link('AdminCategories'),
  202. ],
  203. ],
  204. ];
  205. }
  206. }