Browse Source

+ Add topic counter for User

Visman 6 years ago
parent
commit
695b17fc4b

+ 1 - 0
app/Models/Pages/Install.php

@@ -899,6 +899,7 @@ class Install extends Page
                 'language'         => ['VARCHAR(25)', false, ''],
                 'style'            => ['VARCHAR(25)', false, ''],
                 'num_posts'        => ['INT(10) UNSIGNED', false, 0],
+                'num_topics'       => ['INT(10) UNSIGNED', false, 0],
                 'last_post'        => ['INT(10) UNSIGNED', true],
                 'last_search'      => ['INT(10) UNSIGNED', true],
                 'last_email_sent'  => ['INT(10) UNSIGNED', true],

+ 9 - 4
app/Models/Pages/Post.php

@@ -215,11 +215,16 @@ class Post extends Page
         $this->c->forums->update($forum->calcStat());
 
         // обновление данных текущего пользователя
-        if (! $merge && ! $this->user->isGuest && $forum->no_sum_mess != '1') {
-            $this->user->num_posts = $this->user->num_posts + 1;
+        if (! $merge && ! $this->user->isGuest) {
+            if ($forum->no_sum_mess != '1') {
+                $this->user->num_posts = $this->user->num_posts + 1;
 
-            if ($this->user->g_promote_next_group != '0' && $this->user->num_posts >= $this->user->g_promote_min_posts) {
-                $this->user->group_id = $this->user->g_promote_next_group;
+                if ($this->user->g_promote_next_group != '0' && $this->user->num_posts >= $this->user->g_promote_min_posts) {
+                    $this->user->group_id = $this->user->g_promote_next_group;
+                }
+            }
+            if ($createTopic) {
+                $this->user->num_topics = $this->user->num_topics + 1;
             }
         }
         $this->user->last_post = $now;

+ 1 - 1
app/Models/Pages/Profile/View.php

@@ -218,7 +218,7 @@ class View extends Profile
             'value'   => \ForkBB\dt($this->curUser->last_post, true),
             'caption' => \ForkBB\__('Last post info'),
         ];
-        if ($this->curUser->num_posts) {
+        if ($this->curUser->num_posts || $this->curUser->num_topics) {
             if ('1' == $this->user->g_search) {
                 $fields['posts'] = [
                     'class'   => 'pline',

+ 27 - 0
app/Models/Topic/Delete.php

@@ -28,6 +28,7 @@ class Delete extends Action
 
         $users    = [];
         $usersDel = [];
+        $usersUpd = [];
         $forums   = [];
         $topics   = [];
         $parents  = [];
@@ -67,6 +68,28 @@ class Delete extends Action
             throw new InvalidArgumentException('Expected only User(s), Forum(s) or Topic(s)');
         }
 
+        if ($forums) {
+            $vars = [
+                ':forums' => \array_keys($forums),
+            ];
+            $sql = 'SELECT p.poster_id
+                    FROM ::topics AS t
+                    INNER JOIN ::posts AS p ON t.first_post_id=p.id
+                    WHERE t.forum_id IN (?ai:forums) AND t.moved_to IS NULL
+                    GROUP BY p.poster_id';
+            $usersUpd = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
+        }
+        if ($topics) {
+            $vars = [
+                ':topics' => \array_keys($topics),
+            ];
+            $sql = 'SELECT p.poster_id
+                    FROM ::topics AS t
+                    INNER JOIN ::posts AS p ON t.first_post_id=p.id
+                    WHERE t.id IN (?ai:topics) AND t.moved_to IS NULL
+                    GROUP BY p.poster_id';
+            $usersUpd = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
+        }
         if ($usersDel) {
             $vars = [
                 ':users' => $usersDel,
@@ -131,5 +154,9 @@ class Delete extends Action
                 $this->c->forums->update($forum->calcStat());
             }
         }
+
+        if ($usersUpd) {
+            $this->c->users->UpdateCountTopics(...$usersUpd);
+        }
     }
 }

+ 55 - 0
app/Models/User/UpdateCountTopics.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace ForkBB\Models\User;
+
+use ForkBB\Models\Action;
+use ForkBB\Models\User\Model as User;
+use InvalidArgumentException;
+
+class UpdateCountTopics extends Action
+{
+    /**
+     * Обновляет число тем пользователя(ей)
+     *
+     * @param mixed ...$args
+     *
+     * @throws InvalidArgumentException
+     */
+    public function updateCountTopics(...$args)
+    {
+        $ids = [];
+        foreach ($args as $arg) {
+            if ($arg instanceof User) {
+                $ids[$arg->id] = true;
+            } elseif (is_int($arg) && $arg > 0) {
+                $ids[$arg] = true;
+            } else {
+                throw new InvalidArgumentException('Expected user or positive integer id');
+            }
+        }
+        // темы гостя не считаем
+        unset($ids[1]);
+
+        if (empty($ids)) {
+            $where = 'u.id != 1';
+            $vars  = [];
+        } else {
+            $where = 'u.id IN (?ai:ids)';
+            $vars  = [
+                ':ids' => \array_keys($ids),
+            ];
+        }
+
+        $sql = 'UPDATE ::users AS u
+                SET u.num_topics = (
+                    SELECT COUNT(t.id)
+                    FROM ::topics AS t
+                    INNER JOIN ::posts AS p ON t.first_post_id=p.id
+                    WHERE p.poster_id=u.id AND t.moved_to IS NULL
+                    GROUP BY p.poster_id
+                )
+                WHERE ' . $where;
+
+        $this->c->DB->exec($sql, $vars);
+    }
+}

+ 1 - 0
app/config/main.dist.php

@@ -208,6 +208,7 @@ return [
         'UserManagerCurrent'          => \ForkBB\Models\User\Current::class,
         'UserManagerUpdateLastVisit'  => \ForkBB\Models\User\UpdateLastVisit::class,
         'UserManagerUpdateCountPosts' => \ForkBB\Models\User\UpdateCountPosts::class,
+        'UserManagerUpdateCountTopics' => \ForkBB\Models\User\UpdateCountTopics::class,
         'UserManagerIsUniqueName'     => \ForkBB\Models\User\IsUniqueName::class,
         'UserManagerUsersNumber'      => \ForkBB\Models\User\UsersNumber::class,
         'UserManagerPromote'          => \ForkBB\Models\User\Promote::class,