2018-02-03 15:03:48 +01:00
|
|
|
<?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/>.
|
|
|
|
*/
|
2018-02-03 15:03:48 +01:00
|
|
|
|
2020-08-26 18:03:59 +02:00
|
|
|
use Util\Query;
|
|
|
|
|
2018-02-03 15:03:48 +01:00
|
|
|
/**
|
|
|
|
* Classe per la gestione delle funzioni AJAX richiamabili del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
class AJAX
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Controlla se è in corso una richiesta AJAX generata dal progetto.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isAjaxRequest()
|
|
|
|
{
|
2024-01-15 15:30:45 +01:00
|
|
|
return Whoops\Util\Misc::isAjaxRequest() && filter('ajax') !== null;
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:53:10 +02:00
|
|
|
/**
|
|
|
|
* Individua le opzioni di selezione a partire dalla risorsa richiesta.
|
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
* @param array $elements
|
2019-02-22 10:37:37 +01:00
|
|
|
* @param int $page
|
|
|
|
* @param int $length
|
2020-07-31 10:58:59 +02:00
|
|
|
* @param array $options
|
2018-06-26 15:53:10 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-07-31 10:58:59 +02:00
|
|
|
public static function select($resource, $elements = [], $search = null, $page = 0, $length = 100, $options = [])
|
2018-02-09 15:10:06 +01:00
|
|
|
{
|
|
|
|
if (!isset($elements)) {
|
|
|
|
$elements = [];
|
|
|
|
}
|
|
|
|
$elements = (!is_array($elements)) ? explode(',', $elements) : $elements;
|
2018-02-03 15:03:48 +01:00
|
|
|
|
2018-02-16 14:25:06 +01:00
|
|
|
$files = self::find('ajax/select.php', false);
|
2018-02-03 15:03:48 +01:00
|
|
|
|
2018-02-09 15:10:06 +01:00
|
|
|
// File di gestione predefinita
|
2020-09-23 13:36:37 +02:00
|
|
|
array_unshift($files, base_dir().'/ajax_select.php');
|
2018-02-09 15:10:06 +01:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2019-02-22 10:37:37 +01:00
|
|
|
$results = self::getSelectResults($file, $resource, $elements, [
|
|
|
|
'offset' => $page * $length,
|
|
|
|
'length' => $length,
|
2020-07-31 14:25:50 +02:00
|
|
|
], $search, $options);
|
2019-02-22 10:37:37 +01:00
|
|
|
|
2018-02-03 15:03:48 +01:00
|
|
|
if (isset($results)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:45:52 +01:00
|
|
|
$results = isset($results) ? $results : [];
|
|
|
|
|
2020-10-20 10:46:34 +02:00
|
|
|
$total = array_key_exists('recordsFiltered', $results) ? $results['recordsFiltered'] : count($results);
|
|
|
|
$list = array_key_exists('results', $results) ? $results['results'] : $results;
|
2019-02-22 10:37:37 +01:00
|
|
|
|
|
|
|
return [
|
2020-08-06 14:42:59 +02:00
|
|
|
'results' => $list ?: [],
|
2019-02-22 10:37:37 +01:00
|
|
|
'recordsFiltered' => $total,
|
|
|
|
];
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:53:10 +02:00
|
|
|
/**
|
|
|
|
* Completa la query SQL a partire da parametri definiti e ne restituisce i risultati.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $where
|
|
|
|
* @param array $filter
|
|
|
|
* @param array $search
|
2019-02-22 10:37:37 +01:00
|
|
|
* @param array $limit
|
2018-06-26 15:53:10 +02:00
|
|
|
* @param array $custom
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-02-22 10:37:37 +01:00
|
|
|
public static function selectResults($query, $where, $filter = [], $search = [], $limit = [], $custom = [])
|
2018-02-03 15:03:48 +01:00
|
|
|
{
|
2020-10-29 16:48:37 +01:00
|
|
|
if (string_contains($query, '|filter|')) {
|
2018-02-03 15:03:48 +01:00
|
|
|
$query = str_replace('|filter|', !empty($filter) ? 'WHERE '.implode(' OR ', $filter) : '', $query);
|
|
|
|
} elseif (!empty($filter)) {
|
|
|
|
$where[] = '('.implode(' OR ', $filter).')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($search)) {
|
|
|
|
$where[] = '('.implode(' OR ', $search).')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = str_replace('|where|', !empty($where) ? 'WHERE '.implode(' AND ', $where) : '', $query);
|
2019-02-22 10:37:37 +01:00
|
|
|
$query .= ' LIMIT '.$limit['offset'].', '.$limit['length'];
|
2018-02-03 15:03:48 +01:00
|
|
|
|
2020-08-26 18:03:59 +02:00
|
|
|
$data = Query::executeAndCount($query);
|
2019-02-22 10:37:37 +01:00
|
|
|
$rows = $data['results'];
|
2018-02-03 15:03:48 +01:00
|
|
|
|
|
|
|
$results = [];
|
2019-02-22 10:37:37 +01:00
|
|
|
foreach ($rows as $row) {
|
2020-08-26 18:03:59 +02:00
|
|
|
$result = $row;
|
2018-02-03 15:03:48 +01:00
|
|
|
foreach ($custom as $key => $value) {
|
2019-02-22 10:37:37 +01:00
|
|
|
$result[$key] = $row[$value];
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$results[] = $result;
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:37:37 +01:00
|
|
|
return [
|
|
|
|
'results' => $results,
|
|
|
|
'recordsFiltered' => $data['count'],
|
|
|
|
];
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
/**
|
|
|
|
* Effettua la ricerca di un termine all'intero delle risorse disponibili.
|
|
|
|
*
|
|
|
|
* @param string $term
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function search($term)
|
|
|
|
{
|
|
|
|
if (strlen($term) < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = self::find('ajax/search.php');
|
|
|
|
|
|
|
|
// File di gestione predefinita
|
2020-09-23 13:36:37 +02:00
|
|
|
array_unshift($files, base_dir().'/ajax_search.php');
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$module_results = self::getSearchResults($file, $term);
|
|
|
|
|
|
|
|
$results = array_merge($results, $module_results);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Completa il codice HTML per la risorsa richiesta.
|
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function complete($resource)
|
|
|
|
{
|
|
|
|
$files = self::find('ajax/complete.php');
|
|
|
|
|
|
|
|
// File di gestione predefinita
|
2020-09-23 13:36:37 +02:00
|
|
|
array_unshift($files, base_dir().'/ajax_complete.php');
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$result = self::getCompleteResults($file, $resource);
|
|
|
|
if (!empty($result)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Individua i file per cui l'utente possiede i permessi di accesso.
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param bool $permissions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-02-21 09:58:01 +01:00
|
|
|
public static function find($file, $permissions = true)
|
2018-12-29 12:03:22 +01:00
|
|
|
{
|
|
|
|
$dirname = substr($file, 0, strrpos($file, '/') + 1);
|
|
|
|
|
|
|
|
// Individuazione delle cartelle accessibili
|
|
|
|
if (!empty($permissions)) {
|
|
|
|
$modules = Modules::getAvailableModules();
|
|
|
|
} else {
|
2019-01-25 12:49:59 +01:00
|
|
|
$modules = Models\Module::withoutGlobalScope('enabled')->get();
|
2018-12-29 12:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$modules = $modules->toArray();
|
|
|
|
|
|
|
|
$dirs = array_unique(array_column($modules, 'directory'));
|
|
|
|
$pieces = array_chunk($dirs, 5);
|
|
|
|
|
|
|
|
// Individuazione dei file esistenti
|
|
|
|
$list = [];
|
|
|
|
foreach ($pieces as $piece) {
|
|
|
|
// File nativi
|
2020-09-23 13:36:37 +02:00
|
|
|
$files = glob(base_dir().'/modules/{'.implode(',', $piece).'}/'.$file, GLOB_BRACE);
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
// File personalizzati
|
2020-09-23 13:36:37 +02:00
|
|
|
$custom_files = glob(base_dir().'/modules/{'.implode(',', $piece).'}/custom/'.$file, GLOB_BRACE);
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
// Pulizia dei file nativi che sono stati personalizzati
|
|
|
|
foreach ($custom_files as $key => $value) {
|
|
|
|
$index = array_search(str_replace('custom/'.$dirname, $dirname, $value), $files);
|
|
|
|
if ($index !== false) {
|
|
|
|
unset($files[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = array_merge($list, $files, $custom_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
asort($list);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:53:10 +02:00
|
|
|
/**
|
|
|
|
* Ottiene i risultati del select all'interno di un file specifico (modulo).
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $resource
|
|
|
|
* @param array $elements
|
2019-02-22 10:37:37 +01:00
|
|
|
* @param array $limit
|
2020-07-31 10:58:59 +02:00
|
|
|
* @param array $options
|
2018-06-26 15:53:10 +02:00
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
2020-07-31 10:58:59 +02:00
|
|
|
protected static function getSelectResults($file, $resource, $elements = [], $limit = [], $search = null, $options = [])
|
2018-02-03 15:03:48 +01:00
|
|
|
{
|
|
|
|
$where = [];
|
|
|
|
$filter = [];
|
|
|
|
$search_fields = [];
|
|
|
|
|
|
|
|
$custom = [
|
|
|
|
'id' => 'id',
|
|
|
|
'text' => 'descrizione',
|
|
|
|
];
|
|
|
|
|
2018-02-03 16:29:53 +01:00
|
|
|
// Database
|
2018-09-20 12:05:22 +02:00
|
|
|
$dbo = $database = database();
|
2018-02-03 16:29:53 +01:00
|
|
|
|
2020-07-31 10:58:59 +02:00
|
|
|
// Opzioni di selezione
|
|
|
|
$superselect = $options;
|
|
|
|
|
2018-02-03 15:03:48 +01:00
|
|
|
require $file;
|
|
|
|
|
|
|
|
if (!isset($results) && !empty($query)) {
|
2019-02-22 10:37:37 +01:00
|
|
|
$results = self::selectResults($query, $where, $filter, $search_fields, $limit, $custom);
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:41:32 +02:00
|
|
|
return isset($results) ? $results : null;
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:53:10 +02:00
|
|
|
/**
|
|
|
|
* Ottiene i risultati della ricerca all'interno di un file specifico (modulo).
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $term
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-04 17:26:25 +01:00
|
|
|
protected static function getSearchResults($file, $term)
|
2018-02-03 16:29:53 +01:00
|
|
|
{
|
|
|
|
// Database
|
2018-09-20 12:05:22 +02:00
|
|
|
$dbo = $database = database();
|
2018-02-03 16:29:53 +01:00
|
|
|
|
|
|
|
// Ricerca anagrafiche per ragione sociale per potere mostrare gli interventi, fatture,
|
|
|
|
// ordini, ecc della persona ricercata
|
|
|
|
$idanagrafiche = ['-1'];
|
|
|
|
$ragioni_sociali = ['-1'];
|
|
|
|
$rs = $dbo->fetchArray('SELECT idanagrafica, ragione_sociale FROM an_anagrafiche WHERE ragione_sociale LIKE "%'.$term.'%"');
|
|
|
|
|
|
|
|
for ($a = 0; $a < sizeof($rs); ++$a) {
|
|
|
|
$idanagrafiche[] = $rs[$a]['idanagrafica'];
|
|
|
|
$ragioni_sociali[$rs[$a]['idanagrafica']] = $rs[$a]['ragione_sociale'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
require $file;
|
|
|
|
|
|
|
|
$results = (array) $results;
|
|
|
|
foreach ($results as $key => $value) {
|
|
|
|
$results[$key]['value'] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:53:10 +02:00
|
|
|
/**
|
|
|
|
* Ottiene i risultati della richiesta di completamento all'interno di un file specifico (modulo).
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $resource
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-04 17:26:25 +01:00
|
|
|
protected static function getCompleteResults($file, $resource)
|
2018-02-03 16:29:53 +01:00
|
|
|
{
|
|
|
|
// Database
|
2018-09-20 12:05:22 +02:00
|
|
|
$dbo = $database = database();
|
2018-02-03 16:29:53 +01:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
require $file;
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-02-03 15:03:48 +01:00
|
|
|
}
|