Browse Source

Fix mark of forums and topic

Visman 8 years ago
parent
commit
4efdcbdfde
3 changed files with 17 additions and 36 deletions
  1. 3 19
      app/Models/Pages/Forum.php
  2. 6 6
      app/Models/Pages/ForumsTrait.php
  3. 8 11
      app/Models/Pages/Install.php

+ 3 - 19
app/Models/Pages/Forum.php

@@ -52,7 +52,7 @@ class Forum extends Page
         if ($user->isGuest) {
         if ($user->isGuest) {
             $sql = 'SELECT f.moderators, f.num_topics, f.sort_by, 0 AS is_subscribed FROM ::forums AS f WHERE f.id=?i:fid';
             $sql = 'SELECT f.moderators, f.num_topics, f.sort_by, 0 AS is_subscribed FROM ::forums AS f WHERE f.id=?i:fid';
         } else {
         } else {
-            $sql = 'SELECT f.moderators, f.num_topics, f.sort_by, s.user_id AS is_subscribed, mof.mf_last_visit, mof.mf_mark_all_read FROM ::forums AS f LEFT JOIN ::forum_subscriptions AS s ON (f.id=s.forum_id AND s.user_id=?i:uid) LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:uid AND f.id=mof.fid) WHERE f.id=?i:fid';
+            $sql = 'SELECT f.moderators, f.num_topics, f.sort_by, s.user_id AS is_subscribed, mof.mf_mark_all_read FROM ::forums AS f LEFT JOIN ::forum_subscriptions AS s ON (f.id=s.forum_id AND s.user_id=?i:uid) LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:uid AND f.id=mof.fid) WHERE f.id=?i:fid';
         }
         }
         $curForum = $this->c->DB->query($sql, $vars)->fetch();
         $curForum = $this->c->DB->query($sql, $vars)->fetch();
 
 
@@ -131,7 +131,6 @@ class Forum extends Page
             }
             }
             $topics = $this->c->DB->query($sql, $vars)->fetchAll();
             $topics = $this->c->DB->query($sql, $vars)->fetchAll();
 
 
-            $maxTime = 0;
             foreach ($topics as &$cur) {
             foreach ($topics as &$cur) {
                 // цензура
                 // цензура
                 if ($this->config['o_censoring'] == '1') {
                 if ($this->config['o_censoring'] == '1') {
@@ -168,14 +167,13 @@ class Forum extends Page
                     continue;
                     continue;
                 }
                 }
                 // новые сообщения
                 // новые сообщения
-                if ($time > max($upper, $cur['mt_last_visit'])) {
+                if ($time > max($upper, (int) $cur['mt_last_visit'])) {
                     $cur['link_new'] = $this->c->Router->link('TopicGoToNew', ['id' => $cur['id']]);
                     $cur['link_new'] = $this->c->Router->link('TopicGoToNew', ['id' => $cur['id']]);
-                    $maxTime = max($maxTime, $time);
                 } else {
                 } else {
                     $cur['link_new'] = null;
                     $cur['link_new'] = null;
                 }
                 }
                 // не прочитанные сообщения
                 // не прочитанные сообщения
-                if ($time > max($lower, $cur['mt_last_read'])) {
+                if ($time > max($lower, (int) $cur['mt_last_read'])) {
                     $cur['link_unread'] = $this->c->Router->link('TopicGoToUnread', ['id' => $cur['id']]);
                     $cur['link_unread'] = $this->c->Router->link('TopicGoToUnread', ['id' => $cur['id']]);
                 } else {
                 } else {
                     $cur['link_unread'] = null;
                     $cur['link_unread'] = null;
@@ -184,20 +182,6 @@ class Forum extends Page
                 $cur['dot'] = isset($dots[$cur['id']]);
                 $cur['dot'] = isset($dots[$cur['id']]);
             }
             }
             unset($cur);
             unset($cur);
-
-            // изменение времени последнего посещения
-            if ($maxTime > (int) $curForum['mf_last_visit']) {
-                $vars = [
-                    ':fid' => $args['id'],
-                    ':uid' => $user->id,
-                    ':tm' => $maxTime,
-                ];
-                if (empty($curForum['mf_last_visit']) && empty($curForum['mf_mark_all_read'])) {
-                    $this->c->DB->exec('INSERT INTO ::mark_of_forum (uid, fid, mf_last_visit) SELECT ?i:uid, ?i:fid, ?i:tm FROM ::groups WHERE NOT EXISTS (SELECT 1 FROM ::mark_of_forum WHERE uid=?i:uid AND fid=?i:fid) LIMIT 1', $vars);
-                } else {
-                    $this->c->DB->exec('UPDATE ::mark_of_forum SET mf_last_visit=?i:tm WHERE uid=?i:uid AND fid=?i:fid', $vars);
-                }
-            }
         }
         }
 
 
         $moders = empty($curForum['moderators']) ? [] : array_flip(unserialize($curForum['moderators']));
         $moders = empty($curForum['moderators']) ? [] : array_flip(unserialize($curForum['moderators']));

+ 6 - 6
app/Models/Pages/ForumsTrait.php

@@ -28,7 +28,7 @@ trait ForumsTrait
         if ($user->isGuest) {
         if ($user->isGuest) {
             $stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
             $stmt = $this->c->DB->query('SELECT id, forum_desc, moderators, num_topics, num_posts, last_post, last_post_id, last_poster, last_topic FROM ::forums WHERE id IN (?ai:forums)', $vars);
         } else {
         } else {
-            $stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_last_visit FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
+            $stmt = $this->c->DB->query('SELECT f.id, f.forum_desc, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.last_topic, mof.mf_mark_all_read FROM ::forums AS f LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:id AND f.id=mof.fid) WHERE f.id IN (?ai:forums)', $vars);
         }
         }
         $forums = [];
         $forums = [];
         while ($cur = $stmt->fetch()) {
         while ($cur = $stmt->fetch()) {
@@ -41,22 +41,22 @@ trait ForumsTrait
             // предварительная проверка разделов
             // предварительная проверка разделов
             $max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
             $max = max((int) $user->lastVisit, (int) $user->uMarkAllRead);
             foreach ($forums as $id => $cur) {
             foreach ($forums as $id => $cur) {
-                $t = max($max, (int) $cur['mf_last_visit']);
+                $t = max($max, (int) $cur['mf_mark_all_read']);
                 if ($cur['last_post'] > $t) {
                 if ($cur['last_post'] > $t) {
                     $new[$id] = $t;
                     $new[$id] = $t;
                 }
                 }
             }
             }
-            // проверка по темам //???? возможно не нужна из-за mf_last_visit????
+            // проверка по темам
             if (! empty($new)) {
             if (! empty($new)) {
                 $vars = [
                 $vars = [
                     ':id' => $user->id,
                     ':id' => $user->id,
                     ':forums' => array_keys($new),
                     ':forums' => array_keys($new),
                     ':max' => $max,
                     ':max' => $max,
                 ];
                 ];
-                $stmt = $this->c->DB->query('SELECT t.forum_id, t.id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
+                $stmt = $this->c->DB->query('SELECT t.forum_id, t.last_post FROM ::topics AS t LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:id AND mot.tid=t.id) WHERE t.forum_id IN(?ai:forums) AND t.last_post>?i:max AND t.moved_to IS NULL AND (mot.mt_last_visit IS NULL OR t.last_post>mot.mt_last_visit)', $vars);
                 $tmp = [];
                 $tmp = [];
                 while ($cur = $stmt->fetch()) {
                 while ($cur = $stmt->fetch()) {
-                    if ($cur['last_post']>$new[$cur['forum_id']]) {
+                    if ($cur['last_post'] > $new[$cur['forum_id']]) {
                         $tmp[$cur['forum_id']] = true;
                         $tmp[$cur['forum_id']] = true;
                     }
                     }
                 }
                 }
@@ -145,4 +145,4 @@ trait ForumsTrait
         }
         }
         return $result;
         return $result;
     }
     }
-}
+}

+ 8 - 11
app/Models/Pages/Install.php

@@ -815,16 +815,14 @@ class Install extends Page
         // mark_of_forum
         // mark_of_forum
         $schema = [
         $schema = [
             'FIELDS' => [
             'FIELDS' => [
-                'uid'              => ['INT(10) UNSIGNED', true],
-                'fid'              => ['INT(10) UNSIGNED', true],
-                'mf_last_visit'    => ['INT(10) UNSIGNED', true],
-                'mf_mark_all_read' => ['INT(10) UNSIGNED', true],
+                'uid'              => ['INT(10) UNSIGNED', false],
+                'fid'              => ['INT(10) UNSIGNED', false],
+                'mf_mark_all_read' => ['INT(10) UNSIGNED', false, 0],
             ],
             ],
             'UNIQUE KEYS' => [
             'UNIQUE KEYS' => [
                 'uid_fid_idx' => ['uid', 'fid'],
                 'uid_fid_idx' => ['uid', 'fid'],
             ],
             ],
             'INDEXES' => [
             'INDEXES' => [
-                'mf_last_visit_idx'    => ['mf_last_visit'],
                 'mf_mark_all_read_idx' => ['mf_mark_all_read'],
                 'mf_mark_all_read_idx' => ['mf_mark_all_read'],
             ],
             ],
             'ENGINE' => $this->DBEngine,
             'ENGINE' => $this->DBEngine,
@@ -833,14 +831,13 @@ class Install extends Page
         // mark_of_topic
         // mark_of_topic
         $schema = [
         $schema = [
             'FIELDS' => [
             'FIELDS' => [
-                'uid'           => ['INT(10) UNSIGNED', true],
-                'fid'           => ['INT(10) UNSIGNED', true], //????
-                'tid'           => ['INT(10) UNSIGNED', true],
-                'mt_last_visit' => ['INT(10) UNSIGNED', true],
-                'mt_last_read'  => ['INT(10) UNSIGNED', true],
+                'uid'           => ['INT(10) UNSIGNED', false],
+                'tid'           => ['INT(10) UNSIGNED', false],
+                'mt_last_visit' => ['INT(10) UNSIGNED', false, 0],
+                'mt_last_read'  => ['INT(10) UNSIGNED', false, 0],
             ],
             ],
             'UNIQUE KEYS' => [
             'UNIQUE KEYS' => [
-                'uid_fid_tid_idx' => ['uid', 'fid', 'tid'],
+                'uid_tid_idx' => ['uid', 'tid'],
             ],
             ],
             'INDEXES' => [
             'INDEXES' => [
                 'mt_last_visit_idx' => ['mt_last_visit'],
                 'mt_last_visit_idx' => ['mt_last_visit'],