Delete.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\Post;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Post\Post;
  14. use ForkBB\Models\Topic\Topic;
  15. use ForkBB\Models\User\User;
  16. use PDO;
  17. use InvalidArgumentException;
  18. use RuntimeException;
  19. class Delete extends Action
  20. {
  21. /**
  22. * Удаляет сообщение(я)
  23. */
  24. public function delete(DataModel ...$args): void
  25. {
  26. if (empty($args)) {
  27. throw new InvalidArgumentException('No arguments, expected User(s), Forum(s), Topic(s) or Post(s)');
  28. }
  29. $uidsToGuest = [];
  30. $uidsDelete = [];
  31. $uidsUpdate = [];
  32. $forums = [];
  33. $topics = [];
  34. $pids = [];
  35. $parents = [];
  36. $isUser = 0;
  37. $isForum = 0;
  38. $isTopic = 0;
  39. $isPost = 0;
  40. foreach ($args as $arg) {
  41. if ($arg instanceof User) {
  42. if ($arg->isGuest) {
  43. throw new RuntimeException('Guest can not be deleted');
  44. }
  45. if (true === $arg->deleteAllPost) {
  46. $uidsDelete[$arg->id] = $arg->id;
  47. } else {
  48. $uidsToGuest[$arg->id] = $arg->id;
  49. }
  50. $isUser = 1;
  51. } elseif ($arg instanceof Forum) {
  52. if (! $this->c->forums->get($arg->id) instanceof Forum) {
  53. throw new RuntimeException('Forum unavailable');
  54. }
  55. $forums[$arg->id] = $arg;
  56. $isForum = 1;
  57. } elseif ($arg instanceof Topic) {
  58. if (! $arg->parent instanceof Forum) {
  59. throw new RuntimeException('Parent unavailable');
  60. }
  61. $topics[$arg->id] = $arg;
  62. $isTopic = 1;
  63. } elseif ($arg instanceof Post) {
  64. if (
  65. ! $arg->parent instanceof Topic
  66. || ! $arg->parent->parent instanceof Forum
  67. ) {
  68. throw new RuntimeException('Parents unavailable');
  69. }
  70. $pids[$arg->id] = $arg->id;
  71. $parents[$arg->topic_id] = $arg->parent;
  72. $uidsUpdate[$arg->poster_id] = $arg->poster_id;
  73. $isPost = 1;
  74. } else {
  75. throw new InvalidArgumentException('Expected User(s), Forum(s), Topic(s) or Post(s)');
  76. }
  77. }
  78. if ($isUser + $isForum + $isTopic + $isPost > 1) {
  79. throw new InvalidArgumentException('Expected only User(s), Forum(s), Topic(s) or Post(s)');
  80. }
  81. $this->c->search->delete(...$args);
  82. //???? предупреждения
  83. if ($uidsToGuest) {
  84. $vars = [
  85. ':users' => $uidsToGuest,
  86. ];
  87. $query = 'UPDATE ::posts
  88. SET poster_id=0
  89. WHERE poster_id IN (?ai:users)';
  90. $this->c->DB->exec($query, $vars);
  91. $query = 'UPDATE ::posts
  92. SET editor_id=0
  93. WHERE editor_id IN (?ai:users)';
  94. $this->c->DB->exec($query, $vars);
  95. }
  96. if ($uidsDelete) {
  97. $vars = [
  98. ':users' => $uidsDelete,
  99. ];
  100. $query = 'SELECT p.topic_id
  101. FROM ::posts as p
  102. WHERE p.poster_id IN (?ai:users)
  103. GROUP BY p.topic_id';
  104. $tids = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  105. # $query = 'SELECT t.id
  106. # FROM ::topics AS t
  107. # WHERE t.poster_id IN (?ai:users)';
  108. #
  109. # $notUse = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN); // эти темы удаляются
  110. #
  111. # $tids = \array_diff($tids, $notUse);
  112. $parents = $this->c->topics->loadByIds($tids, false);
  113. $query = 'UPDATE ::posts
  114. SET editor_id=0
  115. WHERE editor_id IN (?ai:users)';
  116. $this->c->DB->exec($query, $vars);
  117. $query = 'DELETE
  118. FROM ::posts
  119. WHERE poster_id IN (?ai:users)';
  120. $this->c->DB->exec($query, $vars);
  121. }
  122. if ($forums) {
  123. $vars = [
  124. ':forums' => \array_keys($forums),
  125. ];
  126. $query = 'SELECT p.poster_id
  127. FROM ::posts AS p
  128. INNER JOIN ::topics AS t ON t.id=p.topic_id
  129. WHERE t.forum_id IN (?ai:forums) AND p.poster_id>0
  130. GROUP BY p.poster_id';
  131. $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  132. $query = 'DELETE
  133. FROM ::posts
  134. WHERE topic_id IN (
  135. SELECT id
  136. FROM ::topics
  137. WHERE forum_id IN (?ai:forums)
  138. )';
  139. $this->c->DB->exec($query, $vars);
  140. }
  141. if ($topics) {
  142. $vars = [
  143. ':topics' => \array_keys($topics),
  144. ];
  145. $query = 'SELECT p.poster_id
  146. FROM ::posts AS p
  147. WHERE p.topic_id IN (?ai:topics) AND p.poster_id>0
  148. GROUP BY p.poster_id';
  149. $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  150. $query = 'DELETE
  151. FROM ::posts
  152. WHERE topic_id IN (?ai:topics)';
  153. $this->c->DB->exec($query, $vars);
  154. }
  155. if ($pids) {
  156. $vars = [
  157. ':posts' => $pids,
  158. ];
  159. $query = 'DELETE
  160. FROM ::posts
  161. WHERE id IN (?ai:posts)';
  162. $this->c->DB->exec($query, $vars);
  163. }
  164. if ($parents) {
  165. $topics = $parents;
  166. $parents = [];
  167. foreach ($topics as $topic) {
  168. $parents[$topic->parent->id] = $topic->parent;
  169. $this->c->topics->update($topic->calcStat());
  170. }
  171. if (! $forums) {
  172. foreach ($parents as $forum) {
  173. $this->c->forums->update($forum->calcStat());
  174. }
  175. }
  176. }
  177. if ($uidsUpdate) {
  178. $this->c->users->updateCountPosts(...$uidsUpdate);
  179. }
  180. }
  181. }