Refresh.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Forum;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Group\Group;
  12. class Refresh extends Action
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $list = [];
  18. /**
  19. * Возвращает список доступных разделов для группы
  20. */
  21. public function refresh(Group $group = null): array
  22. {
  23. if (null === $group) {
  24. $gid = $this->c->user->group_id;
  25. $read = $this->c->user->g_read_board;
  26. } else {
  27. $gid = $group->g_id;
  28. $read = $group->g_read_board;
  29. }
  30. $this->list = [];
  31. if (1 === $read) {
  32. $list = [];
  33. $vars = [
  34. ':gid' => $gid,
  35. ];
  36. $query = 'SELECT f.cat_id, c.cat_name, f.id, f.forum_name, f.redirect_url, f.parent_forum_id,
  37. f.moderators, f.no_sum_mess, f.disp_position, fp.post_topics, fp.post_replies
  38. FROM ::categories AS c
  39. INNER JOIN ::forums AS f ON c.id=f.cat_id
  40. LEFT JOIN ::forum_perms AS fp ON (fp.group_id=?i:gid AND fp.forum_id=f.id)
  41. WHERE fp.read_forum IS NULL OR fp.read_forum=1
  42. ORDER BY c.disp_position, c.id, f.disp_position';
  43. $stmt = $this->c->DB->query($query, $vars);
  44. while ($row = $stmt->fetch()) {
  45. $row['moderators'] = $this->formatModers($row['moderators']);
  46. $list[$row['id']] = $row;
  47. }
  48. if (! empty($list)) {
  49. $this->createList($list);
  50. }
  51. }
  52. return $this->list;
  53. }
  54. /**
  55. * Преобразует строку со списком модераторов в массив
  56. */
  57. protected function formatModers(string $str): ?array
  58. {
  59. $moderators = \json_decode($str, true);
  60. return $moderators ?: null;
  61. }
  62. /**
  63. * Формирует список доступных разделов
  64. */
  65. protected function createList(array $list, int $parent = 0): array
  66. {
  67. $sub = [];
  68. $all = [];
  69. foreach ($list as $id => $f) {
  70. if (
  71. $parent === $id
  72. || $parent !== $f['parent_forum_id']
  73. ) {
  74. continue;
  75. }
  76. $sub[] = $id;
  77. $all = \array_merge($this->createList($list, $id), $all);
  78. }
  79. if (0 === $parent) {
  80. if (empty($sub)) {
  81. return [];
  82. }
  83. $list[0]['id'] = $parent;
  84. $list[0]['ready'] = true;
  85. }
  86. $all = \array_merge($sub, $all);
  87. $list[$parent]['subforums'] = $sub ?: null;
  88. $list[$parent]['descendants'] = $all ?: null;
  89. $this->list[$parent] = \array_filter($list[$parent], function ($val) {
  90. return null !== $val;
  91. });
  92. return $all;
  93. }
  94. }