Skip to content

Commit

Permalink
Fixing #5447 - Duplication of Device Template API
Browse files Browse the repository at this point in the history
Duplication of Device Templates happens in the base Cacti file and not in an API file
  • Loading branch information
TheWitness committed Aug 12, 2023
1 parent 4a6fa95 commit 296074e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Cacti CHANGELOG
-issue#5434: Remove SQL Mode NO_AUTO_CREATE_USER as it's deprecated and removed from recent MySQL releases
-issue#5440: Device class not saved when importing a template during a new installation
-issue#5446: Duplication functions for Graph/Template and Data Source/Template do not return an id
-issue#5447: Duplication of Device Templates happens in the base Cacti file and not in an API file
-feature#5375: Add template for Fortinet firewall and Aruba Instant cluster
-feature#5393: Add template for SNMP printer
-feature#5418: Display device class before package import
Expand Down
51 changes: 2 additions & 49 deletions host_templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,53 +134,6 @@ function form_save() {
}
}

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('<template_title>', $host_template['name'], $host_template_title);

/* create new entry: host_template */
$save['id'] = 0;
$save['hash'] = get_hash_host_template(0);

foreach ($fields_host_template_edit as $field => $array) {
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 (cacti_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 (cacti_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
------------------------ */
Expand Down Expand Up @@ -237,8 +190,8 @@ function form_actions() {
/* "undo" any device that is currently using this template */
db_execute('UPDATE host SET host_template_id = 0 WHERE deleted = "" AND ' . array_to_sql_or($selected_items, 'host_template_id'));
} elseif (get_nfilter_request_var('drp_action') == '2') { // duplicate
for ($i = 0; ($i < cacti_count($selected_items)); $i++) {
duplicate_host_template($selected_items[$i], get_nfilter_request_var('title_format'));
for ($i=0;($i<cacti_count($selected_items));$i++) {
api_duplicate_device_template($selected_items[$i], get_nfilter_request_var('title_format'));
}
} elseif (get_nfilter_request_var('drp_action') == '3') { // sync
for ($i = 0; ($i < cacti_count($selected_items)); $i++) {
Expand Down
57 changes: 57 additions & 0 deletions lib/api_device.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,3 +1712,60 @@ function api_device_ping_device($device_id, $from_remote = false) {
print __('No Ping or SNMP Availability Check in Use') . "<br><br>\n";
}
}

/**
* api_duplicate_device_template - given a device_template_id, and a title, duplicate it.
*
* @param (int) The Device Template id to duplicate
* @param (string) The name of the new Device Template
*
* @return (void)
*/
function api_duplicate_device_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));

if (cacti_sizeof($host_template)) {
/* substitute the title variable */
$host_template['name'] = str_replace('<template_title>', $host_template['name'], $host_template_title);

/* create new entry: host_template */
$save['id'] = 0;
$save['hash'] = get_hash_host_template(0);

foreach ($fields_host_template_edit as $field => $array) {
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 (cacti_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 (cacti_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']));
}
}

return $host_template_id;
} else {
return false;
}
}

0 comments on commit 296074e

Please sign in to comment.