From 6e081576229b9ce02ebcefeb4a9f0a26d6d30432 Mon Sep 17 00:00:00 2001 From: "V. Radev" Date: Wed, 27 Jun 2018 09:39:29 +0300 Subject: [PATCH] feat(searching): persist search term for host and host group (#6386) --- www/class/centreon.class.php | 2 + .../configObject/host/listHost.php | 127 +++++++++--------- .../configObject/hostgroup/listHostGroup.php | 27 ++-- 3 files changed, 85 insertions(+), 71 deletions(-) diff --git a/www/class/centreon.class.php b/www/class/centreon.class.php index 2b1f5f30f93..9c5a97ae487 100644 --- a/www/class/centreon.class.php +++ b/www/class/centreon.class.php @@ -69,6 +69,8 @@ class Centreon public $template; public $hostgroup; public $host_id; + public $host_group_search; + public $host_list_search; /** * @var \CentreonUser diff --git a/www/include/configuration/configObject/host/listHost.php b/www/include/configuration/configObject/host/listHost.php index 76df6c18241..2fe23042cbc 100644 --- a/www/include/configuration/configObject/host/listHost.php +++ b/www/include/configuration/configObject/host/listHost.php @@ -37,9 +37,9 @@ exit(); } -include_once("./class/centreonUtils.class.php"); -require_once("./include/common/autoNumLimit.php"); -require_once(_CENTREON_PATH_ . "/www/class/centreonHost.class.php"); +include_once('./class/centreonUtils.class.php'); +require_once('./include/common/autoNumLimit.php'); +require_once(_CENTREON_PATH_ . '/www/class/centreonHost.class.php'); /* * Init Host Method @@ -55,18 +55,21 @@ * Get Extended informations */ $ehiCache = array(); -$DBRESULT = $pearDB->query("SELECT ehi_icon_image, host_host_id FROM extended_host_information"); -while ($ehi = $DBRESULT->fetchRow()) { - $ehiCache[$ehi["host_host_id"]] = $ehi["ehi_icon_image"]; +$DBRESULT = $pearDB->query('SELECT ehi_icon_image, host_host_id FROM extended_host_information'); + +while ($ehi = $DBRESULT->fetch()) { + $ehiCache[$ehi['host_host_id']] = $ehi['ehi_icon_image']; } + $DBRESULT->closeCursor(); +$mainQueryParameters = []; -if (isset($_POST["searchH"])) { +if (isset($_POST['searchH'])) { $num = 0; - $search = $_POST["searchH"]; + $search = $_POST['searchH']; $centreon->historySearch[$url] = $search; -} elseif (isset($_GET["searchH"])) { - $search = $_GET["searchH"]; +} elseif (isset($_GET['searchH'])) { + $search = $_GET['searchH']; $centreon->historySearch[$url] = $search; } elseif (isset($centreon->historySearch[$url])) { $search = $centreon->historySearch[$url]; @@ -77,40 +80,40 @@ /* * Get Poller -> used for poller section in host display list */ -if (isset($_POST["poller"])) { - $poller = $_POST["poller"]; -} elseif (isset($_GET["poller"])) { - $poller = $_GET["poller"]; +if (isset($_POST['poller'])) { + $poller = $_POST['poller']; +} elseif (isset($_GET['poller'])) { + $poller = $_GET['poller']; } elseif (isset($centreon->poller) && $centreon->poller) { $poller = $centreon->poller; } else { $poller = 0; } -if (isset($_POST["hostgroup"])) { - $hostgroup = $_POST["hostgroup"]; -} elseif (isset($_GET["hostgroup"])) { - $hostgroup = $_GET["hostgroup"]; +if (isset($_POST['hostgroup'])) { + $hostgroup = $_POST['hostgroup']; +} elseif (isset($_GET['hostgroup'])) { + $hostgroup = $_GET['hostgroup']; } elseif (isset($centreon->hostgroup) && $centreon->hostgroup) { $hostgroup = $centreon->hostgroup; } else { $hostgroup = 0; } -if (isset($_POST["template"])) { - $template = $_POST["template"]; -} elseif (isset($_GET["template"])) { - $template = $_GET["template"]; +if (isset($_POST['template'])) { + $template = $_POST['template']; +} elseif (isset($_GET['template'])) { + $template = $_GET['template']; } elseif (isset($centreon->template) && $centreon->template) { $template = $centreon->template; } else { $template = 0; } -if (isset($_POST["status"])) { - $status = $_POST["status"]; -} elseif (isset($_GET["status"])) { - $status = $_GET["status"]; +if (isset($_POST['status'])) { + $status = $_POST['status']; +} elseif (isset($_GET['status'])) { + $status = $_GET['status']; } else { $status = -1; } @@ -129,7 +132,7 @@ $statusFilter .= ""; $statusFilter .= ""; -$sqlFilterCase = ""; +$sqlFilterCase = ''; if ($status == 1) { $sqlFilterCase = " AND host_activate = '1' "; } elseif ($status == 0 && $status != "") { @@ -139,20 +142,21 @@ /* * Search active */ -$SearchTool = ""; +$searchFilterQuery = ''; if (isset($search) && $search) { $search = str_replace('_', "\_", $search); - $SearchTool = "(h.host_name LIKE '%" . $pearDB->escape($search) . "%' - OR host_alias LIKE '%" . $pearDB->escape($search) . "%' - OR host_address LIKE '%" . $pearDB->escape($search) . "%') AND "; + $mainQueryParameters[':search_string'] = "%{$search}%"; + $searchFilterQuery = '(h.host_name LIKE :search_string + OR host_alias LIKE :search_string + OR host_address LIKE :search_string) AND '; } if ($template) { - $templateFROM = ", host_template_relation htr "; - $templateWHERE = " htr.host_host_id = h.host_id AND htr.host_tpl_id = '$template' AND "; + $templateFROM = ', host_template_relation htr '; + $templateWHERE = " htr.host_host_id = h.host_id AND htr.host_tpl_id = '{$template}' AND "; } else { - $templateFROM = ""; - $templateWHERE = ""; + $templateFROM = ''; + $templateWHERE = ''; } /* * Smarty template Init @@ -179,21 +183,22 @@ * Host list */ $nagios_server = array(); -$DBRESULT = $pearDB->query("SELECT ns.name, ns.id FROM nagios_server ns " . - ($aclPollerString != "''" ? $acl->queryBuilder('WHERE', 'ns.id', $aclPollerString) : "") . - " ORDER BY ns.name"); -while ($relation = $DBRESULT->fetchRow()) { - $nagios_server[$relation["id"]] = $relation["name"]; +$DBRESULT = $pearDB->query('SELECT ns.name, ns.id FROM nagios_server ns ' . + ($aclPollerString != "''" ? $acl->queryBuilder('WHERE', 'ns.id', $aclPollerString) : '') . + ' ORDER BY ns.name'); + +while ($relation = $DBRESULT->fetch()) { + $nagios_server[$relation['id']] = $relation['name']; } $DBRESULT->closeCursor(); unset($relation); $tab_relation = array(); $tab_relation_id = array(); -$DBRESULT = $pearDB->query("SELECT nhr.host_host_id, nhr.nagios_server_id FROM ns_host_relation nhr"); +$DBRESULT = $pearDB->query('SELECT nhr.host_host_id, nhr.nagios_server_id FROM ns_host_relation nhr'); while ($relation = $DBRESULT->fetchRow()) { - $tab_relation[$relation["host_host_id"]] = $nagios_server[$relation["nagios_server_id"]]; - $tab_relation_id[$relation["host_host_id"]] = $relation["nagios_server_id"]; + $tab_relation[$relation['host_host_id']] = $nagios_server[$relation['nagios_server_id']]; + $tab_relation_id[$relation['host_host_id']] = $relation['nagios_server_id']; } $DBRESULT->closeCursor(); @@ -201,13 +206,13 @@ * Init Formulary */ -$form = new HTML_QuickFormCustom('select_form', 'POST', "?p=" . $p); +$form = new HTML_QuickFormCustom('select_form', 'POST', "?p={$p}"); /* * Different style between each lines */ -$style = "one"; +$style = 'one'; /* * Fill a tab with a mutlidimensionnal Array we put in $tpl @@ -216,12 +221,12 @@ /* * Select hosts */ -$aclFrom = ""; -$aclCond = ""; +$aclFrom = ''; +$aclCond = ''; if (!$centreon->user->admin) { - $aclFrom = ", $aclDbName.centreon_acl acl"; - $aclCond = " AND h.host_id = acl.host_id AND acl.service_id IS NULL AND acl.group_id IN (" - . $acl->getAccessGroupsString() . ") "; + $aclFrom = ", {$aclDbName}.centreon_acl acl"; + $aclCond = ' AND h.host_id = acl.host_id AND acl.service_id IS NULL AND acl.group_id IN (' + . $acl->getAccessGroupsString() . ') '; } if ($hostgroup) { @@ -229,41 +234,41 @@ $DBRESULT = $pearDB->query("SELECT SQL_CALC_FOUND_ROWS DISTINCT h.host_id, h.host_name, host_alias, host_address, host_activate, host_template_model_htm_id FROM host h, ns_host_relation, hostgroup_relation hr $templateFROM $aclFrom - WHERE $SearchTool $templateWHERE host_register = '1' + WHERE $searchFilterQuery $templateWHERE host_register = '1' AND h.host_id = ns_host_relation.host_host_id AND ns_host_relation.nagios_server_id = '$poller' AND h.host_id = hr.host_host_id AND hr.hostgroup_hg_id = '$hostgroup' $sqlFilterCase $aclCond - ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit); + ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit, $mainQueryParameters); } else { $DBRESULT = $pearDB->query("SELECT SQL_CALC_FOUND_ROWS DISTINCT h.host_id, h.host_name, host_alias, host_address, host_activate, host_template_model_htm_id FROM host h, hostgroup_relation hr $templateFROM $aclFrom - WHERE $SearchTool $templateWHERE host_register = '1' + WHERE $searchFilterQuery $templateWHERE host_register = '1' AND h.host_id = hr.host_host_id AND hr.hostgroup_hg_id = '$hostgroup' $sqlFilterCase $aclCond - ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit); + ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit, $mainQueryParameters); } } else { if ($poller) { $DBRESULT = $pearDB->query("SELECT SQL_CALC_FOUND_ROWS DISTINCT h.host_id, h.host_name, host_alias, host_address, host_activate, host_template_model_htm_id FROM host h, ns_host_relation $templateFROM $aclFrom - WHERE $SearchTool $templateWHERE host_register = '1' + WHERE $searchFilterQuery $templateWHERE host_register = '1' AND h.host_id = ns_host_relation.host_host_id AND ns_host_relation.nagios_server_id = '$poller' $sqlFilterCase $aclCond - ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit); + ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit, $mainQueryParameters); } else { $DBRESULT = $pearDB->query("SELECT SQL_CALC_FOUND_ROWS DISTINCT h.host_id, h.host_name, host_alias, host_address, host_activate, host_template_model_htm_id FROM host h $templateFROM $aclFrom - WHERE $SearchTool $templateWHERE host_register = '1' $sqlFilterCase $aclCond - ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit); + WHERE $searchFilterQuery $templateWHERE host_register = '1' $sqlFilterCase $aclCond + ORDER BY h.host_name LIMIT " . $num * $limit . ", " . $limit, $mainQueryParameters); } } -$rows = $pearDB->numberRows(); -include("./include/common/checkPagination.php"); +$rows = $DBRESULT->rowCount(); +include('./include/common/checkPagination.php'); $search = tidySearchKey($search, $advanced_search); @@ -434,7 +439,7 @@ function setO(_i) { $DBRESULT = $pearDB->query("SELECT host_id, host_name FROM host WHERE host_register = '0' ORDER BY host_name"); $options = ""; } diff --git a/www/include/configuration/configObject/hostgroup/listHostGroup.php b/www/include/configuration/configObject/hostgroup/listHostGroup.php index 7c452975952..ab0824fa91c 100644 --- a/www/include/configuration/configObject/hostgroup/listHostGroup.php +++ b/www/include/configuration/configObject/hostgroup/listHostGroup.php @@ -49,12 +49,19 @@ /* * Search */ -$SearchTool = null; -$search = ""; -if (isset($_POST['searchHg']) && $_POST['searchHg']) { - $search = $_POST['searchHg']; - $SearchTool = " (hg_name LIKE '%" . $pearDB->escape($search) - . "%' OR hg_alias LIKE '%" . $pearDB->escape($search) . "%') AND "; +$searchFilterQuery = null; +$mainQueryParameters = []; +$search = ''; + +if (isset($_POST['searchHg'])) { + $centreon->host_group_search = $search; +} elseif (isset($centreon->host_group_search) && $centreon->host_group_search != '') { + $search = $centreon->host_group_search; +} + +if ($search != '') { + $mainQueryParameters[':search_string'] = "%{$search}%"; + $searchFilterQuery = " (hg_name LIKE :search_string OR hg_alias LIKE :search_string) AND "; } /* @@ -84,7 +91,7 @@ */ $rq = "SELECT SQL_CALC_FOUND_ROWS hg_id, hg_name, hg_alias, hg_activate, hg_icon_image FROM hostgroup - WHERE $SearchTool hg_id NOT IN (SELECT hg_child_id FROM hostgroup_hg_relation) " . + WHERE {$searchFilterQuery} hg_id NOT IN (SELECT hg_child_id FROM hostgroup_hg_relation) " . $acl->queryBuilder('AND', 'hg_id', $hgString) . " ORDER BY hg_name LIMIT " . $num * $limit . ", $limit"; $DBRESULT = $pearDB->query($rq); @@ -92,7 +99,7 @@ /* * Pagination */ -$rows = $pearDB->numberRows(); +$rows = $DBRESULT->rowCount(); include("./include/common/checkPagination.php"); $search = tidySearchKey($search, $advanced_search); @@ -107,7 +114,7 @@ * Fill a tab with a mutlidimensionnal Array we put in $tpl */ $elemArr = array(); -for ($i = 0; $hg = $DBRESULT->fetchRow(); $i++) { +for ($i = 0; $hg = $DBRESULT->fetch(); $i++) { $selectedElements = $form->addElement('checkbox', "select[" . $hg['hg_id'] . "]"); $moptions = ""; if ($hg["hg_activate"]) { @@ -148,7 +155,7 @@ $DBRESULT2 = $pearDB->query($rq); $nbrhostActArr = array(); $nbrhostDeactArr = array(); - while ($row = $DBRESULT2->fetchRow()) { + while ($row = $DBRESULT2->fetch()) { if ($row['host_activate']) { $nbrhostActArr[$row['host_id']] = true; } else {