From 3e5b3e2fa6e5e178115304807e85ebbc9ed6bb52 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Thu, 16 Jul 2020 17:31:30 +0200 Subject: [PATCH] Impostazione "Totali delle tabelle ristretti alla selezione" --- ajax.php | 10 +++++++ assets/src/js/functions/datatables.js | 40 ++++++++++++++++++++++++--- include/top.php | 1 + src/Util/Query.php | 17 ++++++++---- update/2_4_16.sql | 3 ++ 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/ajax.php b/ajax.php index f3e658377..0bc8fa6e4 100755 --- a/ajax.php +++ b/ajax.php @@ -140,5 +140,15 @@ switch (filter('op')) { echo json_encode($response); + break; + + case 'summable-results': + $ids = post('ids') ?: []; + $results = Util\Query::getSums($structure, [ + 'id' => $ids, + ]); + + echo json_encode($results); + break; } diff --git a/assets/src/js/functions/datatables.js b/assets/src/js/functions/datatables.js index 497535957..ab52acf17 100755 --- a/assets/src/js/functions/datatables.js +++ b/assets/src/js/functions/datatables.js @@ -350,7 +350,7 @@ function getTable(selector) { var selected = new Map(); var selected_ids = table.data('selected') ? table.data('selected').split(';') : []; - selected_ids.forEach(function(item, index) { + selected_ids.forEach(function (item, index) { selected.set(item, true); }); @@ -360,7 +360,7 @@ function getTable(selector) { id_module: table.data('idmodule'), id_plugin: table.data('idplugin'), - initDatatable: function() { + initDatatable: function () { if (table.hasClass('datatables')) { start_local_datatables(); } else { @@ -395,10 +395,15 @@ function getTable(selector) { bulk_container.addClass('disabled').attr('disabled', true); btn_container.addClass('disabled').attr('disabled', true); } + + // Aggiornamento del footer nel caso sia richiesto + if (globals.restrict_summables_to_selected){ + this.updateSelectedFooter(); + } }, addSelectedRows: function (row_ids) { row_ids = Array.isArray(row_ids) ? row_ids : [row_ids]; - row_ids.forEach(function(item, index) { + row_ids.forEach(function (item, index) { selected.set(item, true); }); @@ -406,7 +411,7 @@ function getTable(selector) { }, removeSelectedRows: function (row_ids) { row_ids = Array.isArray(row_ids) ? row_ids : [row_ids]; - row_ids.forEach(function(item, index) { + row_ids.forEach(function (item, index) { selected.delete(item); }); @@ -416,5 +421,32 @@ function getTable(selector) { selected.clear(); this.saveSelectedRows(); }, + + // Aggiornamento dei campi summable + updateSelectedFooter: function () { + let datatable = this.datatable; + let ids = this.getSelectedRows(); + + $.ajax({ + url: globals.rootdir + "/ajax.php", + type: "POST", + dataType: "json", + data: { + id_module: this.id_module, + id_plugin: this.id_plugin, + op: "summable-results", + ids: ids, + }, + success: function (response) { + for (let [column, value] of Object.entries(response)) { + let index = parseInt(column) + 1; + let sel = datatable.column(index).footer(); + $(sel).addClass("text-right") + .attr("id", "summable") + .html(value); + } + } + }); + }, }; } diff --git a/include/top.php b/include/top.php index fe3ec897e..1d89e6617 100755 --- a/include/top.php +++ b/include/top.php @@ -174,6 +174,7 @@ if (Auth::check()) { order_manager_id: "'.($dbo->isInstalled() ? Modules::get('Stato dei servizi')['id'] : '').'", dataload_page_buffer: '.setting('Lunghezza in pagine del buffer Datatables').', tempo_attesa_ricerche: '.setting('Tempo di attesa ricerche in secondi').', + restrict_summables_to_selected: '.setting('Totali delle tabelle ristretti alla selezione').', }; '; } else { diff --git a/src/Util/Query.php b/src/Util/Query.php index e82c4e981..0cca2f229 100755 --- a/src/Util/Query.php +++ b/src/Util/Query.php @@ -140,11 +140,11 @@ class Query return ''; } - // Filtri di ricerica + // Filtri di ricerca $search_filters = []; - foreach ($search as $field => $value) { + foreach ($search as $field => $original_value) { $pos = array_search($field, $total['fields']); - $value = trim($value); + $value = trim($original_value); if (isset($value) && $pos !== false) { $search_query = $total['search_inside'][$pos]; @@ -201,7 +201,14 @@ class Query // Campo id: ricerca tramite comparazione elseif ($field == 'id') { - $search_filters[] = $field.' = '.prepare($value); + // Filtro per una serie di ID + if (is_array($original_value)) { + if (!empty($original_value)) { + $search_filters[] = $field.' IN ('.implode(', ', $original_value).')'; + } + } else { + $search_filters[] = $field.' = '.prepare($value); + } } // Ricerca @@ -264,7 +271,7 @@ class Query */ public static function getSums($structure, $search = []) { - $total = self::readQuery($structure, $search); + $total = self::readQuery($structure); // Calcolo di eventuali somme if (empty($total['summable'])) { diff --git a/update/2_4_16.sql b/update/2_4_16.sql index 0698f11e5..2895d52a8 100644 --- a/update/2_4_16.sql +++ b/update/2_4_16.sql @@ -122,3 +122,6 @@ UPDATE `em_accounts` SET `connected_at` = NOW(); -- Aggiunta del flag is_importabile sulle causali per permettere/bloccare l'importazione dei DDT ALTER TABLE `dt_causalet` ADD `is_importabile` BOOLEAN DEFAULT TRUE AFTER `descrizione`; + +-- Impostazione "Totali delle tabelle ristretti alla selezione" +INSERT INTO `zz_settings` (`id`, `nome`, `valore`, `tipo`, `editable`, `sezione`, `order`) VALUES (NULL, 'Totali delle tabelle ristretti alla selezione', '0', 'boolean', '1', 'Generali', 119);