Delete.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ForkBB\Models\Forum\Forum;
  13. use InvalidArgumentException;
  14. use RuntimeException;
  15. class Delete extends Action
  16. {
  17. /**
  18. * Удаляет пользователя(ей)
  19. */
  20. public function delete(User ...$users): void
  21. {
  22. if (empty($users)) {
  23. throw new InvalidArgumentException('No arguments, expected User(s)');
  24. }
  25. $ids = [];
  26. $moderators = [];
  27. $resetAdmin = false;
  28. foreach ($users as $user) {
  29. if ($user->isGuest) {
  30. throw new RuntimeException('Guest can not be deleted');
  31. }
  32. if ($user->isAdmMod) {
  33. $moderators[$user->id] = $user;
  34. }
  35. if ($user->isAdmin) {
  36. $resetAdmin = true;
  37. }
  38. $ids[] = $user->id;
  39. }
  40. if ($moderators) {
  41. $forums = $this->c->ForumManager->init($this->c->groups->get(FORK_GROUP_ADMIN));
  42. $root = $forums->get(0);
  43. if ($root instanceof Forum) {
  44. foreach ($root->descendants as $forum) {
  45. $forum->modDelete(...$moderators);
  46. $this->c->forums->update($forum);
  47. }
  48. }
  49. }
  50. $this->c->pms->delete(...$users);
  51. $this->c->subscriptions->unsubscribe(...$users);
  52. $this->c->forums->delete(...$users);
  53. //???? предупреждения
  54. foreach ($users as $user) {
  55. $this->c->Online->delete($user);
  56. $user->deleteAvatar();
  57. }
  58. $vars = [
  59. ':users' => $ids,
  60. ];
  61. $query = 'DELETE
  62. FROM ::users
  63. WHERE id IN (?ai:users)';
  64. $this->c->DB->exec($query, $vars);
  65. if ($resetAdmin) {
  66. $this->c->admins->reset();
  67. }
  68. $this->c->stats->reset();
  69. }
  70. }