/**
* 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 $otherChild) {
if (array_key_exists($otherChild->name, $prefixes)) {
$prefixes[$otherChild->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]++;
}
}
}
// It is not a group if it has only one item
foreach ($prefixes as $key => $value) {
if ($value == 1) {
unset($prefixes[$key]);
}
}
// rfe #1634 Don't group if there's only one group and no other items
if (count($prefixes) == 1) {
$keys = array_keys($prefixes);
$key = $keys[0];
if ($prefixes[$key] == count($node->children) - 1) {
unset($prefixes[$key]);
}
}
if (count($prefixes)) {
/** @var Node[] $groups */
$groups = array();
foreach ($prefixes as $key => $value) {
// warn about large groups
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;
}
$groups[$key] = new Node($key, Node::CONTAINER, true);
$groups[$key]->separator = $node->separator;
$groups[$key]->separator_depth = $node->separator_depth - 1;
$groups[$key]->icon = Util::getImage('b_group.png');
$groups[$key]->pos2 = $node->pos2;
$groups[$key]->pos3 = $node->pos3;
if ($node instanceof NodeTableContainer || $node instanceof NodeViewContainer) {
$tblGroup = '&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);
$className = substr($class, strrpos($class, '\\') + 1);
unset($class);
$new_child = NodeFactory::getInstance($className, mb_substr($child->name, $keySeparatorLength));
if ($new_child instanceof NodeDatabase && $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";
}
}
}