UpdateCountPosts.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\User;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\User\User;
  12. use InvalidArgumentException;
  13. class UpdateCountPosts extends Action
  14. {
  15. /**
  16. * Обновляет число сообщений пользователя(ей)
  17. */
  18. public function updateCountPosts(/* mixed */ ...$args): void
  19. {
  20. $ids = [];
  21. foreach ($args as $arg) {
  22. if (
  23. $arg instanceof User
  24. && ! $arg->isGuest
  25. ) {
  26. $ids[$arg->id] = true;
  27. } elseif (
  28. \is_int($arg)
  29. && $arg > 0
  30. ) {
  31. $ids[$arg] = true;
  32. } else {
  33. throw new InvalidArgumentException('Expected user or positive integer id');
  34. }
  35. }
  36. if (empty($ids)) {
  37. $where = '::users.id > 0';
  38. $vars = [];
  39. } else {
  40. $where = '::users.id IN (?ai:ids)';
  41. $vars = [
  42. ':ids' => \array_keys($ids),
  43. ];
  44. }
  45. $query = 'UPDATE ::users
  46. SET num_posts = COALESCE((
  47. SELECT COUNT(p.id)
  48. FROM ::posts AS p
  49. INNER JOIN ::topics AS t ON t.id=p.topic_id
  50. INNER JOIN ::forums AS f ON f.id=t.forum_id
  51. WHERE p.poster_id=::users.id AND f.no_sum_mess=0
  52. GROUP BY p.poster_id
  53. ), 0)
  54. WHERE ' . $where;
  55. $this->c->DB->exec($query, $vars);
  56. }
  57. }