UpdateCountTopics.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 UpdateCountTopics extends Action
  14. {
  15. /**
  16. * Обновляет число тем пользователя(ей)
  17. */
  18. public function updateCountTopics(/* 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_topics = COALESCE((
  47. SELECT COUNT(t.id)
  48. FROM ::topics AS t
  49. INNER JOIN ::posts AS p ON t.first_post_id=p.id
  50. WHERE p.poster_id=::users.id AND t.moved_to=0
  51. GROUP BY p.poster_id
  52. ), 0)
  53. WHERE ' . $where;
  54. $this->c->DB->exec($query, $vars);
  55. }
  56. }