Delete.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Topic;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Topic\Topic;
  14. use ForkBB\Models\User\User;
  15. use PDO;
  16. use InvalidArgumentException;
  17. use RuntimeException;
  18. class Delete extends Action
  19. {
  20. /**
  21. * Удаляет тему(ы)
  22. */
  23. public function delete(DataModel ...$args): void
  24. {
  25. if (empty($args)) {
  26. throw new InvalidArgumentException('No arguments, expected User(s), Forum(s) or Topic(s)');
  27. }
  28. $uids = [];
  29. $uidsToGuest = [];
  30. $uidsDelete = [];
  31. $uidsUpdate = [];
  32. $forums = [];
  33. $topics = [];
  34. $parents = [];
  35. $isUser = 0;
  36. $isForum = 0;
  37. $isTopic = 0;
  38. foreach ($args as $arg) {
  39. if ($arg instanceof User) {
  40. if ($arg->isGuest) {
  41. throw new RuntimeException('Guest can not be deleted');
  42. }
  43. if (true === $arg->deleteAllPost) {
  44. $uidsDelete[$arg->id] = $arg->id;
  45. } else {
  46. $uidsToGuest[$arg->id] = $arg->id;
  47. }
  48. $uids[$arg->id] = $arg->id;
  49. $isUser = 1;
  50. } elseif ($arg instanceof Forum) {
  51. if (! $this->c->forums->get($arg->id) instanceof Forum) {
  52. throw new RuntimeException('Forum unavailable');
  53. }
  54. $forums[$arg->id] = $arg;
  55. $isForum = 1;
  56. } elseif ($arg instanceof Topic) {
  57. if (! $arg->parent instanceof Forum) {
  58. throw new RuntimeException('Parent unavailable');
  59. }
  60. $topics[$arg->id] = $arg;
  61. $parents[$arg->parent->id] = $arg->parent;
  62. $isTopic = 1;
  63. } else {
  64. throw new InvalidArgumentException('Expected User(s), Forum(s) or Topic(s)');
  65. }
  66. }
  67. if ($isUser + $isForum + $isTopic > 1) {
  68. throw new InvalidArgumentException('Expected only User(s), Forum(s) or Topic(s)');
  69. }
  70. if ($forums) {
  71. $vars = [
  72. ':forums' => \array_keys($forums),
  73. ];
  74. $query = 'SELECT t.id
  75. FROM ::topics AS t
  76. WHERE t.forum_id IN (?ai:forums)';
  77. $tids = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  78. $topics = $this->manager->loadByIds($tids, false);
  79. }
  80. if ($topics) {
  81. foreach ($topics as $topic) {
  82. if ($topic->poster_id > 0) {
  83. $uidsUpdate[$topic->poster_id] = $topic->poster_id;
  84. }
  85. }
  86. }
  87. if ($uidsDelete) {
  88. $vars = [
  89. ':users' => $uidsDelete,
  90. ];
  91. $query = 'SELECT t.id
  92. FROM ::topics AS t
  93. WHERE t.poster_id IN (?ai:users)';
  94. $tids = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  95. $topics = $this->manager->loadByIds($tids, false);
  96. foreach ($topics as $topic) {
  97. $parents[$topic->parent->id] = $topic->parent;
  98. }
  99. }
  100. $this->c->posts->delete(...$args);
  101. if ($uids) {
  102. $vars = [
  103. ':users' => $uids,
  104. ];
  105. $query = 'DELETE
  106. FROM ::mark_of_topic
  107. WHERE uid IN (?ai:users)';
  108. $this->c->DB->exec($query, $vars);
  109. }
  110. if ($uidsToGuest) {
  111. $vars = [
  112. ':users' => $uidsToGuest,
  113. ];
  114. $query = 'UPDATE ::topics
  115. SET poster_id=0
  116. WHERE poster_id IN (?ai:users)';
  117. $this->c->DB->exec($query, $vars);
  118. $query = 'UPDATE ::topics
  119. SET last_poster_id=0
  120. WHERE last_poster_id IN (?ai:users)';
  121. $this->c->DB->exec($query, $vars);
  122. }
  123. if ($topics) {
  124. if (isset($topics[0])) { // O_o
  125. throw new RuntimeException('Bad topic');
  126. }
  127. $this->c->subscriptions->unsubscribe(...$topics);
  128. $this->c->polls->delete(...$topics);
  129. $vars = [
  130. ':topics' => \array_keys($topics),
  131. ];
  132. $query = 'DELETE
  133. FROM ::mark_of_topic
  134. WHERE tid IN (?ai:topics)';
  135. $this->c->DB->exec($query, $vars);
  136. $query = 'DELETE
  137. FROM ::topics
  138. WHERE id IN (?ai:topics)';
  139. $this->c->DB->exec($query, $vars);
  140. $query = 'DELETE
  141. FROM ::topics
  142. WHERE moved_to IN (?ai:topics)';
  143. $this->c->DB->exec($query, $vars);
  144. }
  145. if ($parents) {
  146. foreach ($parents as $forum) {
  147. $this->c->forums->update($forum->calcStat());
  148. }
  149. }
  150. if ($uidsUpdate) {
  151. $this->c->users->updateCountTopics(...$uidsUpdate);
  152. }
  153. }
  154. }