Feed.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Post;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\Topic\Topic;
  13. use ForkBB\Models\Forum\Forum;
  14. use InvalidArgumentException;
  15. use RuntimeException;
  16. class Feed extends Action
  17. {
  18. /**
  19. * Загружает данные для feed
  20. */
  21. public function Feed(DataModel $model): array
  22. {
  23. if ($model instanceof Topic) {
  24. if (0 !== $model->moved_to) {
  25. return [];
  26. }
  27. $vars = [
  28. ':id' => $model->id,
  29. ];
  30. $query = 'SELECT p.id as pid, p.poster as username, p.poster_id as uid, p.message as content,
  31. p.hide_smilies, p.posted, p.edited
  32. FROM ::posts AS p
  33. WHERE p.topic_id=?i:id
  34. ORDER BY p.id DESC
  35. LIMIT 50';
  36. } elseif ($model instanceof Forum) {
  37. $ids = \array_keys($model->descendants);
  38. if ($model->id) {
  39. $ids[] = $model->id;
  40. }
  41. if (empty($ids)) {
  42. return [];
  43. }
  44. $vars = [
  45. ':forums' => $ids,
  46. ];
  47. $query = 'SELECT p.id as pid, p.poster as username, p.poster_id as uid, p.message as content,
  48. p.hide_smilies, p.posted, p.edited, t.id as tid, t.subject as topic_name, t.forum_id as fid
  49. FROM ::posts AS p
  50. INNER JOIN ::topics AS t ON t.id=p.topic_id
  51. WHERE t.forum_id IN(?ai:forums)
  52. ORDER BY p.id DESC
  53. LIMIT 50';
  54. } else {
  55. throw new InvalidArgumentException('Expected Topic or Forum');
  56. }
  57. return $this->c->DB->query($query, $vars)->fetchAll();
  58. }
  59. }