Groups.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Group;
  10. use ForkBB\Models\Manager;
  11. use ForkBB\Models\Group\Group;
  12. class Groups extends Manager
  13. {
  14. /**
  15. * Ключ модели для контейнера
  16. * @var string
  17. */
  18. protected $cKey = 'Groups';
  19. /**
  20. * Флаг загрузки групп
  21. * @var bool
  22. */
  23. protected $flag;
  24. /**
  25. * Создает новую модель раздела
  26. */
  27. public function create(array $attrs = []): Group
  28. {
  29. return $this->c->GroupModel->setAttrs($attrs);
  30. }
  31. public function getList(): array
  32. {
  33. return $this->repository;
  34. }
  35. /**
  36. * Загрузка списка групп
  37. */
  38. public function init(): Groups
  39. {
  40. if (empty($this->flag)) {
  41. $query = 'SELECT g.*
  42. FROM ::groups AS g
  43. ORDER BY g.g_id';
  44. $stmt = $this->c->DB->query($query);
  45. while ($row = $stmt->fetch()) {
  46. $this->set($row['g_id'], $this->create($row));
  47. }
  48. $this->flag = true;
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Получение модели группы
  54. */
  55. public function get($id): ?Group
  56. {
  57. $group = parent::get($id);
  58. return $group instanceof Group ? $group : null;
  59. }
  60. /**
  61. * Обновляет группу в БД
  62. */
  63. public function update(Group $group): Group
  64. {
  65. return $this->Save->update($group);
  66. }
  67. /**
  68. * Добавляет новую группу в БД
  69. */
  70. public function insert(Group $group): int
  71. {
  72. $id = $this->Save->insert($group);
  73. $this->set($id, $group);
  74. return $id;
  75. }
  76. }