From 42c08f152b2d78580e04831072b3eb1f8f2c9dc9 Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Wed, 17 Dec 2014 18:58:34 +0100 Subject: [PATCH] Working in the new code of treeview --- pandora_console/include/ajax/tree.ajax.php | 24 ++-- .../class/{tree.class.php => Tree.class.php} | 111 ++++++++++++++---- .../include/javascript/tree/TreeController.js | 41 ++++--- pandora_console/operation/tree.php | 2 +- pandora_console/operation/tree2.php | 52 ++++++-- 5 files changed, 163 insertions(+), 67 deletions(-) rename pandora_console/include/class/{tree.class.php => Tree.class.php} (59%) diff --git a/pandora_console/include/ajax/tree.ajax.php b/pandora_console/include/ajax/tree.ajax.php index 40509c6ee1..4fecccb8a9 100644 --- a/pandora_console/include/ajax/tree.ajax.php +++ b/pandora_console/include/ajax/tree.ajax.php @@ -13,21 +13,23 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -require_once("include/class/tree.class.php"); +require_once("include/class/Tree.class.php"); -$get_data = (bool)get_parameter('get_data', 0); +$getChildren = (bool)get_parameter('getChildren', 0); -if ($get_data) { - $tab = get_parameter('type', 'group'); - $search = get_parameter('search', ''); - $status = (int)get_parameter('status', AGENT_STATUS_ALL); +if ($getChildren) { + $filter = get_parameter('filter', + array('type' => 'groupz', + 'search' => '', + 'status' => AGENT_STATUS_ALL)); $root = (int)get_parameter('root', 0); + $method = get_parameter('method', 'on_demand'); - $tree = new Tree($tab); - $tree->set_filter(array( - 'status' => $status, - 'search' => $search)); - echo $tree->get_json(); + $tree = new Tree($filter['type'], $method, $root); + $tree->setFilter(array( + 'status' => $filter['status'], + 'search' => $filter['search'])); + echo json_encode(array('success' => 1, 'tree' => $tree->getArray())); return; } ?> \ No newline at end of file diff --git a/pandora_console/include/class/tree.class.php b/pandora_console/include/class/Tree.class.php similarity index 59% rename from pandora_console/include/class/tree.class.php rename to pandora_console/include/class/Tree.class.php index 5a3c8d686d..22a851fe17 100644 --- a/pandora_console/include/class/tree.class.php +++ b/pandora_console/include/class/Tree.class.php @@ -18,57 +18,55 @@ class Tree { private $tree = array(); private $filter = array(); private $root = null; + private $children = "on_demand"; - public function __construct($type, $root = null) { + public function __construct($type, $childrenMethod = "on_demand", $root = null) { $this->type = $type; $this->root = $root; + $this->childrenMethod = $childrenMethod; } - public function set_type($type) { + public function setType($type) { $this->type = $type; } - public function set_filter($filter) { + public function setFilter($filter) { $this->filter = $filter; } - public function get_data() { + public function getData() { switch ($this->type) { case 'os': - $this->get_data_os(); + $this->getDataOS(); break; case 'group': - $this->get_data_group(); + $this->getDataGroup(); break; case 'module_group': - $this->get_data_module_group(); + $this->getDataModuleGroup(); break; case 'module': - $this->get_data_module(); + $this->getDataModule(); break; case 'tag': - $this->get_data_tag(); + $this->getDataTag(); break; } } - public function get_data_os() { + public function getDataOS() { } - public function get_data_group() { + private function getRecursiveGroup($parent, $limit = null) { $filter = array(); - if (!empty($this->root)) { - $filter['parent'] = $this->root; - } - else { - $filter['parent'] = 0; - } + + $filter['parent'] = $parent; + if (!empty($this->filter['search'])) { $filter['nombre'] = "%" . $this->filter['search'] . "%"; } - // First filter by name and father $groups = db_get_all_rows_filter('tgrupo', $filter, @@ -76,12 +74,15 @@ class Tree { if (empty($groups)) $groups = array(); + // Filter by status $status = AGENT_STATUS_ALL; if (!empty($this->filter['status'])) { $status = $this->filter['status']; } + + if ($status != AGENT_STATUS_ALL) { foreach ($groups as $iterator => $group) { $count_ok = groups_monitor_ok( @@ -126,33 +127,97 @@ class Tree { if ($remove_group) unset($groups[$iterator]); + else { + if (is_null($limit)) { + $groups[$iterator]['children'] = + $this->getRecursiveGroup($group['id_grupo']); + } + else if ($limit >= 1) { + $groups[$iterator]['children'] = + $this->getRecursiveGroup( + $group['id_grupo'], + ($limit - 1)); + } + } + } + } + else { + foreach ($groups as $iterator => $group) { + if (is_null($limit)) { + $groups[$iterator]['children'] = + $this->getRecursiveGroup($group['id_grupo']); + } + else if ($limit >= 1) { + $groups[$iterator]['children'] = + $this->getRecursiveGroup( + $group['id_grupo'], + ($limit - 1)); + } } } + + return $groups; + } + + public function getDataGroup() { + + if (!empty($this->root)) { + $parent = $this->root; + } + else { + $parent = 0; + } + + switch ($this->childrenMethod) { + case 'on_demand': + $groups = $this->getRecursiveGroup($parent, 1); + foreach ($groups as $iterator => $group) { + if (!empty($group['children'])) { + $groups[$iterator]['searchChildren'] = 1; + // I hate myself + unset($groups[$iterator]['children']); + } + else { + $groups[$iterator]['searchChildren'] = 0; + // I hate myself + unset($groups[$iterator]['children']); + } + } + break; + } // Make the data $this->tree = array(); foreach ($groups as $group) { $data = array(); $data['id'] = $group['id_grupo']; + $data['type'] = 'group'; $data['name'] = $group['nombre']; + $data['searchChildren'] = $group['searchChildren']; $this->tree[] = $data; } } - public function get_data_module_group() { + public function getDataModuleGroup() { } - public function get_data_module() { + public function getDataModule() { } - public function get_data_tag() { + public function getDataTag() { } - public function get_json() { - $this->get_data(); + public function getJSON() { + $this->getData(); return json_encode($this->tree); } + + public function getArray() { + $this->getData(); + + return $this->tree; + } } ?> diff --git a/pandora_console/include/javascript/tree/TreeController.js b/pandora_console/include/javascript/tree/TreeController.js index d83e4dd5cb..405bbf1317 100644 --- a/pandora_console/include/javascript/tree/TreeController.js +++ b/pandora_console/include/javascript/tree/TreeController.js @@ -28,7 +28,7 @@ TreeController = { if (typeof this.recipient == 'undefined' || this.recipient.length == 0) { return; } - + function _processGroup (container, elements, baseURL, rootGroup) { var $group = $(""); @@ -44,9 +44,9 @@ TreeController = { .addClass("tree-group") .hide(); } - + container.append($group); - + var lastNode; var firstNode; elements.forEach(function(element, index) { @@ -54,17 +54,17 @@ TreeController = { firstNode = rootGroup && index == 0 ? true : false; element.jqObject = _processNode($group, element, lastNode, firstNode); }, $group); - + return $group; } function _processNode (container, element, lastNode, firstNode) { var $node = $("
  • "); var $leafIcon = $("
    "); var $content = $("
    "); - + // Leaf icon $leafIcon.addClass("leaf-icon"); - + // Content $content.addClass("node-content"); switch (element.type) { @@ -75,28 +75,28 @@ TreeController = { $content.append(element.name); break; } - + $node .addClass("tree-node") .append($leafIcon) .append($content); - + if (typeof lastNode != 'undefinded' && lastNode == true) { $node.addClass("tree-last"); } if (typeof firstNode != 'undefinded' && firstNode == true) { $node.addClass("tree-first"); } - + container.append($node); - + if (typeof element.children != 'undefined' && element.children.length > 0) { $node.addClass("leaf-closed"); - + // Add children var $children = _processGroup($node, element.children, this.baseURL); $node.data('children', $children); - + $leafIcon.click(function () { if ($node.hasClass("leaf-open")) { $node @@ -116,14 +116,14 @@ TreeController = { } else if (typeof element.searchChildren != 'undefined' && element.searchChildren) { $node.addClass("leaf-closed"); - + $leafIcon.click(function () { if (! $node.hasClass("children-loaded")) { $node .removeClass("leaf-closed") .removeClass("leaf-error") .addClass("leaf-loading"); - + $.ajax({ url: this.ajaxURL, type: 'POST', @@ -140,10 +140,10 @@ TreeController = { success: function(data, textStatus, xhr) { if (data.success) { $node.addClass("leaf-open"); - + var $children = _processGroup($node, data.elements, this.baseURL); $children.slideDown(); - + $node.data('children', $children); } else { @@ -176,10 +176,10 @@ TreeController = { else { $node.addClass("leaf-empty"); } - + return $node; } - + if (this.recipient.length == 0) { return; } @@ -187,12 +187,11 @@ TreeController = { this.recipient.html("
    " + this.emptyMessage + "
    "); return; } - + this.recipient.empty(); - var $children = _processGroup(this.recipient, this.tree, this.baseURL, true); $children.show(); - + this.recipient.data('children', $children); }, load: function () { diff --git a/pandora_console/operation/tree.php b/pandora_console/operation/tree.php index 4997e8e221..2f07865e6c 100755 --- a/pandora_console/operation/tree.php +++ b/pandora_console/operation/tree.php @@ -17,7 +17,7 @@ require_once("tree2.php"); -//~ return; +return; //////////////////////////////////////////////////////////////////////// diff --git a/pandora_console/operation/tree2.php b/pandora_console/operation/tree2.php index e04cfb1945..144f0793fd 100644 --- a/pandora_console/operation/tree2.php +++ b/pandora_console/operation/tree2.php @@ -16,7 +16,7 @@ global $config; -require_once("include/class/tree.class.php"); +require_once("include/class/Tree.class.php"); $tab = get_parameter('tab', 'group'); $search = get_parameter('search', ''); @@ -120,13 +120,43 @@ html_print_table($table); // --------------------- form filter ----------------------------------- - - -$tree = new Tree($tab); -$tree->set_filter(array( - 'status' => $status, - 'search' => $search)); -$json_tree = $tree->get_json(); - -html_debug_print($json_tree); -?> \ No newline at end of file +ui_require_javascript_file("TreeController", "include/javascript/tree/"); +html_print_image('images/spinner.gif', false, array('class' => "loading_tree")); +echo "
    "; +echo "
    "; +?> + \ No newline at end of file