/** * Load category. * * @param int $id Category id. * @throws \Exception Throws exception if no category found. * @return null */ protected function load_category($id) { $this->id = (int) $id; if (!$this->id || !$this->category->load($this->id)) { throw new \Exception($this->user->lang['NO_CATEGORY']); } }
WHERE ' . $row['left_id'] . ' BETWEEN left_id AND right_id'; phpbb::$db->sql_query($sql); $category_object->left_id = $row['right_id']; $category_object->right_id = $row['right_id'] + 1; } else { $sql = 'SELECT MAX(right_id) AS right_id FROM ' . TITANIA_CATEGORIES_TABLE; $result = phpbb::$db->sql_query($sql); $row = phpbb::$db->sql_fetchrow($result); phpbb::$db->sql_freeresult($result); $category_object->left_id = $row['right_id'] + 1; $category_object->right_id = $row['right_id'] + 2; } } else { $category_check = new titania_category(); $category_check->load($category_id); if ($category_check->parent_id != $category_object->parent_id) { if ($category_check->category_id != $category_object->parent_id) { $errors_extra = $category_object->move_category($category_object->parent_id); // Check for errors from moving the category if (sizeof($errors_extra)) { $error = array_merge($error, $errors_extra); } } else { $category_object->parent_id = $category_check->parent_id; } } } // Only update category if no errors occurred from moving it if (!sizeof($error)) { // Now we submit the category information...
/** * Move category * * @param int $to_id New parent id * @param sync|null $sync If given sync class, category counts are resynchronized * @return array */ function move_category($to_id, $sync) { $to_data = $moved_ids = $errors = array(); if ($to_id > 0) { // Retrieve $to_data $to_data = new titania_category(); $to_data->load($to_id); } // Make sure we're not moving this category under one if its own children if ($to_id > 0 && $to_data->left_id > $this->left_id && $to_data->right_id < $this->right_id) { $errors[] = phpbb::$user->lang['CATEGORY_CHILD_AS_PARENT']; } else { $moved_categories = $this->get_category_branch('children'); $diff = sizeof($moved_categories) * 2; $moved_ids = array(); for ($i = 0; $i < sizeof($moved_categories); ++$i) { $moved_ids[] = (int) $moved_categories[$i]['category_id']; } // Resync parents $sql = 'UPDATE ' . $this->sql_table . "\n\t\t\t\tSET right_id = right_id - {$diff}\n\t\t\t\tWHERE left_id < " . $this->right_id . "\n\t\t\t\t\tAND right_id > " . $this->right_id; phpbb::$db->sql_query($sql); // Resync righthand side of tree $sql = 'UPDATE ' . $this->sql_table . "\n\t\t\t\tSET left_id = left_id - {$diff}, right_id = right_id - {$diff}\n\t\t\t\tWHERE left_id > " . $this->right_id; phpbb::$db->sql_query($sql); if ($to_id > 0) { // Retrieve $to_data again, it may have been changed... unset($to_data); $to_data = new titania_category(); $to_data->load($to_id); // Resync new parents $sql = 'UPDATE ' . $this->sql_table . "\n\t\t\t\t\tSET right_id = right_id + {$diff}\n\t\t\t\t\tWHERE " . $to_data->right_id . ' BETWEEN left_id AND right_id AND ' . phpbb::$db->sql_in_set('category_id', $moved_ids, true); phpbb::$db->sql_query($sql); // Resync the righthand side of the tree $sql = 'UPDATE ' . $this->sql_table . "\n\t\t\t\t\tSET left_id = left_id + {$diff}, right_id = right_id + {$diff}\n\t\t\t\t\tWHERE left_id > " . $to_data->right_id . ' AND ' . phpbb::$db->sql_in_set('category_id', $moved_ids, true); phpbb::$db->sql_query($sql); // Resync moved branch $to_data->right_id += $diff; if ($to_data->right_id > $this->right_id) { $diff = '+ ' . ($to_data->right_id - $this->right_id - 1); } else { $diff = '- ' . abs($to_data->right_id - $this->right_id - 1); } } else { $sql = 'SELECT MAX(right_id) AS right_id FROM ' . $this->sql_table . ' WHERE ' . phpbb::$db->sql_in_set('category_id', $moved_ids, true); $result = phpbb::$db->sql_query($sql); $row = phpbb::$db->sql_fetchrow($result); phpbb::$db->sql_freeresult($result); $diff = '+ ' . ($row['right_id'] - $this->left_id + 1); } $sql = 'UPDATE ' . $this->sql_table . "\n\t\t\t\tSET left_id = left_id {$diff}, right_id = right_id {$diff}\n\t\t\t\tWHERE " . phpbb::$db->sql_in_set('category_id', $moved_ids); phpbb::$db->sql_query($sql); if ($sync) { // Resync counters $sync->categories('count'); } } return $errors; }