__('Delete'), 2 => __('Duplicate') ); /* set default action */ set_default_action(); api_plugin_hook('device_template_top'); switch (get_request_var('action')) { case 'save': form_save(); break; case 'actions': form_actions(); break; case 'item_add_gt': template_item_add_gt(); header('Location: host_templates.php?header=false&action=edit&id=' . get_filter_request_var('host_template_id')); break; case 'item_remove_gt_confirm': template_item_remove_gt_confirm(); break; case 'item_remove_gt': template_item_remove_gt(); header('Location: host_templates.php?action=edit&id=' . get_filter_request_var('host_template_id')); break; case 'item_add_dq': template_item_add_dq(); header('Location: host_templates.php?header=false&action=edit&id=' . get_filter_request_var('host_template_id')); break; case 'item_remove_dq_confirm': template_item_remove_dq_confirm(); break; case 'item_remove_dq': template_item_remove_dq(); header('Location: host_templates.php?action=edit&id=' . get_filter_request_var('host_template_id')); break; case 'edit': top_header(); template_edit(); bottom_footer(); break; default: top_header(); template(); bottom_footer(); break; } /* -------------------------- The Save Function -------------------------- */ function form_save() { /* ================= input validation ================= */ get_filter_request_var('id'); get_filter_request_var('host_template_id'); get_filter_request_var('snmp_query_id'); get_filter_request_var('graph_template_id'); /* ==================================================== */ if (isset_request_var('save_component_template')) { $save['id'] = get_nfilter_request_var('id'); $save['hash'] = get_hash_host_template(get_nfilter_request_var('id')); $save['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3); if (!is_error_message()) { $host_template_id = sql_save($save, 'host_template'); if ($host_template_id) { raise_message(1); }else{ raise_message(2); } } header('Location: host_templates.php?header=false&action=edit&id=' . (empty($host_template_id) ? get_nfilter_request_var('id') : $host_template_id)); } } function duplicate_host_template($_host_template_id, $host_template_title) { global $fields_host_template_edit; $host_template = db_fetch_row_prepared('SELECT * FROM host_template WHERE id = ?', array($_host_template_id)); $host_template_graphs = db_fetch_assoc_prepared('SELECT * FROM host_template_graph WHERE host_template_id = ?', array($_host_template_id)); $host_template_data_queries = db_fetch_assoc_prepared('SELECT * FROM host_template_snmp_query WHERE host_template_id = ?', array($_host_template_id)); /* substitute the title variable */ $host_template['name'] = str_replace('', $host_template['name'], $host_template_title); /* create new entry: host_template */ $save['id'] = 0; $save['hash'] = get_hash_host_template(0); reset($fields_host_template_edit); while (list($field, $array) = each($fields_host_template_edit)) { if (!preg_match('/^hidden/', $array['method'])) { $save[$field] = $host_template[$field]; } } $host_template_id = sql_save($save, 'host_template'); /* create new entry(s): host_template_graph */ if (sizeof($host_template_graphs)) { foreach ($host_template_graphs as $host_template_graph) { db_execute_prepared('INSERT INTO host_template_graph (host_template_id,graph_template_id) VALUES (?, ?)', array($host_template_id, $host_template_graph['graph_template_id'])); } } /* create new entry(s): host_template_snmp_query */ if (sizeof($host_template_data_queries)) { foreach ($host_template_data_queries as $host_template_data_query) { db_execute_prepared('INSERT INTO host_template_snmp_query (host_template_id,snmp_query_id) VALUES (?, ?)', array($host_template_id, $host_template_data_query['snmp_query_id'])); } } } /* ------------------------ The "actions" function ------------------------ */ function template_item_add_dq() { /* ================= input validation ================= */ get_filter_request_var('host_template_id'); get_filter_request_var('snmp_query_id'); /* ==================================================== */ db_execute_prepared('REPLACE INTO host_template_snmp_query (host_template_id, snmp_query_id) VALUES (?, ?)', array(get_request_var('host_template_id'), get_request_var('snmp_query_id'))); } function template_item_add_gt() { /* ================= input validation ================= */ get_filter_request_var('host_template_id'); get_filter_request_var('graph_template_id'); /* ==================================================== */ db_execute_prepared('REPLACE INTO host_template_graph (host_template_id, graph_template_id) VALUES (?, ?)', array(get_request_var('host_template_id'), get_request_var('graph_template_id'))); } function form_actions() { global $host_actions; /* ================= input validation ================= */ get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_]+)$/'))); /* ==================================================== */ /* if we are to save this form, instead of display it */ if (isset_request_var('selected_items')) { $selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items')); if ($selected_items != false) { if (get_nfilter_request_var('drp_action') == '1') { /* delete */ db_execute('DELETE FROM host_template WHERE ' . array_to_sql_or($selected_items, 'id')); db_execute('DELETE FROM host_template_snmp_query WHERE ' . array_to_sql_or($selected_items, 'host_template_id')); db_execute('DELETE FROM host_template_graph WHERE ' . array_to_sql_or($selected_items, 'host_template_id')); /* "undo" any device that is currently using this template */ db_execute('UPDATE host SET host_template_id=0 WHERE ' . array_to_sql_or($selected_items, 'host_template_id')); }elseif (get_nfilter_request_var('drp_action') == '2') { /* duplicate */ for ($i=0;($i'; $host_array[$i] = $matches[1]; $i++; } } top_header(); form_start('host_templates.php'); html_start_box($host_actions{get_nfilter_request_var('drp_action')}, '60%', '', '3', 'center', ''); if (isset($host_array) && sizeof($host_array)) { if (get_request_var('drp_action') == '1') { /* delete */ print "

" . __('Click \'Continue\' to delete the following Device Template(s).') . "

    $host_list
\n"; $save_html = " "; }elseif (get_request_var('drp_action') == '2') { /* duplicate */ print "

" . __('Click \'Continue\' to duplicate the following Device Template(s). Optionally change the title for the new Device Template(s).') ."

    $host_list

" . __('Title Format:'). "
\n"; form_text_box('title_format', ' (1)', '', '255', '30', 'text'); print "

\n"; $save_html = " "; } }else{ print "" . __('You must select at least one host template.') . "\n"; $save_html = ""; } print " $save_html \n"; html_end_box(); form_end(); bottom_footer(); } /* --------------------- Template Functions --------------------- */ function template_item_remove_gt_confirm() { /* ================= input validation ================= */ get_filter_request_var('id'); get_filter_request_var('host_template_id'); /* ==================================================== */ form_start('host_templates.php?action=edit&id' . get_request_var('host_template_id')); html_start_box('', '100%', '', '3', 'center', ''); $template = db_fetch_row_prepared('SELECT * FROM graph_templates WHERE id = ?', array(get_request_var('id'))); ?>

'
' onClick='$("#cdialog").dialog("close")' name='cancel'> ' name='continue' title=''>

'
' onClick='$("#cdialog").dialog("close")' name='cancel'> ' name='continue' title=''> array('no_form_tag' => 'true'), 'fields' => inject_form_variables($fields_host_template_edit, (isset($host_template) ? $host_template : array())) ) ); /* we have to hide this button to make a form change in the main form trigger the correct * submit action */ echo "

"; html_end_box(); if (!isempty_request_var('id')) { html_start_box(__('Associated Graph Templates'), '100%', '', '3', 'center', ''); $selected_graph_templates = db_fetch_assoc_prepared('SELECT graph_templates.id, graph_templates.name FROM (graph_templates,host_template_graph) WHERE graph_templates.id = host_template_graph.graph_template_id AND host_template_graph.host_template_id = ? ORDER BY graph_templates.name', array(get_request_var('id'))); $i = 0; if (sizeof($selected_graph_templates)) { foreach ($selected_graph_templates as $item) { form_alternate_row("gt$i", true); ?> ) ' href=''> ' . __('No associated graph templates.') . ''; } ?>
' id='add_gt' title=''>
) ' href=''> ' . __('No associated data queries.') . ''; } ?>
' id='add_dq' title=''>
array( 'filter' => FILTER_VALIDATE_INT, 'pageset' => true, 'default' => '-1' ), 'page' => array( 'filter' => FILTER_VALIDATE_INT, 'default' => '1' ), 'filter' => array( 'filter' => FILTER_CALLBACK, 'pageset' => true, 'default' => '', 'options' => array('options' => 'sanitize_search_string') ), 'sort_column' => array( 'filter' => FILTER_CALLBACK, 'default' => 'name', 'options' => array('options' => 'sanitize_search_string') ), 'sort_direction' => array( 'filter' => FILTER_CALLBACK, 'default' => 'ASC', 'options' => array('options' => 'sanitize_search_string') ), 'has_hosts' => array( 'filter' => FILTER_VALIDATE_REGEXP, 'options' => array('options' => array('regexp' => '(true|false)')), 'pageset' => true, 'default' => read_config_option('default_has') == 'on' ? 'true':'false' ) ); validate_store_request_vars($filters, 'sess_ht'); /* ================= input validation ================= */ if (get_request_var('rows') == '-1') { $rows = read_config_option('num_rows_table'); }else{ $rows = get_request_var('rows'); } html_start_box(__('Device Templates'), '100%', '', '3', 'center', 'host_templates.php?action=edit'); ?>
'> > ' title=''> ' title=''>
'>
0'; }else{ $sql_having = ''; } $total_rows = db_fetch_cell("SELECT COUNT(rows) FROM ( SELECT COUNT(host_template.id) AS rows, COUNT(DISTINCT host.id) AS hosts FROM host_template LEFT JOIN host ON host.host_template_id=host_template.id $sql_where GROUP BY host_template.id $sql_having ) AS rs"); $template_list = db_fetch_assoc("SELECT host_template.id,host_template.name, COUNT(DISTINCT host.id) AS hosts FROM host_template LEFT JOIN host ON host.host_template_id=host_template.id $sql_where GROUP BY host_template.id $sql_having ORDER BY " . get_request_var('sort_column') . ' ' . get_request_var('sort_direction') . ' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows); $nav = html_nav_bar('host_templates.php?filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 5, __('Device Templates'), 'page', 'main'); form_start('host_templates.php', 'chk'); print $nav; html_start_box('', '100%', '', '3', 'center', ''); $display_text = array( 'name' => array('display' => __('Device Template Name'), 'align' => 'left', 'sort' => 'ASC', 'tip' => __('The name of this Device Template.')), 'host_template.id' => array('display' => __('ID'), 'align' => 'right', 'sort' => 'ASC', 'tip' => __('The internal database ID for this Device Template. Useful when performing automation or debugging.')), "nosort" => array('display' => __('Deletable'), 'align' => 'right', 'sort' => '', 'tip' => __('Device Templates in use can not be Deleted. In use is defined as being referenced by a Device.')), 'hosts' => array('display' => __('Devices Using'), 'align' => 'right', 'sort' => 'DESC', 'tip' => __('The number of Devices using this Device Template.')) ); html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false); $i = 0; if (sizeof($template_list)) { foreach ($template_list as $template) { if ($template['hosts'] > 0) { $disabled = true; }else{ $disabled = false; } form_alternate_row('line' . $template['id'], true); form_selectable_cell(filter_value($template['name'], get_request_var('filter'), 'host_templates.php?action=edit&id=' . $template['id']), $template['id']); form_selectable_cell($template['id'], $template['id'], '', 'text-align:right', $disabled); form_selectable_cell($disabled ? __('No'):__('Yes'), $template['id'], '', 'text-align:right', $disabled); form_selectable_cell(number_format_i18n($template['hosts']), $template['id'], '', 'text-align:right', $disabled); form_checkbox_cell($template['name'], $template['id'], $disabled); form_end_row(); } }else{ print "" . __('No Device Templates Found') . "\n"; } html_end_box(false); if (sizeof($template_list)) { print $nav; } /* draw the dropdown containing a list of available actions for this form */ draw_actions_dropdown($host_actions); form_end(); }