#12687 Added import/export prd 2

This commit is contained in:
Daniel Maya 2024-01-24 12:15:16 +01:00
parent 95f8519d5d
commit 401b1973be
4 changed files with 381 additions and 62 deletions

View File

@ -114,6 +114,7 @@ html_print_table($table);
$("#button-export_button").addClass("invisible_important"); $("#button-export_button").addClass("invisible_important");
$("#export_data_table-1-0").html(''); $("#export_data_table-1-0").html('');
} else { } else {
$("#export_data_table-1-0").html('');
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "ajax.php", url: "ajax.php",
@ -137,10 +138,18 @@ html_print_table($table);
$("#button-export_button").click(function(e) { $("#button-export_button").click(function(e) {
const value = $("#select_value").val(); const value = $("#select_value").val();
if (value !== '0') { if (value !== '0') {
//Show dialog.
confirmDialog({
title: "<?php echo __('Exporting resource'); ?>",
message: "<?php echo __('Exporting resource and downloading, please wait'); ?>",
hideCancelButton: true
},
"downloadDialog"
);
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "ajax.php", url: "ajax.php",
dataType: "html",
data: { data: {
page: 'include/ajax/resources.ajax', page: 'include/ajax/resources.ajax',
exportPrd: 1, exportPrd: 1,
@ -149,7 +158,24 @@ html_print_table($table);
name: $("#select_value").text(), name: $("#select_value").text(),
}, },
success: function(data) { success: function(data) {
let a = document.createElement('a');
const url = '<?php echo $config['homeurl'].'/attachment/'; ?>' + data;
a.href = url;
a.download = data;
a.click();
setTimeout(() => {
$.ajax({
type: "DELETE",
url: "ajax.php",
data: {
page: 'include/ajax/resources.ajax',
deleteFile: 1,
filename: data,
},
});
$("#confirm_downloadDialog").dialog("close");
}, 3000);
}, },
error: function(data) { error: function(data) {
console.error("Fatal error in AJAX call to interpreter order", data) console.error("Fatal error in AJAX call to interpreter order", data)

View File

@ -20,6 +20,7 @@ if ((bool) is_ajax() === true) {
$getResource = (bool) get_parameter('getResource', false); $getResource = (bool) get_parameter('getResource', false);
$exportPrd = (bool) get_parameter('exportPrd', false); $exportPrd = (bool) get_parameter('exportPrd', false);
$deleteFile = (bool) get_parameter('deleteFile', false);
$prd = new Prd(); $prd = new Prd();
@ -27,15 +28,18 @@ if ((bool) is_ajax() === true) {
$type = (string) get_parameter('type', ''); $type = (string) get_parameter('type', '');
$result = false; $result = false;
$check = $prd->getOnePrdData($type); $data = $prd->getOnePrdData($type);
if (empty($check) === false) { if (empty($data) === false) {
switch ($type) { $sql = sprintf(
case 'visual_console': 'SELECT %s FROM %s',
reset($data['items']['value']).', '.reset($data['items']['show']),
$data['items']['table']
);
$result = html_print_label_input_block( $result = html_print_label_input_block(
__('Visual console'), $data['label'],
io_safe_output( io_safe_output(
html_print_select_from_sql( html_print_select_from_sql(
'SELECT id, name FROM tlayout', $sql,
'select_value', 'select_value',
'', '',
'', '',
@ -52,12 +56,6 @@ if ((bool) is_ajax() === true) {
), ),
) )
); );
break;
default:
// TODO.
break;
}
} }
echo $result; echo $result;
@ -69,6 +67,38 @@ if ((bool) is_ajax() === true) {
$value = (int) get_parameter('value', 0); $value = (int) get_parameter('value', 0);
$name = (string) get_parameter('name', ''); $name = (string) get_parameter('name', '');
$prd->exportPrd($type, $value, $name); $data = $prd->exportPrd($type, $value, $name);
$return = '';
if (empty($data) === false) {
$filename = $type.'-'.date('Ymd').'-'.date('His').'.prd';
$file = $config['attachment_store'].'/'.$filename;
$file_pointer = fopen($file, 'a');
if ($file_pointer !== false) {
$write = fwrite($file_pointer, $data);
if ($write === false) {
$return = -2;
} else {
$return = $filename;
}
fclose($file_pointer);
} else {
$return = -1;
}
}
echo $return;
return;
}
if ($deleteFile === true) {
$filename = (string) get_parameter('filename', '');
unlink($config['attachment_store'].'/'.$filename);
} }
} }

View File

@ -76,17 +76,219 @@ class Prd
'label' => __('Visual console'), 'label' => __('Visual console'),
'items' => [ 'items' => [
'table' => 'tlayout', 'table' => 'tlayout',
'value' => 'id', 'value' => ['id'],
'show' => 'name', 'show' => ['name'],
],
'data' => [ 'data' => [
[ [
'table' => 'tlayout', 'table' => 'tlayout_data',
'ref' => 'id', 'ref' => ['id_layout'],
'value' => ['id'],
],
],
],
],
'custom_report' => [
'label' => __('Custom report'),
'items' => [
'table' => 'treport',
'value' => ['id_report'],
'show' => ['name'],
'data' => [
[
'table' => 'treport_content',
'ref' => ['id_report'],
'value' => ['id_rc'],
'data' => [
[
'table' => 'treport_content_item',
'ref' => ['id_report_content'],
'value' => ['id'],
], ],
[ [
'table' => 'tlayout_data', 'table' => 'treport_content_sla_combined',
'ref' => 'id_layout', 'ref' => ['id_report_content'],
'value' => ['id'],
],
],
],
],
],
],
'policy' => [
'label' => __('Policy'),
'items' => [
'table' => 'tpolicies',
'value' => ['id'],
'show' => ['name'],
'data' => [
[
'table' => 'tpolicy_agents',
'ref' => ['id_policy'],
'value' => ['id'],
],
[
'table' => 'tpolicy_alerts',
'ref' => ['id_policy'],
'value' => ['id'],
'data' => [
[
'table' => 'tpolicy_alerts_actions',
'ref' => ['id_policy_alert'],
'value' => ['id'],
],
],
],
[
'table' => 'tpolicy_collections',
'ref' => ['id_policy'],
'value' => ['id'],
],
[
'table' => 'tpolicy_group_agents',
'ref' => ['id_policy'],
'value' => ['id'],
],
[
'table' => 'tpolicy_groups',
'ref' => ['id_policy'],
'value' => ['id'],
],
[
'table' => 'tpolicy_modules',
'ref' => ['id_policy'],
'value' => ['id'],
'data' => [
[
'table' => 'ttag_policy_module',
'ref' => ['id_policy_module'],
'value' => [
'id_tag',
'id_policy_module',
],
],
[
'table' => 'tpolicy_modules_synth',
'ref' => ['id_agent_module_target'],
'value' => ['id'],
],
],
],
[
'table' => 'tpolicy_modules_inventory',
'ref' => ['id_policy'],
'value' => ['id'],
],
[
'table' => 'tpolicy_plugins',
'ref' => ['id_policy'],
'value' => ['id'],
],
],
],
],
'service' => [
'label' => __('Service'),
'items' => [
'table' => 'tservice',
'value' => ['id'],
'show' => ['name'],
'data' => [
[
'table' => 'tservice_element',
'ref' => ['id_service'],
'value' => ['id'],
],
],
],
],
'network_map' => [
'label' => __('Network map'),
'items' => [
'table' => 'tmap',
'value' => ['id'],
'show' => ['name'],
'data' => [
[
'table' => 'titem',
'ref' => ['id_map'],
'value' => ['id'],
],
[
'table' => 'trel_item',
'ref' => ['id_map'],
'value' => ['id'],
],
],
],
],
'gis_map' => [
'label' => __('GIS map'),
'items' => [
'table' => 'tgis_map',
'value' => ['id_tgis_map'],
'show' => ['map_name'],
'data' => [
[
'table' => 'tgis_map_layer',
'ref' => ['tgis_map_id_tgis_map'],
'value' => ['id_tmap_layer'],
'data' => [
[
'table' => 'tgis_map_layer_groups',
'ref' => ['layer_id'],
'value' => [
'layer_id',
'group_id',
],
],
[
'table' => 'tgis_map_layer_has_tagente',
'ref' => ['tgis_map_layer_id_tmap_layer'],
'value' => [
'tgis_map_layer_id_tmap_layer',
'tagente_id_agente',
],
],
],
],
[
'table' => 'tgis_map_has_tgis_map_con',
'ref' => ['tgis_map_id_tgis_map'],
'value' => [
'tgis_map_id_tgis_map',
'tgis_map_con_id_tmap_con',
],
],
],
],
],
'custom_graph' => [
'label' => __('Custom graph'),
'items' => [
'table' => 'tgraph',
'value' => ['id_graph'],
'show' => ['name'],
'data' => [
[
'table' => 'tgraph_source',
'ref' => ['id_graph'],
'value' => ['id_gs'],
],
],
],
],
'dashboard' => [
'label' => __('Dashboard'),
'items' => [
'table' => 'tdashboard',
'value' => ['id'],
'show' => ['name'],
'data' => [
[
'table' => 'twidget_dashboard',
'ref' => ['id_dashboard'],
'value' => ['id'],
],
], ],
], ],
], ],
@ -243,38 +445,96 @@ class Prd
/** /**
* Export prd. * Converts a resource into a string.
* *
* @return array * @param string $type Item type.
* @param mixed $id Item value.
* @param string $name Item name.
*
* @return string
*/ */
public function exportPrd(string $type, $value, $name) :array public function exportPrd(string $type, mixed $id, string $name) :string
{ {
$result = []; $result = '';
$prd_data = $this->getOnePrdData($type); $prd_data = $this->getOnePrdData($type);
if (empty($prd_data) === false) { if (empty($prd_data) === false) {
$result['prd_data'] = [ $result .= '[prd_data]'.LINE_BREAK.LINE_BREAK;
'type' => $type, $result .= 'type="'.$type.'"'.LINE_BREAK;
'name' => $name, $result .= 'name="'.$name.'"'.LINE_BREAK.LINE_BREAK;
];
foreach ($prd_data['data'] as $key => $element) { $result .= '['.$prd_data['items']['table'].']'.LINE_BREAK.LINE_BREAK;
$sql = sprintf(
'SELECT * FROM %s WHERE %s = %s',
$prd_data['items']['table'],
reset($prd_data['items']['value']),
$id,
);
$row = db_get_row_sql($sql);
$primary_key = $row[reset($prd_data['items']['value'])];
foreach ($row as $column => $value) {
$result .= $column.'['.$primary_key.']="'.$value.'"'.LINE_BREAK;
}
$result .= LINE_BREAK;
$result .= $this->recursiveExport($prd_data['items']['data'], $id);
}
return $result;
}
/**
* Recursive function to traverse all data
*
* @param mixed $data Data.
* @param mixed $id Id value for search.
*
* @return string
*/
private function recursiveExport($data, $id): string
{
$result = '';
foreach ($data as $key => $element) {
$result .= '['.$element['table'].']'.LINE_BREAK.LINE_BREAK;
$sql = sprintf( $sql = sprintf(
'SELECT * FROM %s WHERE %s = %s', 'SELECT * FROM %s WHERE %s = %s',
$element['table'], $element['table'],
$element['ref'], reset($element['ref']),
$value, $id,
); );
$test = db_get_all_rows_sql($sql); $rows = db_get_all_rows_sql($sql);
foreach ($rows as $row) {
if (count($element['value']) > 1) {
$primary_key = '';
foreach ($element['value'] as $value) {
$primary_key .= $row[$value].'-';
}
// $result[$element['table']] $primary_key = substr($primary_key, 0, -1);
// hd($test, true); hd($primary_key, true);
} else {
$primary_key = $row[reset($element['value'])];
}
foreach ($row as $column => $value) {
$result .= $column.'['.$primary_key.']="'.$value.'"'.LINE_BREAK;
}
$result .= LINE_BREAK;
}
if (isset($element['data']) === true) {
$result .= $this->recursiveExport($element['data'], $primary_key);
} }
} }
return $result; return $result;
} }

View File

@ -906,3 +906,6 @@ define('DEMO_SERVICE', 6);
define('DEMO_DASHBOARD', 7); define('DEMO_DASHBOARD', 7);
define('DEMO_VISUAL_CONSOLE', 8); define('DEMO_VISUAL_CONSOLE', 8);
define('DEMO_PLUGIN', 9); define('DEMO_PLUGIN', 9);
// Export resources.
define('LINE_BREAK', "\n");