View.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Post\Post;
  12. use ForkBB\Models\Search\Search;
  13. use ForkBB\Models\Topic\Topic;
  14. use PDO;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. class View extends Action
  18. {
  19. /**
  20. * Возвращает список сообщений
  21. */
  22. public function view(/* mixed */ $arg, bool $review = false): array
  23. {
  24. if (
  25. ! $arg instanceof Topic
  26. && ! $arg instanceof Search
  27. ) {
  28. throw new InvalidArgumentException('Expected Topic or Search');
  29. }
  30. if (
  31. empty($arg->idsList)
  32. || ! \is_array($arg->idsList)
  33. ) {
  34. throw new RuntimeException('Model does not contain of posts list for display');
  35. }
  36. if (! $review) {
  37. $vars = [
  38. ':ids' => $arg->idsList,
  39. ];
  40. $query = 'SELECT w.id, w.message, w.poster, w.posted
  41. FROM ::warnings AS w
  42. WHERE w.id IN (?ai:ids)';
  43. $warnings = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_GROUP);
  44. }
  45. $userIds = [];
  46. $result = $this->manager->loadByIds($arg->idsList, ! $arg instanceof Topic);
  47. foreach ($result as $post) {
  48. if ($post instanceof Post) {
  49. if (isset($warnings[$post->id])) {
  50. $post->__warnings = $warnings[$post->id];
  51. }
  52. $userIds[$post->poster_id] = $post->poster_id;
  53. }
  54. }
  55. $this->c->users->loadByIds($userIds);
  56. $offset = ($arg->page - 1) * $this->c->user->disp_posts;
  57. $timeMax = 0;
  58. if ($review) {
  59. $postCount = $arg->num_replies + 2;
  60. $sign = -1;
  61. } else {
  62. $postCount = 0;
  63. $sign = 1;
  64. }
  65. if ($arg instanceof Topic) {
  66. foreach ($result as $post) {
  67. if ($post->id === $arg->last_post_id) { // время последнего сообщения в теме может равняться
  68. $timeMax = $arg->last_post; // времени его редактирования, а не создания
  69. } elseif ($post->posted > $timeMax) {
  70. $timeMax = $post->posted;
  71. }
  72. if (
  73. $post->id === $arg->first_post_id
  74. && $offset > 0
  75. ) {
  76. if (empty($post->id)) {
  77. continue;
  78. }
  79. $post->__postNumber = 1;
  80. } else {
  81. $postCount += $sign;
  82. if (empty($post->id)) {
  83. continue;
  84. }
  85. $post->__postNumber = $offset + $postCount;
  86. }
  87. }
  88. $arg->timeMax = $timeMax;
  89. } else {
  90. foreach ($result as $post) {
  91. ++$postCount;
  92. if (empty($post->id)) {
  93. continue;
  94. }
  95. $post->__postNumber = $offset + $postCount; //????
  96. }
  97. }
  98. return $result;
  99. }
  100. }