예제 #1
0
    /**
     * Acquire posts from another topic
     *
     * @param object $donor Object for topic where we'll be obtaining the posts from
     * @param array $post_ids Array of post id's that we want to acquire.
     * @param array $range Range of post times to transfer - array(min => (int), max => (int))
     */
    public function acquire_posts($donor, $post_ids, $range = false)
    {
        if (!$this->topic_id || empty($post_ids)) {
            return;
        }
        $sql_where = 'topic_id = ' . (int) $donor->topic_id . ' AND ';
        if (!empty($range)) {
            $sql_where .= 'post_time >= ' . (int) $range['min'] . ' AND post_time <= ' . (int) $range['max'];
        } else {
            $sql_where .= phpbb::$db->sql_in_set('post_id', $post_ids);
        }
        $sql = 'SELECT *
			FROM ' . TITANIA_POSTS_TABLE . '
			WHERE ' . $sql_where;
        $result = phpbb::$db->sql_query($sql);
        $posts = phpbb::$db->sql_fetchrowset($result);
        phpbb::$db->sql_freeresult($result);
        if (empty($posts)) {
            trigger_error('NO_POSTS');
        }
        // Update posts before resynchronizing topic
        $sql = 'UPDATE ' . TITANIA_POSTS_TABLE . '
			SET topic_id = ' . (int) $this->topic_id . ',
				post_url = "' . phpbb::$db->sql_escape($this->topic_url) . '",
				post_type = ' . (int) $this->topic_type . '
			WHERE ' . $sql_where;
        phpbb::$db->sql_query($sql);
        $new_post = new titania_post();
        $old_post = new titania_post();
        $new_post->topic = $this;
        $old_post->topic = $donor;
        $posters = array();
        // Reindex posts and update topic post counts
        foreach ($posts as $post_data) {
            if (!$post_data['post_deleted'] && $post_data['post_approved']) {
                $posters[] = $post_data['post_user_id'];
            }
            // Reindex the post
            $new_post->__set_array($post_data);
            $new_post->sql_data = $post_data;
            $new_post->topic_id = $this->topic_id;
            $new_post->post_url = $this->topic_url;
            $new_post->post_type = $this->topic_type;
            $new_post->index();
            // Set the post_id to 0 so it's counted as a new reply
            $new_post->post_id = 0;
            $new_post->update_topic_postcount();
            // Decrease count for donor topic
            $old_post->__set_array($post_data);
            $old_post->sql_data = $post_data;
            $old_post->update_topic_postcount(true);
        }
        $posters = array_unique($posters);
        // Update posted status
        foreach ($posters as $user_id) {
            $this->update_posted_status('add', $user_id);
            $donor->update_posted_status('remove', $user_id);
        }
        $donor->topic_posts = $old_post->topic->topic_posts;
        $this->topic_posts = $new_post->topic->topic_posts;
        unset($old_post, $new_post, $posts);
        // Resync topic
        $this->sync_topic_state(true);
        $this->sync_first_post();
        $this->sync_last_post();
        $this->submit();
        // Resync donor topic
        $donor->sync_topic_state(true);
        $donor->sync_first_post();
        $donor->sync_last_post();
        $donor->submit();
    }