PMEdit.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\PM;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\PM\AbstractPM;
  13. use ForkBB\Models\Pages\PostFormTrait;
  14. use ForkBB\Models\Pages\PostValidatorTrait;
  15. use ForkBB\Models\PM\Cnst;
  16. use ForkBB\Models\PM\PPost;
  17. use ForkBB\Models\PM\PTopic;
  18. use InvalidArgumentException;
  19. use function \ForkBB\__;
  20. class PMEdit extends AbstractPM
  21. {
  22. use PostFormTrait;
  23. use PostValidatorTrait;
  24. /**
  25. * Редактирование сообщения
  26. */
  27. public function edit(array $args, string $method): Page
  28. {
  29. if (isset($args['more2'])) {
  30. return $this->c->Message->message('Bad request');
  31. }
  32. $post = $this->pms->load(Cnst::PPOST, $args['more1']);
  33. if (
  34. ! $post instanceof PPost
  35. || ! $post->canEdit
  36. ) {
  37. return $this->c->Message->message('Bad request');
  38. }
  39. $topic = $post->parent;
  40. $firstPost = $post->id === $topic->first_post_id;
  41. $this->c->Lang->load('post');
  42. if ('POST' === $method) {
  43. $v = $this->messageValidatorPM(null, 'PMAction', $args, true, $firstPost);
  44. if (
  45. $v->validation($_POST)
  46. && null === $v->preview
  47. && null !== $v->submit
  48. ) {
  49. return $this->endEdit($post, $v);
  50. }
  51. $this->fIswev = $v->getErrors();
  52. $args['_vars'] = $v->getData();
  53. if (
  54. null !== $v->preview
  55. && ! $v->getErrors()
  56. ) {
  57. $this->previewHtml = $this->c->censorship->censor(
  58. $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies)
  59. );
  60. }
  61. } else {
  62. $args['_vars'] = [
  63. 'message' => $post->message,
  64. 'subject' => $topic->subject,
  65. 'hide_smilies' => $post->hide_smilies,
  66. ];
  67. }
  68. $this->targetUser = $topic->ztUser;
  69. $this->pms->area = $this->pms->inArea($topic);
  70. $this->pmIndex = $this->pms->area;
  71. $this->nameTpl = 'pm/post';
  72. $this->formTitle = $firstPost ? 'Edit PT title' : 'Edit PM title';
  73. $this->form = $this->messageFormPM(null, 'PMAction', $args, true, $firstPost, false);
  74. $this->pmCrumbs[] = [
  75. $this->c->Router->link('PMAction', $args),
  76. $firstPost ? 'Edit dialogue' : 'Edit message',
  77. ];
  78. $this->pmCrumbs[] = $topic;
  79. return $this;
  80. }
  81. protected function messageFormPM(?Model $model, string $marker, array $args, bool $edit, bool $first, bool $quick): array
  82. {
  83. $form = $this->messageForm($model, $marker, $args, $edit, $first, $quick);
  84. if (Cnst::ACTION_ARCHIVE === $this->pms->area) {
  85. $form['btns']['submit']['value'] = __('Save');
  86. }
  87. return $form;
  88. }
  89. protected function messageValidatorPM(?Model $model, string $marker, array $args, bool $edit, bool $first): Validator
  90. {
  91. $v = $this->messageValidator($model, $marker, $args, $edit, $first)
  92. ->addRules([
  93. 'message' => 'required|string:trim|max:65535 bytes|check_message',
  94. ])->addArguments([
  95. 'submit.check_timeout' => $this->user->u_pm_last_post,
  96. ]);
  97. return $v;
  98. }
  99. /**
  100. * Сохранение сообщения
  101. */
  102. protected function endEdit(PPost $post, Validator $v): Page
  103. {
  104. $now = \time();
  105. $topic = $post->parent;
  106. $firstPost = $post->id === $topic->first_post_id;
  107. $calcUser = false;
  108. $calcTopic = false;
  109. // текст сообщения
  110. if ($post->message !== $v->message) {
  111. $post->message = $v->message;
  112. $post->edited = $now;
  113. $calcUser = true;
  114. if ($post->id === $topic->last_post_id) {
  115. $calcTopic = true;
  116. }
  117. }
  118. // показ смайлов
  119. if (
  120. 1 === $this->c->config->b_smilies
  121. && (bool) $post->hide_smilies !== (bool) $v->hide_smilies
  122. ) {
  123. $post->hide_smilies = $v->hide_smilies ? 1 : 0;
  124. }
  125. if ($firstPost) {
  126. // заголовок темы
  127. if ($topic->subject !== $v->subject) {
  128. $topic->subject = $v->subject;
  129. $post->edited = $now;
  130. }
  131. }
  132. $this->pms->update(Cnst::PPOST, $post);
  133. // пересчет темы
  134. if ($calcTopic) {
  135. $topic->calcStat();
  136. }
  137. $this->pms->update(Cnst::PTOPIC, $topic);
  138. // антифлуд
  139. if ($calcUser) {
  140. $this->user->u_pm_last_post = $now;
  141. $this->c->users->update($this->user);
  142. }
  143. return $this->c->Redirect->url($post->link)->message('Edit redirect');
  144. }
  145. }