CalcStat.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\PM;
  10. use ForkBB\Models\Method;
  11. use ForkBB\Models\PM\PTopic;
  12. use PDO;
  13. use RuntimeException;
  14. class CalcStat extends Method
  15. {
  16. /**
  17. * Пересчитывает статистику темы
  18. */
  19. public function calcStat(): PTopic
  20. {
  21. if ($this->model->id < 1) {
  22. throw new RuntimeException('The model does not have ID');
  23. }
  24. $vars = [
  25. ':tid' => $this->model->id,
  26. ];
  27. $query = 'SELECT COUNT(pp.id), MAX(pp.id)
  28. FROM ::pm_posts AS pp
  29. WHERE pp.topic_id=?i:tid';
  30. list($count, $maxId) = $this->c->DB->query($query, $vars)->fetch(PDO::FETCH_NUM);
  31. if (
  32. empty($count)
  33. || empty($maxId)
  34. ) {
  35. throw new RuntimeException("Bad ptopic: {$this->model->id}");
  36. }
  37. $this->model->num_replies = $count - 1;
  38. $vars = [
  39. ':id' => $maxId,
  40. ];
  41. $query = 'SELECT pp.poster_id, pp.posted, pp.edited
  42. FROM ::pm_posts AS pp
  43. WHERE pp.id=?i:id';
  44. $row = $this->c->DB->query($query, $vars)->fetch();
  45. $this->model->last_post_id = $maxId;
  46. $this->model->last_post = $row['edited'] > $row['posted'] ? $row['edited'] : $row['posted'];
  47. if ($row['poster_id'] === $this->model->poster_id) {
  48. $this->model->last_number = 0;
  49. $this->model->poster_visit = $this->model->last_post;
  50. } elseif ($row['poster_id'] === $this->model->target_id) {
  51. $this->model->last_number = 1;
  52. $this->model->target_visit = $this->model->last_post;
  53. } else {
  54. throw new RuntimeException("Bad user ID in ppost number {$maxId}");
  55. }
  56. return $this->model;
  57. }
  58. }