diff --git a/src/mibew/libs/classes/Mibew/Thread.php b/src/mibew/libs/classes/Mibew/Thread.php index b2f2a0ce..4c6094e0 100644 --- a/src/mibew/libs/classes/Mibew/Thread.php +++ b/src/mibew/libs/classes/Mibew/Thread.php @@ -438,6 +438,7 @@ public static function closeOldThreads() $thread->modified = $now; $thread->closed = $now; $thread->state = self::STATE_CLOSED; + $thread->messageCount = $thread->getMessageCount(); $thread->save(false); unset($thread); } @@ -955,27 +956,13 @@ public function close($is_user) } } - // Get messages count - $db = Database::getInstance(); - list($message_count) = $db->query( - ("SELECT COUNT(*) FROM {message} " - . "WHERE {message}.threadid = :threadid AND ikind = :kind_user"), - array( - ':threadid' => $this->id, - ':kind_user' => Thread::KIND_USER, - ), - array( - 'return_rows' => Database::RETURN_ONE_ROW, - 'fetch_type' => Database::FETCH_NUM, - ) - ); // Close thread if it's not already closed if ($this->state != self::STATE_CLOSED) { $this->state = self::STATE_CLOSED; $this->closed = time(); - $this->messageCount = $message_count; + $this->messageCount = $this->getMessageCount(); $this->save(); $args = array('thread' => $this); @@ -1091,6 +1078,32 @@ public function renameUser($new_name) } } + /** + * Get actual count of visitor's messages + * + * @return int Count + */ + public function getMessageCount() + { + // Get messages count + $db = Database::getInstance(); + + list($message_count) = $db->query( + ("SELECT COUNT(*) FROM {message} " + . "WHERE {message}.threadid = :threadid AND ikind = :kind_user"), + array( + ':threadid' => $this->id, + ':kind_user' => Thread::KIND_USER, + ), + array( + 'return_rows' => Database::RETURN_ONE_ROW, + 'fetch_type' => Database::FETCH_NUM, + ) + ); + + return $message_count; + } + /** * Sets thread's fields according to the fields from Database. *