Load.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\PM;
  10. use ForkBB\Models\Method;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\PM\Cnst;
  13. use ForkBB\Models\PM\PPost;
  14. use ForkBB\Models\PM\PTopic;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. class Load extends Method
  18. {
  19. /**
  20. * Создает текст запрос
  21. */
  22. protected function getSql(int $type, bool $solo = true): string
  23. {
  24. switch ($type) {
  25. case Cnst::PTOPIC:
  26. $where = $solo ? 'pt.id=?i:tid' : 'pt.id IN (?ai:ids)';
  27. return "SELECT * FROM ::pm_topics AS pt WHERE {$where}";
  28. case Cnst::PPOST:
  29. $where = $solo ? 'pp.id=?i:pid' : 'pp.id IN (?ai:ids)';
  30. return "SELECT * FROM ::pm_posts AS pp WHERE {$where}";
  31. default:
  32. throw new InvalidArgumentException("Unknown request type: {$type}");
  33. }
  34. }
  35. public function load(int $type, int $id): ?DataModel
  36. {
  37. switch ($type) {
  38. case Cnst::PTOPIC:
  39. return $this->loadTopic($id);
  40. case Cnst::PPOST:
  41. return $this->loadPost($id);
  42. default:
  43. return null;
  44. }
  45. }
  46. public function loadByIds(int $type, array $ids): array
  47. {
  48. switch ($type) {
  49. case Cnst::PTOPIC:
  50. return $this->loadTopics($ids);
  51. case Cnst::PPOST:
  52. return $this->loadPosts($ids);
  53. default:
  54. return [];
  55. }
  56. }
  57. /**
  58. * Загружает приватную тему из БД
  59. */
  60. protected function loadTopic(int $id): ?PTopic
  61. {
  62. if ($id < 1) {
  63. throw new InvalidArgumentException('Expected a positive ptopic id');
  64. }
  65. $vars = [
  66. ':tid' => $id,
  67. ];
  68. $query = $this->getSql(Cnst::PTOPIC);
  69. $data = $this->c->DB->query($query, $vars)->fetch();
  70. // тема отсутствует
  71. if (empty($data)) {
  72. return null;
  73. }
  74. $topic = $this->model->create(Cnst::PTOPIC, $data);
  75. $this->c->users->loadByIds([$topic->poster_id, $topic->target_id]);
  76. return $topic;
  77. }
  78. /**
  79. * Загружает приватное сообщение из БД
  80. */
  81. protected function loadPost(int $id): ?PPost
  82. {
  83. if ($id < 1) {
  84. throw new InvalidArgumentException('Expected a positive ppost id');
  85. }
  86. $vars = [
  87. ':pid' => $id,
  88. ];
  89. $query = $this->getSql(Cnst::PPOST);
  90. $data = $this->c->DB->query($query, $vars)->fetch();
  91. if (empty($data)) {
  92. return null;
  93. } else {
  94. return $this->model->create(Cnst::PPOST, $data);
  95. }
  96. }
  97. /**
  98. * Загружает список приватных тем из БД
  99. */
  100. protected function loadTopics(array $ids): array
  101. {
  102. foreach ($ids as $id) {
  103. if (
  104. ! \is_int($id)
  105. || $id < 1
  106. ) {
  107. throw new InvalidArgumentException('Expected a positive ptopic id');
  108. }
  109. }
  110. $vars = [
  111. ':ids' => $ids,
  112. ];
  113. $query = $this->getSql(Cnst::PTOPIC, false);
  114. $stmt = $this->c->DB->query($query, $vars);
  115. $result = [];
  116. $userIds = [];
  117. while ($row = $stmt->fetch()) {
  118. $result[] = $this->model->create(Cnst::PTOPIC, $row);
  119. $userIds[$row['poster_id']] = $row['poster_id'];
  120. $userIds[$row['target_id']] = $row['target_id'];
  121. }
  122. $this->c->users->loadByIds($userIds);
  123. return $result;
  124. }
  125. /**
  126. * Загружает список приватных сообщений из БД
  127. */
  128. protected function loadPosts(array $ids): array
  129. {
  130. foreach ($ids as $id) {
  131. if (
  132. ! \is_int($id)
  133. || $id < 1
  134. ) {
  135. throw new InvalidArgumentException('Expected a positive ppost id');
  136. }
  137. }
  138. $vars = [
  139. ':ids' => $ids,
  140. ];
  141. $query = $this->getSql(Cnst::PPOST, false);
  142. $stmt = $this->c->DB->query($query, $vars);
  143. $result = [];
  144. while ($row = $stmt->fetch()) {
  145. $result[] = $this->model->create(Cnst::PPOST, $row);
  146. }
  147. return $result;
  148. }
  149. }