openstamanager/ajax_dataload.php

244 lines
9.0 KiB
PHP
Raw Normal View History

<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
include_once __DIR__.'/core.php';
2019-02-22 10:37:37 +01:00
use Util\Query;
// Informazioni fondamentali
2023-09-12 10:12:12 +02:00
$columns = (array) filter('columns', null, true);
2020-09-23 12:00:21 +02:00
$order = filter('order') ? filter('order')[0] : [];
2019-07-09 12:29:39 +02:00
$draw_numer = intval(filter('draw'));
2020-09-23 12:00:21 +02:00
if (!empty(filter('order'))) {
$order['column'] = $order['column'] - 1;
}
$_SESSION['module_'.$id_module]['order'] = $order;
array_shift($columns);
2024-01-15 15:30:45 +01:00
$total = Query::readQuery($structure);
2019-02-01 17:44:31 +01:00
// Ricerca
$search = [];
for ($i = 0; $i < count($columns); ++$i) {
2020-06-09 16:59:26 +02:00
if (!empty($columns[$i]['search']['value']) || $columns[$i]['search']['value'] == '0') {
2019-02-01 17:44:31 +01:00
$search[$total['fields'][$i]] = $columns[$i]['search']['value'];
}
2019-02-01 17:44:31 +01:00
}
2019-02-01 17:44:31 +01:00
$limit = [
'start' => filter('start'),
'length' => filter('length'),
];
2019-02-01 17:44:31 +01:00
// Predisposizione della risposta
$results = [
'data' => [],
'recordsTotal' => 0,
'recordsFiltered' => 0,
'summable' => [],
'avg' => [],
2019-07-09 12:29:39 +02:00
'draw' => $draw_numer,
2019-02-01 17:44:31 +01:00
];
2019-02-22 10:37:37 +01:00
$query = Query::getQuery($structure);
2019-02-01 17:44:31 +01:00
if (!empty($query)) {
// CONTEGGIO TOTALE
$results['recordsTotal'] = $dbo->fetchNum($query);
2019-02-01 17:44:31 +01:00
// RISULTATI VISIBILI
2019-02-22 10:37:37 +01:00
$query = Query::getQuery($structure, $search, $order, $limit);
2019-02-01 17:44:31 +01:00
// Filtri derivanti dai permessi (eventuali)
if (empty($id_plugin)) {
$query = Modules::replaceAdditionals($id_module, $query);
}
// Conteggio dei record filtrati
2019-02-22 10:37:37 +01:00
$data = Query::executeAndCount($query);
$rows = $data['results'];
$results['recordsFiltered'] = $data['count'];
2019-02-01 17:44:31 +01:00
// SOMME
2024-01-15 15:30:45 +01:00
$results['summable'] = Query::getSums($structure, $search);
2019-02-01 17:44:31 +01:00
// MEDIE
$results['avg'] = Query::getAverages($structure, $search);
2019-02-01 17:44:31 +01:00
// Allineamento delle righe
$align = [];
$row = isset($rows[0]) ? $rows[0] : [];
2019-02-01 17:44:31 +01:00
foreach ($row as $field => $value) {
2023-08-04 14:54:28 +02:00
if (!empty($value)) {
2022-03-18 17:43:50 +01:00
$value = trim($value);
}
2019-02-01 17:44:31 +01:00
// Allineamento a destra se il valore della prima riga risulta numerica
2022-11-14 12:48:19 +01:00
if (is_numeric($value) && formatter()->isStandardNumber($value)) {
2019-02-01 17:44:31 +01:00
$align[$field] = 'text-right';
}
// Allineamento al centro se il valore della prima riga risulta relativo a date o icone
elseif (formatter()->isStandardDate($value) || preg_match('/^icon_(.+?)$/', $field)) {
$align[$field] = 'text-center';
}
2019-02-01 17:44:31 +01:00
}
// Creazione della tabella
foreach ($rows as $i => $r) {
// Evitare risultati con ID a null
// Codice non applicabile in ogni caso: sulla base dei permessi, ID può non essere impostato
/*
2020-09-24 09:25:44 +02:00
if (empty($r['id'])) {
continue;
}*/
2020-09-24 09:25:44 +02:00
2019-02-01 17:44:31 +01:00
$result = [
'id' => $r['id'],
2019-02-01 17:44:31 +01:00
'<span class="hide" data-id="'.$r['id'].'"></span>', // Colonna ID
];
foreach ($total['fields'] as $pos => $field) {
$column = [];
if (!empty($r['_bg_'])) {
2024-02-12 18:13:39 +01:00
if (preg_match('/-light$/', $r['_bg_'])) {
2024-02-13 15:50:26 +01:00
$column['data-background'] = substr($r['_bg_'], 0, -6); // Remove the "-light" suffix from the word
} else {
2024-02-12 18:13:39 +01:00
$column['data-background'] = $r['_bg_'];
}
}
// Allineamento
if (!empty($align[$field])) {
$column['class'] = $align[$field];
}
2022-03-23 19:22:43 +01:00
$value = trim((string) $r[$field]);
// Formattazione HTML
if (empty($total['html_format'][$pos]) && !empty($value)) {
2024-09-10 12:38:16 +02:00
$value = strip_tags($value ?: '');
}
// Formattazione automatica
if (!empty($total['format'][$pos]) && !empty($value)) {
2020-04-07 16:31:34 +02:00
if (formatter()->isStandardTimestamp($value)) {
$value = Translator::timestampToLocale($value);
} elseif (formatter()->isStandardDate($value)) {
$value = Translator::dateToLocale($value);
2018-09-20 12:05:22 +02:00
} elseif (formatter()->isStandardTime($value)) {
$value = Translator::timeToLocale($value);
2020-04-14 08:49:48 +02:00
} elseif (formatter()->isStandardNumber($value)) {
2017-09-19 18:14:23 +02:00
$value = Translator::numberToLocale($value);
}
}
// Icona
2018-02-08 11:04:26 +01:00
if (preg_match('/^color_(.+?)$/', $field, $m)) {
2018-06-23 18:35:08 +02:00
$value = isset($r['color_title_'.$m[1]]) ? $r['color_title_'.$m[1]] : '';
// Formattazione automatica
if (!empty($total['format'][$pos]) && !empty($value)) {
if (formatter()->isStandardTimestamp($value)) {
$value = Translator::timestampToLocale($value);
} elseif (formatter()->isStandardDate($value)) {
$value = Translator::dateToLocale($value);
} elseif (formatter()->isStandardTime($value)) {
$value = Translator::timeToLocale($value);
} elseif (formatter()->isStandardNumber($value)) {
$value = Translator::numberToLocale($value);
}
}
$column['class'] = 'text-center small';
$column['data-background'] = $r[$field];
}
// Icona di stampa
elseif ($field == '_print_') {
2018-08-09 13:54:04 +02:00
$print = $r['_print_'];
2018-08-09 13:54:04 +02:00
$print_url = Prints::getHref($print, $r['id']);
2018-08-09 13:54:04 +02:00
$value = '<a href="'.$print_url.'" target="_blank"><i class="fa fa-2x fa-print"></i></a>';
}
// Icona
elseif (preg_match('/^icon_(.+?)$/', trim($field), $m)) {
2024-05-16 18:02:50 +02:00
$value = '<span class=\'badge text-black\' style=\'font-weight:normal;\' ><i class="'.$r[$field].'" title="'.$r['icon_title_'.$m[1]].'" ></i> <span>'.$r['icon_title_'.$m[1]].'</span></span>';
}
// Colore del testo
if (!empty($column['data-background'])) {
$column['data-color'] = isset($column['data-color']) ? $column['data-color'] : color_inverse(trim($column['data-background']));
}
elseif (preg_match('/^mailto_(.+?)$/', trim($field), $m)) {
$column['class'] = '';
$value = ($r[$field] ? '<a class="btn btn-default btn-sm btn-block" style="font-weight:normal;" href="mailto:'.$r[$field].'" target="_blank"><i class="fa fa-envelope text-primary"></i> '.$r[$field].'</a>' : '');
}
elseif (preg_match('/^tel_(.+?)$/', trim($field), $m)) {
$column['class'] = '';
$value = ($r[$field] ? '<a class="btn btn-default btn-sm btn-block" href="tel:'.$r[$field].'" target="_blank"><i class="fa fa-phone text-primary"></i> '.$r[$field].'</a>' : '');
}
// Link della colonna
if ($field != '_print_' && !preg_match('/^tel_(.+?)$/', trim($field), $m) && !preg_match('/^mailto_(.+?)$/', trim($field), $m)) {
$id_record = $r['id'];
$hash = '';
2020-09-23 12:00:21 +02:00
$id_module = !empty($r['_link_module_']) ? $r['_link_module_'] : $id_module;
if (!empty($r['_link_record_'])) {
$id_record = $r['_link_record_'];
$hash = !empty($r['_link_hash_']) ? '#'.$r['_link_hash_'] : '';
unset($id_plugin);
}
2018-07-18 15:20:10 +02:00
// Link per i moduli
if (empty($id_plugin)) {
$column['data-link'] = base_path().'/editor.php?id_module='.$id_module.'&id_record='.$id_record.$hash;
2018-07-18 15:20:10 +02:00
}
// Link per i plugin
else {
$column['data-link'] = base_path().'/add.php?id_module='.$id_module.'&id_record='.$id_record.'&id_plugin='.$id_plugin.'&id_parent='.$id_parent.'&edit=1'.$hash;
$column['data-type'] = 'dialog';
}
}
$attributes = [];
foreach ($column as $key => $val) {
$val = is_array($val) ? implode(' ', $val) : $val;
$attributes[] = $key.'="'.$val.'"';
}
// Replace base_link() per le query
$value = str_replace('base_link()', base_path(), $value);
$result[] = str_replace('|attr|', implode(' ', $attributes), '<div |attr|>'.$value.'</div>');
}
$results['data'][] = $result;
}
}
2019-02-01 17:44:31 +01:00
$json = json_encode($results);
echo $json;