From 500fb0a12686a60620d24537292f2df7ef453fb0 Mon Sep 17 00:00:00 2001 From: Dimitar Kalinov Date: Thu, 21 Feb 2019 16:48:43 +0200 Subject: [PATCH] enh(host templates): change host template dropdown to be select2 instead of plain select - Change host template dropdown from select to centreonSelect2 - Remove "Template" title - Add additional parameter to centreonHost => getList() to exclude current host template when editing --- www/class/centreonHost.class.php | 9 ++++++++- .../configObject/host/formHost.ihtml | 15 +++++++++++++++ .../configuration/configObject/host/formHost.php | 4 +++- .../host_template_model/formHostTemplateModel.php | 12 ++++-------- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/www/class/centreonHost.class.php b/www/class/centreonHost.class.php index 8725cbb6e4c..8ede35fdea1 100755 --- a/www/class/centreonHost.class.php +++ b/www/class/centreonHost.class.php @@ -78,10 +78,11 @@ public function __construct($db) /** * @param bool $enable * @param bool $template + * @param null|int $exclude - host id to exclude in returned result * @return array * @throws Exception */ - public function getList($enable = false, $template = false) + public function getList($enable = false, $template = false, $exclude = null) { $hostType = 1; if ($template) { @@ -93,9 +94,15 @@ public function getList($enable = false, $template = false) if ($enable) { $queryList .= 'AND host_activate = "1" '; } + if ($exclude !== null) { + $queryList .= 'AND host_id <> :exclude_id '; + } $queryList .= 'ORDER BY host_name'; $stmt = $this->db->prepare($queryList); $stmt->bindParam(':register', $hostType, PDO::PARAM_STR); + if ($exclude !== null) { + $stmt->bindParam(':exclude_id', $exclude, PDO::PARAM_INT); + } $dbResult = $stmt->execute(); if (!$dbResult) { throw new \Exception("An error occured"); diff --git a/www/include/configuration/configObject/host/formHost.ihtml b/www/include/configuration/configObject/host/formHost.ihtml index 3677812e40d..7d4f73a1284 100644 --- a/www/include/configuration/configObject/host/formHost.ihtml +++ b/www/include/configuration/configObject/host/formHost.ihtml @@ -524,6 +524,21 @@ jQuery(function() { }); function setListener(elem){ + elem.centreonSelect2({ + multiple: false, + select2: { + allowClear: false, + placeholder: "{/literal}{$select_template}{literal}" + } + }); + var id = elem.attr('id').replace('tpSelect_', ''); + if (!isNaN(id) && typeof(jQuery("#clone-values-template").data('clone-values-template')[id]) != 'undefined') { + var selected = jQuery("#clone-values-template").data('clone-values-template')[id]['tpSelect_#index#']; + if (!isNaN(selected)) { + elem.val(selected).trigger('change'); + } + } + elem.on('change',function(event,data){ if(typeof data != "undefined" && typeof data.origin !=undefined && data.origin == "select2defaultinit"){ diff --git a/www/include/configuration/configObject/host/formHost.php b/www/include/configuration/configObject/host/formHost.php index 08da3d12a9b..568cf161d5a 100644 --- a/www/include/configuration/configObject/host/formHost.php +++ b/www/include/configuration/configObject/host/formHost.php @@ -487,10 +487,11 @@ function allInSameInstance($hosts, $instanceId) $cloneSetTemplate[] = $form->addElement( 'select', 'tpSelect[#index#]', - _("Template"), + '', (array(null => null) + $hostObj->getList(false, true)), array( "id" => "tpSelect_#index#", + "class" => "select2", "type" => "select-one" ) ); @@ -1250,6 +1251,7 @@ function myReplace() $tpl->assign('custom_macro_label', _('Custom macros')); $tpl->assign('template_inheritance', _('Template inheritance')); $tpl->assign('command_inheritance', _('Command inheritance')); + $tpl->assign('select_template', _('Select a template')); $tpl->assign('cloneSetMacro', $cloneSetMacro); $tpl->assign('cloneSetTemplate', $cloneSetTemplate); $tpl->assign('centreon_path', $centreon->optGen['oreon_path']); diff --git a/www/include/configuration/configObject/host_template_model/formHostTemplateModel.php b/www/include/configuration/configObject/host_template_model/formHostTemplateModel.php index ff951a6e6c7..ba31f7abae7 100755 --- a/www/include/configuration/configObject/host_template_model/formHostTemplateModel.php +++ b/www/include/configuration/configObject/host_template_model/formHostTemplateModel.php @@ -279,19 +279,14 @@ $cloneSetTemplate[] = $form->addElement( 'select', 'tpSelect[#index#]', - _("Template"), - (array(null => null) + $hostObj->getList(false, true)), + '', + (array(null => null) + $hostObj->getList(false, true, $host_id)), array( "id" => "tpSelect_#index#", + "class" => "select2", "type" => "select-one" ) ); -$cloneSetMacro[] = $form->addElement( - 'hidden', - 'macroFrom[#index#]', - 'direct', - array('id' => 'macroFrom_#index#') -); /* * Check information @@ -943,6 +938,7 @@ function myReplace() $tpl->assign("History_Options", _("History Options")); $tpl->assign("Event_Handler", _("Event Handler")); $tpl->assign("add_mtp_label", _("Add a template")); + $tpl->assign('select_template', _('Select a template')); $tpl->assign("seconds", _("seconds")); $tpl->assign("tpl", 1); $tpl->display("formHost.ihtml");