#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");
$("#export_data_table-1-0").html('');
} else {
$("#export_data_table-1-0").html('');
$.ajax({
type: "GET",
url: "ajax.php",
@ -137,10 +138,18 @@ html_print_table($table);
$("#button-export_button").click(function(e) {
const value = $("#select_value").val();
if (value !== '0') {
//Show dialog.
confirmDialog({
title: "<?php echo __('Exporting resource'); ?>",
message: "<?php echo __('Exporting resource and downloading, please wait'); ?>",
hideCancelButton: true
},
"downloadDialog"
);
$.ajax({
type: "GET",
url: "ajax.php",
dataType: "html",
data: {
page: 'include/ajax/resources.ajax',
exportPrd: 1,
@ -149,7 +158,24 @@ html_print_table($table);
name: $("#select_value").text(),
},
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) {
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);
$exportPrd = (bool) get_parameter('exportPrd', false);
$deleteFile = (bool) get_parameter('deleteFile', false);
$prd = new Prd();
@ -27,37 +28,34 @@ if ((bool) is_ajax() === true) {
$type = (string) get_parameter('type', '');
$result = false;
$check = $prd->getOnePrdData($type);
if (empty($check) === false) {
switch ($type) {
case 'visual_console':
$result = html_print_label_input_block(
__('Visual console'),
io_safe_output(
html_print_select_from_sql(
'SELECT id, name FROM tlayout',
'select_value',
'',
'',
'',
0,
true,
false,
true,
false,
false,
false,
GENERIC_SIZE_TEXT,
'w40p',
),
)
);
break;
default:
// TODO.
break;
}
$data = $prd->getOnePrdData($type);
if (empty($data) === false) {
$sql = sprintf(
'SELECT %s FROM %s',
reset($data['items']['value']).', '.reset($data['items']['show']),
$data['items']['table']
);
$result = html_print_label_input_block(
$data['label'],
io_safe_output(
html_print_select_from_sql(
$sql,
'select_value',
'',
'',
'',
0,
true,
false,
true,
false,
false,
false,
GENERIC_SIZE_TEXT,
'w40p',
),
)
);
}
echo $result;
@ -69,6 +67,38 @@ if ((bool) is_ajax() === true) {
$value = (int) get_parameter('value', 0);
$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'),
'items' => [
'table' => 'tlayout',
'value' => 'id',
'show' => 'name',
],
'data' => [
[
'table' => 'tlayout',
'ref' => 'id',
'value' => ['id'],
'show' => ['name'],
'data' => [
[
'table' => 'tlayout_data',
'ref' => ['id_layout'],
'value' => ['id'],
],
],
[
'table' => 'tlayout_data',
'ref' => 'id_layout',
],
],
'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' => 'treport_content_sla_combined',
'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);
if (empty($prd_data) === false) {
$result['prd_data'] = [
'type' => $type,
'name' => $name,
];
$result .= '[prd_data]'.LINE_BREAK.LINE_BREAK;
$result .= 'type="'.$type.'"'.LINE_BREAK;
$result .= 'name="'.$name.'"'.LINE_BREAK.LINE_BREAK;
foreach ($prd_data['data'] as $key => $element) {
$sql = sprintf(
'SELECT * FROM %s WHERE %s = %s',
$element['table'],
$element['ref'],
$value,
);
$result .= '['.$prd_data['items']['table'].']'.LINE_BREAK.LINE_BREAK;
$test = db_get_all_rows_sql($sql);
$sql = sprintf(
'SELECT * FROM %s WHERE %s = %s',
$prd_data['items']['table'],
reset($prd_data['items']['value']),
$id,
);
// $result[$element['table']]
// hd($test, true);
$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(
'SELECT * FROM %s WHERE %s = %s',
$element['table'],
reset($element['ref']),
$id,
);
$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].'-';
}
$primary_key = substr($primary_key, 0, -1);
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;
}

View File

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