Forums.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Manager;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\Group\Group;
  13. use RuntimeException;
  14. class Forums extends Manager
  15. {
  16. const CACHE_KEY = 'forums_mark';
  17. /**
  18. * Ключ модели для контейнера
  19. */
  20. protected string $cKey = 'Forums';
  21. /**
  22. * Закешированные данные по разделам
  23. */
  24. protected array $forumList = [];
  25. /**
  26. * Создает новую модель раздела
  27. */
  28. public function create(array $attrs = []): Forum
  29. {
  30. return $this->c->ForumModel->setAttrs($attrs);
  31. }
  32. /**
  33. * Инициализация списка разделов
  34. * Обновляет кеш разделов
  35. */
  36. public function init(Group $group = null): Forums
  37. {
  38. if (null === $group) {
  39. $gid = $this->c->user->group_id;
  40. } else {
  41. $gid = $group->g_id;
  42. }
  43. $mark = $this->c->Cache->get(self::CACHE_KEY);
  44. if (empty($mark)) {
  45. $mark = \time();
  46. if (true !== $this->c->Cache->set(self::CACHE_KEY, $mark)) {
  47. throw new RuntimeException('Unable to write value to cache - forums_mark');
  48. }
  49. $result = [];
  50. } else {
  51. $result = $this->c->Cache->get("forums_{$gid}");
  52. }
  53. if (
  54. ! isset($result['time'], $result['list'])
  55. || $result['time'] < $mark
  56. ) {
  57. $result = [
  58. 'time' => $mark,
  59. 'list' => $this->refresh($group),
  60. ];
  61. if (true !== $this->c->Cache->set("forums_{$gid}", $result)) {
  62. throw new RuntimeException('Unable to write value to cache - forums_' . $gid);
  63. }
  64. }
  65. $this->forumList = $result['list'];
  66. return $this;
  67. }
  68. /**
  69. * Получение модели раздела
  70. */
  71. public function get(int|string $id): ?Forum
  72. {
  73. $forum = parent::get($id);
  74. if (! $forum instanceof Forum) {
  75. if (empty($this->forumList[$id])) {
  76. return null;
  77. }
  78. $forum = $this->create($this->forumList[$id]);
  79. $this->set($id, $forum);
  80. }
  81. return $forum;
  82. }
  83. /**
  84. * Обновляет раздел в БД
  85. */
  86. public function update(Forum $forum): Forum
  87. {
  88. return $this->Save->update($forum);
  89. }
  90. /**
  91. * Добавляет новый раздел в БД
  92. */
  93. public function insert(Forum $forum): int
  94. {
  95. $id = $this->Save->insert($forum);
  96. $this->set($id, $forum);
  97. return $id;
  98. }
  99. /**
  100. * Получение списка разделов и подразделов с указанием глубины вложения
  101. */
  102. public function depthList(Forum $forum, int $depth, array $list = []): array
  103. {
  104. ++$depth;
  105. foreach ($forum->subforums as $sub) {
  106. $sub->__depth = $depth;
  107. $list[] = $sub;
  108. $list = $this->depthList($sub, $depth, $list);
  109. }
  110. return $list;
  111. }
  112. /**
  113. * Сбрасывает кеш
  114. */
  115. public function reset(): Forums
  116. {
  117. if (true !== $this->c->Cache->delete(self::CACHE_KEY)) {
  118. throw new RuntimeException('Unable to remove key from cache - forums_mark');
  119. }
  120. return $this;
  121. }
  122. }