Topic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace ForkBB\Models\Pages;
  3. use ForkBB\Models\Page;
  4. class Topic extends Page
  5. {
  6. use CrumbTrait;
  7. use PostFormTrait;
  8. /**
  9. * Переход к первому новому сообщению темы (или в конец)
  10. *
  11. * @param array $args
  12. *
  13. * @return Page
  14. */
  15. public function viewNew(array $args)
  16. {
  17. return $this->view('new', $args);
  18. }
  19. /**
  20. * Переход к первому непрочитанному сообщению (или в конец)
  21. *
  22. * @param array $args
  23. *
  24. * @return Page
  25. */
  26. public function viewUnread(array $args)
  27. {
  28. return $this->view('unread', $args);
  29. }
  30. /**
  31. * Переход к последнему сообщению темы
  32. *
  33. * @param array $args
  34. *
  35. * @return Page
  36. */
  37. public function viewLast(array $args)
  38. {
  39. return $this->view('last', $args);
  40. }
  41. /**
  42. * Просмотр темы по номеру сообщения
  43. *
  44. * @param array $args
  45. *
  46. * @return Page
  47. */
  48. public function viewPost(array $args)
  49. {
  50. return $this->view('post', $args);
  51. }
  52. /**
  53. * Просмотр темы по ее номеру
  54. *
  55. * @param array $args
  56. *
  57. * @return Page
  58. */
  59. public function viewTopic(array $args)
  60. {
  61. return $this->view('topic', $args);
  62. }
  63. /**
  64. * @param string $type
  65. * @param Models\Topic $topic
  66. *
  67. * @param Page
  68. */
  69. protected function go($type, $topic)
  70. {
  71. switch ($type) {
  72. case 'new':
  73. $pid = $topic->firstNew;
  74. break;
  75. case 'unread':
  76. $pid = $topic->firstUnread;
  77. break;
  78. case 'last':
  79. $pid = $topic->last_post_id;
  80. break;
  81. default:
  82. return $this->c->Message->message('Bad request');
  83. }
  84. return $this->c->Redirect->page('ViewPost', ['id' => $pid ?: $topic->last_post_id]);
  85. }
  86. /**
  87. * Подготовка данных для шаблона
  88. *
  89. * @param string $type
  90. * @param array $args
  91. *
  92. * @return Page
  93. */
  94. protected function view($type, array $args)
  95. {
  96. $topic = $this->c->ModelTopic->load((int) $args['id'], $type === 'post');
  97. if (empty($topic) || ! $topic->last_post_id) {
  98. return $this->c->Message->message('Bad request');
  99. }
  100. if ($topic->moved_to) {
  101. return $this->c->Redirect->page('Topic', ['id' => $topic->moved_to]);
  102. }
  103. switch ($type) {
  104. case 'topic':
  105. $topic->page = isset($args['page']) ? (int) $args['page'] : 1;
  106. break;
  107. case 'post':
  108. $topic->calcPage((int) $args['id']);
  109. break;
  110. default:
  111. return $this->go($type, $topic);
  112. }
  113. if (! $topic->hasPage()) {
  114. return $this->c->Message->message('Bad request');
  115. }
  116. if (! $posts = $topic->posts()) {
  117. return $this->go('last', $topic);
  118. }
  119. $this->c->Lang->load('topic');
  120. $this->nameTpl = 'topic';
  121. $this->onlinePos = 'topic-' . $topic->id;
  122. $this->onlineDetail = true;
  123. $this->canonical = $this->c->Router->link('Topic', ['id' => $topic->id, 'name' => $topic->cens()->subject, 'page' => $topic->page]);
  124. $this->topic = $topic;
  125. $this->posts = $posts;
  126. $this->crumbs = $this->crumbs($topic);
  127. $this->online = $this->c->Online->calc($this)->info();
  128. $this->stats = null;
  129. $this->form = $topic->canReply && $this->c->config->o_quickpost == '1'
  130. ? $this->messageForm($topic, 'NewReply', ['id' => $topic->id], false, true)
  131. : null;
  132. if ($topic->showViews) {
  133. $topic->incViews();
  134. }
  135. $topic->updateVisits();
  136. return $this;
  137. }
  138. }