From a65a78b2bc374b6774c9886a6adbd2e516f6f469 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Wed, 22 Aug 2012 12:10:16 +0200 Subject: [PATCH] Support for nested serveur groups Add ability to nest server groups in some other groups. Thus, config files now allows to have as many level of server groups as wanted. --- CREDITS | 1 + browser.php | 9 +-- classes/Misc.php | 88 +++++++++++++++++++++++----- conf/config.inc.php-dist | 29 +++++++--- lang/english.php | 1 + lang/french.php | 1 + libraries/decorator.inc.php | 2 +- servers.php | 112 ++++++++++++++++-------------------- 8 files changed, 150 insertions(+), 93 deletions(-) diff --git a/CREDITS b/CREDITS index c552a7de..b5aec411 100644 --- a/CREDITS +++ b/CREDITS @@ -76,6 +76,7 @@ Contributors - Ivan Zolotukhin - Kristoffer `spq` Janke - Leonardo Augusto Sapiras (Improve phpPgAdmin ergonomy during the GSoC 2010, with ioguix as mentor) +- Julien Rouhaud, aka. rjuju (nested groups) Third Party Libraries diff --git a/browser.php b/browser.php index f3e09128..09dd2d73 100644 --- a/browser.php +++ b/browser.php @@ -13,13 +13,6 @@ $_no_bottom_link = true; include_once('./libraries/lib.inc.php'); - if (isset($conf['srv_groups'])) { - $treeaction = 'groupstree'; - } - else { - $treeaction = 'tree'; - } - // Output header $misc->printHeader('', ' @@ -72,7 +65,7 @@ WebFXTreeAbstractNode.prototype._ondblclick = function(e){ return false; }; */ -var tree = new WebFXLoadTree("", "servers.php?action=", "servers.php"); +var tree = new WebFXLoadTree("", "servers.php?action=tree", "servers.php"); tree.write(); tree.setExpanded(true); diff --git a/classes/Misc.php b/classes/Misc.php index df85faad..fd756ed8 100644 --- a/classes/Misc.php +++ b/classes/Misc.php @@ -2079,24 +2079,63 @@ * Get list of servers' groups if existing in the conf * @return a recordset of servers' groups */ - function getServersGroups() { + function getServersGroups($recordset = false, $group_id = false) { global $conf, $lang; $grps = array(); - + foreach ($conf['srv_groups'] as $i => $group) { - $grps[$i] = array( - 'id' => $i, - 'desc' => $group['desc'], - ); + if ( + (($group_id === false) and (! isset($group['parents']))) /* root */ + or ( + ($group_id !== false) + and isset($group['parents']) + and in_array($group_id,explode(',', + preg_replace('/\s/', '', $group['parents']) + )) + ) /* nested group */ + ) + $grps[$i] = array( + 'id' => $i, + 'desc' => $group['desc'], + 'icon' => 'Servers', + 'action' => url('servers.php', + array( + 'group' => field('id') + ) + ), + 'branch' => url('servers.php', + array( + 'action' => 'tree', + 'group' => $i + ) + ) + ); } - - $grps['all'] = array( - 'id' => 'all', - 'desc' => $lang['strallservers'], - ); - include_once('./classes/ArrayRecordSet.php'); - return new ArrayRecordSet($grps); + if ($group_id === false) + $grps['all'] = array( + 'id' => 'all', + 'desc' => $lang['strallservers'], + 'icon' => 'Servers', + 'action' => url('servers.php', + array( + 'group' => field('id') + ) + ), + 'branch' => url('servers.php', + array( + 'action' => 'tree', + 'group' => 'all' + ) + ) + ); + + if ($recordset) { + include_once('./classes/ArrayRecordSet.php'); + return new ArrayRecordSet($grps); + } + + return $grps; } @@ -2113,7 +2152,8 @@ $srvs = array(); if (($group !== false) and ($group !== 'all')) - $group = array_fill_keys(explode(',', $conf['srv_groups'][$group]['servers']), 1); + $group = array_fill_keys(explode(',', preg_replace('/\s/', '', + $conf['srv_groups'][$group]['servers'])), 1); foreach($conf['servers'] as $idx => $info) { $server_id = $info['host'].':'.$info['port'].':'.$info['sslmode']; @@ -2127,6 +2167,26 @@ else $srvs[$server_id] = $info; $srvs[$server_id]['id'] = $server_id; + $srvs[$server_id]['action'] = url('redirect.php', + array( + 'subject' => 'server', + 'server' => field('id') + ) + ); + if (isset($srvs[$server_id]['username'])) { + $srvs[$server_id]['icon'] = 'Server'; + $srvs[$server_id]['branch'] = url('all_db.php', + array( + 'action' => 'tree', + 'subject' => 'server', + 'server' => field('id') + ) + ); + } + else { + $srvs[$server_id]['icon'] = 'DisconnectedServer'; + $srvs[$server_id]['branch'] = false; + } } } diff --git a/conf/config.inc.php-dist b/conf/config.inc.php-dist index 81b12523..e08f9a39 100644 --- a/conf/config.inc.php-dist +++ b/conf/config.inc.php-dist @@ -53,18 +53,31 @@ //$conf['servers'][1]['slony_sql'] = 'C:\\Program Files\\PostgreSQL\\8.0\\share'; - // Example of groups definition. - // Groups allow administrators to logicaly group servers together under group nodes in the left browser tree - // - // The group '0' description + /* Groups definition */ + /* Groups allow administrators to logicaly group servers together under + * group nodes in the left browser tree + * + * The group '0' description + */ //$conf['srv_groups'][0]['desc'] = 'group one'; - // - // Add here servers indexes belonging to the group '0' seperated by comma + + /* Add here servers indexes belonging to the group '0' seperated by comma */ //$conf['srv_groups'][0]['servers'] = '0,1,2'; - // - // A server can belong to multi groups + + /* A server can belong to multi groups. Here server 1 is referenced in both + * 'group one' and 'group two'*/ //$conf['srv_groups'][1]['desc'] = 'group two'; //$conf['srv_groups'][1]['servers'] = '3,1'; + + /* A group can be nested in one or more existing groups using the 'parents' + * parameter. Here the group 'group three' contains only one server and will + * appear as a subgroup in both 'group one' and 'group two': + */ + //$conf['srv_groups'][2]['desc'] = 'group three'; + //$conf['srv_groups'][2]['servers'] = '4'; + //$conf['srv_groups'][2]['parents'] = '0,1'; + + /* Warning: Only groups with no parents appears at the root of the tree. */ // Default language. E.g.: 'english', 'polish', etc. See lang/ directory diff --git a/lang/english.php b/lang/english.php index 9e9f36b5..6b4a1060 100644 --- a/lang/english.php +++ b/lang/english.php @@ -305,6 +305,7 @@ // Groups $lang['strgroup'] = 'Group'; $lang['strgroups'] = 'Groups'; + $lang['strgroupgroups'] = 'Groups in group "%s"'; $lang['strshowallgroups'] = 'Show all groups'; $lang['strnogroup'] = 'Group not found.'; $lang['strnogroups'] = 'No groups found.'; diff --git a/lang/french.php b/lang/french.php index b851e1a5..3d3a8d68 100644 --- a/lang/french.php +++ b/lang/french.php @@ -306,6 +306,7 @@ // Groups $lang['strgroup'] = 'Groupe'; $lang['strgroups'] = 'Groupes'; + $lang['strgroupgroups'] = 'Groupes du groupe "%s"'; $lang['strshowallgroups'] = 'Afficher tous les groupes'; $lang['strnogroup'] = 'Groupe introuvable.'; $lang['strnogroups'] = 'Aucun groupe trouvé.'; diff --git a/libraries/decorator.inc.php b/libraries/decorator.inc.php index 8f3dc155..c31e6181 100644 --- a/libraries/decorator.inc.php +++ b/libraries/decorator.inc.php @@ -108,7 +108,7 @@ class FieldDecorator extends Decorator } function value($fields) { - return isset($fields[$this->f]) ? $fields[$this->f] : (isset($this->d) ? $this->d : null); + return isset($fields[$this->f]) ? value($fields[$this->f], $fields) : (isset($this->d) ? $this->d : null); } } diff --git a/servers.php b/servers.php index 3211b921..775fb321 100644 --- a/servers.php +++ b/servers.php @@ -32,9 +32,26 @@ $misc->printTabs('root','servers'); $misc->printMsg($msg); - $group = isset($_GET['group']) ? $_GET['group'] : false; + $groups = $misc->getServersGroups(true,$group); + + $columns = array( + 'group' => array( + 'title' => $lang['strgroup'], + 'field' => field('desc'), + 'url' => 'servers.php?', + 'vars' => array('group' => 'id'), + ), + ); + $actions = array(); + + if (($group !== false) and (isset($conf['srv_groups'][$group])) and ($groups->recordCount()>0)) { + $misc->printTitle(sprintf($lang['strgroupgroups'],htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8'))); + } + + $misc->printTable($groups, $columns, $actions); + $servers = $misc->getServers(true, $group); function svPre(&$rowdata, $actions) { @@ -75,26 +92,38 @@ ); if (($group !== false) and isset($conf['srv_groups'][$group])) { - printf("

{$lang['strgroupservers']}

", htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')); + $misc->printTitle(sprintf($lang['strgroupservers'],htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8'))); $actions['logout']['url'] .= "group=" . htmlentities($group, ENT_COMPAT, 'UTF-8') . "&"; } $misc->printTable($servers, $columns, $actions, $lang['strnoobjects'], 'svPre'); - - if (isset($conf['srv_groups'])) { - echo "
\n"; - } + } - function doTree($group = false) { - global $misc; - - $servers = $misc->getServers(true, $group); + function doTree() { + global $misc, $conf; + + $nodes = array(); + $group_id = isset($_GET['group']) ? $_GET['group'] : false; + + /* root with srv_groups */ + if (isset($conf['srv_groups']) and count($conf['srv_groups']) > 0 + and $group_id === false) + { + $nodes = $misc->getServersGroups(true); + } + /* group subtree */ + else if (isset($conf['srv_groups']) and $group_id !== false) { + if ($group_id !== 'all') + $nodes = $misc->getServersGroups(false, $group_id); + $nodes = array_merge($nodes, $misc->getServers(false, $group_id)); + include_once('./classes/ArrayRecordSet.php'); + $nodes = new ArrayRecordSet($nodes); + } + /* no srv_group */ + else { + $nodes = $misc->getServers(true, false); + } $reqvars = $misc->getRequestVars('server'); @@ -102,64 +131,23 @@ 'text' => field('desc'), // Show different icons for logged in/out - 'icon' => ifempty(field('username'), 'DisconnectedServer', 'Server'), + 'icon' => field('icon'), 'toolTip'=> field('id'), - 'action' => url('redirect.php', - $reqvars, - array('server' => field('id')) - ), - + 'action' => field('action'), + // Only create a branch url if the user has // logged into the server. - 'branch' => ifempty(field('username'), false, - url('all_db.php', - $reqvars, - array( - 'action' => 'tree', - 'server' => field('id') - ) - ) - ), + 'branch' => field('branch'), ); - $misc->printTreeXML($servers, $attrs); + $misc->printTreeXML($nodes, $attrs); exit; } - - function doGroupsTree() { - global $misc; - - $groups = $misc->getServersGroups(); - $attrs = array( - 'text' => field('desc'), - 'icon' => 'Servers', - 'action' => url('servers.php', - array( - 'group' => field('id') - ) - ), - 'branch' => url('servers.php', - array( - 'action' => 'tree', - 'group' => field('id') - ) - ) - ); - - $misc->printTreeXML($groups, $attrs); - exit; - } - - if ($action == 'tree') { - if (isset($_GET['group'])) doTree($_GET['group']); - else doTree(false); - } + if ($action == 'tree') doTree(); - if ($action == 'groupstree') doGroupsTree(); - $misc->printHeader($lang['strservers']); $misc->printBody(); $misc->printTrail('root'); -- 2.39.5