123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * This file is part of the ForkBB <https://github.com/forkbb>.
- *
- * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
- * @license The MIT License (MIT)
- */
- declare(strict_types=1);
- namespace ForkBB\Models\Pages;
- use ForkBB\Models\Page;
- use ForkBB\Models\Topic\Topic as TopicModel;
- use function \ForkBB\__;
- class Topic extends Page
- {
- use PostFormTrait;
- /**
- * Переход к первому новому сообщению темы (или в конец)
- */
- public function viewNew(array $args): Page
- {
- return $this->view('new', $args);
- }
- /**
- * Переход к первому непрочитанному сообщению (или в конец)
- */
- public function viewUnread(array $args): Page
- {
- return $this->view('unread', $args);
- }
- /**
- * Переход к последнему сообщению темы
- */
- public function viewLast(array $args): Page
- {
- return $this->view('last', $args);
- }
- /**
- * Просмотр темы по номеру сообщения
- */
- public function viewPost(array $args): Page
- {
- return $this->view('post', $args);
- }
- /**
- * Просмотр темы по ее номеру
- */
- public function viewTopic(array $args): Page
- {
- return $this->view('topic', $args);
- }
- /**
- * Просмотр
- */
- protected function go(string $type, TopicModel $topic): Page
- {
- switch ($type) {
- case 'new':
- $pid = $topic->firstNew;
- break;
- case 'unread':
- $pid = $topic->firstUnread;
- break;
- case 'last':
- $pid = $topic->last_post_id;
- break;
- default:
- return $this->c->Message->message('Bad request');
- }
- return $this->c->Redirect->page('ViewPost', ['id' => $pid ?: $topic->last_post_id]);
- }
- /**
- * Подготовка данных для шаблона
- */
- protected function view(string $type, array $args): Page
- {
- if ('post' === $type) {
- $post = $this->c->posts->load($args['id']);
- $topic = null === $post ? null : $post->parent;
- } else {
- $topic = $this->c->topics->load($args['id']);
- }
- if (! $topic instanceof TopicModel) {
- return $this->c->Message->message('Bad request');
- }
- if ($topic->moved_to) {
- return $this->c->Redirect->url($topic->link);
- }
- if (! $topic->last_post_id) {
- return $this->c->Message->message('Bad request');
- }
- switch ($type) {
- case 'topic':
- $topic->page = $args['page'] ?? 1;
- break;
- case 'post':
- $topic->calcPage($args['id']);
- break;
- default:
- return $this->go($type, $topic);
- }
- if (! $topic->hasPage()) {
- return $this->c->Message->message('Not Found', true, 404);
- }
- $this->posts = $topic->pageData();
- if (empty($this->posts)) { // ???? зацикливание?
- return $this->go('last', $topic);
- }
- $this->c->Lang->load('topic');
- $this->nameTpl = 'topic';
- $this->onlinePos = 'topic-' . $topic->id;
- $this->onlineDetail = true;
- $this->canonical = $this->c->Router->link(
- 'Topic',
- [
- 'id' => $topic->id,
- 'name' => $topic->name,
- 'page' => $topic->page
- ]
- );
- $this->model = $topic;
- $this->crumbs = $this->crumbs($topic);
- $this->online = $this->c->Online->calc($this)->info();
- $this->stats = null;
- if (
- $topic->canReply
- && 1 === $this->c->config->b_quickpost
- ) {
- $this->form = $this->messageForm($topic, 'NewReply', ['id' => $topic->id], false, false, true);
- }
- if (
- $this->user->isAdmin
- || $this->user->isModerator($topic)
- ) {
- $this->c->Lang->load('misc');
- $this->enableMod = true;
- $this->formMod = $this->formMod($topic);
- }
- if ($topic->showViews) {
- $topic->incViews();
- }
- $topic->updateVisits();
- if ($this->c->config->i_feed_type > 0) {
- $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
- $this->pageHeader('feed', 'link', 0, [
- 'rel' => 'alternate',
- 'type' => "application/{$feedType}+xml",
- 'href' => $this->c->Router->link('Feed', ['type' => $feedType, 'tid' => $topic->id]),
- ]);
- }
- if (
- $topic->poll_type > 0
- && 1 === $this->c->config->b_poll_enabled
- ) {
- $this->c->Lang->load('poll');
- $this->poll = $topic->poll;
- }
- return $this;
- }
- /**
- * Создает массив данных для формы модерации
- */
- protected function formMod(TopicModel $topic): array
- {
- $form = [
- 'id' => 'id-form-mod',
- 'action' => $this->c->Router->link('Moderate'),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('Moderate'),
- 'forum' => $topic->parent->id,
- 'topic' => $topic->id,
- 'page' => $topic->page,
- 'ids' => [$topic->first_post_id => $topic->first_post_id],
- 'step' => 1,
- ],
- 'sets' => [],
- 'btns' => [],
- ];
- if ($topic->closed) {
- $form['btns']['open'] = [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __(['Open topic btn', 1]),
- ];
- } else {
- $form['btns']['close'] = [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __(['Close topic btn', 1]),
- ];
- }
- if ($topic->sticky) {
- $form['btns']['unstick'] = [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __(['Unstick btn', 1]),
- ];
- } else {
- $form['btns']['stick'] = [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __(['Stick btn', 1]),
- ];
- }
- $form['btns'] += [
- 'delete' => [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __('Delete'),
- ],
- 'move' => [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __(['Move topic btn', 1]),
- ],
- 'split' => [
- 'class' => ['origin'],
- 'type' => 'submit',
- 'value' => __('Split'),
- ],
- ];
- return $form;
- }
- }
|