Delete.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\DataModel;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\User\User;
  14. use InvalidArgumentException;
  15. use RuntimeException;
  16. class Delete extends Action
  17. {
  18. /**
  19. * Удаляет раздел(ы)
  20. */
  21. public function delete(DataModel ...$args): void
  22. {
  23. if (empty($args)) {
  24. throw new InvalidArgumentException('No arguments, expected User(s) or Forum(s)');
  25. }
  26. $uids = [];
  27. $forums = [];
  28. $all = [];
  29. $isUser = 0;
  30. $isForum = 0;
  31. foreach ($args as $arg) {
  32. if ($arg instanceof User) {
  33. if ($arg->isGuest) {
  34. throw new RuntimeException('Guest can not be deleted');
  35. }
  36. $uids[$arg->id] = $arg->id;
  37. $isUser = 1;
  38. } elseif ($arg instanceof Forum) {
  39. if (! $this->c->forums->get($arg->id) instanceof Forum) {
  40. throw new RuntimeException('Forum unavailable');
  41. }
  42. $forums[$arg->id] = $arg;
  43. $all[$arg->id] = true;
  44. $isForum = 1;
  45. foreach (\array_keys($arg->descendants) as $id) { //???? а если не админ?
  46. $all[$id] = true;
  47. }
  48. } else {
  49. throw new InvalidArgumentException('Expected User(s) or Forum(s)');
  50. }
  51. }
  52. if ($isUser + $isForum > 1) {
  53. throw new InvalidArgumentException('Expected only User(s) or Forum(s)');
  54. }
  55. if (\array_diff_key($all, $forums)) {
  56. throw new RuntimeException('Descendants should not be or they should be deleted too');
  57. }
  58. $this->c->topics->delete(...$args);
  59. if ($uids) {
  60. $vars = [
  61. ':users' => $uids,
  62. ];
  63. $query = 'DELETE
  64. FROM ::mark_of_forum
  65. WHERE uid IN (?ai:users)';
  66. $this->c->DB->exec($query, $vars);
  67. $query = 'UPDATE ::forums
  68. SET last_poster_id=0
  69. WHERE last_poster_id IN (?ai:users)';
  70. $this->c->DB->exec($query, $vars);
  71. }
  72. if ($forums) {
  73. $this->c->subscriptions->unsubscribe(...$forums);
  74. foreach ($forums as $forum) {
  75. $this->c->groups->Perm->reset($forum);
  76. }
  77. $vars = [
  78. ':forums' => \array_keys($forums),
  79. ];
  80. $query = 'DELETE
  81. FROM ::mark_of_forum
  82. WHERE fid IN (?ai:forums)';
  83. $this->c->DB->exec($query, $vars);
  84. $query = 'DELETE
  85. FROM ::forums
  86. WHERE id IN (?ai:forums)';
  87. $this->c->DB->exec($query, $vars);
  88. }
  89. }
  90. }