/**
  * Creates a new NestedCategory object and saves into database. If parent of new category contains posts, these posts are moved into new category
  * @param Db object <code>$db</code>, string <code>$name</code>, UserController object <code>$user_controller</code>
  */
 public function add(Db $db, $name, UserController $user_controller)
 {
     $user = $user_controller->get_user();
     $user_id = $user->get_id();
     $parent_id = $this->nested_category->get_id();
     $name_clean = filter_var($name, FILTER_SANITIZE_STRING);
     $nested_category = new NestedCategory();
     $nested_category->set_name($name_clean);
     $nested_category->set_user_id($user_id);
     if ($parent_id == null) {
         $max_droite = NestedCategory::max_droite($db, $user_id);
         if ($max_droite == null) {
             $max_droite = 0;
         }
         $nested_category->set_gauche($max_droite + 1);
         $nested_category->set_droite($max_droite + 2);
         $nested_category->save($db);
     } else {
         $nested_category->set_parent_id($parent_id);
         $nested_category->parent_gauche_droite($db);
         $parent_gauche = $nested_category->get_parent_gauche_droite()['gauche'];
         $parent_droite = $nested_category->get_parent_gauche_droite()['droite'];
         $nested_category->set_gauche((int) $parent_droite);
         $nested_category->set_droite($parent_droite + 1);
         $nested_category->save($db);
         // add posts to new category
         $post_results = Post::posts_in_nested_category($db, $parent_id);
         if (isset($post_results)) {
             $new_nested_category_id = NestedCategory::max_id($db, $user_id);
             foreach ($post_results as $post_result) {
                 $post = new Post();
                 $post->set_id((int) $post_result['id']);
                 $post->set_nested_category_id((int) $new_nested_category_id);
                 $post->save($db);
             }
         }
         // increase parent category droite value by two
         $parent_nested_category = new NestedCategory();
         $parent_nested_category->set_id($parent_id);
         $parent_nested_category->set_droite($parent_droite + 2);
         $parent_nested_category->save($db);
         // increase parent category's ancestors droite value by two
         $nested_category->parent_ancestors($db);
         if ($nested_category->get_parent_ancestors() !== null) {
             foreach ($nested_category->get_parent_ancestors() as $x) {
                 $parent_ancestor = new NestedCategory();
                 $parent_ancestor->set_id((int) $x['id']);
                 $parent_ancestor->set_droite($x['droite'] + 2);
                 $parent_ancestor->save($db);
             }
         }
         // for all categories where gauche is > parent category's droite value, increase their gauche and droite values by 2
         $nested_category->parent_siblings_and_parent_siblings_children($db);
         if ($nested_category->get_parent_siblings_and_parent_siblings_children() !== null) {
             foreach ($nested_category->get_parent_siblings_and_parent_siblings_children() as $y) {
                 $parent_sibling_or_parent_sibling_child = new NestedCategory();
                 $parent_sibling_or_parent_sibling_child->set_id((int) $y['id']);
                 $parent_sibling_or_parent_sibling_child->set_gauche($y['gauche'] + 2);
                 $parent_sibling_or_parent_sibling_child->set_droite($y['droite'] + 2);
                 $parent_sibling_or_parent_sibling_child->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();
 }