Advertisement
tabvn

Untitled

Aug 17th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.      * Get  elements by parent ID.
  6.      */
  7.     public function getChildren($parent_id)
  8.     {
  9.         $elements = array();
  10.         foreach ($this->elements as $k => $element) {
  11.             if (isset($v['#parent']) && $v['#parent'] == $parent_id) {
  12.                 $elements[$k] = $element;
  13.             }
  14.         }
  15.  
  16.         return $elements;
  17.     }
  18.  
  19.  
  20.     /**
  21.      * Remove an element by ID, Check it has children items. We also remove it.
  22.      */
  23.  
  24.     public function removeElement($id)
  25.     {
  26.  
  27.         $elements = $this->elements;
  28.         if (isset($elements[$id])) {
  29.             unset($elements[$id]);
  30.  
  31.             $this->elements = $elements;
  32.  
  33.             $children = $this->getChildren($id);
  34.  
  35.             if (!empty($children)) {
  36.                 foreach ($children as $k => $element) {
  37.                     $this->removeElement($k); // Recursive!!!
  38.  
  39.                 }
  40.             }
  41.         }
  42.  
  43.         $this->update(); // save builder to cache after updated
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement