Пример #1
0
    /**
     * Get post counts for a topic.
     *
     * @param int $topic_id
     * @return string	Returns counts in db storable form
     */
    public function _get_post_count($topic_id)
    {
        $sql = 'SELECT COUNT(post_id) AS cnt
			FROM ' . $this->posts_table . '
			WHERE topic_id = ' . (int) $topic_id . '
				AND post_access = ' . access::TEAM_LEVEL . '
				AND post_deleted = 0
				AND post_approved = 1';
        $result = $this->db->sql_query($sql);
        $teams = $this->db->sql_fetchfield('cnt', $result);
        $this->db->sql_freeresult($result);
        $sql = 'SELECT COUNT(post_id) AS cnt
			FROM ' . $this->posts_table . '
			WHERE topic_id = ' . (int) $topic_id . '
				AND post_access = ' . access::AUTHOR_LEVEL . '
				AND post_deleted = 0
				AND post_approved = 1';
        $result = $this->db->sql_query($sql);
        $authors = $this->db->sql_fetchfield('cnt', $result);
        $this->db->sql_freeresult($result);
        $sql = 'SELECT COUNT(post_id) AS cnt
			FROM ' . $this->posts_table . '
			WHERE topic_id = ' . (int) $topic_id . '
				AND post_access = ' . access::PUBLIC_LEVEL . '
				AND post_deleted = 0
				AND post_approved = 1';
        $result = $this->db->sql_query($sql);
        $public = $this->db->sql_fetchfield('cnt', $result);
        $this->db->sql_freeresult($result);
        $sql = 'SELECT COUNT(post_id) AS cnt
			FROM ' . $this->posts_table . '
			WHERE topic_id = ' . (int) $topic_id . '
				AND post_deleted <> 0';
        $result = $this->db->sql_query($sql);
        $deleted = $this->db->sql_fetchfield('cnt', $result);
        $this->db->sql_freeresult($result);
        $sql = 'SELECT COUNT(post_id) AS cnt
			FROM ' . $this->posts_table . '
			WHERE topic_id = ' . (int) $topic_id . '
				AND post_deleted = 0
				AND post_approved = 0';
        $result = $this->db->sql_query($sql);
        $unapproved = $this->db->sql_fetchfield('cnt', $result);
        $this->db->sql_freeresult($result);
        return count::to_db(array('teams' => $teams, 'authors' => $authors, 'public' => $public, 'deleted' => $deleted, 'unapproved' => $unapproved));
    }
Пример #2
0
 /**
  * Update postcount on the parent topic
  */
 public function update_topic_postcount($hard_delete = false)
 {
     // shouldn't need to load through load() to delete it...
     if ($hard_delete && empty($this->sql_data)) {
         $this->sql_data = $this->__get_array();
     }
     if ($this->post_id && empty($this->sql_data)) {
         throw new exception('Modifying a post requires you load it through the load() function (we require the original information).');
     }
     // Get the current count
     $to_db = count::from_db($this->topic->topic_posts, false);
     // Revert the old count from this post
     if ($this->post_id) {
         if ($this->sql_data['post_deleted'] != 0) {
             $to_db['deleted']--;
         } else {
             if (!$this->sql_data['post_approved']) {
                 $to_db['unapproved']--;
             } else {
                 switch ($this->sql_data['post_access']) {
                     case access::PUBLIC_LEVEL:
                         $to_db['public']--;
                         break;
                     case access::AUTHOR_LEVEL:
                         $to_db['authors']--;
                         break;
                     case access::TEAM_LEVEL:
                         $to_db['teams']--;
                         break;
                 }
             }
         }
     }
     // Then recount those options for this post if we are not hard deleting it.
     if (!$hard_delete) {
         if ($this->post_deleted != 0) {
             $to_db['deleted']++;
         } else {
             if (!$this->post_approved) {
                 $to_db['unapproved']++;
             } else {
                 switch ($this->post_access) {
                     case access::PUBLIC_LEVEL:
                         $to_db['public']++;
                         break;
                     case access::AUTHOR_LEVEL:
                         $to_db['authors']++;
                         break;
                     case access::TEAM_LEVEL:
                         $to_db['teams']++;
                         break;
                 }
             }
         }
     }
     // Update the field on the topic
     $this->topic->topic_posts = count::to_db($to_db);
 }