openstamanager/ajax_dataload.php

177 lines
5.7 KiB
PHP
Raw Normal View History

<?php
include_once __DIR__.'/core.php';
2019-02-22 10:37:37 +01:00
use Util\Query;
// Informazioni fondamentali
$columns = filter('columns');
$order = filter('order')[0];
2019-07-09 12:29:39 +02:00
$draw_numer = intval(filter('draw'));
$order['column'] = $order['column'] - 1;
array_shift($columns);
2019-02-01 17:44:31 +01:00
$total = Util\Query::readQuery($structure);
2019-02-01 17:44:31 +01:00
// Ricerca
$search = [];
for ($i = 0; $i < count($columns); ++$i) {
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' => [],
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
2019-09-10 09:44:16 +02:00
$results['summable'] = Util\Query::getSums($structure, $search);
2019-02-01 17:44:31 +01:00
// Allineamento delle righe
$align = [];
2019-02-01 17:44:31 +01:00
$row = $rows[0] ?: [];
foreach ($row as $field => $value) {
$value = trim($value);
2019-02-01 17:44:31 +01:00
// Allineamento a destra se il valore della prima riga risulta numerica
if (formatter()->isStandardNumber($value)) {
$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) {
$result = [
'<span class="hide" data-id="'.$r['id'].'"></span>', // Colonna ID
];
foreach ($total['fields'] as $pos => $field) {
$column = [];
if (!empty($r['_bg_'])) {
$column['data-background'] = $r['_bg_'];
}
// Allineamento
if (!empty($align[$field])) {
$column['class'] = $align[$field];
}
$value = trim($r[$field]);
// 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]] : '';
$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)) {
2017-11-15 19:32:23 +01:00
$value = '<span class=\'label 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'])) {
2018-06-23 15:41:32 +02:00
$column['data-color'] = isset($column['data-color']) ? $column['data-color'] : color_inverse($column['data-background']);
}
// Link della colonna
if ($field != '_print_') {
$id_record = $r['id'];
$hash = '';
if (!empty($r['_link_record_'])) {
$id_module = $r['_link_module_'];
$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'] = $rootdir.'/editor.php?id_module='.$id_module.'&id_record='.$id_record.$hash;
}
// Link per i plugin
else {
$column['data-link'] = $rootdir.'/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.'"';
}
2018-02-24 11:23:20 +01:00
// Replace rootdir per le query
$value = str_replace('ROOTDIR', ROOTDIR, $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;