Topic.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 = $this->c->Router->link(
  113. 'Topic',
  114. [
  115. 'id' => $topic->id,
  116. 'name' => $topic->name,
  117. 'page' => $topic->page
  118. ]
  119. );
  120. $this->model = $topic;
  121. $this->crumbs = $this->crumbs($topic);
  122. $this->online = $this->c->Online->calc($this)->info();
  123. $this->stats = null;
  124. if (
  125. $topic->canReply
  126. && 1 === $this->c->config->b_quickpost
  127. ) {
  128. $this->form = $this->messageForm($topic, 'NewReply', ['id' => $topic->id], false, false, true);
  129. }
  130. if (
  131. $this->user->isAdmin
  132. || $this->user->isModerator($topic)
  133. ) {
  134. $this->c->Lang->load('misc');
  135. $this->enableMod = true;
  136. $this->formMod = $this->formMod($topic);
  137. }
  138. if ($topic->showViews) {
  139. $topic->incViews();
  140. }
  141. $topic->updateVisits();
  142. if ($this->c->config->i_feed_type > 0) {
  143. $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
  144. $this->pageHeader('feed', 'link', 0, [
  145. 'rel' => 'alternate',
  146. 'type' => "application/{$feedType}+xml",
  147. 'href' => $this->c->Router->link('Feed', ['type' => $feedType, 'tid' => $topic->id]),
  148. ]);
  149. }
  150. if (
  151. $topic->poll_type > 0
  152. && 1 === $this->c->config->b_poll_enabled
  153. ) {
  154. $this->c->Lang->load('poll');
  155. $this->poll = $topic->poll;
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Создает массив данных для формы модерации
  161. */
  162. protected function formMod(TopicModel $topic): array
  163. {
  164. $form = [
  165. 'id' => 'id-form-mod',
  166. 'action' => $this->c->Router->link('Moderate'),
  167. 'hidden' => [
  168. 'token' => $this->c->Csrf->create('Moderate'),
  169. 'forum' => $topic->parent->id,
  170. 'topic' => $topic->id,
  171. 'page' => $topic->page,
  172. 'ids' => [$topic->first_post_id => $topic->first_post_id],
  173. 'step' => 1,
  174. ],
  175. 'sets' => [],
  176. 'btns' => [],
  177. ];
  178. if ($topic->closed) {
  179. $form['btns']['open'] = [
  180. 'class' => ['origin'],
  181. 'type' => 'submit',
  182. 'value' => __(['Open topic btn', 1]),
  183. ];
  184. } else {
  185. $form['btns']['close'] = [
  186. 'class' => ['origin'],
  187. 'type' => 'submit',
  188. 'value' => __(['Close topic btn', 1]),
  189. ];
  190. }
  191. if ($topic->sticky) {
  192. $form['btns']['unstick'] = [
  193. 'class' => ['origin'],
  194. 'type' => 'submit',
  195. 'value' => __(['Unstick btn', 1]),
  196. ];
  197. } else {
  198. $form['btns']['stick'] = [
  199. 'class' => ['origin'],
  200. 'type' => 'submit',
  201. 'value' => __(['Stick btn', 1]),
  202. ];
  203. }
  204. $form['btns'] += [
  205. 'delete' => [
  206. 'class' => ['origin'],
  207. 'type' => 'submit',
  208. 'value' => __('Delete'),
  209. ],
  210. 'move' => [
  211. 'class' => ['origin'],
  212. 'type' => 'submit',
  213. 'value' => __(['Move topic btn', 1]),
  214. ],
  215. 'split' => [
  216. 'class' => ['origin'],
  217. 'type' => 'submit',
  218. 'value' => __('Split'),
  219. ],
  220. ];
  221. return $form;
  222. }
  223. }