Model.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace ForkBB\Models\Topic;
  3. use ForkBB\Models\DataModel;
  4. use PDO;
  5. use RuntimeException;
  6. class Model extends DataModel
  7. {
  8. /**
  9. * Получение родительского раздела
  10. *
  11. * @throws RuntimeException
  12. *
  13. * @return Forum
  14. */
  15. protected function getparent()
  16. {
  17. if ($this->forum_id < 1) {
  18. throw new RuntimeException('Parent is not defined');
  19. }
  20. return $this->c->forums->get($this->forum_id);
  21. }
  22. /**
  23. * Статус возможности ответа в теме
  24. *
  25. * @return bool
  26. */
  27. protected function getcanReply()
  28. {
  29. if ($this->c->user->isAdmin) {
  30. return true;
  31. } elseif ($this->closed || $this->c->user->isBot) {
  32. return false;
  33. } elseif ($this->parent->post_replies == '1'
  34. || (null === $this->parent->post_replies && $this->c->user->g_post_replies == '1')
  35. || $this->c->user->isModerator($this)
  36. ) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. /**
  43. * Ссылка на тему
  44. *
  45. * @return string
  46. */
  47. protected function getlink()
  48. {
  49. return $this->c->Router->link('Topic', ['id' => $this->moved_to ?: $this->id, 'name' => \ForkBB\cens($this->subject)]);
  50. }
  51. /**
  52. * Ссылка для ответа в теме
  53. *
  54. * @return string
  55. */
  56. protected function getlinkReply()
  57. {
  58. return $this->c->Router->link('NewReply', ['id' => $this->id]);
  59. }
  60. /**
  61. * Ссылка для перехода на последнее сообщение темы
  62. *
  63. * @return null|string
  64. */
  65. protected function getlinkLast()
  66. {
  67. if ($this->moved_to) {
  68. return null;
  69. } else {
  70. return $this->c->Router->link('ViewPost', ['id' => $this->last_post_id]);
  71. }
  72. }
  73. /**
  74. * Ссылка для перехода на первое новое сообщение в теме
  75. *
  76. * @return string
  77. */
  78. protected function getlinkNew()
  79. {
  80. return $this->c->Router->link('TopicViewNew', ['id' => $this->id]);
  81. }
  82. /**
  83. * Ссылка для перехода на первое не прочитанное сообщение в теме
  84. */
  85. protected function getlinkUnread()
  86. {
  87. return $this->c->Router->link('TopicViewUnread', ['id' => $this->id]);
  88. }
  89. /**
  90. * Статус наличия новых сообщений в теме
  91. *
  92. * @return false|int
  93. */
  94. protected function gethasNew()
  95. {
  96. if ($this->c->user->isGuest || $this->moved_to) {
  97. return false;
  98. }
  99. $time = \max(
  100. (int) $this->c->user->u_mark_all_read,
  101. (int) $this->parent->mf_mark_all_read,
  102. (int) $this->c->user->last_visit,
  103. (int) $this->mt_last_visit
  104. );
  105. return $this->last_post > $time ? $time : false;
  106. }
  107. /**
  108. * Статус наличия непрочитанных сообщений в теме
  109. *
  110. * @return false|int
  111. */
  112. protected function gethasUnread()
  113. {
  114. if ($this->c->user->isGuest || $this->moved_to) {
  115. return false;
  116. }
  117. $time = \max(
  118. (int) $this->c->user->u_mark_all_read,
  119. (int) $this->parent->mf_mark_all_read,
  120. (int) $this->mt_last_read
  121. );
  122. return $this->last_post > $time ? $time : false;
  123. }
  124. /**
  125. * Номер первого нового сообщения в теме
  126. *
  127. * @return int
  128. */
  129. protected function getfirstNew()
  130. {
  131. if (false === $this->hasNew) {
  132. return 0;
  133. }
  134. $vars = [
  135. ':tid' => $this->id,
  136. ':visit' => $this->hasNew,
  137. ];
  138. $sql = 'SELECT MIN(p.id) FROM ::posts AS p WHERE p.topic_id=?i:tid AND p.posted>?i:visit';
  139. $pid = $this->c->DB->query($sql, $vars)->fetchColumn();
  140. return $pid ?: 0;
  141. }
  142. /**
  143. * Номер первого не прочитанного сообщения в теме
  144. *
  145. * @return int
  146. */
  147. protected function getfirstUnread()
  148. {
  149. if (false === $this->hasUnread) {
  150. return 0;
  151. }
  152. $vars = [
  153. ':tid' => $this->id,
  154. ':visit' => $this->hasUnread,
  155. ];
  156. $sql = 'SELECT MIN(p.id) FROM ::posts AS p WHERE p.topic_id=?i:tid AND p.posted>?i:visit';
  157. $pid = $this->c->DB->query($sql, $vars)->fetchColumn();
  158. return $pid ?: 0;
  159. }
  160. /**
  161. * Количество страниц в теме
  162. *
  163. * @throws RuntimeException
  164. *
  165. * @return int
  166. */
  167. protected function getnumPages()
  168. {
  169. if (null === $this->num_replies) {
  170. throw new RuntimeException('The model does not have the required data');
  171. }
  172. return (int) \ceil(($this->num_replies + 1) / $this->c->user->disp_posts);
  173. }
  174. /**
  175. * Массив страниц темы
  176. *
  177. * @return array
  178. */
  179. protected function getpagination()
  180. {
  181. $page = (int) $this->page;
  182. if ($page < 1 && $this->numPages === 1) {
  183. // 1 страницу в списке тем раздела не отображаем
  184. return [];
  185. } else { //????
  186. return $this->c->Func->paginate($this->numPages, $page, 'Topic', ['id' => $this->id, 'name' => \ForkBB\cens($this->subject)]);
  187. }
  188. }
  189. /**
  190. * Статус наличия установленной страницы в теме
  191. *
  192. * @return bool
  193. */
  194. public function hasPage()
  195. {
  196. return $this->page > 0 && $this->page <= $this->numPages;
  197. }
  198. /**
  199. * Возвращает массив сообщений с установленной страницы
  200. *
  201. * @throws InvalidArgumentException
  202. *
  203. * @return array
  204. */
  205. public function pageData()
  206. {
  207. if (! $this->hasPage()) {
  208. throw new InvalidArgumentException('Bad number of displayed page');
  209. }
  210. $vars = [
  211. ':tid' => $this->id,
  212. ':offset' => ($this->page - 1) * $this->c->user->disp_posts,
  213. ':rows' => $this->c->user->disp_posts,
  214. ];
  215. $sql = 'SELECT p.id
  216. FROM ::posts AS p
  217. WHERE p.topic_id=?i:tid
  218. ORDER BY p.id
  219. LIMIT ?i:offset, ?i:rows';
  220. $list = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
  221. if (! empty($list) && ($this->stick_fp || $this->poll_type) && ! \in_array($this->first_post_id, $list)) {
  222. \array_unshift($list, $this->first_post_id);
  223. }
  224. $this->idsList = $list;
  225. return empty($this->idsList) ? [] : $this->c->posts->view($this);
  226. }
  227. /**
  228. * Возвращает массив сообщений обзора темы
  229. *
  230. * @return array
  231. */
  232. public function review()
  233. {
  234. if ($this->c->config->o_topic_review < 1) {
  235. return [];
  236. }
  237. $this->page = 1;
  238. $vars = [
  239. ':tid' => $this->id,
  240. ':rows' => $this->c->config->o_topic_review,
  241. ];
  242. $sql = 'SELECT p.id
  243. FROM ::posts AS p
  244. WHERE p.topic_id=?i:tid
  245. ORDER BY p.id DESC
  246. LIMIT 0, ?i:rows';
  247. $this->idsList = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
  248. return empty($this->idsList) ? [] : $this->c->posts->view($this, true);
  249. }
  250. /**
  251. * Вычисляет страницу темы на которой находится данное сообщение
  252. *
  253. * @param int $pid
  254. */
  255. public function calcPage($pid)
  256. {
  257. $vars = [
  258. ':tid' => $this->id,
  259. ':pid' => $pid,
  260. ];
  261. $sql = 'SELECT COUNT(p.id) AS num, j.id AS flag
  262. FROM ::posts AS p
  263. INNER JOIN ::posts AS j ON (j.topic_id=?i:tid AND j.id=?i:pid)
  264. WHERE p.topic_id=?i:tid AND p.id<?i:pid';
  265. $result = $this->c->DB->query($sql, $vars)->fetch();
  266. $this->page = empty($result['flag']) ? null : (int) \ceil(($result['num'] + 1) / $this->c->user->disp_posts);
  267. }
  268. /**
  269. * Статус показа/подсчета просмотров темы
  270. *
  271. * @return bool
  272. */
  273. protected function getshowViews()
  274. {
  275. return $this->c->config->o_topic_views == '1';
  276. }
  277. /**
  278. * Увеличивает на 1 количество просмотров темы
  279. */
  280. public function incViews()
  281. {
  282. $vars = [
  283. ':tid' => $this->id,
  284. ];
  285. $sql = 'UPDATE ::topics SET num_views=num_views+1 WHERE id=?i:tid';
  286. $this->c->DB->query($sql, $vars);
  287. }
  288. /**
  289. * Обновление меток последнего визита и последнего прочитанного сообщения
  290. */
  291. public function updateVisits()
  292. {
  293. if ($this->c->user->isGuest) {
  294. return;
  295. }
  296. $vars = [
  297. ':uid' => $this->c->user->id,
  298. ':tid' => $this->id,
  299. ':read' => $this->mt_last_read,
  300. ':visit' => $this->mt_last_visit,
  301. ];
  302. $flag = false;
  303. if (false !== $this->hasNew) {
  304. $flag = true;
  305. $vars[':visit'] = $this->last_post;
  306. }
  307. if (false !== $this->hasUnread && $this->timeMax > $this->hasUnread) {
  308. $flag = true;
  309. $vars[':read'] = $this->timeMax;
  310. $vars[':visit'] = $this->last_post;
  311. }
  312. if ($flag) {
  313. if (empty($this->mt_last_read) && empty($this->mt_last_visit)) {
  314. $sql = 'INSERT INTO ::mark_of_topic (uid, tid, mt_last_visit, mt_last_read)
  315. SELECT ?i:uid, ?i:tid, ?i:visit, ?i:read
  316. FROM ::groups
  317. WHERE NOT EXISTS (SELECT 1
  318. FROM ::mark_of_topic
  319. WHERE uid=?i:uid AND tid=?i:tid)
  320. LIMIT 1';
  321. } else {
  322. $sql = 'UPDATE ::mark_of_topic
  323. SET mt_last_visit=?i:visit, mt_last_read=?i:read
  324. WHERE uid=?i:uid AND tid=?i:tid';
  325. }
  326. $this->c->DB->exec($sql, $vars);
  327. }
  328. }
  329. }