diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 21e6dca02d..391df1b30d 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -3639,6 +3639,74 @@ function html_print_input($data, $wrapper='div', $input_only=false) $output .= html_print_input_multicheck($data); break; + case 'autocomplete_agent': + /* + if (is_metaconsole() === true) { + $connection = metaconsole_get_connection($server_name); + $agent_name = ''; + + if (metaconsole_load_external_db($connection) == NOERR) { + $agent_name = db_get_value_filter( + 'alias', + 'tagente', + ['id_agente' => $idAgent] + ); + } + + // Append server name. + if (!empty($agent_name)) { + $agent_name .= ' ('.$server_name.')'; + } + + // Restore db connection. + metaconsole_restore_db(); + } else { + + } + */ + $agent_name = ''; + if (isset($data['id_agent_hidden']) === true + && empty($data['id_agent_hidden']) === false + ) { + $agent_name = agents_get_alias($data['id_agent_hidden']); + } + + $params = []; + $params['return'] = $data['return']; + $params['show_helptip'] = false; + $params['input_name'] = $data['name']; + $params['value'] = $agent_name; + $params['javascript_is_function_select'] = true; + // $params['selectbox_id'] = 'id_agent_module'; + // $params['add_none_module'] = true; + $params['use_hidden_input_idagent'] = true; + $params['hidden_input_idagent_id'] = 'hidden-'.$data['name_agent_hidden']; + /* + if (is_metaconsole()) { + $params['use_input_id_server'] = true; + $params['input_id_server_id'] = 'hidden-server_id'; + $params['metaconsole_enabled'] = true; + $params['input_id'] = 'agent_autocomplete_events'; + $params['javascript_page'] = 'include/ajax/agent'; + $params['input_name'] = 'agent_text'; + } + */ + + $output .= html_print_input_hidden( + $data['name_agent_hidden'], + $data['id_agent_hidden'], + $data['return'] + ); + // $output .= html_print_input_hidden('server_name', $server_name); + $output .= html_print_input_hidden( + $data['name_server_hidden'], + $data['server_id_hidden'], + $data['return'] + ); + // $output .= html_print_input_hidden('id_server', ''); + $output .= ui_print_agent_autocomplete_input($params); + break; + default: // Ignore. break; diff --git a/pandora_console/include/javascript/pandora.js b/pandora_console/include/javascript/pandora.js index 2679a5de8a..22ae4ffc3c 100644 --- a/pandora_console/include/javascript/pandora.js +++ b/pandora_console/include/javascript/pandora.js @@ -2010,54 +2010,55 @@ function load_modal(settings) { formdata.append("method", settings.onsubmit.method); var flagError = false; - - $("#" + settings.form + " :input").each(function() { - if (this.checkValidity() === false) { - $(this).attr("title", this.validationMessage); - $(this).tooltip({ - tooltipClass: "uitooltip", - position: { - my: "right bottom", - at: "right top", - using: function(position, feedback) { - $(this).css(position); - $("
") - .addClass("arrow") - .addClass(feedback.vertical) - .addClass(feedback.horizontal) - .appendTo(this); + if (Array.isArray(settings.form) === false) { + $("#" + settings.form + " :input").each(function() { + if (this.checkValidity() === false) { + $(this).attr("title", this.validationMessage); + $(this).tooltip({ + tooltipClass: "uitooltip", + position: { + my: "right bottom", + at: "right top", + using: function(position, feedback) { + $(this).css(position); + $("
") + .addClass("arrow") + .addClass(feedback.vertical) + .addClass(feedback.horizontal) + .appendTo(this); + } } - } - }); - $(this).tooltip("open"); + }); + $(this).tooltip("open"); - var element = $(this); - setTimeout( - function(element) { - element.tooltip("destroy"); - element.removeAttr("title"); - }, - 3000, - element - ); + var element = $(this); + setTimeout( + function(element) { + element.tooltip("destroy"); + element.removeAttr("title"); + }, + 3000, + element + ); - flagError = true; - } - - if (this.type == "file") { - if ($(this).prop("files")[0]) { - formdata.append(this.name, $(this).prop("files")[0]); + flagError = true; } - } else { - if ($(this).attr("type") == "checkbox") { - if (this.checked) { - formdata.append(this.name, "on"); + + if (this.type == "file") { + if ($(this).prop("files")[0]) { + formdata.append(this.name, $(this).prop("files")[0]); } } else { - formdata.append(this.name, $(this).val()); + if ($(this).attr("type") == "checkbox") { + if (this.checked) { + formdata.append(this.name, "on"); + } + } else { + formdata.append(this.name, $(this).val()); + } } - } - }); + }); + } if (flagError === false) { $.ajax({ @@ -2153,16 +2154,29 @@ function load_modal(settings) { } formdata.append("page", settings.onsubmit.page); formdata.append("method", settings.onsubmit.method); - - $("#" + settings.form + " :input").each(function() { - if (this.type == "file") { - if ($(this).prop("files")[0]) { - formdata.append(this.name, $(this).prop("files")[0]); + if (Array.isArray(settings.form) === false) { + $("#" + settings.form + " :input").each(function() { + if (this.type == "file") { + if ($(this).prop("files")[0]) { + formdata.append(this.name, $(this).prop("files")[0]); + } + } else { + formdata.append(this.name, $(this).val()); } - } else { - formdata.append(this.name, $(this).val()); - } - }); + }); + } else { + settings.form.forEach(function(element) { + $("#" + element + " :input").each(function() { + if (this.type == "file") { + if ($(this).prop("files")[0]) { + formdata.append(this.name, $(this).prop("files")[0]); + } + } else { + formdata.append(this.name, $(this).val()); + } + }); + }); + } $.ajax({ method: "post", diff --git a/pandora_console/include/javascript/pandora_visual_console.js b/pandora_console/include/javascript/pandora_visual_console.js index 2710953cbd..ddc0918eac 100755 --- a/pandora_console/include/javascript/pandora_visual_console.js +++ b/pandora_console/include/javascript/pandora_visual_console.js @@ -1439,18 +1439,25 @@ function createOrUpdateVisualConsoleItem( load_modal({ target: $("#modalVCItemForm"), - form: "itemForm", + form: ["itemForm-label", "itemForm-general", "itemForm-specific"], url: baseUrl + "ajax.php", ajax_callback: function(response) { - var item = handleFormResponse(response); + var data = JSON.parse(response); //handleFormResponse(response); + console.log(data); - if (item == false) { + if (data == false) { // Error. return; } - // Success. - console.log(item); + $("#modalVCItemForm").dialog("close"); + if (item.itemProps.id) { + visualConsole.updateElement(data); + } else { + data["receivedAt"] = new Date(); + var newItem = visualConsole.addElement(data); + newItem.setMeta({ editMode: true }); + } }, cleanup: cleanupDOM, modal: { @@ -1463,14 +1470,14 @@ function createOrUpdateVisualConsoleItem( name: "item", value: item }, - /*{ - name: "elementsVc", - value: elementsVc - },*/ { name: "vCId", value: visualConsole.props.id } + /*{ + name: "elementsVc", + value: elementsVc + },*/ ], onshow: { page: "include/rest-api/index", diff --git a/pandora_console/include/rest-api/index.php b/pandora_console/include/rest-api/index.php index 20c60b2707..32bae6db56 100644 --- a/pandora_console/include/rest-api/index.php +++ b/pandora_console/include/rest-api/index.php @@ -314,48 +314,7 @@ if ($getVisualConsole === true) { echo json_encode(io_safe_output($result)); return; } else if ($getImagesVisualConsole) { - $result = []; - - // Extract images. - $all_images = list_files( - $config['homedir'].'/images/console/icons/', - 'png', - 1, - 0 - ); - - if (isset($all_images) === true && is_array($all_images) === true) { - $base_url = ui_get_full_url( - '/images/console/icons/', - false, - false, - false - ); - - foreach ($all_images as $image_file) { - $image_file = substr($image_file, 0, (strlen($image_file) - 4)); - - if (strpos($image_file, '_bad') !== false) { - continue; - } - - if (strpos($image_file, '_ok') !== false) { - continue; - } - - if (strpos($image_file, '_warning') !== false) { - continue; - } - - $result[] = [ - 'name' => $image_file, - 'src' => $base_url.$image_file, - ]; - } - } - - echo json_encode(io_safe_output($result)); - return; + // TODO: Remove. } else if ($autocompleteAgentsVisualConsole) { $params = (array) get_parameter('data', []); diff --git a/pandora_console/include/rest-api/models/VisualConsole/Item.php b/pandora_console/include/rest-api/models/VisualConsole/Item.php index 798c97c145..32def05cb9 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Item.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Item.php @@ -1942,28 +1942,6 @@ class Item extends CachedModel ], ]; - // Link enabled. - $inputs[] = [ - 'label' => __('Link enabled'), - 'arguments' => [ - 'name' => 'isLinkEnabled', - 'id' => 'isLinkEnabled', - 'type' => 'switch', - 'value' => $values->isLinkEnabled, - ], - ]; - - // Show on top. - $inputs[] = [ - 'label' => __('Show on top'), - 'arguments' => [ - 'name' => 'isOnTop', - 'id' => 'isOnTop', - 'type' => 'switch', - 'value' => $values->isOnTop, - ], - ]; - // Position. $inputs[] = [ 'block_id' => 'position-item', @@ -1994,6 +1972,28 @@ class Item extends CachedModel ], ]; + // Link enabled. + $inputs[] = [ + 'label' => __('Link enabled'), + 'arguments' => [ + 'name' => 'isLinkEnabled', + 'id' => 'isLinkEnabled', + 'type' => 'switch', + 'value' => $values->isLinkEnabled, + ], + ]; + + // Show on top. + $inputs[] = [ + 'label' => __('Show on top'), + 'arguments' => [ + 'name' => 'isOnTop', + 'id' => 'isOnTop', + 'type' => 'switch', + 'value' => $values->isOnTop, + ], + ]; + // Parent. // TODO:XXX $fields = get_parameter('elementsVc', []); @@ -2050,4 +2050,54 @@ class Item extends CachedModel } + /** + * List images for Vc Icons. + * + * @return array + */ + public function getListImagesVC():array + { + global $config; + + $result = []; + + // Extract images. + $all_images = \list_files( + $config['homedir'].'/images/console/icons/', + 'png', + 1, + 0 + ); + + if (isset($all_images) === true && is_array($all_images) === true) { + $base_url = \ui_get_full_url( + '/images/console/icons/', + false, + false, + false + ); + + foreach ($all_images as $image_file) { + $image_file = substr($image_file, 0, (strlen($image_file) - 4)); + + if (strpos($image_file, '_bad') !== false) { + continue; + } + + if (strpos($image_file, '_ok') !== false) { + continue; + } + + if (strpos($image_file, '_warning') !== false) { + continue; + } + + $result[$image_file] = $image_file; + } + } + + return $result; + } + + } diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/StaticGraph.php b/pandora_console/include/rest-api/models/VisualConsole/Items/StaticGraph.php index 87c23794c6..23c1ebf8bb 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/StaticGraph.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/StaticGraph.php @@ -256,7 +256,56 @@ final class StaticGraph extends Item $inputs = Item::getFormInputs($values); if (is_array($inputs) !== true) { - throw new Exception('[StaticGraph]::getFormInputs parent class return is not an array'); + throw new Exception( + '[StaticGraph]::getFormInputs parent class return is not an array' + ); + } + + if ($values->tabSelected === 'specific') { + // List images VC. + // TODO: Show images. + $inputs[] = [ + 'label' => __('Image'), + 'arguments' => [ + 'type' => 'select', + 'fields' => self::getListImagesVC(), + 'name' => 'imageSrc', + 'selected' => $values->imageSrc, + 'return' => true, + ], + ]; + + // Autocomplete agents. + $inputs[] = [ + 'label' => __('Agent'), + 'arguments' => [ + 'type' => 'autocomplete_agent', + 'name' => 'agentAlias', + 'id_agent_hidden' => $values->agentId, + 'name_agent_hidden' => 'agentId', + 'server_id_hidden' => $values->metaconsoleId, + 'name_server_hidden' => 'metaconsoleId', + 'return' => true, + ], + ]; + + $fields = [ + 'default' => __('Hide last value on boolean modules'), + 'disabled' => __('Disabled'), + 'enabled' => __('Enabled'), + ]; + + // Show Last Value. + $inputs[] = [ + 'label' => __('Show Last Value'), + 'arguments' => [ + 'type' => 'select', + 'fields' => $fields, + 'name' => 'showLastValueTooltip', + 'selected' => $values->showLastValueTooltip, + 'return' => true, + ], + ]; } return $inputs; diff --git a/pandora_console/include/rest-api/models/VisualConsole/View.php b/pandora_console/include/rest-api/models/VisualConsole/View.php index d423895266..95dc0d3be3 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/View.php +++ b/pandora_console/include/rest-api/models/VisualConsole/View.php @@ -160,40 +160,136 @@ class View extends \HTML */ public function processForm() { + global $config; + + hd('++++++++++++++++++++++++++++++++++++', true); hd($_POST, true); + hd('++++++++++++++++++++++++++++++++++++', true); + + $item = json_decode(io_safe_output(\get_parameter('item')), true); // Inserted data in new item. - // $data = json_decode($_REQUEST['item'])->itemProps; $vCId = \get_parameter('vCId', 0); - $data['type'] = 0; - $data['label'] = \get_parameter('label', 'vacio'); + $data['type'] = $item['itemProps']['type']; - $class = VisualConsole::getItemClass((int) $data['type']); - try { - // Save the new item. - $data['id_layout'] = $vCId; - hd($data, true); - $result = $class::save($data); - } catch (\Throwable $th) { - // There is no item in the database. - // hd($th, true); - echo false; - return; - } + // Page Label. + $data['label'] = \get_parameter('label'); + + // Page general. + $data['width'] = \get_parameter('width'); + $data['height'] = \get_parameter('height'); + $data['x'] = \get_parameter('x'); + $data['y'] = \get_parameter('y'); + $data['isLinkEnabled'] = \get_parameter('isLinkEnabled'); + $data['isOnTop'] = \get_parameter('isOnTop'); + $data['parentId'] = \get_parameter('parentId'); + $data['aclGroupId'] = \get_parameter('aclGroupId'); + $data['cacheExpiration_select'] = \get_parameter( + 'cacheExpiration_select' + ); + $data['cacheExpiration_text'] = \get_parameter('cacheExpiration_text'); + $data['cacheExpiration'] = \get_parameter('cacheExpiration'); + $data['cacheExpiration_units'] = \get_parameter( + 'cacheExpiration_units' + ); + + // Page specific. + $data['imageSrc'] = \get_parameter('imageSrc'); + $data['agentId'] = \get_parameter('agentId'); + $data['metaconsoleId'] = \get_parameter('metaconsoleId'); + $data['agentAlias'] = \get_parameter('agentAlias'); + $data['showLastValueTooltip'] = \get_parameter('showLastValueTooltip'); + + if (isset($item['itemProps']['id']) === false) { + // CreateVC. + $class = VisualConsole::getItemClass((int) $data['type']); + try { + // Save the new item. + $data['id_layout'] = $vCId; + $itemId = $class::save($data); + hd('he creado:'.$itemId, true); + } catch (\Throwable $th) { + // Bad params. + http_response_code(400); + return false; + } - /* // Extract data new item inserted. try { - $item = VisualConsole::getItemFromDB($result); + $item = VisualConsole::getItemFromDB($itemId); + $result = $item->toArray(); } catch (Throwable $e) { - // Bad params. - http_response_code(400); - return; + // Bad params. + http_response_code(400); + return false; } - */ + } else { + // UpdateVC. + $itemId = $item['itemProps']['id']; - return json_encode(['error' => obhd($item)]); + try { + $item = VisualConsole::getItemFromDB($itemId); + } catch (Throwable $e) { + // Bad params. + http_response_code(400); + return false; + } + + $itemData = $item->toArray(); + $itemType = $itemData['type']; + $itemAclGroupId = $itemData['aclGroupId']; + + // ACL. + $aclRead = check_acl($config['id_user'], $itemAclGroupId, 'VR'); + $aclWrite = check_acl($config['id_user'], $itemAclGroupId, 'VW'); + $aclManage = check_acl($config['id_user'], $itemAclGroupId, 'VM'); + + if (!$aclRead && !$aclWrite && !$aclManage) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access visual console without group access' + ); + http_response_code(403); + return false; + } + + // Check also the group Id for the group item. + if ($itemType === GROUP_ITEM) { + $itemGroupId = $itemData['groupId']; + // ACL. + $aclRead = check_acl($config['id_user'], $itemGroupId, 'VR'); + $aclWrite = check_acl($config['id_user'], $itemGroupId, 'VW'); + $aclManage = check_acl($config['id_user'], $itemGroupId, 'VM'); + + if (!$aclRead && !$aclWrite && !$aclManage) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access visual console without group access' + ); + http_response_code(403); + return false; + } + } + + if (is_array($data) === true && empty($data) === false) { + try { + // Save the new item. + $data['id_layout'] = $vCId; + $data['id'] = $itemId; + $item->save($data); + hd('he actualizado: '.$itemId, true); + $result = $item->toArray(); + } catch (\Throwable $th) { + // There is no item in the database. + echo false; + return false; + } + } + } + + hd($result, true); + return json_encode($result); }