Topic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\Models\Page;
  11. use ForkBB\Models\Topic\Topic as TopicModel;
  12. use function \ForkBB\__;
  13. class Topic extends Page
  14. {
  15. use PostFormTrait;
  16. /**
  17. * Переход к первому новому сообщению темы (или в конец)
  18. */
  19. public function viewNew(array $args): Page
  20. {
  21. return $this->view('new', $args);
  22. }
  23. /**
  24. * Переход к первому непрочитанному сообщению (или в конец)
  25. */
  26. public function viewUnread(array $args): Page
  27. {
  28. return $this->view('unread', $args);
  29. }
  30. /**
  31. * Переход к последнему сообщению темы
  32. */
  33. public function viewLast(array $args): Page
  34. {
  35. return $this->view('last', $args);
  36. }
  37. /**
  38. * Просмотр темы по номеру сообщения
  39. */
  40. public function viewPost(array $args): Page
  41. {
  42. return $this->view('post', $args);
  43. }
  44. /**
  45. * Просмотр темы по ее номеру
  46. */
  47. public function viewTopic(array $args): Page
  48. {
  49. return $this->view('topic', $args);
  50. }
  51. /**
  52. * Просмотр
  53. */
  54. protected function go(string $type, TopicModel $topic): Page
  55. {
  56. switch ($type) {
  57. case 'new':
  58. $pid = $topic->firstNew;
  59. break;
  60. case 'unread':
  61. $pid = $topic->firstUnread;
  62. break;
  63. case 'last':
  64. $pid = $topic->last_post_id;
  65. break;
  66. default:
  67. return $this->c->Message->message('Bad request');
  68. }
  69. return $this->c->Redirect->page('ViewPost', ['id' => $pid ?: $topic->last_post_id]);
  70. }
  71. /**
  72. * Подготовка данных для шаблона
  73. */
  74. protected function view(string $type, array $args): Page
  75. {
  76. if ('post' === $type) {
  77. $post = $this->c->posts->load($args['id']);
  78. $topic = null === $post ? null : $post->parent;
  79. } else {
  80. $topic = $this->c->topics->load($args['id']);
  81. }
  82. if (! $topic instanceof TopicModel) {
  83. return $this->c->Message->message('Bad request');
  84. }
  85. if ($topic->moved_to) {
  86. return $this->c->Redirect->url($topic->link);
  87. }
  88. if (! $topic->last_post_id) {
  89. return $this->c->Message->message('Bad request');
  90. }
  91. switch ($type) {
  92. case 'topic':
  93. $topic->page = $args['page'] ?? 1;
  94. break;
  95. case 'post':
  96. $topic->calcPage($args['id']);
  97. break;
  98. default:
  99. return $this->go($type, $topic);
  100. }
  101. if (! $topic->hasPage()) {
  102. return $this->c->Message->message('Not Found', true, 404);
  103. }
  104. $this->posts = $topic->pageData();
  105. if (empty($this->posts)) { // ???? зацикливание?
  106. return $this->go('last', $topic);
  107. }
  108. $this->c->Lang->load('topic');
  109. $this->nameTpl = 'topic';
  110. $this->onlinePos = 'topic-' . $topic->id;
  111. $this->onlineDetail = true;
  112. $this->canonical = $topic->link;
  113. $this->model = $topic;
  114. $this->crumbs = $this->crumbs($topic);
  115. $this->online = $this->c->Online->calc($this)->info();
  116. $this->stats = null;
  117. $this->useMediaJS = true;
  118. if (
  119. $topic->canReply
  120. && 1 === $this->c->config->b_quickpost
  121. ) {
  122. $this->form = $this->messageForm($topic, 'NewReply', ['id' => $topic->id], false, false, true);
  123. }
  124. if (
  125. $this->user->isAdmin
  126. || $this->user->isModerator($topic)
  127. ) {
  128. $this->c->Lang->load('misc');
  129. $this->enableMod = true;
  130. $this->formMod = $this->formMod($topic);
  131. }
  132. if ($topic->showViews) {
  133. $topic->incViews();
  134. }
  135. $topic->updateVisits();
  136. if ($this->c->config->i_feed_type > 0) {
  137. $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
  138. $this->pageHeader('feed', 'link', 0, [
  139. 'rel' => 'alternate',
  140. 'type' => "application/{$feedType}+xml",
  141. 'href' => $this->c->Router->link('Feed', ['type' => $feedType, 'tid' => $topic->id]),
  142. ]);
  143. }
  144. if (
  145. $topic->poll_type > 0
  146. && 1 === $this->c->config->b_poll_enabled
  147. ) {
  148. $this->c->Lang->load('poll');
  149. $this->poll = $topic->poll;
  150. }
  151. return $this;
  152. }
  153. /**
  154. * Создает массив данных для формы модерации
  155. */
  156. protected function formMod(TopicModel $topic): array
  157. {
  158. $form = [
  159. 'id' => 'id-form-mod',
  160. 'action' => $this->c->Router->link('Moderate'),
  161. 'hidden' => [
  162. 'token' => $this->c->Csrf->create('Moderate'),
  163. 'forum' => $topic->parent->id,
  164. 'topic' => $topic->id,
  165. 'page' => $topic->page,
  166. 'ids' => [$topic->first_post_id => $topic->first_post_id],
  167. 'step' => 1,
  168. ],
  169. 'sets' => [],
  170. 'btns' => [],
  171. ];
  172. if ($topic->closed) {
  173. $form['btns']['open'] = [
  174. 'class' => ['origin'],
  175. 'type' => 'submit',
  176. 'value' => __(['Open topic btn', 1]),
  177. ];
  178. } else {
  179. $form['btns']['close'] = [
  180. 'class' => ['origin'],
  181. 'type' => 'submit',
  182. 'value' => __(['Close topic btn', 1]),
  183. ];
  184. }
  185. if ($topic->sticky) {
  186. $form['btns']['unstick'] = [
  187. 'class' => ['origin'],
  188. 'type' => 'submit',
  189. 'value' => __(['Unstick btn', 1]),
  190. ];
  191. } else {
  192. $form['btns']['stick'] = [
  193. 'class' => ['origin'],
  194. 'type' => 'submit',
  195. 'value' => __(['Stick btn', 1]),
  196. ];
  197. }
  198. $form['btns'] += [
  199. 'delete' => [
  200. 'class' => ['origin'],
  201. 'type' => 'submit',
  202. 'value' => __('Delete'),
  203. ],
  204. 'move' => [
  205. 'class' => ['origin'],
  206. 'type' => 'submit',
  207. 'value' => __(['Move topic btn', 1]),
  208. ],
  209. 'split' => [
  210. 'class' => ['origin'],
  211. 'type' => 'submit',
  212. 'value' => __('Split'),
  213. ],
  214. ];
  215. return $form;
  216. }
  217. }