CalcStat.php 2.2 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\Forum;
  10. use ForkBB\Models\Method;
  11. use ForkBB\Models\Forum\Forum;
  12. use RuntimeException;
  13. class CalcStat extends Method
  14. {
  15. /**
  16. * Пересчитывает статистику раздела
  17. */
  18. public function calcStat(): Forum
  19. {
  20. if ($this->model->id < 1) {
  21. throw new RuntimeException('The model does not have ID');
  22. }
  23. $vars = [
  24. ':fid' => $this->model->id,
  25. ];
  26. $query = 'SELECT COUNT(t.id)
  27. FROM ::topics AS t
  28. WHERE t.forum_id=?i:fid AND t.moved_to!=0';
  29. $moved = (int) $this->c->DB->query($query, $vars)->fetchColumn();
  30. $query = 'SELECT COUNT(t.id) as num_topics, SUM(t.num_replies) as num_replies
  31. FROM ::topics AS t
  32. WHERE t.forum_id=?i:fid AND t.moved_to=0';
  33. $result = $this->c->DB->query($query, $vars)->fetch();
  34. $this->model->num_topics = $result['num_topics'] + $moved;
  35. $this->model->num_posts = $result['num_topics'] + $result['num_replies'];
  36. $query = 'SELECT t.last_post, t.last_post_id, t.last_poster, t.last_poster_id, t.subject as last_topic
  37. FROM ::topics AS t
  38. WHERE t.forum_id=?i:fid AND t.moved_to=0
  39. ORDER BY t.last_post DESC
  40. LIMIT 1';
  41. $result = $this->c->DB->query($query, $vars)->fetch();
  42. if (empty($result)) {
  43. $this->model->last_post = 0;
  44. $this->model->last_post_id = 0;
  45. $this->model->last_poster = 0;
  46. $this->model->last_poster_id = 0;
  47. $this->model->last_topic = 0;
  48. } else {
  49. $this->model->last_post = $result['last_post'];
  50. $this->model->last_post_id = $result['last_post_id'];
  51. $this->model->last_poster = $result['last_poster'];
  52. $this->model->last_poster_id = $result['last_poster_id'];
  53. $this->model->last_topic = $result['last_topic'];
  54. }
  55. return $this->model;
  56. }
  57. }