示例#1
0
 public function Test_of_Test_of_init()
 {
     $Categories = new NestedCategory();
     $Categories->nested_set->init(array('scope' => array('category_id = ? AND completed = 0', $Categories->getId()), 'custom_attribute' => 'This is not allowed here'));
     $this->assertEqual($Categories->nested_set->getScopeCondition(), array(0 => 'category_id = ? AND completed = 0', 1 => null));
     $this->assertTrue(empty($Categories->nested_set->custom_attribute));
 }
示例#2
0
 /**
  * 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);
                 }
             }
         }
     }
 }
示例#3
0
 /**
  * Attemps to set value of <code>$nested_categories</code> property
  * @param Db object <code>$db</code>
  */
 public function nested_categories(Db $db)
 {
     if (isset($this->id)) {
         $this->nested_categories = NestedCategory::find($db, ['name', 'id', 'parent_id', 'gauche', 'droite'], "user_id = {$this->id}");
     }
 }
 /**
  * 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();
 }