Browse Source

* Delete moderators in Forum for delete users

Visman 6 years ago
parent
commit
e6c978bd14
2 changed files with 30 additions and 15 deletions
  1. 2 1
      app/Models/Pages/Admin/Users/Action.php
  2. 28 14
      app/Models/User/Delete.php

+ 2 - 1
app/Models/Pages/Admin/Users/Action.php

@@ -122,7 +122,8 @@ class Action extends Users
 
 
             $this->c->DB->commit();
             $this->c->DB->commit();
 
 
-            $this->c->Cache->delete('stats');
+            $this->c->Cache->delete('stats');       //???? перенести в manager
+            $this->c->Cache->delete('forums_mark'); //???? с авто обновлением кеша
 
 
             return $this->c->Redirect->page('AdminUsers')->message('Users delete redirect');
             return $this->c->Redirect->page('AdminUsers')->message('Users delete redirect');
         }
         }

+ 28 - 14
app/Models/User/Delete.php

@@ -12,40 +12,54 @@ class Delete extends Action
     /**
     /**
      * Удаляет пользователя(ей)
      * Удаляет пользователя(ей)
      *
      *
-     * @param mixed ...$args
+     * @param mixed ...$users
      *
      *
      * @throws InvalidArgumentException
      * @throws InvalidArgumentException
      * @throws RuntimeException
      * @throws RuntimeException
      */
      */
-    public function delete(...$args)
+    public function delete(...$users)
     {
     {
-        if (empty($args)) {
+        if (empty($users)) {
             throw new InvalidArgumentException('No arguments, expected User(s)');
             throw new InvalidArgumentException('No arguments, expected User(s)');
         }
         }
 
 
-        $users = [];
+        $ids = [];
+        $moderators = [];
+        foreach ($users as $user) {
+            if (! $user instanceof User) {
+                throw new InvalidArgumentException('Expected User');
+            }
+            if ($user->isGuest) {
+                throw new RuntimeException('Guest can not be deleted');
+            }
+
+            $ids[] = $user->id;
 
 
-        foreach ($args as $arg) {
-            if ($arg instanceof User) {
-                if ($arg->isGuest) {
-                    throw new RuntimeException('Guest can not be deleted');
+            if ($user->isAdmMod) {
+                $moderators[$user->id] = $user;
+            }
+        }
+
+        if (! empty($moderators)) {
+            $root = $this->c->forums->get(0);
+            if ($root instanceof Forum) {
+                foreach ($this->c->forums->depthList($root, 0) as $forum) {
+                    $forum->modDelete(...$moderators);
+                    $this->c->forums->update($forum);
                 }
                 }
-                $users[] = $arg->id;
-            } else {
-                throw new InvalidArgumentException('Expected User');
             }
             }
         }
         }
 
 
-        $this->c->forums->delete(...$args);
+        $this->c->forums->delete(...$users);
 
 
         //???? подписки, опросы, предупреждения
         //???? подписки, опросы, предупреждения
 
 
-        foreach ($args as $user) {
+        foreach ($users as $user) {
             $this->c->Online->delete($user);
             $this->c->Online->delete($user);
         }
         }
 
 
         $vars = [
         $vars = [
-            ':users' => $users,
+            ':users' => $ids,
         ];
         ];
         $sql = 'DELETE FROM ::users
         $sql = 'DELETE FROM ::users
                 WHERE id IN (?ai:users)';
                 WHERE id IN (?ai:users)';