Forums.php 3.6 KB

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