From 1a8b09229c18f37537ba4fc63afd29036a976c2e Mon Sep 17 00:00:00 2001 From: Tatiana Llorente Date: Tue, 4 Jun 2019 17:28:55 +0200 Subject: [PATCH] Export csv from custom field view meta - #3474 --- .../include/functions_custom_fields.php | 82 +++++++++++++++++-- .../include/javascript/buttons.html5.min.js | 35 ++++++++ .../javascript/dataTables.buttons.min.js | 45 ++++++++++ .../styles/js/buttons.dataTables.min.css | 1 + 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 pandora_console/include/javascript/buttons.html5.min.js create mode 100644 pandora_console/include/javascript/dataTables.buttons.min.js create mode 100644 pandora_console/include/styles/js/buttons.dataTables.min.css diff --git a/pandora_console/include/functions_custom_fields.php b/pandora_console/include/functions_custom_fields.php index 9570098766..a09acc5404 100644 --- a/pandora_console/include/functions_custom_fields.php +++ b/pandora_console/include/functions_custom_fields.php @@ -188,10 +188,16 @@ function get_custom_fields_data($custom_field_name) } $array_result = []; - if (isset($result_meta) && is_array($result_meta)) { + if (isset($result_meta) === true + && is_array($result_meta) === true + ) { foreach ($result_meta as $result) { - foreach ($result as $k => $v) { - $array_result[$v['description']] = $v['description']; + if (isset($result) === true + && is_array($result) === true + ) { + foreach ($result as $k => $v) { + $array_result[$v['description']] = $v['description']; + } } } } @@ -385,9 +391,13 @@ function agent_counters_custom_fields($filters) // Filter custom data. $custom_data_and = ''; - if (!in_array(-1, $filters['id_custom_fields_data'])) { - $custom_data_array = implode("', '", $filters['id_custom_fields_data']); - $custom_data_and = "AND tcd.description IN ('".$custom_data_array."')"; + if (isset($filters['id_custom_fields_data']) === true + && is_array($filters['id_custom_fields_data']) === true + ) { + if (!in_array(-1, $filters['id_custom_fields_data'])) { + $custom_data_array = implode("', '", $filters['id_custom_fields_data']); + $custom_data_and = "AND tcd.description IN ('".$custom_data_array."')"; + } } // Filter custom name. @@ -693,3 +703,63 @@ function print_counters_cfv( $html_result .= ''; return $html_result; } + + +/** + * Undocumented function + * + * @param [type] $filters + * @return void + */ +function export_custom_fields_csv($filters) +{ + $data = agent_counters_custom_fields($filters); + $indexed_descriptions = $data['indexed_descriptions']; + + // Table temporary for save array in table + // by order and search custom_field data. + $table_temporary = 'CREATE TEMPORARY TABLE temp_custom_fields ( + id_server int(10), + id_agent int(10), + name_custom_fields varchar(2048), + critical_count int, + warning_count int, + unknown_count int, + notinit_count int, + normal_count int, + total_count int, + `status` int(2), + KEY `data_index_temp_1` (`id_server`, `id_agent`) + )'; + db_process_sql($table_temporary); + + // Insert values array in table temporary. + $values_insert = []; + foreach ($indexed_descriptions as $key => $value) { + $values_insert[] = '('.$value['id_server'].', '.$value['id_agente'].", '".$value['description']."', '".$value['critical_count']."', '".$value['warning_count']."', '".$value['unknown_count']."', '".$value['notinit_count']."', '".$value['normal_count']."', '".$value['total_count']."', ".$value['status'].')'; + } + + $values_insert_implode = implode(',', $values_insert); + $query_insert = 'INSERT INTO temp_custom_fields VALUES '.$values_insert_implode; + db_process_sql($query_insert); + + // Query all fields result. + $query = sprintf( + 'SELECT + tma.id_agente, + tma.id_tagente, + tma.id_tmetaconsole_setup, + tma.alias, + tma.direccion, + tma.server_name, + temp.name_custom_fields, + temp.status + FROM tmetaconsole_agent tma + INNER JOIN temp_custom_fields temp + ON temp.id_agent = tma.id_tagente + AND temp.id_server = tma.id_tmetaconsole_setup' + ); + + $result = db_get_all_rows_sql($query); + return $result; +} diff --git a/pandora_console/include/javascript/buttons.html5.min.js b/pandora_console/include/javascript/buttons.html5.min.js new file mode 100644 index 0000000000..deee7fee68 --- /dev/null +++ b/pandora_console/include/javascript/buttons.html5.min.js @@ -0,0 +1,35 @@ +/*! + HTML5 export buttons for Buttons and DataTables. + 2016 SpryMedia Ltd - datatables.net/license + + FileSaver.js (1.3.3) - MIT license + Copyright © 2016 Eli Grey - http://eligrey.com +*/ +(function(f){"function"===typeof define&&define.amd?define(["jquery","datatables.net","datatables.net-buttons"],function(g){return f(g,window,document)}):"object"===typeof exports?module.exports=function(g,p,z,t){g||(g=window);p&&p.fn.dataTable||(p=require("datatables.net")(g,p).$);p.fn.dataTable.Buttons||require("datatables.net-buttons")(g,p);return f(p,g,g.document,z,t)}:f(jQuery,window,document)})(function(f,g,p,z,t,w){function A(a){for(var b="";0<=a;)b=String.fromCharCode(a%26+65)+b,a=Math.floor(a/ +26)-1;return b}function E(a,b){y===w&&(y=-1===C.serializeToString(f.parseXML(F["xl/worksheets/sheet1.xml"])).indexOf("xmlns:r"));f.each(b,function(b,c){if(f.isPlainObject(c))b=a.folder(b),E(b,c);else{if(y){var d=c.childNodes[0],e,h=[];for(e=d.attributes.length-1;0<=e;e--){var m=d.attributes[e].nodeName;var k=d.attributes[e].nodeValue;-1!==m.indexOf(":")&&(h.push({name:m,value:k}),d.removeAttribute(m))}e=0;for(m=h.length;e'+c),c=c.replace(/_dt_b_namespace_token_/g,":"),c=c.replace(/xmlns:NS[\d]+="" NS[\d]+:/g,""));c=c.replace(/<([^<>]*?) xmlns=""([^<>]*?)>/g,"<$1 $2>");a.file(b,c)}})}function r(a,b,d){var c=a.createElement(b);d&&(d.attr&&f(c).attr(d.attr),d.children&&f.each(d.children,function(a,b){c.appendChild(b)}),null!==d.text&&d.text!==w&&c.appendChild(a.createTextNode(d.text))); +return c}function L(a,b){var d=a.header[b].length;a.footer&&a.footer[b].length>d&&(d=a.footer[b].length);for(var c=0,f=a.body.length;cd&&(d=e);if(401*a[1]?!0:!1};try{var C=new XMLSerializer,y}catch(a){}var F={"_rels/.rels":'', +"xl/_rels/workbook.xml.rels":'',"[Content_Types].xml":'', +"xl/workbook.xml":'', +"xl/worksheets/sheet1.xml":'',"xl/styles.xml":''}, +K=[{match:/^\-?\d+\.\d%$/,style:60,fmt:function(a){return a/100}},{match:/^\-?\d+\.?\d*%$/,style:56,fmt:function(a){return a/100}},{match:/^\-?\$[\d,]+.?\d*$/,style:57},{match:/^\-?£[\d,]+.?\d*$/,style:58},{match:/^\-?€[\d,]+.?\d*$/,style:59},{match:/^\-?\d+$/,style:65},{match:/^\-?\d+\.\d{2}$/,style:66},{match:/^\([\d,]+\)$/,style:61,fmt:function(a){return-1*a.replace(/[\(\)]/g,"")}},{match:/^\([\d,]+\.\d{2}\)$/,style:62,fmt:function(a){return-1*a.replace(/[\(\)]/g,"")}},{match:/^\-?[\d,]+$/,style:63}, +{match:/^\-?[\d,]+\.\d{2}$/,style:64}];v.ext.buttons.copyHtml5={className:"buttons-copy buttons-html5",text:function(a){return a.i18n("buttons.copy","Copy")},action:function(a,b,d,c){this.processing(!0);var g=this;a=I(b,c);var e=b.buttons.exportInfo(c),h=H(c),m=a.str;d=f("
").css({height:1,width:1,overflow:"hidden",position:"fixed",top:0,left:0});e.title&&(m=e.title+h+h+m);e.messageTop&&(m=e.messageTop+h+h+m);e.messageBottom&&(m=m+h+h+e.messageBottom);c.customize&&(m=c.customize(m,c,b));c=f("