Posts.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace ForkBB\Models\Topic;
  3. use ForkBB\Models\Method;
  4. use PDO;
  5. use InvalidArgumentException;
  6. use RuntimeException;
  7. class Posts extends Method
  8. {
  9. /**
  10. * Возвращает массив сообщений с установленной ранее страницы темы
  11. *
  12. * @throws InvalidArgumentException
  13. *
  14. * @return array
  15. */
  16. public function posts()
  17. {
  18. if (! $this->model->hasPage()) {
  19. throw new InvalidArgumentException('Bad number of displayed page');
  20. }
  21. $offset = ($this->model->page - 1) * $this->c->user->disp_posts;
  22. $vars = [
  23. ':tid' => $this->model->id,
  24. ':offset' => $offset,
  25. ':rows' => $this->c->user->disp_posts,
  26. ];
  27. $sql = 'SELECT id
  28. FROM ::posts
  29. WHERE topic_id=?i:tid
  30. ORDER BY id LIMIT ?i:offset, ?i:rows';
  31. $ids = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
  32. if (empty($ids)) {
  33. return [];
  34. }
  35. // приклейка первого сообщения темы
  36. if ($this->model->stick_fp || $this->model->poll_type) {
  37. $ids[] = $this->model->first_post_id;
  38. }
  39. $vars = [
  40. ':ids' => $ids,
  41. ];
  42. $sql = 'SELECT id, message, poster, posted
  43. FROM ::warnings
  44. WHERE id IN (?ai:ids)';
  45. $warnings = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_GROUP);
  46. $vars = [
  47. ':ids' => $ids,
  48. ];
  49. $sql = 'SELECT u.warning_all, u.gender, u.email, u.title, u.url, u.location, u.signature,
  50. u.email_setting, u.num_posts, u.registered, u.admin_note, u.messages_enable,
  51. u.group_id,
  52. p.id, p.poster as username, p.poster_id, p.poster_ip, p.poster_email, p.message,
  53. p.hide_smilies, p.posted, p.edited, p.edited_by, p.edit_post, p.user_agent, p.topic_id,
  54. g.g_user_title, g.g_promote_next_group, g.g_pm
  55. FROM ::posts AS p
  56. INNER JOIN ::users AS u ON u.id=p.poster_id
  57. INNER JOIN ::groups AS g ON g.g_id=u.group_id
  58. WHERE p.id IN (?ai:ids) ORDER BY p.id';
  59. $stmt = $this->c->DB->query($sql, $vars);
  60. $postCount = 0;
  61. $timeMax = 0;
  62. $result = [];
  63. while ($cur = $stmt->fetch()) {
  64. if ($cur['posted'] > $timeMax) {
  65. $timeMax = $cur['posted'];
  66. }
  67. // номер сообшения в теме
  68. if ($cur['id'] == $this->model->first_post_id && $offset > 0) {
  69. $cur['postNumber'] = 1;
  70. } else {
  71. ++$postCount;
  72. $cur['postNumber'] = $offset + $postCount;
  73. }
  74. if (isset($warnings[$cur['id']])) {
  75. $cur['warnings'] = $warnings[$cur['id']];
  76. }
  77. $result[] = $this->c->posts->create($cur);
  78. }
  79. $this->model->timeMax = $timeMax;
  80. return $result;
  81. }
  82. }