ForumsTrait.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace ForkBB\Models\Pages;
  3. trait ForumsTrait
  4. {
  5. /**
  6. * Получение данных по разделам
  7. * @param int $parent
  8. * @return array
  9. */
  10. protected function getForumsData($parent = 0)
  11. {
  12. list($fTree, $fDesc, $fAsc) = $this->c->forums;
  13. // раздел $parent не имеет подразделов для вывода или они не доступны
  14. if (empty($fTree[$parent])) {
  15. return [];
  16. }
  17. $user = $this->c->user;
  18. // текущие данные по подразделам
  19. $vars = [
  20. ':id' => $user->id,
  21. ':forums' => array_slice($fAsc[$parent], 1),
  22. ];
  23. if ($user->isGuest) {
  24. $stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
  25. } else {
  26. $stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_last_visit FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
  27. }
  28. $forums = [];
  29. while ($cur = $stmt->fetch()) {
  30. $forums[$cur['id']] = $cur;
  31. }
  32. // поиск новых
  33. $new = [];
  34. if (! $user->isGuest) {
  35. // предварительная проверка разделов
  36. $max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
  37. foreach ($forums as $id => $cur) {
  38. $t = max($max, (int) $cur['mf_last_visit']);
  39. if ($cur['last_post'] > $t) {
  40. $new[$id] = $t;
  41. }
  42. }
  43. // проверка по темам //???? возможно не нужна из-за mf_last_visit????
  44. if (! empty($new)) {
  45. $vars = [
  46. ':id' => $user->id,
  47. ':forums' => array_keys($new),
  48. ':max' => $max,
  49. ];
  50. $stmt = $this->c->DB->query('SELECT t.forum_id, t.id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
  51. $tmp = [];
  52. while ($cur = $stmt->fetch()) {
  53. if ($cur['last_post']>$new[$cur['forum_id']]) {
  54. $tmp[$cur['forum_id']] = true;
  55. }
  56. }
  57. $new = $tmp;
  58. }
  59. }
  60. $r = $this->c->Router;
  61. // формированием таблицы разделов
  62. $result = [];
  63. foreach ($fTree[$parent] as $fId => $cur) {
  64. // список подразделов
  65. $subForums = [];
  66. if (isset($fTree[$fId])) {
  67. foreach ($fTree[$fId] as $f) {
  68. $subForums[] = [
  69. $r->link('Forum', [
  70. 'id' => $f['fid'],
  71. 'name' => $f['forum_name']
  72. ]),
  73. $f['forum_name']
  74. ];
  75. }
  76. }
  77. // модераторы
  78. $moderators = [];
  79. if (!empty($forums[$fId]['moderators'])) {
  80. $mods = unserialize($forums[$fId]['moderators']);
  81. foreach ($mods as $name => $id) {
  82. if ($user->gViewUsers == '1') {
  83. $moderators[] = [
  84. $r->link('User', [
  85. 'id' => $id,
  86. 'name' => $name,
  87. ]),
  88. $name
  89. ];
  90. } else {
  91. $moderators[] = $name;
  92. }
  93. }
  94. }
  95. // статистика по разделам
  96. $numT = 0;
  97. $numP = 0;
  98. $time = 0;
  99. $postId = 0;
  100. $poster = '';
  101. $topic = '';
  102. $fnew = false;
  103. foreach ($fAsc[$fId] as $id) {
  104. $fnew = $fnew || isset($new[$id]);
  105. $numT += $forums[$id]['num_topics'];
  106. $numP += $forums[$id]['num_posts'];
  107. if ($forums[$id]['last_post'] > $time) {
  108. $time = $forums[$id]['last_post'];
  109. $postId = $forums[$id]['last_post_id'];
  110. $poster = $forums[$id]['last_poster'];
  111. $topic = $forums[$id]['last_topic'];
  112. }
  113. }
  114. $result[$cur['cid']]['name'] = $cur['cat_name'];
  115. $result[$cur['cid']]['forums'][] = [
  116. 'fid' => $fId,
  117. 'forum_name' => $cur['forum_name'],
  118. 'forum_desc' => $forums[$fId]['forum_desc'],
  119. 'forum_link' => $r->link('Forum', [
  120. 'id' => $fId,
  121. 'name' => $cur['forum_name']
  122. ]),
  123. 'redirect_url' => $cur['redirect_url'],
  124. 'subforums' => $subForums,
  125. 'moderators' => $moderators,
  126. 'num_topics' => $numT,
  127. 'num_posts' => $numP,
  128. 'topics' => $this->number($numT),
  129. 'posts' => $this->number($numP),
  130. 'last_post' => $this->time($time),
  131. 'last_post_id' => $postId > 0 ? $r->link('ViewPost', ['id' => $postId]) : null,
  132. 'last_poster' => $poster,
  133. 'last_topic' => $topic,
  134. 'new' => $fnew,
  135. ];
  136. }
  137. return $result;
  138. }
  139. }