diff -rupN original/include/views/configuration.template.edit.php zabbix/include/views/configuration.template.edit.php --- original/include/views/configuration.template.edit.php 2014-08-18 15:21:02.020186523 +0200 +++ zabbix/include/views/configuration.template.edit.php 2014-08-18 15:13:33.158184636 +0200 @@ -173,6 +173,15 @@ $templateList->addRow(_('Hosts / templat $cmbGroups ))); +$templatesDiv = new CDiv(array( + new CCheckBox('mass_clear_hosts', $this->data['mass_clear_hosts']), + SPACE, + _('Clear when unlinking') + ), 'floatleft'); + +$templatesDiv->setAttribute('id', 'templateDiv'); +$templateList->addRow($templatesDiv); + // FULL CLONE { if ($_REQUEST['form'] == 'full_clone') { // template applications diff -rupN original/templates.php new/templates.php --- original/templates.php 2014-08-18 15:20:48.076193978 +0200 +++ zabbix/templates.php 2014-08-18 15:13:33.157184232 +0200 @@ -56,6 +56,7 @@ $fields = array( 'visiblename' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})'), 'groupid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, null), 'twb_groupid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, null), + 'mass_clear_hosts' => array(T_ZBX_STR, O_OPT, null, null, null), 'newgroup' => array(T_ZBX_STR, O_OPT, null, null, null), 'macros_rem' => array(T_ZBX_STR, O_OPT, P_SYS|P_ACT, null, null), 'macros' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), @@ -215,6 +216,13 @@ elseif (isset($_REQUEST['save'])) { 'templated_hosts' => true, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL) )); + + $hostIds = zbx_objectValues($hosts, 'hostid'); + $templateHosts = API::Host()->get(array( + 'templateids' => $templateId + )); + $templateHostsIds = zbx_objectValues($templateHosts, 'hostid'); + $hostsToDelete = array_diff($templateHostsIds, $hostIds); $template = array( 'host' => $templateName, @@ -230,6 +238,18 @@ elseif (isset($_REQUEST['save'])) { $created = false; $template['templateid'] = $templateId; $template['templates_clear'] = $templatesClear; + + if (isset($_REQUEST['mass_clear_hosts'])) { + if ($hostsToDelete) { + $hosts = array( + 'hosts' => zbx_toObject($hostsToDelete, 'hostid' ), + 'templates_clear' => array('templateid' => $templateId) + ); + if (!API::Host()->massupdate($hosts)) { + throw new Exception(); + } + } + } if (!API::Template()->update($template)) { throw new Exception(); @@ -425,7 +445,9 @@ if (isset($_REQUEST['form'])) { $templateWidget->addItem(get_header_host_table('', $templateId)); } - $data = array(); + $data = array( + 'mass_clear_hosts' => get_request('mass_clear_hosts','yes'), + ); if ($templateId) { $dbTemplates = API::Template()->get(array(