Load.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Topic;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\Topic\Topic;
  13. use InvalidArgumentException;
  14. class Load extends Action
  15. {
  16. /**
  17. * Создает текст запрос
  18. */
  19. protected function getSql(string $where, bool $full): string
  20. {
  21. if ($this->c->user->isGuest) {
  22. $query = 'SELECT t.*
  23. FROM ::topics AS t
  24. WHERE ' . $where;
  25. } elseif ($full) {
  26. $query = 'SELECT t.*, s.user_id AS is_subscribed, mof.mf_mark_all_read, mot.mt_last_visit, mot.mt_last_read
  27. FROM ::topics AS t
  28. LEFT JOIN ::topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id=?i:uid)
  29. LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:uid AND t.forum_id=mof.fid)
  30. LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:uid AND t.id=mot.tid)
  31. WHERE ' . $where;
  32. } else {
  33. $query = 'SELECT t.*, mot.mt_last_visit, mot.mt_last_read
  34. FROM ::topics AS t
  35. LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:uid AND t.id=mot.tid)
  36. WHERE ' . $where;
  37. }
  38. return $query;
  39. }
  40. /**
  41. * Загружает тему из БД
  42. */
  43. public function load(int $id): ?Topic
  44. {
  45. if ($id < 1) {
  46. throw new InvalidArgumentException('Expected a positive topic id');
  47. }
  48. $vars = [
  49. ':tid' => $id,
  50. ':uid' => $this->c->user->id,
  51. ];
  52. $query = $this->getSql('t.id=?i:tid', true);
  53. $data = $this->c->DB->query($query, $vars)->fetch();
  54. // тема отсутствует или недоступна
  55. if (empty($data)) {
  56. return null;
  57. }
  58. $topic = $this->manager->create($data);
  59. $forum = $topic->parent;
  60. if ($forum instanceof Forum) {
  61. $forum->__mf_mark_all_read = $topic->mf_mark_all_read;
  62. return $topic;
  63. } else {
  64. return null;
  65. }
  66. }
  67. /**
  68. * Загружает список тем из БД
  69. */
  70. public function loadByIds(array $ids, bool $full): array
  71. {
  72. foreach ($ids as $id) {
  73. if (
  74. ! \is_int($id)
  75. || $id < 1
  76. ) {
  77. throw new InvalidArgumentException('Expected a positive topic id');
  78. }
  79. }
  80. $vars = [
  81. ':ids' => $ids,
  82. ':uid' => $this->c->user->id,
  83. ];
  84. $query = $this->getSql('t.id IN (?ai:ids)', $full);
  85. $stmt = $this->c->DB->query($query, $vars);
  86. $result = [];
  87. while ($row = $stmt->fetch()) {
  88. $topic = $this->manager->create($row);
  89. if ($topic->parent instanceof Forum) {
  90. $result[] = $topic;
  91. if (! empty($row['mf_mark_all_read'])) {
  92. $topic->parent->__mf_mark_all_read = $row['mf_mark_all_read'];
  93. }
  94. }
  95. }
  96. return $result;
  97. }
  98. }