Edit.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\PostFormTrait;
  13. use ForkBB\Models\Pages\PostValidatorTrait;
  14. use ForkBB\Models\Poll\Poll;
  15. use ForkBB\Models\Post\Post;
  16. use ForkBB\Models\Topic\Topic;
  17. use function \ForkBB\__;
  18. class Edit extends Page
  19. {
  20. use PostFormTrait;
  21. use PostValidatorTrait;
  22. /**
  23. * Редактирование сообщения
  24. */
  25. public function edit(array $args, string $method): Page
  26. {
  27. $post = $this->c->posts->load($args['id']);
  28. if (
  29. ! $post instanceof Post
  30. || ! $post->canEdit
  31. ) {
  32. return $this->c->Message->message('Bad request');
  33. }
  34. $topic = $post->parent;
  35. $firstPost = $post->id === $topic->first_post_id;
  36. $this->c->Lang->load('post');
  37. if (1 === $this->c->config->b_poll_enabled) {
  38. $this->c->Lang->load('poll');
  39. }
  40. if ('POST' === $method) {
  41. $v = $this->messageValidator($post, 'EditPost', $args, true, $firstPost);
  42. if (
  43. $v->validation($_POST)
  44. && null === $v->preview
  45. && null !== $v->submit
  46. ) {
  47. return $this->endEdit($post, $v);
  48. }
  49. $this->fIswev = $v->getErrors();
  50. $args['_vars'] = $v->getData();
  51. if (
  52. null !== $v->preview
  53. && ! $v->getErrors()
  54. ) {
  55. $this->previewHtml = $this->c->censorship->censor(
  56. $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies)
  57. );
  58. if (
  59. $firstPost
  60. && $this->user->usePoll
  61. && $v->poll_enable
  62. ) {
  63. $this->poll = $this->c->polls->create($v->poll);
  64. $this->c->polls->revision($this->poll, true);
  65. }
  66. }
  67. } else {
  68. $args['_vars'] = [
  69. 'message' => $post->message,
  70. 'subject' => $topic->subject,
  71. 'hide_smilies' => $post->hide_smilies,
  72. 'stick_topic' => $topic->sticky,
  73. 'stick_fp' => $topic->stick_fp,
  74. 'edit_post' => $post->edit_post,
  75. ];
  76. }
  77. if (
  78. $firstPost
  79. && $this->user->usePoll
  80. ) {
  81. $poll = $topic->poll;
  82. if (
  83. $poll instanceof Poll
  84. && (
  85. ! $poll->canEdit
  86. || 'POST' !== $method
  87. )
  88. ) {
  89. $args['_vars'] = \array_merge($args['_vars'], [
  90. 'pollNoEdit' => ! $poll->canEdit,
  91. 'poll_enable' => $topic->poll_type > 0,
  92. 'poll' => [
  93. 'duration' => $topic->poll_type > 1000 ? $topic->poll_type - 1000 : 0, // ???? перенести в модель poll?
  94. 'hide_result' => $topic->poll_term > 0,
  95. 'question' => $poll->question,
  96. 'type' => $poll->type,
  97. 'answer' => $poll->answer,
  98. ],
  99. ]);
  100. }
  101. if (
  102. null !== $this->previewHtml
  103. && $args['_vars']['poll_enable']
  104. ) {
  105. $this->poll = $this->c->polls->create($args['_vars']['poll']);
  106. $this->c->polls->revision($this->poll, true);
  107. }
  108. }
  109. $this->nameTpl = 'post';
  110. $this->onlinePos = 'topic-' . $topic->id;
  111. $this->canonical = $post->linkEdit;
  112. $this->robots = 'noindex';
  113. $this->formTitle = $firstPost ? 'Edit topic' : 'Edit post';
  114. $this->crumbs = $this->crumbs($this->formTitle, $topic);
  115. $this->form = $this->messageForm($post, 'EditPost', $args, true, $firstPost, false);
  116. return $this;
  117. }
  118. /**
  119. * Сохранение сообщения
  120. */
  121. protected function endEdit(Post $post, Validator $v): Page
  122. {
  123. $now = \time();
  124. $executive = $this->user->isAdmin || $this->user->isModerator($post);
  125. $topic = $post->parent;
  126. $firstPost = $post->id === $topic->first_post_id;
  127. $calcPost = false;
  128. $calcTopic = false;
  129. $calcForum = false;
  130. // текст сообщения
  131. if ($post->message !== $v->message) {
  132. $post->message = $v->message;
  133. $post->edited = $now;
  134. $post->editor = $this->user->username;
  135. $post->editor_id = $this->user->id;
  136. $calcPost = true;
  137. if ($post->id === $topic->last_post_id) {
  138. $calcTopic = true;
  139. $calcForum = true;
  140. }
  141. }
  142. // показ смайлов
  143. if (
  144. 1 === $this->c->config->b_smilies
  145. && (bool) $post->hide_smilies !== (bool) $v->hide_smilies
  146. ) {
  147. $post->hide_smilies = $v->hide_smilies ? 1 : 0;
  148. }
  149. // редактирование без ограничений
  150. if (
  151. $executive
  152. && (bool) $post->edit_post !== (bool) $v->edit_post
  153. ) {
  154. $post->edit_post = $v->edit_post ? 1 : 0;
  155. }
  156. if ($firstPost) {
  157. // заголовок темы
  158. if ($topic->subject !== $v->subject) {
  159. $topic->subject = $v->subject;
  160. $post->edited = $now;
  161. $post->editor = $this->user->username;
  162. $post->editor_id = $this->user->id;
  163. $calcForum = true;
  164. }
  165. // выделение темы
  166. if (
  167. $executive
  168. && (bool) $topic->sticky !== (bool) $v->stick_topic
  169. ) {
  170. $topic->sticky = $v->stick_topic ? 1 : 0;
  171. }
  172. // закрепление первого сообшения
  173. if (
  174. $executive
  175. && (bool) $topic->stick_fp !== (bool) $v->stick_fp
  176. ) {
  177. $topic->stick_fp = $v->stick_fp ? 1 : 0;
  178. }
  179. // опрос
  180. if ($this->user->usePoll) {
  181. $this->changePoll($topic, $v);
  182. }
  183. }
  184. // обновление сообщения
  185. $this->c->posts->update($post);
  186. // обновление темы
  187. if ($calcTopic) {
  188. $topic->calcStat();
  189. }
  190. $this->c->topics->update($topic);
  191. // обновление раздела
  192. if ($calcForum) {
  193. $topic->parent->calcStat();
  194. }
  195. $this->c->forums->update($topic->parent);
  196. // антифлуд
  197. if (
  198. $calcPost
  199. || $calcForum
  200. ) {
  201. $this->user->last_post = $now; //????
  202. $this->c->users->update($this->user);
  203. }
  204. $this->c->search->index($post, 'edit');
  205. return $this->c->Redirect->url($post->link)->message('Edit redirect');
  206. }
  207. /**
  208. * Изменяет(удаляет/добавляет) данные опроса
  209. */
  210. protected function changePoll(Topic $topic, Validator $v): void
  211. {
  212. if ($topic->poll_type > 0 ) {
  213. $poll = $topic->poll;
  214. if (! $poll->canEdit) {
  215. return;
  216. }
  217. // редактирование
  218. if ($v->poll_enable) {
  219. $topic->poll_type = $v->poll['duration'] > 0 ? 1000 + $v->poll['duration'] : 1; // ???? перенести в модель poll?
  220. # $topic->poll_time = 0;
  221. $topic->poll_term = $v->poll['hide_result']
  222. ? ($topic->poll_term ?: $this->c->config->i_poll_term)
  223. : 0;
  224. $poll->__question = $v->poll['question'];
  225. $poll->__answer = $v->poll['answer'];
  226. $poll->__type = $v->poll['type'];
  227. $this->c->polls->update($poll);
  228. // удаление
  229. } else {
  230. $topic->poll_type = 0;
  231. $topic->poll_time = 0;
  232. $topic->poll_term = 0;
  233. $this->c->polls->delete($poll);
  234. }
  235. // добавление
  236. } elseif ($v->poll_enable) {
  237. $topic->poll_type = $v->poll['duration'] > 0 ? 1000 + $v->poll['duration'] : 1; // ???? перенести в модель poll?
  238. $topic->poll_time = \time();
  239. $topic->poll_term = $v->poll['hide_result'] ? $this->c->config->i_poll_term : 0;
  240. $poll = $this->c->polls->create([
  241. 'tid' => $topic->id,
  242. 'question' => $v->poll['question'],
  243. 'answer' => $v->poll['answer'],
  244. 'type' => $v->poll['type'],
  245. ]);
  246. $this->c->polls->insert($poll);
  247. }
  248. }
  249. }