PMTopic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Models\Page;
  11. use ForkBB\Models\Pages\PM\AbstractPM;
  12. use ForkBB\Models\Pages\PostFormTrait;
  13. use ForkBB\Models\PM\Cnst;
  14. use ForkBB\Models\PM\PPost;
  15. use ForkBB\Models\PM\PTopic;
  16. use function \ForkBB\__;
  17. class PMTopic extends AbstractPM
  18. {
  19. use PostFormTrait;
  20. /**
  21. * Отображает приватный диалог по номеру поста
  22. */
  23. public function post(array $args, string $method): Page
  24. {
  25. if (
  26. ! isset($args['more1'])
  27. || isset($args['more2'])
  28. ) {
  29. return $this->c->Message->message('Bad request');
  30. }
  31. $post = $this->pms->load(Cnst::PPOST, $args['more1']);
  32. if (! $post instanceof PPost) {
  33. return $this->c->Message->message('Not Found', true, 404);
  34. }
  35. $this->model = $post->parent;
  36. $this->model->calcPage($post->id);
  37. return $this->view($args, $method);
  38. }
  39. /**
  40. * Отображает приватный диалог по его номеру
  41. */
  42. public function topic(array $args, string $method): Page
  43. {
  44. if (! isset($args['more1'])) {
  45. return $this->c->Message->message('Bad request');
  46. }
  47. $this->model = $this->pms->load(Cnst::PTOPIC, $args['more1']);
  48. if (! $this->model instanceof PTopic) {
  49. return $this->c->Message->message('Not Found', true, 404);
  50. }
  51. if (! isset($args['more2'])) {
  52. $this->model->page = 1;
  53. } elseif (Cnst::ACTION_NEW === $args['more2']) {
  54. $new = $this->model->firstNew;
  55. if ($new > 0) {
  56. $new = $this->pms->load(Cnst::PPOST, $new);
  57. }
  58. return $this->c->Redirect->url($new instanceof PPost ? $new->link : $this->model->linkLast);
  59. } elseif (Cnst::ACTION_SEND === $args['more2']) {
  60. return $this->send($args, $method);
  61. } elseif ('' === \trim($args['more2'], '1234567890')) {
  62. $this->model->page = (int) $args['more2'];
  63. } else {
  64. return $this->c->Message->message('Not Found', true, 404);
  65. }
  66. return $this->view($args, $method);
  67. }
  68. /**
  69. * Подготовка формы и отправка диалога
  70. */
  71. protected function send(array $args, string $method): Page
  72. {
  73. if (! $this->model->canSend) {
  74. return $this->c->Message->message('Bad request');
  75. }
  76. $this->args = $args;
  77. $this->targetUser = $this->model->ztUser;
  78. if ('POST' === $method) {
  79. $v = $this->c->Validator->reset()
  80. ->addRules([
  81. 'token' => 'token:PMAction',
  82. 'confirm' => 'checkbox',
  83. 'send' => 'required|string',
  84. ])->addAliases([
  85. ])->addArguments([
  86. 'token' => $args,
  87. ]);
  88. if (
  89. ! $v->validation($_POST)
  90. || '1' !== $v->confirm
  91. ) {
  92. return $this->c->Redirect->url($this->model->link)->message('No confirm redirect');
  93. }
  94. $this->model->poster_status = Cnst::PT_NORMAL; //????
  95. $this->model->target_status = Cnst::PT_NORMAL; //????
  96. $this->pms->update(Cnst::PTOPIC, $this->model);
  97. $this->pms->recalculate($this->targetUser);
  98. $this->pms->recalculate($this->user);
  99. return $this->c->Redirect->url($this->model->link)->message('Send dialogue redirect');
  100. }
  101. $this->pms->area = $this->pms->inArea($this->model);
  102. $this->pmIndex = $this->pms->area;
  103. $this->nameTpl = 'pm/post';
  104. $this->formTitle = 'Send PT title';
  105. $this->form = $this->formSend($args);
  106. $this->postsTitle = 'Send info';
  107. $this->posts = [$this->pms->load(Cnst::PPOST, $this->model->first_post_id)];
  108. $this->pmCrumbs[] = [$this->c->Router->link('PMAction', $args), 'Send dialogue'];
  109. $this->pmCrumbs[] = $this->model;
  110. return $this;
  111. }
  112. /**
  113. * Подготавливает массив данных для формы
  114. */
  115. protected function formSend(array $args): array
  116. {
  117. return [
  118. 'action' => $this->c->Router->link('PMAction', $args),
  119. 'hidden' => [
  120. 'token' => $this->c->Csrf->create('PMAction', $args),
  121. ],
  122. 'sets' => [
  123. 'info' => [
  124. 'info' => [
  125. [
  126. 'value' => __(['Dialogue %s', $this->model->name]),
  127. ],
  128. [
  129. 'value' => __(['Recipient: %s', $this->targetUser->username]),
  130. ],
  131. ],
  132. ],
  133. 'confirm' => [
  134. 'fields' => [
  135. 'confirm' => [
  136. 'type' => 'checkbox',
  137. 'label' => 'Confirm action',
  138. 'checked' => false,
  139. ],
  140. ],
  141. ],
  142. ],
  143. 'btns' => [
  144. 'send' => [
  145. 'type' => 'submit',
  146. 'value' => __('Send dialogue'),
  147. ],
  148. 'cancel' => [
  149. 'type' => 'btn',
  150. 'value' => __('Cancel'),
  151. 'link' => $this->model->link,
  152. ],
  153. ],
  154. ];
  155. }
  156. /**
  157. * Подготовка данных для шаблона
  158. */
  159. protected function view(array $args, string $method): Page
  160. {
  161. if (! $this->model->hasPage()) {
  162. return $this->c->Message->message('Not Found', true, 404);
  163. }
  164. $this->posts = $this->model->pageData();
  165. if (
  166. empty($this->posts)
  167. && $this->model->page > 1
  168. ) {
  169. return $this->c->Redirect->url($this->model->link);
  170. }
  171. $this->c->Lang->load('topic');
  172. $this->args = $args;
  173. $this->targetUser = $this->model->ztUser;
  174. $this->pms->area = $this->pms->inArea($this->model);
  175. $this->pmIndex = $this->pms->area;
  176. $this->nameTpl = 'pm/topic';
  177. $this->pmCrumbs[] = $this->model;
  178. if (
  179. $this->model->canReply
  180. && 1 === $this->c->config->b_quickpost
  181. ) {
  182. $form = $this->messageForm(null, 'PMAction', $this->model->dataReply, false, false, true);
  183. if (Cnst::ACTION_ARCHIVE === $this->pmIndex) {
  184. $form['btns']['submit']['value'] = __('Save');
  185. }
  186. $this->form = $form;
  187. }
  188. $this->model->updateVisit();
  189. return $this;
  190. }
  191. }