/** * Merge hookup into different topic * * @param int $to_topic_id */ public function merge($to_topic_id) { if ($this->topic_id == $to_topic_id && $to_topic_id != 0) { return true; } if ($to_topic_id == 0) { $this->delete(); } // Load destination hookup first: $dest = new hookup($this->db, $this->hookup_members_table, $this->hookup_dates_table, $this->hookup_available_table); // Only do this if the topic exists if ($dest->load_hookup($to_topic_id)) { $dest->hookup_enabled = $dest->hookup_enabled || $this->hookup_enabled; $dest->hookup_active_date = $dest->hookup_active_date ? $dest->hookup_active_date : $this->hookup_active_date; $dest->hookup_autoreset = $dest->hookup_enabled ? $dest->hookup_autoreset : $this->hookup_autoreset; $dest->hookup_self_invite = $dest->hookup_enabled ? $dest->hookup_self_invite : $this->hookup_self_invite; foreach ($this->hookup_dates as $date) { $dest->add_date($date['date_time'], $date['text']); } foreach ($this->hookup_users as $user) { $dest->add_user($user['user_id'], $user['comment'], $user['notify_status']); } // We need to store and reload to ensure to have date ids $dest->submit(); foreach ($this->hookup_availables as $user => $availables) { foreach ($availables as $date => $available) { $dest->set_user_date($user, $dest->get_date_id($this->hookup_dates[$date]['date_time']), $available); } } $this->delete(); $dest->submit(false); $this->load_hookup($to_topic_id); return true; } return false; }
/** * Stores the changes made in the database. Does NOT notify any users. Returns an array of the changes made. * * @param string $reload_data * @param string $return_changes * @param boolean $force_run * @return boolean|array */ public function submit($reload_data = true, $return_changes = false, $force_run = false) { $db = $this->db; $changed = array(); $topic_id = $this->topic_id; //For checking for differences, just load the old version from database: if ($return_changes) { $old = new hookup(); $old->load_hookup($this->topic_id); $changed = $this->hookup_enabled == $old->hookup_enabled ? $changed : array_merge($changed, array('hookup_enabled')); $changed = $this->hookup_self_invite == $old->hookup_self_invite ? $changed : array_merge($changed, array('hookup_self_invite')); $changed = $this->hookup_active_date == $old->hookup_active_date ? $changed : array_merge($changed, array('hookup_active_date')); $changed = $this->hookup_dates == $old->hookup_dates ? $changed : array_merge($changed, array('hookup_dates')); $changed = $this->hookup_availables == $old->hookup_availables ? $changed : array_merge($changed, array('hookup_availables')); $changed = $this->hookup_users == $old->hookup_users ? $changed : array_merge($changed, array('hookup_users')); $changed = $this->hookup_autoreset == $old->hookup_autoreset ? $changed : array_merge($changed, array('hookup_autoreset')); } else { $changed = array(); } //start with updating the topic: $row = array('hookup_enabled' => $this->hookup_enabled, 'hookup_self_invite' => $this->hookup_self_invite, 'hookup_active_date' => $this->hookup_active_date, 'hookup_autoreset' => $this->hookup_autoreset); $sql = 'UPDATE ' . TOPICS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $row) . " WHERE topic_id = {$topic_id}"; // This might fail if the topic does not exist anymore $db->sql_query($sql); if (!$db->sql_affectedrows() && !$force_run) { //the topic does not exist? $sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . ' WHERE topic_id = ' . $topic_id; $result = $db->sql_query($sql); if (!$db->sql_fetchrow($result)) { return false; } } //Now update the users $sql = 'DELETE FROM ' . $this->hookup_members_table . " WHERE topic_id = {$topic_id}"; $db->sql_query($sql); foreach ($this->hookup_users as $user_id => $user) { //Insert: $user['topic_id'] = $topic_id; $sql = 'INSERT INTO ' . $this->hookup_members_table . ' ' . $db->sql_build_array('INSERT', $user); $db->sql_query($sql); } //Update the dates: $sql = 'DELETE FROM ' . $this->hookup_dates_table . " WHERE topic_id = {$topic_id}"; $db->sql_query($sql); foreach ($this->hookup_dates as $date_id => $date) { //Insert (uses old ID if available): if (isset($date['date_id']) && !$date['date_id']) { unset($date['date_id']); } $date['topic_id'] = $topic_id; $sql = 'INSERT INTO ' . $this->hookup_dates_table . ' ' . $db->sql_build_array('INSERT', $date); $db->sql_query($sql); } //Update the entries for availability: $sql = 'DELETE FROM ' . $this->hookup_available_table . " WHERE topic_id = {$topic_id}"; $db->sql_query($sql); if ($this->hookup_availables) { foreach ($this->hookup_availables as $user_id => $availables) { foreach ($availables as $date_id => $available) { $rows[] = array('user_id' => $user_id, 'topic_id' => $topic_id, 'date_id' => $date_id, 'available' => $available); } } $db->sql_multi_insert($this->hookup_available_table, $rows); } //Now update this object: if ($reload_data) { $this->load_hookup($topic_id); } //Done, return changes: return $return_changes ? $changed : true; }