From 0c853a83d15123bd868b3cc20e4c6c6bbd9f2c77 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 18 Sep 2018 09:28:22 +0200 Subject: [PATCH] add new tree view edition group --- pandora_console/godmode/groups/group_list.php | 391 ++++++++++++------ pandora_console/include/ajax/tree.ajax.php | 4 + .../include/class/TreeGroupEdition.class.php | 108 +++++ pandora_console/include/javascript/pandora.js | 9 +- .../include/javascript/tree/TreeController.js | 217 +++++----- 5 files changed, 509 insertions(+), 220 deletions(-) create mode 100644 pandora_console/include/class/TreeGroupEdition.class.php diff --git a/pandora_console/godmode/groups/group_list.php b/pandora_console/godmode/groups/group_list.php index d6efbc2d5f..0efe11ad31 100644 --- a/pandora_console/godmode/groups/group_list.php +++ b/pandora_console/godmode/groups/group_list.php @@ -215,20 +215,57 @@ if (! check_acl($config['id_user'], 0, "PM")) { return; } +$sec = defined('METACONSOLE') ? 'advanced' : 'gagente'; +$url_tree = "index.php?sec=$sec&sec2=godmode/groups/group_list&tab=tree"; +$url_groups = "index.php?sec=$sec&sec2=godmode/groups/group_list&tab=groups"; +$buttons['tree'] = array( + 'active' => false, + 'text' => "" . + html_print_image( + "images/gm_massive_operations.png", + true, + array ( + "title" => __('Tree Group view') + ) + ) . "" +); + +$buttons['groups'] = array( + 'active' => false, + 'text' => "" . + html_print_image( + "images/group.png", + true, + array( + "title" => __('Group view') + ) + ) . "" +); + +$tab = (string)get_parameter('tab', 'groups'); + +// Marks correct tab +switch ($tab) { + case 'tree': + $buttons['tree']['active'] = true; + break; + case 'groups': + default: + $buttons['groups']['active'] = true; + break; +} + // Header if (defined('METACONSOLE')) { agents_meta_print_header(); - $sec = 'advanced'; echo '
'; echo __("Edit or delete groups can cause problems with synchronization"); echo '
'; -} -else { +} else { ui_print_page_header ( __("Groups defined in %s", get_product_name()), - "images/group.png", false, "", true, "" + "images/group.png", false, "", true, $buttons ); - $sec = 'gagente'; } $create_group = (bool) get_parameter ('create_group'); @@ -365,148 +402,158 @@ if (($delete_group) && (check_acl($config['id_user'], 0, "PM"))) { } } -$acl=''; -$search_name = ''; -$offset = (int)get_parameter('offset', 0); -$search = (string)get_parameter('search', ''); -$block_size = $config['block_size']; +if($tab == 'tree'){ + echo html_print_image('images/spinner.gif', true, + array('class' => "loading_tree", + 'style' => 'display: none;')); + echo "
"; -if(!empty($search)){ - $search_name = "AND t.nombre LIKE '%$search%'"; } +else{ + $acl=''; + $search_name = ''; + $offset = (int)get_parameter('offset', 0); + $search = (string)get_parameter('search', ''); + $block_size = $config['block_size']; -if (!users_can_manage_group_all("AR")){ - $user_groups_acl = users_get_groups(false, "AR"); - $groups_acl = implode(",", $user_groups_ACL); - if(empty($groups_acl)) return ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no defined groups') ) ); + if(!empty($search)){ + $search_name = "AND t.nombre LIKE '%$search%'"; + } - $acl = "AND t.id_grupo IN ($groups_acl)"; -} + if (!users_can_manage_group_all("AR")){ + $user_groups_acl = users_get_groups(false, "AR"); + $groups_acl = implode(",", $user_groups_ACL); + if(empty($groups_acl)) return ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no defined groups') ) ); -$form = "
"; - $form .= ""; - $form .= ""; - $form .= "
" . __('Search') . ' '; - $form .= html_print_input_text ("search", $search, '', 100, 100, true); - $form .= ""; - $form .= ""; - $form .= "
"; -$form .= "
"; + $acl = "AND t.id_grupo IN ($groups_acl)"; + } -echo $form; + $form = "
"; + $form .= ""; + $form .= ""; + $form .= "
" . __('Search') . ' '; + $form .= html_print_input_text ("search", $search, '', 100, 100, true); + $form .= ""; + $form .= ""; + $form .= "
"; + $form .= "
"; -$groups_sql = - "SELECT t.*, - p.nombre AS parent_name, - IF(t.parent=p.id_grupo, 1, 0) AS has_child - FROM tgrupo t - LEFT JOIN tgrupo p - ON t.parent=p.id_grupo - WHERE 1=1 - $acl - $search_name - ORDER BY nombre - LIMIT $offset, $block_size -"; + echo $form; -$groups = db_get_all_rows_sql($groups_sql); - -if (!empty($groups)) { - //Count all groups for pagination only saw user and filters - $groups_sql_count = "SELECT count(*) + $groups_sql = + "SELECT t.*, + p.nombre AS parent_name, + IF(t.parent=p.id_grupo, 1, 0) AS has_child FROM tgrupo t + LEFT JOIN tgrupo p + ON t.parent=p.id_grupo WHERE 1=1 $acl $search_name + ORDER BY nombre + LIMIT $offset, $block_size "; - $groups_count = db_get_value_sql($groups_sql_count); - $table = new StdClass(); - $table->width = '100%'; - $table->class = "databox data"; - $table->head = array (); - $table->head[0] = __('ID'); - $table->head[1] = __('Name'); - $table->head[2] = __('Icon'); - $table->head[3] = __('Alerts'); - $table->head[4] = __('Parent'); - $table->head[5] = __('Description'); - $table->head[6] = __('Actions'); - $table->align = array (); - $table->align[0] = 'right'; - $table->align[2] = 'left'; - $table->align[6] = 'left'; - $table->size[5] = '30%'; - $table->size[6] = '10%'; - $table->data = array (); + $groups = db_get_all_rows_sql($groups_sql); - foreach ($groups as $key => $group) { - $url = "index.php?sec=gagente&sec2=godmode/groups/configure_group&id_group=".$group['id_grupo']; - $url_delete = "index.php?sec=gagente&sec2=godmode/groups/group_list&delete_group=1&id_group=" . $group['id_grupo']; - $table->data[$key][0] = $group['id_grupo']; - $table->data[$key][1] = "" . $group['nombre'] . ""; - $table->data[$key][2] = html_print_image( - "images/groups_small/" . $group['icon'] . ".png", - true, - array( - "style" => '', - "class" => "bot", - "alt" => $group['nombre'], - "title" => $group['nombre'], - false, false, false, true - ) - ); + if (!empty($groups)) { + //Count all groups for pagination only saw user and filters + $groups_sql_count = "SELECT count(*) + FROM tgrupo t + WHERE 1=1 + $acl + $search_name + "; + $groups_count = db_get_value_sql($groups_sql_count); - //reporting_get_group_stats - $table->data[$key][3] = $group['disabled'] ? __('Disabled') : __('Enabled'); - $table->data[$key][4] = $group['parent_name']; - $table->data[$key][5] = $group['description']; - $table->data[$key][6] = "" . - html_print_image( - "images/config.png", + $table = new StdClass(); + $table->width = '100%'; + $table->class = "databox data"; + $table->head = array (); + $table->head[0] = __('ID'); + $table->head[1] = __('Name'); + $table->head[2] = __('Icon'); + $table->head[3] = __('Alerts'); + $table->head[4] = __('Parent'); + $table->head[5] = __('Description'); + $table->head[6] = __('Actions'); + $table->align = array (); + $table->align[0] = 'left'; + $table->align[2] = 'left'; + $table->align[6] = 'left'; + $table->size[0] = '3%'; + $table->size[5] = '30%'; + $table->size[6] = '5%'; + $table->data = array (); + + foreach ($groups as $key => $group) { + $url = "index.php?sec=gagente&sec2=godmode/groups/configure_group&id_group=".$group['id_grupo']; + $url_delete = "index.php?sec=gagente&sec2=godmode/groups/group_list&delete_group=1&id_group=" . $group['id_grupo']; + $table->data[$key][0] = $group['id_grupo']; + $table->data[$key][1] = "" . $group['nombre'] . ""; + $table->data[$key][2] = html_print_image( + "images/groups_small/" . $group['icon'] . ".png", true, array( - "alt" => __('Edit'), - "title" => __('Edit'), - "border" => '0' + "style" => '', + "class" => "bot", + "alt" => $group['nombre'], + "title" => $group['nombre'], + false, false, false, true ) - ) . - ""; + ); + + //reporting_get_group_stats + $table->data[$key][3] = $group['disabled'] ? __('Disabled') : __('Enabled'); + $table->data[$key][4] = $group['parent_name']; + $table->data[$key][5] = $group['description']; + $table->data[$key][6] = "" . + html_print_image( + "images/config.png", + true, + array( + "alt" => __('Edit'), + "title" => __('Edit'), + "border" => '0' + ) + ) . + ""; + + $confirm_message = __('Are you sure?'); + if ($group['has_child']) { + $confirm_message = __('The child groups will be updated to use the parent id of the deleted group') . ". " . $confirm_message; + } + + $table->data[$key][6] .= '  ' . + '' . + html_print_image( + "images/cross.png", + true, + array( + "alt" => __('Delete'), + "title" => __('Delete'), + "border" => '0' + ) + ) . + ""; - $confirm_message = __('Are you sure?'); - if ($group['has_child']) { - $confirm_message = __('The child groups will be updated to use the parent id of the deleted group') . ". " . $confirm_message; } - $table->data[$key][6] .= '  ' . - '' . - html_print_image( - "images/cross.png", - true, - array( - "alt" => __('Delete'), - "title" => __('Delete'), - "border" => '0' - ) - ) . - ""; - + echo ui_pagination( + $groups_count, false, + $offset, $block_size, + true, 'offset', false + ); + html_print_table ($table); + echo ui_pagination( + $groups_count, false, + $offset, $block_size, + true, 'offset', true + ); + } + else { + ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no defined groups') ) ); } - - echo ui_pagination( - $groups_count, false, - $offset, $block_size, - true, 'offset', false - ); - html_print_table ($table); - echo ui_pagination( - $groups_count, false, - $offset, $block_size, - true, 'offset', true - ); -} -else { - ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no defined groups') ) ); } if (check_acl($config['id_user'], 0, "PM")) { @@ -517,5 +564,101 @@ if (check_acl($config['id_user'], 0, "PM")) { echo ''; } +ui_require_javascript_file("TreeController", "include/javascript/tree/"); + enterprise_hook('close_meta_frame'); +$tab = "group_edition"; + ?> + + + + + + + + diff --git a/pandora_console/include/ajax/tree.ajax.php b/pandora_console/include/ajax/tree.ajax.php index c2e051c3f4..07435d9109 100644 --- a/pandora_console/include/ajax/tree.ajax.php +++ b/pandora_console/include/ajax/tree.ajax.php @@ -43,6 +43,7 @@ if (is_ajax ()) { require_once($config['homedir'] . "/include/class/TreeModule.class.php"); require_once($config['homedir'] . "/include/class/TreeTag.class.php"); require_once($config['homedir'] . "/include/class/TreeGroup.class.php"); + require_once($config['homedir'] . "/include/class/TreeGroupEdition.class.php"); enterprise_include_once("include/class/TreePolicies.class.php"); enterprise_include_once("include/class/TreeGroupMeta.class.php"); require_once($config['homedir'] . "/include/functions_reporting.php"); @@ -104,6 +105,9 @@ if (is_ajax ()) { if (!class_exists('TreePolicies')) break; $tree = new TreePolicies($type, $rootType, $id, $rootID, $serverID, $childrenMethod, $access); break; + case 'group_edition': + $tree = new TreeGroupEdition($type, $rootType, $id, $rootID, $serverID, $childrenMethod, $access); + break; default: // FIXME. No error handler return; diff --git a/pandora_console/include/class/TreeGroupEdition.class.php b/pandora_console/include/class/TreeGroupEdition.class.php new file mode 100644 index 0000000000..dea62da238 --- /dev/null +++ b/pandora_console/include/class/TreeGroupEdition.class.php @@ -0,0 +1,108 @@ +id == -1) { + $this->getFirstLevel(); + } + } + + protected function getProcessedGroups () { + $processed_groups = array(); + // Index and process the groups + $groups = $this->getGroupCounters(); + + // If user have not permissions in parent, set parent node to 0 (all) + // Avoid to do foreach for admins + if (!users_can_manage_group_all("AR")) { + foreach ($groups as $id => $group) { + if (!isset($this->userGroups[$groups[$id]['parent']])) { + $groups[$id]['parent'] = 0; + } + } + } + // Build the group hierarchy + foreach ($groups as $id => $group) { + if (isset($groups[$id]['parent']) && ($groups[$id]['parent'] != 0)) { + $parent = $groups[$id]['parent']; + // Parent exists + if (!isset($groups[$parent]['children'])) { + $groups[$parent]['children'] = array(); + } + // Store a reference to the group into the parent + $groups[$parent]['children'][] = &$groups[$id]; + // This group was introduced into a parent + $groups[$id]['have_parent'] = true; + } + } + + // Sort the children groups + foreach ($groups as $id => $group) { + if (isset($groups[$id]['children'])) { + usort($groups[$id]['children'], array("Tree", "cmpSortNames")); + } + } + //Filter groups and eliminates the reference to children groups out of her parent + $groups = array_filter($groups, function ($group) { + return !$group['have_parent']; + }); + + usort($groups, array("Tree", "cmpSortNames")); + return $groups; + } + + + protected function getGroupCounters() { + $messages = array( + 'confirm' => __('Confirm'), + 'cancel' => __('Cancel'), + 'messg' => __('Are you sure?') + ); + $sql = "SELECT id_grupo AS gid, + nombre as name, parent, icon + FROM tgrupo + "; + + $stats = db_get_all_rows_sql($sql); + $group_stats = array(); + foreach ($stats as $group) { + $group_stats[$group['gid']]['name'] = $group['name']; + $group_stats[$group['gid']]['parent'] = $group['parent']; + $group_stats[$group['gid']]['icon'] = $group['icon']; + $group_stats[$group['gid']]['id'] = $group['gid']; + $group_stats[$group['gid']]['type'] = 'group'; + + $group_stats[$group['gid']] = $this->getProcessedItem($group_stats[$group['gid']]); + $group_stats[$group['gid']]['delete']['messages'] = $messages; + $group_stats[$group['gid']]['edit'] = 1; + $group_stats[$group['gid']]['alerts'] = ''; + } + + return $group_stats; + } +} + +?> + diff --git a/pandora_console/include/javascript/pandora.js b/pandora_console/include/javascript/pandora.js index bc8432ae48..11157ed3ff 100644 --- a/pandora_console/include/javascript/pandora.js +++ b/pandora_console/include/javascript/pandora.js @@ -1585,6 +1585,10 @@ function display_confirm_dialog (message, ok_text, cancel_text, ok_function) { clean_function(); } + var buttons_obj = {}; + buttons_obj[cancel_text] = clean_function; + buttons_obj[ok_text] = ok_function_clean; + // Display the dialog $("body").append('

' + message + '

'); $("#pandora_confirm_dialog_text").dialog({ @@ -1598,10 +1602,7 @@ function display_confirm_dialog (message, ok_text, cancel_text, ok_function) { }, closeOnEscape: true, modal: true, - buttons: { - Cancel: clean_function, - "Confirm": ok_function_clean - } + buttons: buttons_obj }); } diff --git a/pandora_console/include/javascript/tree/TreeController.js b/pandora_console/include/javascript/tree/TreeController.js index 409fc517d1..13a0b31cd4 100644 --- a/pandora_console/include/javascript/tree/TreeController.js +++ b/pandora_console/include/javascript/tree/TreeController.js @@ -55,7 +55,7 @@ var TreeController = { } container.append($group); - + _.each(elements, function(element) { element.jqObject = _processNode($group, element); }); @@ -279,14 +279,13 @@ var TreeController = { // Load leaf function _processNode (container, element) { - // type, [id], [serverID], callback function _getTreeDetailData (type, id, serverID, callback) { var lastParam = arguments[arguments.length - 1]; var callback; if (typeof lastParam === 'function') callback = lastParam; - + var serverID; if (arguments.length >= 4) serverID = arguments[2]; @@ -296,23 +295,23 @@ var TreeController = { var type; if (arguments.length >= 2) type = arguments[0]; - + if (typeof type === 'undefined') throw new TypeError('Type required'); if (typeof callback === 'undefined') throw new TypeError('Callback required'); - + var postData = { page: controller.ajaxPage, getDetail: 1, type: type } - + if (typeof id !== 'undefined') postData.id = id; if (typeof serverID !== 'undefined') postData.serverID = serverID; - + $.ajax({ url: controller.ajaxURL, type: 'POST', @@ -346,6 +345,36 @@ var TreeController = { $content.append(element.iconHTML + " "); } $content.append(element.name); + + if(typeof element.edit != 'undefined'){ + var url_edit = controller.baseURL + "index.php?sec=gagente&sec2=godmode/groups/configure_group&tab=tree&id_group=" + element.id; + var $updateicon = $('') + var $updatebtn = $('') + .append($updateicon); + $content.append($updatebtn); + } + + if(typeof element.delete != 'undefined'){ + var url_delete = controller.baseURL + "index.php?sec=gagente&sec2=godmode/groups/group_list&tab=tree&delete_group=1&id_group=" + element.id; + var $deleteBtn = $(''); + $deleteBtn.click(function (event){ + var ok_function = function(){ + window.location.replace(url_delete); + }; + display_confirm_dialog( + element.delete.messages.messg, + element.delete.messages.confirm, + element.delete.messages.cancel, + ok_function + ) + }); + $content.append($deleteBtn); + } + + if(typeof element.alerts != 'undefined'){ + $content.append(element.alerts); + } + break; case 'agent': // Is quiet @@ -583,95 +612,99 @@ var TreeController = { if (typeof(public_user) === 'undefined') public_user = 0; if (typeof element.searchChildren != 'undefined' && element.searchChildren) { - $node - .removeClass("leaf-empty") - .addClass("leaf-closed"); + if(element.rootType == "group_edition" + && typeof element.children == 'undefined'){ + $node.addClass("leaf-empty"); + } + else{ + $node + .removeClass("leaf-empty") + .addClass("leaf-closed"); + $leafIcon.click(function (e) { + e.preventDefault(); - $leafIcon.click(function (e) { - e.preventDefault(); - - if (!$node.hasClass("leaf-loading") && !$node.hasClass("children-loaded") && !$node.hasClass("leaf-empty")) { - $node - .removeClass("leaf-closed") - .removeClass("leaf-error") - .addClass("leaf-loading"); - - $.ajax({ - url: controller.ajaxURL, - type: 'POST', - dataType: 'json', - data: { - page: controller.ajaxPage, - getChildren: 1, - id: element.id, - type: element.type, - rootID: element.rootID, - serverID: element.serverID, - rootType: element.rootType, - filter: controller.filter, - hash: public_hash, - id_user: public_user - }, - complete: function(xhr, textStatus) { - $node.removeClass("leaf-loading"); - $node.addClass("children-loaded"); - }, - success: function(data, textStatus, xhr) { - if (data.success) { - var $group = $node.children("ul.tree-group"); - - if ((typeof data.tree != 'undefined' && data.tree.length > 0) || $group.length > 0) { - $node.addClass("leaf-open"); - - if ($group.length <= 0) { - $group = $(""); - $group - .addClass("tree-group") - .hide(); - $node.append($group); - } - - _.each(data.tree, function(element) { - element.jqObject = _processNode($group, element); - }); - - $group.slideDown(); - - $node.data('children', $group); - - // Add again the hover event to the 'force_callback' elements - forced_title_callback(); - } - else { - $node.addClass("leaf-empty"); - } - } - else { - $node.addClass("leaf-error"); - } - }, - error: function(xhr, textStatus, errorThrown) { - $node.addClass("leaf-error"); - } - }); - } - else if (! $node.hasClass("leaf-empty")) { - if ($node.hasClass("leaf-open")) { - $node - .removeClass("leaf-open") - .addClass("leaf-closed") - .data('children') - .slideUp(); - } - else { + if (!$node.hasClass("leaf-loading") && !$node.hasClass("children-loaded") && !$node.hasClass("leaf-empty")) { $node .removeClass("leaf-closed") - .addClass("leaf-open") - .data('children') - .slideDown(); + .removeClass("leaf-error") + .addClass("leaf-loading"); + + $.ajax({ + url: controller.ajaxURL, + type: 'POST', + dataType: 'json', + data: { + page: controller.ajaxPage, + getChildren: 1, + id: element.id, + type: element.type, + rootID: element.rootID, + serverID: element.serverID, + rootType: element.rootType, + filter: controller.filter, + hash: public_hash, + id_user: public_user + }, + complete: function(xhr, textStatus) { + $node.removeClass("leaf-loading"); + $node.addClass("children-loaded"); + }, + success: function(data, textStatus, xhr) { + if (data.success) { + var $group = $node.children("ul.tree-group"); + if ((typeof data.tree != 'undefined' && data.tree.length > 0) || $group.length > 0) { + $node.addClass("leaf-open"); + + if ($group.length <= 0) { + $group = $(""); + $group + .addClass("tree-group") + .hide(); + $node.append($group); + } + + _.each(data.tree, function(element) { + element.jqObject = _processNode($group, element); + }); + + $group.slideDown(); + + $node.data('children', $group); + + // Add again the hover event to the 'force_callback' elements + forced_title_callback(); + } + else { + $node.addClass("leaf-empty"); + } + } + else { + $node.addClass("leaf-error"); + } + }, + error: function(xhr, textStatus, errorThrown) { + $node.addClass("leaf-error"); + } + }); } - } - }); + else if (! $node.hasClass("leaf-empty")) { + if ($node.hasClass("leaf-open")) { + $node + .removeClass("leaf-open") + .addClass("leaf-closed") + .data('children') + .slideUp(); + } + else { + $node + .removeClass("leaf-closed") + .addClass("leaf-open") + .data('children') + .slideDown(); + } + } + }); + } } return $node;