From c16ca0d90c27cb261f893d506eb8acda33d378a1 Mon Sep 17 00:00:00 2001
From: mdtrooper <tres.14159@gmail.com>
Date: Thu, 11 Apr 2013 15:02:22 +0000
Subject: [PATCH] 2013-04-11 Miguel de Dios <miguel.dedios@artica.es>

	* godmode/groups/group_list.php, include/functions_groups.php:
	optimize the access to the groups in systems with a huge groups.

	* include/functions_snmp_browser.php, include/functions_users.php:
	clean source code style.

	* pandoradb.sql: added key for the parent group.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7963 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     | 10 ++++
 pandora_console/godmode/groups/group_list.php |  2 +-
 pandora_console/include/functions_groups.php  | 48 ++++++++++++-------
 .../include/functions_snmp_browser.php        |  2 +-
 pandora_console/include/functions_users.php   |  2 +-
 pandora_console/pandoradb.sql                 |  3 +-
 6 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 475a2f920f..dbd2db4689 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,13 @@
+2013-04-11 Miguel de Dios <miguel.dedios@artica.es>
+
+	* godmode/groups/group_list.php, include/functions_groups.php:
+	optimize the access to the groups in systems with a huge groups.
+	
+	* include/functions_snmp_browser.php, include/functions_users.php:
+	clean source code style.
+	
+	* pandoradb.sql: added key for the parent group.
+
 2013-04-11  Sergio Martin <sergio.martin@artica.es>
 
 	* operation/visual_console/render_view.php
diff --git a/pandora_console/godmode/groups/group_list.php b/pandora_console/godmode/groups/group_list.php
index fd2a1b2f3f..dec48ad7ae 100644
--- a/pandora_console/godmode/groups/group_list.php
+++ b/pandora_console/godmode/groups/group_list.php
@@ -245,7 +245,7 @@ if ($delete_group) {
 	}
 }
 db_clean_cache();
-$groups = users_get_groups_tree ($config['id_user'], "AR", true); 
+$groups = users_get_groups_tree ($config['id_user'], "AR", true);
 $table->width = '98%';
 
 if (!empty($groups)) {
diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php
index 24a34b480b..b111b4caa2 100644
--- a/pandora_console/include/functions_groups.php
+++ b/pandora_console/include/functions_groups.php
@@ -458,6 +458,21 @@ function groups_get_id_recursive($id_parent, $all = false) {
 	return $return;
 }
 
+function groups_flatten_tree_groups($tree, $deep) {
+	foreach ($tree as $key => $group) {
+		$return[$key] = $group;
+		unset($return[$key]['branch']);
+		$return[$key]['deep'] = $deep;
+		
+		if (!empty($group['branch'])) {
+			$return = $return +
+				groups_flatten_tree_groups($group['branch'], $deep + 1);
+		}
+	}
+	
+	return $return;
+}
+
 /**
  * Make with a list of groups a treefied list of groups.
  *
@@ -467,30 +482,29 @@ function groups_get_id_recursive($id_parent, $all = false) {
  *
  * @return array The treefield list of groups.
  */
-function groups_get_groups_tree_recursive($groups, $parent = 0, $deep = 0) {
+function groups_get_groups_tree_recursive($groups, $trash = 0, $trash2 = 0) {
 	$return = array();
 	
+	$pointer_to_parent = array();
+	$tree = array();
 	foreach ($groups as $key => $group) {
-		if (($key === 0 || $key === __('All')) && ($parent === 0 || $parent === __('All'))) {
-			//When the groups is the all group
-			$group['deep'] = $deep;
-			$group['hash_branch'] = true;
-			$deep ++;
-			$return = $return + array($key => $group);
+		if (!isset($pointer_to_parent[$group['parent']])) {
+			$group['branch'] = array();
+			$group['hash_branch'] = false;
+			$tree[$key] = $group;
+			$pointer_to_parent[$key] = &$tree[$key];
 		}
-		else if ($group['parent'] == $parent) {
-			$group['deep'] = $deep;
-			$branch = groups_get_groups_tree_recursive($groups, $key, $deep + 1);
-			if (empty($branch)) {
-				$group['hash_branch'] = false;
-			}
-			else {
-				$group['hash_branch'] = true;
-			}
-			$return = $return + array($key => $group) + $branch;
+		else {
+			$group['branch'] = array();
+			$pointer_to_parent[$group['parent']]['branch'][$key] = $group;
+			$pointer_to_parent[$group['parent']]['hash_branch'] = true;
+			
+			$pointer_to_parent[$key] = &$pointer_to_parent[$group['parent']]['branch'][$key];
 		}
 	}
 	
+	$return = groups_flatten_tree_groups($tree, 0);
+	
 	return $return;
 }
 
diff --git a/pandora_console/include/functions_snmp_browser.php b/pandora_console/include/functions_snmp_browser.php
index 439ec2be50..efb49a015d 100644
--- a/pandora_console/include/functions_snmp_browser.php
+++ b/pandora_console/include/functions_snmp_browser.php
@@ -207,7 +207,7 @@ function snmp_browser_get_tree ($target_ip, $community, $starting_oid = '.') {
 		$sub_oid = "";
 	}
 	
-	return$oid_tree;
+	return $oid_tree;
 }
 
 /**
diff --git a/pandora_console/include/functions_users.php b/pandora_console/include/functions_users.php
index 9ac4b4bd63..8dae940afe 100644
--- a/pandora_console/include/functions_users.php
+++ b/pandora_console/include/functions_users.php
@@ -117,7 +117,7 @@ function users_get_groups_for_select($id_user,  $privilege = "AR", $returnAllGro
 	
 	foreach ($user_groups_tree as $group) {
 		$groupName = ui_print_truncate_text($group['nombre'], GENERIC_SIZE_TEXT, false, true, false);
-
+		
 		$fields[$group[$keys_field]] = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $group['deep']) . $groupName;
 	}
 	
diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql
index 4f7c1b644a..7618f7c2b6 100644
--- a/pandora_console/pandoradb.sql
+++ b/pandora_console/pandoradb.sql
@@ -528,7 +528,8 @@ CREATE TABLE IF NOT EXISTS `tgrupo` (
 	`description` text,
 	`contact` text,
 	`other` text,
- 	PRIMARY KEY  (`id_grupo`)
+ 	PRIMARY KEY  (`id_grupo`),
+ 	KEY `parent_index` (`parent`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 -- ---------------------------------------------------------------------