Parcourir la source

Add Poll\Delete (poll 10)

Visman il y a 4 ans
Parent
commit
70e9c0d754
2 fichiers modifiés avec 69 ajouts et 2 suppressions
  1. 68 0
      app/Models/Poll/Delete.php
  2. 1 2
      app/Models/Topic/Delete.php

+ 68 - 0
app/Models/Poll/Delete.php

@@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+
+namespace ForkBB\Models\Poll;
+
+use ForkBB\Models\Action;
+use ForkBB\Models\DataModel;
+use ForkBB\Models\Poll\Model as Poll;
+use ForkBB\Models\Topic\Model as Topic;
+use PDO;
+use InvalidArgumentException;
+use RuntimeException;
+
+class Delete extends Action
+{
+    /**
+     * Удаление индекса
+     */
+    public function delete(DataModel ...$args): void
+    {
+        if (empty($args)) {
+            throw new InvalidArgumentException('No arguments, expected Poll(s) or Topic(s)');
+        }
+
+        $polls   = [];
+        $topics  = [];
+        $isPoll  = 0;
+        $isTopic = 0;
+
+        foreach ($args as $arg) {
+            if ($arg instanceof Poll) {
+                $arg->parent; // проверка доступности опроса
+
+                $polls[$arg->tid] = $arg;
+                $isPoll           = 1;
+            } elseif ($arg instanceof Topic) {
+                if (! $arg->parent instanceof Forum) {
+                    throw new RuntimeException('Parent unavailable');
+                }
+
+                $topics[$arg->id] = $arg;
+                $isTopic          = 1;
+            } else {
+                throw new InvalidArgumentException('Expected Poll(s) or Topic(s)');
+            }
+        }
+
+        if ($isPoll + $isTopic > 1) {
+            throw new InvalidArgumentException('Expected only Poll(s) or Topic(s)');
+        }
+
+        $vars  = [
+            ':tids' => \array_keys($polls ?: $topics),
+        ];
+        $query = 'DELETE
+            FROM ::poll
+            WHERE tid IN (?ai:tids)';
+
+        $this->c->DB->exec($query, $vars);
+
+        $query = 'DELETE
+            FROM ::poll_voted
+            WHERE tid IN (?ai:tids)';
+
+        $this->c->DB->exec($query, $vars);
+    }
+}

+ 1 - 2
app/Models/Topic/Delete.php

@@ -108,8 +108,6 @@ class Delete extends Action
 
         $this->c->posts->delete(...$args);
 
-        //???? опросы, предупреждения
-
         if ($uids) {
             $vars = [
                 ':users' => $uids,
@@ -144,6 +142,7 @@ class Delete extends Action
             }
 
             $this->c->subscriptions->unsubscribe(...$topics);
+            $this->c->polls->delete(...$topics);
 
             $vars = [
                 ':topics' => \array_keys($topics),