/**
  * Sets the parent node.
  *
  * @param AbstractNode $parent
  * @chainable
  * @throws CircularException
  */
 public function setParent(AbstractNode $parent)
 {
     // check integrity
     if ($this->isDescendant($parent->id())) {
         throw new CircularException('Can not add descendant "' . $parent->id() . '" as my parent.');
     }
     // remove from old parent
     if (!is_null($this->parent)) {
         if ($this->parent->id() == $parent->id()) {
             // already the parent
             return $this;
         }
         $this->parent->removeChild($this->id);
     }
     $this->parent = $parent;
     // assign child to parent
     $this->parent->addChild($this);
     //clear any cache
     $this->clear();
     return $this;
 }
 /**
  * Recursively groups tree nodes given a separator
  *
  * @param Node $node The node to group
  *
  * @return void
  */
 public function groupNode($node)
 {
     if ($node->type != Node::CONTAINER || !$GLOBALS['cfg']['NavigationTreeEnableExpansion']) {
         return;
     }
     $separators = array();
     if (is_array($node->separator)) {
         $separators = $node->separator;
     } else {
         if (strlen($node->separator)) {
             $separators[] = $node->separator;
         }
     }
     $prefixes = array();
     if ($node->separator_depth > 0) {
         foreach ($node->children as $child) {
             $prefix_pos = false;
             foreach ($separators as $separator) {
                 $sep_pos = mb_strpos($child->name, $separator);
                 if ($sep_pos != false && $sep_pos != mb_strlen($child->name) && $sep_pos != 0 && ($prefix_pos == false || $sep_pos < $prefix_pos)) {
                     $prefix_pos = $sep_pos;
                 }
             }
             if ($prefix_pos !== false) {
                 $prefix = mb_substr($child->name, 0, $prefix_pos);
                 if (!isset($prefixes[$prefix])) {
                     $prefixes[$prefix] = 1;
                 } else {
                     $prefixes[$prefix]++;
                 }
             }
             //Bug #4375: Check if prefix is the name of a DB, to create a group.
             foreach ($node->children as $child) {
                 if (array_key_exists($child->name, $prefixes)) {
                     $prefixes[$child->name]++;
                 }
             }
         }
         //Check if prefix is the name of a DB, to create a group.
         foreach ($node->children as $child) {
             if (array_key_exists($child->name, $prefixes)) {
                 $prefixes[$child->name]++;
             }
         }
     }
     foreach ($prefixes as $key => $value) {
         if ($value == 1) {
             unset($prefixes[$key]);
         } else {
             if ($value > 500 && !$this->_largeGroupWarning) {
                 trigger_error(__('There are large item groups in navigation panel which ' . 'may affect the performance. Consider disabling item ' . 'grouping in the navigation panel.'), E_USER_WARNING);
                 $this->_largeGroupWarning = true;
             }
         }
     }
     if (count($prefixes)) {
         $groups = array();
         foreach ($prefixes as $key => $value) {
             $groups[$key] = new Node($key, Node::CONTAINER, true);
             $groups[$key]->separator = $node->separator;
             $groups[$key]->separator_depth = $node->separator_depth - 1;
             $groups[$key]->icon = PMA_Util::getImage('b_group.png');
             $groups[$key]->pos2 = $node->pos2;
             $groups[$key]->pos3 = $node->pos3;
             if ($node instanceof Node_Table_Container || $node instanceof Node_View_Container) {
                 $tblGroup = '&amp;tbl_group=' . urlencode($key);
                 $groups[$key]->links = array('text' => $node->links['text'] . $tblGroup, 'icon' => $node->links['icon'] . $tblGroup);
             }
             $node->addChild($groups[$key]);
             foreach ($separators as $separator) {
                 $separatorLength = strlen($separator);
                 // FIXME: this could be more efficient
                 foreach ($node->children as $child) {
                     $keySeparatorLength = mb_strlen($key) + $separatorLength;
                     $name_substring = mb_substr($child->name, 0, $keySeparatorLength);
                     if ($name_substring != $key . $separator && $child->name != $key || $child->type != Node::OBJECT) {
                         continue;
                     }
                     $class = get_class($child);
                     $new_child = PMA_NodeFactory::getInstance($class, mb_substr($child->name, $keySeparatorLength));
                     if ($new_child instanceof Node_Database && $child->getHiddenCount() > 0) {
                         $new_child->setHiddenCount($child->getHiddenCount());
                     }
                     $new_child->real_name = $child->real_name;
                     $new_child->icon = $child->icon;
                     $new_child->links = $child->links;
                     $new_child->pos2 = $child->pos2;
                     $new_child->pos3 = $child->pos3;
                     $groups[$key]->addChild($new_child);
                     foreach ($child->children as $elm) {
                         $new_child->addChild($elm);
                     }
                     $node->removeChild($child->name);
                 }
             }
         }
         foreach ($prefixes as $key => $value) {
             $this->groupNode($groups[$key]);
             $groups[$key]->classes = "navGroup";
         }
     }
 }
 /**
  * Recursively groups tree nodes given a sperarator
  *
  * @param Node $node The node to group
  *
  * @return void
  */
 public function groupNode($node)
 {
     if ($node->type == Node::CONTAINER) {
         $separators = array();
         if (is_array($node->separator)) {
             $separators = $node->separator;
         } else {
             if (strlen($node->separator)) {
                 $separators[] = $node->separator;
             }
         }
         $prefixes = array();
         if ($node->separator_depth > 0) {
             foreach ($node->children as $child) {
                 $prefix_pos = false;
                 foreach ($separators as $separator) {
                     $sep_pos = strpos($child->name, $separator);
                     if ($sep_pos != false && $sep_pos != strlen($child->name) && $sep_pos != 0 && ($prefix_pos == false || $sep_pos < $prefix_pos)) {
                         $prefix_pos = $sep_pos;
                     }
                 }
                 if ($prefix_pos !== false) {
                     $prefix = substr($child->name, 0, $prefix_pos);
                     if (!isset($prefixes[$prefix])) {
                         $prefixes[$prefix] = 1;
                     } else {
                         $prefixes[$prefix]++;
                     }
                 }
             }
         }
         foreach ($prefixes as $key => $value) {
             if ($value == 1) {
                 unset($prefixes[$key]);
             }
         }
         if (count($prefixes)) {
             $groups = array();
             foreach ($prefixes as $key => $value) {
                 $groups[$key] = new Node($key, Node::CONTAINER, true);
                 $groups[$key]->separator = $node->separator;
                 $groups[$key]->separator_depth = $node->separator_depth - 1;
                 $groups[$key]->icon = '';
                 if (in_array($GLOBALS['cfg']['TableNavigationLinksMode'], array('icons', 'both'))) {
                     $groups[$key]->icon = PMA_Util::getImage('b_group.png');
                 }
                 $groups[$key]->pos2 = $node->pos2;
                 $groups[$key]->pos3 = $node->pos3;
                 $node->addChild($groups[$key]);
                 foreach ($separators as $separator) {
                     // FIXME: this could be more efficient
                     foreach ($node->children as $child) {
                         $name_substring = substr($child->name, 0, strlen($key) + strlen($separator));
                         if ($name_substring == $key . $separator && $child->type == Node::OBJECT) {
                             $class = get_class($child);
                             $new_child = PMA_NodeFactory::getInstance($class, substr($child->name, strlen($key) + strlen($separator)));
                             $new_child->real_name = $child->real_name;
                             $new_child->icon = $child->icon;
                             $new_child->links = $child->links;
                             $new_child->pos2 = $child->pos2;
                             $new_child->pos3 = $child->pos3;
                             $groups[$key]->addChild($new_child);
                             foreach ($child->children as $elm) {
                                 $new_child->addChild($elm);
                             }
                             $node->removeChild($child->name);
                         }
                     }
                 }
             }
             foreach ($prefixes as $key => $value) {
                 $this->groupNode($groups[$key]);
                 $groups[$key]->classes = "navGroup";
             }
         }
     }
 }
 /**
  * Recursively groups tree nodes given a separator
  *
  * @param Node $node The node to group
  *
  * @return void
  */
 public function groupNode($node)
 {
     if ($node->type != Node::CONTAINER || $GLOBALS['cfg']['NavigationTreeDisableDatabaseExpansion']) {
         return;
     }
     $separators = array();
     if (is_array($node->separator)) {
         $separators = $node->separator;
     } else {
         if (strlen($node->separator)) {
             $separators[] = $node->separator;
         }
     }
     $prefixes = array();
     if ($node->separator_depth > 0) {
         foreach ($node->children as $child) {
             $prefix_pos = false;
             foreach ($separators as $separator) {
                 $sep_pos = strpos($child->name, $separator);
                 if ($sep_pos != false && $sep_pos != strlen($child->name) && $sep_pos != 0 && ($prefix_pos == false || $sep_pos < $prefix_pos)) {
                     $prefix_pos = $sep_pos;
                 }
             }
             if ($prefix_pos !== false) {
                 $prefix = substr($child->name, 0, $prefix_pos);
                 if (!isset($prefixes[$prefix])) {
                     $prefixes[$prefix] = 1;
                 } else {
                     $prefixes[$prefix]++;
                 }
             }
             //Bug #4375: Check if prefix is the name of a DB, to create a group.
             foreach ($node->children as $child) {
                 if (array_key_exists($child->name, $prefixes)) {
                     $prefixes[$child->name]++;
                 }
             }
         }
         //Check if prefix is the name of a DB, to create a group.
         foreach ($node->children as $child) {
             if (array_key_exists($child->name, $prefixes)) {
                 $prefixes[$child->name]++;
             }
         }
     }
     foreach ($prefixes as $key => $value) {
         if ($value == 1) {
             unset($prefixes[$key]);
         }
     }
     if (count($prefixes)) {
         $groups = array();
         foreach ($prefixes as $key => $value) {
             $groups[$key] = new Node($key, Node::CONTAINER, true);
             $groups[$key]->separator = $node->separator;
             $groups[$key]->separator_depth = $node->separator_depth - 1;
             $groups[$key]->icon = '';
             if (PMA_Util::showIcons('TableNavigationLinksMode')) {
                 $groups[$key]->icon = PMA_Util::getImage('b_group.png');
             }
             $groups[$key]->pos2 = $node->pos2;
             $groups[$key]->pos3 = $node->pos3;
             if ($node instanceof Node_Table_Container || $node instanceof Node_View_Container) {
                 $tblGroup = '&amp;tbl_group=' . urlencode($key);
                 $groups[$key]->links = array('text' => $node->links['text'] . $tblGroup, 'icon' => $node->links['icon'] . $tblGroup);
             }
             $node->addChild($groups[$key]);
             foreach ($separators as $separator) {
                 // FIXME: this could be more efficient
                 foreach ($node->children as $child) {
                     $name_substring = substr($child->name, 0, strlen($key) + strlen($separator));
                     if ($name_substring != $key . $separator && $child->name != $key || $child->type != Node::OBJECT) {
                         continue;
                     }
                     $class = get_class($child);
                     $new_child = PMA_NodeFactory::getInstance($class, substr($child->name, strlen($key) + strlen($separator)));
                     $new_child->real_name = $child->real_name;
                     $new_child->icon = $child->icon;
                     $new_child->links = $child->links;
                     $new_child->pos2 = $child->pos2;
                     $new_child->pos3 = $child->pos3;
                     $groups[$key]->addChild($new_child);
                     foreach ($child->children as $elm) {
                         $new_child->addChild($elm);
                     }
                     $node->removeChild($child->name);
                 }
             }
         }
         foreach ($prefixes as $key => $value) {
             $this->groupNode($groups[$key]);
             $groups[$key]->classes = "navGroup";
         }
     }
 }