Promote.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Group\Group;
  12. use RuntimeException;
  13. class Promote extends Action
  14. {
  15. /**
  16. * Обновляет данные пользователя
  17. */
  18. public function promote(Group ...$args): int
  19. {
  20. $count = \count($args);
  21. // перемещение всех пользователей из группы 0 в группу 1
  22. if (2 == $count) {
  23. $vars = [
  24. ':old' => $args[0]->g_id,
  25. ':new' => $args[1]->g_id,
  26. ];
  27. $query = 'UPDATE ::users
  28. SET group_id=?i:new
  29. WHERE group_id=?i:old';
  30. return $this->c->DB->exec($query, $vars);
  31. // продвижение всех пользователей в группе 0
  32. } elseif (1 == $count) {
  33. $vars = [
  34. ':old' => $args[0]->g_id,
  35. ':new' => $args[0]->g_promote_next_group,
  36. ':count' => $args[0]->g_promote_min_posts,
  37. ];
  38. $query = 'UPDATE ::users
  39. SET group_id=?i:new
  40. WHERE group_id=?i:old AND num_posts>=?i:count';
  41. return $this->c->DB->exec($query, $vars);
  42. } else {
  43. throw new RuntimeException("Illegal number of parameters ({$count})");
  44. }
  45. }
  46. }