/** * 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(); }
/** * Change the permalink to the contribution * Do not change it yourself, always use this function to do so * * @param string $new_permalink */ public function change_permalink($new_permalink) { $old_permalink = $this->contrib_name_clean; $new_permalink = url::generate_slug($new_permalink); if ($this->validate_permalink($new_permalink, $old_permalink)) { return false; } $this->contrib_name_clean = $new_permalink; $params = serialize(array('contrib_type' => $this->type->url, 'contrib' => $this->contrib_name_clean)); // Attention items $sql = 'UPDATE ' . TITANIA_ATTENTION_TABLE . ' SET attention_url = "' . phpbb::$db->sql_escape($params) . '" WHERE attention_object_type = ' . TITANIA_CONTRIB . ' AND attention_object_id = ' . $this->contrib_id; phpbb::$db->sql_query($sql); // Update the topics/posts under this $topics = $post_ids = array(); $topic_where_sql = ' WHERE ' . phpbb::$db->sql_in_set('topic_type', array(TITANIA_SUPPORT, TITANIA_QUEUE_DISCUSSION)) . ' AND parent_id = ' . $this->contrib_id; $topic = new titania_topic(); $sql = 'SELECT * FROM ' . TITANIA_TOPICS_TABLE . $topic_where_sql; $result = phpbb::$db->sql_query($sql); while ($row = phpbb::$db->sql_fetchrow($result)) { $id = (int) $row['topic_id']; $topics[$id] = $row; } phpbb::$db->sql_freeresult($result); if (sizeof($topics)) { $post = new titania_post(); $post->topic = $topic; $sql = 'SELECT * FROM ' . TITANIA_POSTS_TABLE . ' WHERE ' . phpbb::$db->sql_in_set('topic_id', array_keys($topics)); $result = phpbb::$db->sql_query($sql); while ($row = phpbb::$db->sql_fetchrow($result)) { $topic->__set_array($topics[$row['topic_id']]); $post->__set_array($row); $post->post_url = $params; $post_ids[] = (int) $row['post_id']; // Need to reindex as well... $post->index(); } phpbb::$db->sql_freeresult($result); unset($topic, $post); // Update the posts table $sql = 'UPDATE ' . TITANIA_POSTS_TABLE . ' SET post_url = "' . phpbb::$db->sql_escape($params) . '" WHERE ' . phpbb::$db->sql_in_set('topic_id', array_keys($topics)); phpbb::$db->sql_query($sql); // Update the topics table $sql = 'UPDATE ' . TITANIA_TOPICS_TABLE . ' SET topic_url = "' . phpbb::$db->sql_escape($params) . '"' . $topic_where_sql; phpbb::$db->sql_query($sql); if (sizeof($post_ids)) { // On to attention items for posts $sql = 'SELECT attention_id, attention_object_id FROM ' . TITANIA_ATTENTION_TABLE . ' WHERE attention_object_type = ' . TITANIA_POST . ' AND ' . phpbb::$db->sql_in_set('attention_object_id', $post_ids); $result = phpbb::$db->sql_query($sql); while ($row = phpbb::$db->sql_fetchrow($result)) { $sql = 'UPDATE ' . TITANIA_ATTENTION_TABLE . ' SET attention_url = "' . phpbb::$db->sql_escape($params) . '" WHERE attention_id = ' . $row['attention_id']; phpbb::$db->sql_query($sql); } phpbb::$db->sql_freeresult($result); } } // Finally update the contrib_name_clean $sql = 'UPDATE ' . TITANIA_CONTRIBS_TABLE . ' SET contrib_name_clean = \'' . phpbb::$db->sql_escape($this->contrib_name_clean) . '\' WHERE contrib_id = ' . $this->contrib_id; phpbb::$db->sql_query($sql); }