/** * @param int|array $keys * @return bool */ public function load( $keys = null ) { if ( is_array( $keys ) ) { return false; } $this->_post->load( (int) $keys ); if ( ! $this->_post->exists() ) { $this->_post->set( 'id', 0 ); } $this->post( $this->_post ); return true; }
/** * @param int $topic_id * @param int $start * @param int $limit * @param string $ordering * @param int $hold * @param bool $orderbyid * * @return array */ protected static function loadMessagesByTopic($topic_id, $start = 0, $limit = 0, $ordering = 'ASC', $hold = 0, $orderbyid = false) { $db = JFactory::getDBO(); $query = "SELECT m.*, t.message\n\t\t\tFROM #__kunena_messages AS m\n\t\t\tINNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid\n\t\t\tWHERE m.thread={$db->quote($topic_id)} AND m.hold IN ({$hold}) ORDER BY m.time {$ordering}"; $db->setQuery($query, $start, $limit); $results = (array) $db->loadAssocList('id'); KunenaError::checkDatabaseError(); $location = $orderbyid || $ordering == 'ASC' ? $start : KunenaForumTopicHelper::get($topic_id)->getTotal($hold) - $start - 1; $order = $ordering == 'ASC' ? 1 : -1; $list = array(); foreach ($results as $id => $result) { $instance = new KunenaForumMessage($result); $instance->exists(true); self::$_instances[$id] = $instance; $list[$orderbyid ? $id : $location] = $instance; $location += $order; } unset($results); return $list; }
/** * @param KunenaForumMessage $message * @param int $postdelta * * @return bool */ public function update($message = null, $postdelta = 0) { // Update post count $this->posts += $postdelta; $exists = $message && $message->exists(); if (!$this->exists()) { if (!$exists) { $this->setError(JText::_('COM_KUNENA_LIB_TOPIC_NOT_EXISTS')); return false; } $this->id = $message->id; } if ($exists && $message->thread == $this->id && $message->hold == $this->hold) { // If message belongs into this topic and has same state, we may need to update cache $this->updatePostInfo($message->id, $message->time, $message->userid, $message->message, $message->name); } elseif (!$this->moved_id) { if (!isset($this->hold)) { $this->hold = KunenaForum::TOPIC_DELETED; } // If message isn't visible anymore, check if we need to update cache if (!$exists || $this->first_post_id == $message->id) { // If message got deleted and was cached, we need to find new first post $db = JFactory::getDBO(); $query = "SELECT * FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON t.mesid=m.id\n\t\t\t\t\tWHERE m.thread={$db->quote($this->id)} AND m.hold={$this->hold} ORDER BY m.time ASC, m.id ASC"; $db->setQuery($query, 0, 1); $first = $db->loadObject(); KunenaError::checkDatabaseError(); if ($first) { $this->first_post_time = 0; $this->updatePostInfo($first->id, $first->time, $first->userid, $first->message, $first->name); } else { $this->updatePostInfo(false); } } if (!$exists || $this->last_post_id == $message->id) { // If topic got deleted and was cached, we need to find new last post $db = JFactory::getDBO(); $query = "SELECT * FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON t.mesid=m.id\n\t\t\t\t\tWHERE m.thread={$db->quote($this->id)} AND m.hold={$this->hold} ORDER BY m.time DESC, m.id DESC"; $db->setQuery($query, 0, 1); $last = $db->loadObject(); KunenaError::checkDatabaseError(); if ($last) { $this->last_post_time = 0; $this->updatePostInfo($last->id, $last->time, $last->userid, $last->message, $last->name); } else { $this->updatePostInfo(false); } } } if (!$this->first_post_id || !$this->last_post_id) { // If topic has no visible posts, mark it deleted and recount $this->hold = $exists ? $message->hold : KunenaForum::TOPIC_DELETED; $this->recount(); } if (!($message && $message->exists()) && !$this->posts) { return $this->delete(); } if (!$this->save()) { return false; } if ($exists && $message->userid && abs($postdelta) <= 1) { // Update user topic $usertopic = $this->getUserTopic($message->userid); if (!$usertopic->update($message, $postdelta)) { $this->setError($usertopic->getError()); } // Update post count from user $user = KunenaUserHelper::get($message->userid); $user->posts += $postdelta; if (!$user->save()) { $this->setError($user->getError()); } } else { KunenaForumTopicUserHelper::recount($this->id); // FIXME: optimize KunenaUserHelper::recount(); } return true; }
static protected function loadMessagesByTopic($topic_id, $start=0, $limit=0, $ordering='ASC', $hold=0, $orderbyid = false) { $db = JFactory::getDBO (); $query = "SELECT m.*, t.message FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid WHERE m.thread={$db->quote($topic_id)} AND m.hold IN ({$hold}) ORDER BY m.time {$ordering}"; $db->setQuery ( $query, $start, $limit ); $results = (array) $db->loadAssocList ('id'); KunenaError::checkDatabaseError (); $list = array(); foreach ( $results as $id=>$result ) { $instance = new KunenaForumMessage (false); $instance->setProperties ( $result ); $instance->exists(true); self::$_instances [$id] = $instance; $list[$orderbyid ? $id : $start++] = $instance; } unset ($results); return $list; }