forkbb/app/Models/Post/Model.php
2020-10-14 21:46:49 +07:00

212 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace ForkBB\Models\Post;
use ForkBB\Models\DataModel;
use ForkBB\Models\Forum\Model as Forum;
use ForkBB\Models\Topic\Model as Topic;
use ForkBB\Models\User\Model as User;
use RuntimeException;
class Model extends DataModel
{
/**
* Получение родительской темы
*/
protected function getparent(): ?Topic
{
if ($this->topic_id < 1) {
throw new RuntimeException('Parent is not defined');
}
$topic = $this->c->topics->load($this->topic_id);
if (
! $topic instanceof Topic
|| $topic->moved_to
|| ! $topic->parent instanceof Forum
) {
return null;
} else {
return $topic;
}
}
/**
* Ссылка на сообщение
*/
protected function getlink(): string
{
return $this->c->Router->link(
'ViewPost',
[
'id' => $this->id,
]
);
}
/**
* Автор сообщения
*/
protected function getuser(): User
{
$user = $this->c->users->load($this->poster_id);
if (! $user instanceof User) {
throw new RuntimeException('No user data in post number ' . $this->id);
} elseif (1 === $this->poster_id) {
$user = clone $user;
$user->setAttr('email_normal', false); // заблокировать вычисление в модели User
$user->__email = $this->poster_email;
$user->__username = $this->poster;
}
return $user;
}
/**
* Статус возможности сигналить на сообщение
*/
protected function getcanReport(): bool
{
return ! $this->c->user->isAdmin && ! $this->c->user->isGuest;
}
/**
* Ссылка на страницу отправки сигнала
*/
protected function getlinkReport(): string
{
return $this->c->Router->link(
'ReportPost',
[
'id' => $this->id,
]
);
}
/**
* Статус возможности удаления
*/
protected function getcanDelete(): bool
{
if ($this->c->user->isGuest) {
return false;
} elseif (
$this->c->user->isAdmin
|| (
$this->c->user->isModerator($this)
&& ! $this->user->isAdmin
)
) {
return true;
} elseif ('1' == $this->parent->closed) {
return false;
}
return $this->user->id === $this->c->user->id
&& (
(
$this->id == $this->parent->first_post_id
&& '1' == $this->c->user->g_delete_topics
)
|| (
$this->id != $this->parent->first_post_id
&& '1' == $this->c->user->g_delete_posts
)
)
&& (
'0' == $this->c->user->g_deledit_interval
|| '1' == $this->edit_post
|| \time() - $this->posted < $this->c->user->g_deledit_interval
);
}
/**
* Ссылка на страницу удаления
*/
protected function getlinkDelete(): string
{
return $this->c->Router->link(
'DeletePost',
[
'id' => $this->id,
]
);
}
/**
* Статус возможности редактирования
*/
protected function getcanEdit(): bool
{
if ($this->c->user->isGuest) {
return false;
} elseif (
$this->c->user->isAdmin
|| (
$this->c->user->isModerator($this)
&& ! $this->user->isAdmin
)
) {
return true;
} elseif ('1' == $this->parent->closed) {
return false;
}
return $this->user->id === $this->c->user->id
&& '1' == $this->c->user->g_edit_posts
&& (
'0' == $this->c->user->g_deledit_interval
|| '1' == $this->edit_post
|| \time() - $this->posted < $this->c->user->g_deledit_interval
);
}
/**
* Ссылка на страницу редактирования
*/
protected function getlinkEdit(): string
{
return $this->c->Router->link(
'EditPost',
[
'id' => $this->id,
]
);
}
/**
* Статус возможности ответа с цитированием
*/
protected function getcanQuote(): bool
{
return $this->parent->canReply;
}
/**
* Ссылка на страницу ответа с цитированием
*/
protected function getlinkQuote(): string
{
return $this->c->Router->link(
'NewReply',
[
'id' => $this->parent->id,
'quote' => $this->id,
]
);
}
/**
* HTML код сообщения
*/
public function html(): string
{
return $this->c->censorship->censor($this->c->Parser->parseMessage($this->message, (bool) $this->hide_smilies));
}
}