UpdateUsername.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Action;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\User\User;
  13. use RuntimeException;
  14. class UpdateUsername extends Action
  15. {
  16. /**
  17. * Обновляет имя пользователя в таблице разделов
  18. */
  19. public function updateUsername(User $user): void
  20. {
  21. if ($user->isGuest) {
  22. throw new RuntimeException('User expected, not guest');
  23. }
  24. $vars = [
  25. ':id' => $user->id,
  26. ':name' => $user->username,
  27. ];
  28. $query = 'UPDATE ::forums
  29. SET last_poster=?s:name
  30. WHERE last_poster_id=?i:id';
  31. $this->c->DB->exec($query, $vars);
  32. $forums = $this->c->ForumManager->init($this->c->groups->get(FORK_GROUP_ADMIN))->get(0)->descendants;
  33. $isMod = false;
  34. foreach ($forums as $forum) {
  35. if ($user->isModerator($forum)) {
  36. $isMod = true;
  37. $forum->modAdd($user); // переименование модератора
  38. $this->c->forums->update($forum);
  39. }
  40. }
  41. if ($isMod) {
  42. $this->manager->reset();
  43. }
  44. }
  45. }