Mod.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Profile;
  10. use ForkBB\Core\Image;
  11. use ForkBB\Core\Validator;
  12. use ForkBB\Models\Page;
  13. use ForkBB\Models\Pages\Profile;
  14. use ForkBB\Models\User\User;
  15. use ForkBB\Models\Forum\Forum;
  16. use function \ForkBB\__;
  17. class Mod extends Profile
  18. {
  19. /**
  20. * Подготавливает данные для шаблона конфигурации прав модератора
  21. */
  22. public function moderation(array $args, string $method): Page
  23. {
  24. if (
  25. false === $this->initProfile($args['id'])
  26. || ! $this->rules->confModer
  27. ) {
  28. return $this->c->Message->message('Bad request');
  29. }
  30. $this->c->Lang->load('validator');
  31. if ('POST' === $method) {
  32. $v = $this->c->Validator->reset()
  33. ->addValidators([
  34. ])->addRules([
  35. 'token' => 'token:EditUserModeration',
  36. 'moderator.*' => 'integer|in:' . \implode(',', \array_keys($this->curForums)),
  37. 'save' => 'required|string',
  38. ])->addAliases([
  39. ])->addArguments([
  40. 'token' => $args,
  41. ])->addMessages([
  42. ]);
  43. if ($v->validation($_POST)) {
  44. foreach ($this->c->forums->get(0)->descendants as $forum) {
  45. if (
  46. isset($v->moderator[$forum->id])
  47. && $v->moderator[$forum->id] === $forum->id
  48. ) {
  49. $forum->modAdd($this->curUser);
  50. } else {
  51. $forum->modDelete($this->curUser);
  52. }
  53. $this->c->forums->update($forum);
  54. }
  55. $this->c->forums->reset();
  56. return $this->c->Redirect->page('EditUserModeration', $args)->message('Update rights redirect');
  57. }
  58. $this->fIswev = $v->getErrors();
  59. }
  60. $this->crumbs = $this->crumbs(
  61. [
  62. $this->c->Router->link('EditUserModeration', $args),
  63. 'Moderator rights',
  64. ],
  65. [
  66. $this->c->Router->link('EditUserProfile', $args),
  67. 'Editing profile',
  68. ]
  69. );
  70. $this->form = $this->form($args);
  71. $this->actionBtns = $this->btns('edit');
  72. return $this;
  73. }
  74. /**
  75. * Возвращает список доступных разделов для пользователя текущего профиля
  76. */
  77. protected function getcurForums(): array
  78. {
  79. $root = $this->c->ForumManager->init($this->c->groups->get($this->curUser->group_id))->get(0);
  80. return $root instanceof Forum ? $root->descendants : [];
  81. }
  82. /**
  83. * Создает массив данных для формы
  84. */
  85. protected function form(array $args): array
  86. {
  87. $form = [
  88. 'action' => $this->c->Router->link('EditUserModeration', $args),
  89. 'hidden' => [
  90. 'token' => $this->c->Csrf->create('EditUserModeration', $args),
  91. ],
  92. 'sets' => [],
  93. 'btns' => [
  94. 'save' => [
  95. 'type' => 'submit',
  96. 'value' => __('Save'),
  97. ],
  98. ],
  99. ];
  100. $root = $this->c->forums->get(0);
  101. if ($root instanceof Forum) {
  102. $list = $this->c->forums->depthList($root, 0);
  103. $cid = null;
  104. foreach ($list as $forum) {
  105. if ($cid !== $forum->cat_id) {
  106. $form['sets']["category{$forum->cat_id}-info"] = [
  107. 'info' => [
  108. [
  109. 'value' => $forum->cat_name,
  110. ],
  111. ],
  112. ];
  113. $cid = $forum->cat_id;
  114. }
  115. $fields = [];
  116. $fields["name{$forum->id}"] = [
  117. 'class' => ['modforum', 'name', 'depth' . $forum->depth],
  118. 'type' => 'str',
  119. 'value' => $forum->forum_name,
  120. 'caption' => 'Forum label',
  121. ];
  122. $fields["moderator[{$forum->id}]"] = [
  123. 'class' => ['modforum', 'moderator'],
  124. 'type' => 'checkbox',
  125. 'value' => $forum->id,
  126. 'checked' => isset($this->curForums[$forum->id]) && $this->curUser->isModerator($forum),
  127. 'disabled' => ! isset($this->curForums[$forum->id]) || '' != $this->curForums[$forum->id]->redirect_url,
  128. 'caption' => 'Moderator label',
  129. ];
  130. $form['sets']["forum{$forum->id}"] = [
  131. 'class' => ['modforum'],
  132. 'legend' => $forum->cat_name . ' / ' . $forum->forum_name,
  133. 'fields' => $fields,
  134. ];
  135. }
  136. }
  137. return $form;
  138. }
  139. }