123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?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\PM;
- use ForkBB\Core\Container;
- use ForkBB\Models\DataModel;
- use ForkBB\Models\PM\Cnst;
- use ForkBB\Models\PM\PTopic;
- use ForkBB\Models\User\User;
- use InvalidArgumentException;
- use RuntimeException;
- class PPost extends DataModel
- {
- /**
- * Ключ модели для контейнера
- * @var string
- */
- protected $cKey = 'PPost';
- public function __construct(Container $container)
- {
- parent::__construct($container);
- $this->zDepend = [
- 'id' => ['link', 'user', 'canDelete', 'linkDelete', 'linkEdit', 'linkQuote', 'previousId', 'linkPrevious', 'linkBlock'],
- 'edited' => ['editor'],
- 'posted' => ['canDelete', 'canEdit', 'canBlock'],
- 'poster' => ['editor'],
- 'poster_id' => ['canDelete', 'canEdit', 'canBlock', 'linkBlock'],
- 'topic_id' => ['parent', 'linkQuote', 'previousId', 'linkPrevious'],
- ];
- }
- protected function getparent(): ?PTopic
- {
- if ($this->topic_id < 1) {
- throw new RuntimeException('Parent is not defined');
- }
- return $this->c->pms->load(Cnst::PTOPIC, $this->topic_id);
- }
- public function setuser(User $user): void
- {
- if (
- $user->isGuest
- || $user->isUnverified
- ) {
- throw new InvalidArgumentException('Bad user');
- }
- $this->poster = $user->username;
- $this->poster_id = $user->id;
- }
- protected function getuser(): User
- {
- if (
- $this->poster_id < 1
- || ! ($user = $this->c->users->load($this->poster_id)) instanceof User
- ) {
- $user = $this->c->users->guest(['username' => $this->poster]);
- }
- if (! $user instanceof User) {
- throw new RuntimeException("No user data in ppost number {$this->id}");
- }
- return $user;
- }
- protected function geteditor(): string
- {
- return $this->edited > 0 ? $this->poster : '';
- }
- /**
- * Ссылка на пост
- */
- protected function getlink(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_POST,
- 'more1' => $this->id,
- 'numPost' => $this->id,
- ]
- );
- }
- /**
- * Ссылка на предыдущий пост
- */
- protected function getlinkPrevious(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_POST,
- 'more1' => $this->previousId,
- 'numPost' => $this->previousId,
- ]
- );
- }
- /**
- * Статус возможности удаления
- */
- protected function getcanDelete(): bool
- {
- return $this->parent->actionsAllowed
- && $this->poster_id === $this->c->user->id
- && $this->id !== $this->parent->first_post_id
- && $this->posted > $this->parent->{"{$this->parent->zt}_visit"};
- }
- /**
- * Ссылка на страницу удаления
- */
- protected function getlinkDelete(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_DELETE,
- 'more1' => $this->id,
- 'more2' => Cnst::ACTION_POST,
- ]
- );
- }
- /**
- * Статус возможности редактирования
- */
- protected function getcanEdit(): bool
- {
- return $this->parent->actionsAllowed
- && $this->poster_id === $this->c->user->id
- && $this->posted > $this->parent->{"{$this->parent->zt}_visit"};
- }
- /**
- * Ссылка на страницу редактирования
- */
- protected function getlinkEdit(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_EDIT,
- 'more1' => $this->id,
- ]
- );
- }
- /**
- * Статус возможности ответа с цитированием
- */
- protected function getcanQuote(): bool
- {
- return $this->parent->canReply;
- }
- /**
- * Ссылка на страницу ответа с цитированием
- */
- protected function getlinkQuote(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_SEND,
- 'more1' => $this->topic_id,
- 'more2' => $this->id,
- ]
- );
- }
- /**
- * Статус возможности (раз)блокировки
- */
- protected function getcanBlock(): bool
- {
- return $this->c->pms->block->canBlock($this->user);
- }
- /**
- * Ссылка на (раз)блокировку пользователя
- */
- protected function getlinkBlock(): string
- {
- return $this->c->Router->link(
- 'PMAction',
- [
- 'second' => $this->c->pms->second,
- 'action' => Cnst::ACTION_BLOCK,
- 'more1' => $this->poster_id,
- 'more2' => $this->id,
- ]
- );
- }
- /**
- * HTML код сообщения
- */
- public function html(): string
- {
- return $this->c->censorship->censor($this->c->Parser->parseMessage($this->message, (bool) $this->hide_smilies));
- }
- /**
- * Вычисляет номер сообщения перед данным
- */
- public function getpreviousId(): ?int
- {
- $vars = [
- ':pid' => $this->id,
- ':tid' => $this->topic_id,
- ];
- $query = "SELECT MAX(pp.id)
- FROM ::pm_posts AS pp
- WHERE pp.id < ?i:pid AND pp.topic_id=?i:tid";
- $id = (int) $this->c->DB->query($query, $vars)->fetchColumn();
- return $id ?: null;
- }
- }
|