PPost.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\PM;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\PM\Cnst;
  13. use ForkBB\Models\PM\PTopic;
  14. use ForkBB\Models\User\User;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. class PPost extends DataModel
  18. {
  19. /**
  20. * Ключ модели для контейнера
  21. * @var string
  22. */
  23. protected $cKey = 'PPost';
  24. public function __construct(Container $container)
  25. {
  26. parent::__construct($container);
  27. $this->zDepend = [
  28. 'id' => ['link', 'user', 'canDelete', 'linkDelete', 'linkEdit', 'linkQuote', 'previousId', 'linkPrevious', 'linkBlock'],
  29. 'edited' => ['editor'],
  30. 'posted' => ['canDelete', 'canEdit', 'canBlock'],
  31. 'poster' => ['editor'],
  32. 'poster_id' => ['canDelete', 'canEdit', 'canBlock', 'linkBlock'],
  33. 'topic_id' => ['parent', 'linkQuote', 'previousId', 'linkPrevious'],
  34. ];
  35. }
  36. protected function getparent(): ?PTopic
  37. {
  38. if ($this->topic_id < 1) {
  39. throw new RuntimeException('Parent is not defined');
  40. }
  41. return $this->c->pms->load(Cnst::PTOPIC, $this->topic_id);
  42. }
  43. public function setuser(User $user): void
  44. {
  45. if (
  46. $user->isGuest
  47. || $user->isUnverified
  48. ) {
  49. throw new InvalidArgumentException('Bad user');
  50. }
  51. $this->poster = $user->username;
  52. $this->poster_id = $user->id;
  53. }
  54. protected function getuser(): User
  55. {
  56. if (
  57. $this->poster_id < 1
  58. || ! ($user = $this->c->users->load($this->poster_id)) instanceof User
  59. ) {
  60. $user = $this->c->users->guest(['username' => $this->poster]);
  61. }
  62. if (! $user instanceof User) {
  63. throw new RuntimeException("No user data in ppost number {$this->id}");
  64. }
  65. return $user;
  66. }
  67. protected function geteditor(): string
  68. {
  69. return $this->edited > 0 ? $this->poster : '';
  70. }
  71. /**
  72. * Ссылка на пост
  73. */
  74. protected function getlink(): string
  75. {
  76. return $this->c->Router->link(
  77. 'PMAction',
  78. [
  79. 'second' => $this->c->pms->second,
  80. 'action' => Cnst::ACTION_POST,
  81. 'more1' => $this->id,
  82. 'numPost' => $this->id,
  83. ]
  84. );
  85. }
  86. /**
  87. * Ссылка на предыдущий пост
  88. */
  89. protected function getlinkPrevious(): string
  90. {
  91. return $this->c->Router->link(
  92. 'PMAction',
  93. [
  94. 'second' => $this->c->pms->second,
  95. 'action' => Cnst::ACTION_POST,
  96. 'more1' => $this->previousId,
  97. 'numPost' => $this->previousId,
  98. ]
  99. );
  100. }
  101. /**
  102. * Статус возможности удаления
  103. */
  104. protected function getcanDelete(): bool
  105. {
  106. return $this->parent->actionsAllowed
  107. && $this->poster_id === $this->c->user->id
  108. && $this->id !== $this->parent->first_post_id
  109. && $this->posted > $this->parent->{"{$this->parent->zt}_visit"};
  110. }
  111. /**
  112. * Ссылка на страницу удаления
  113. */
  114. protected function getlinkDelete(): string
  115. {
  116. return $this->c->Router->link(
  117. 'PMAction',
  118. [
  119. 'second' => $this->c->pms->second,
  120. 'action' => Cnst::ACTION_DELETE,
  121. 'more1' => $this->id,
  122. 'more2' => Cnst::ACTION_POST,
  123. ]
  124. );
  125. }
  126. /**
  127. * Статус возможности редактирования
  128. */
  129. protected function getcanEdit(): bool
  130. {
  131. return $this->parent->actionsAllowed
  132. && $this->poster_id === $this->c->user->id
  133. && $this->posted > $this->parent->{"{$this->parent->zt}_visit"};
  134. }
  135. /**
  136. * Ссылка на страницу редактирования
  137. */
  138. protected function getlinkEdit(): string
  139. {
  140. return $this->c->Router->link(
  141. 'PMAction',
  142. [
  143. 'second' => $this->c->pms->second,
  144. 'action' => Cnst::ACTION_EDIT,
  145. 'more1' => $this->id,
  146. ]
  147. );
  148. }
  149. /**
  150. * Статус возможности ответа с цитированием
  151. */
  152. protected function getcanQuote(): bool
  153. {
  154. return $this->parent->canReply;
  155. }
  156. /**
  157. * Ссылка на страницу ответа с цитированием
  158. */
  159. protected function getlinkQuote(): string
  160. {
  161. return $this->c->Router->link(
  162. 'PMAction',
  163. [
  164. 'second' => $this->c->pms->second,
  165. 'action' => Cnst::ACTION_SEND,
  166. 'more1' => $this->topic_id,
  167. 'more2' => $this->id,
  168. ]
  169. );
  170. }
  171. /**
  172. * Статус возможности (раз)блокировки
  173. */
  174. protected function getcanBlock(): bool
  175. {
  176. return $this->c->pms->block->canBlock($this->user);
  177. }
  178. /**
  179. * Ссылка на (раз)блокировку пользователя
  180. */
  181. protected function getlinkBlock(): string
  182. {
  183. return $this->c->Router->link(
  184. 'PMAction',
  185. [
  186. 'second' => $this->c->pms->second,
  187. 'action' => Cnst::ACTION_BLOCK,
  188. 'more1' => $this->poster_id,
  189. 'more2' => $this->id,
  190. ]
  191. );
  192. }
  193. /**
  194. * HTML код сообщения
  195. */
  196. public function html(): string
  197. {
  198. return $this->c->censorship->censor($this->c->Parser->parseMessage($this->message, (bool) $this->hide_smilies));
  199. }
  200. /**
  201. * Вычисляет номер сообщения перед данным
  202. */
  203. public function getpreviousId(): ?int
  204. {
  205. $vars = [
  206. ':pid' => $this->id,
  207. ':tid' => $this->topic_id,
  208. ];
  209. $query = "SELECT MAX(pp.id)
  210. FROM ::pm_posts AS pp
  211. WHERE pp.id < ?i:pid AND pp.topic_id=?i:tid";
  212. $id = (int) $this->c->DB->query($query, $vars)->fetchColumn();
  213. return $id ?: null;
  214. }
  215. }