/** * Recursively deletes all records in database which are descendants of NestedCategory object * @param Db object <code>$db</code> */ public function delete_descendants(Db $db) { if (isset($this->children)) { foreach ($this->children as $child) { $nested_category = new NestedCategory(); $nested_category->set_id((int) $child['id']); $nested_category->db_props($db); if (null !== $nested_category->get_droite() && null !== $nested_category->get_gauche()) { $diff = $nested_category->get_droite() - $nested_category->get_gauche(); $db->delete("id = {$child['id']}", self::$table); if ($diff > 1) { $nested_category->children($db); $nested_category->delete_descendants($db); } } } } }
/** * Deletes nested category and all descendant categories from database as per given id * Checks if id of nested category being deleted belongs to user. If so, updates <code>$nested_categories</code> property of <code>$user</code> property and saves User object into Session variable * @param Db object <code>$db</code>, string <code>$id</code>, UserController object <code>$user_controller</code> */ public function delete(Db $db, $id, UserController $user_controller) { $id_clean = filter_var($id, FILTER_SANITIZE_STRING); $user = $user_controller->get_user(); $user_nested_categories = $user->get_nested_categories(); $deletion_valid = false; foreach ($user_nested_categories as $user_nested_category) { if ($user_nested_category['id'] == $id_clean) { $deletion_valid = true; } } if ($deletion_valid) { $nested_cat_to_del = new NestedCategory(); $nested_cat_to_del->set_id((int) $id_clean); $nested_cat_to_del->db_props($db); $nested_cat_to_del->delete($db); // delete all descendant categories $nested_cat_to_del->children($db); $nested_cat_to_del->delete_descendants($db); // adjust values in <code>nested_categories</code> table accordingly if (null !== $nested_cat_to_del->get_droite() && null !== $nested_cat_to_del->get_gauche()) { $diff = $nested_cat_to_del->get_droite() - $nested_cat_to_del->get_gauche(); $nested_cat_to_del->nested_cats_to_update_after_del($db); $nested_cats_to_update = $nested_cat_to_del->get_nested_cats_to_update_after_del(); if (isset($nested_cats_to_update)) { foreach ($nested_cats_to_update as $x) { $gauche = $x['gauche']; $droite = $x['droite']; $nested_cat_to_update = new NestedCategory(); $nested_cat_to_update->set_id((int) $x['id']); if ($gauche > $nested_cat_to_del->get_droite()) { $nested_cat_to_update->set_gauche($gauche - $diff - 1); } $nested_cat_to_update->set_droite($droite - $diff - 1); $nested_cat_to_update->save($db); } } } $user->nested_categories($db); $_SESSION['user'] = serialize($user); // userProfileHome.php $this->username = $user->get_username(); $this->user_nested_categories = $user->get_nested_categories(); } }