PreviousPost.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. class PreviousPost extends Action
  13. {
  14. /**
  15. * Вычисляет номер сообщения перед указанным или его время публикации
  16. */
  17. public function previousPost(Post $post, bool $returnId = true): ?int
  18. {
  19. $vars = [
  20. ':pid' => $post->id,
  21. ':tid' => $post->topic_id,
  22. ];
  23. if ($returnId) {
  24. $query = 'SELECT MAX(p.id)
  25. FROM ::posts AS p
  26. WHERE p.id < ?i:pid AND p.topic_id=?i:tid';
  27. } else {
  28. $query = 'SELECT p.posted
  29. FROM ::posts AS p
  30. WHERE p.id < ?i:pid AND p.topic_id=?i:tid
  31. ORDER BY p.id DESC
  32. LIMIT 1';
  33. }
  34. $id = (int) $this->c->DB->query($query, $vars)->fetchColumn();
  35. return $id ?: null;
  36. }
  37. }