2018-02-10 10:53:00 +01:00
< ? php
/**
* Classe per la gestione delle utenze .
*
* @ since 2.4
*/
class App
{
2018-06-26 16:33:15 +02:00
/** @var array Identificativo del modulo corrente */
2018-02-10 10:53:00 +01:00
protected static $current_module ;
/** @var int Identificativo dell'elemento corrente */
protected static $current_element ;
2018-06-26 16:33:15 +02:00
/** @var array Elenco degli assets del progetto */
2018-02-14 11:10:03 +01:00
protected static $assets = [
// CSS
'css' => [
'app.min.css' ,
'style.min.css' ,
'themes.min.css' ,
2018-06-23 15:41:32 +02:00
],
// Print CSS
'print' => [
'print.min.css' ,
2018-02-14 11:10:03 +01:00
],
// JS
'js' => [
'app.min.js' ,
'custom.min.js' ,
'i18n/parsleyjs/|lang|.min.js' ,
'i18n/select2/|lang|.min.js' ,
'i18n/moment/|lang|.min.js' ,
'i18n/fullcalendar/|lang|.min.js' ,
],
];
2018-02-10 10:53:00 +01:00
/**
* Restituisce l ' identificativo del modulo attualmente in utilizzo .
*
2018-06-26 16:33:15 +02:00
* @ return array
2018-02-10 10:53:00 +01:00
*/
public static function getCurrentModule ()
{
2018-06-23 18:35:08 +02:00
$id = filter ( 'id_module' );
if ( empty ( self :: $current_module ) && ! empty ( $id )) {
self :: $current_module = Modules :: get ( $id );
2018-02-10 10:53:00 +01:00
}
return self :: $current_module ;
}
/**
* Restituisce l 'identificativo dell' elemento attualmente in utilizzo .
*
* @ return int
*/
public static function getCurrentElement ()
{
if ( empty ( self :: $current_element )) {
2018-06-26 16:33:15 +02:00
self :: $current_element = intval ( filter ( 'id_record' ));
2018-02-10 10:53:00 +01:00
}
return self :: $current_element ;
}
2018-02-14 11:10:03 +01:00
/**
* Restituisce la configurazione di default del gestionale .
*
* @ return array
*/
protected static function getDefaultConfig ()
{
if ( file_exists ( DOCROOT . '/config.example.php' )) {
include DOCROOT . '/config.example.php' ;
}
return get_defined_vars ();
}
2018-02-10 10:53:00 +01:00
/**
* Restituisce la configurazione dell ' installazione .
*
* @ return array
*/
2018-02-14 11:10:03 +01:00
public static function getConfig ()
2018-02-10 10:53:00 +01:00
{
if ( file_exists ( DOCROOT . '/config.inc.php' )) {
include DOCROOT . '/config.inc.php' ;
2018-02-14 11:10:03 +01:00
$config = get_defined_vars ();
} else {
$config = [];
2018-02-10 10:53:00 +01:00
}
2018-02-14 11:10:03 +01:00
$defaultConfig = self :: getDefaultConfig ();
2018-03-02 19:01:30 +01:00
$result = array_merge ( $defaultConfig , $config );
// Operazioni di normalizzazione sulla configurazione
$result [ 'debug' ] = ! empty ( $result [ 'debug' ]);
return $result ;
2018-02-14 11:10:03 +01:00
}
/**
* Individuazione dei percorsi di base .
*
* @ return array
*/
public static function definePaths ( $docroot )
{
// Individuazione di $rootdir
$rootdir = substr ( $_SERVER [ 'SCRIPT_NAME' ], 0 , strrpos ( $_SERVER [ 'SCRIPT_NAME' ], '/' )) . '/' ;
if ( strrpos ( $rootdir , '/' . basename ( $docroot ) . '/' ) !== false ) {
$rootdir = substr ( $rootdir , 0 , strrpos ( $rootdir , '/' . basename ( $docroot ) . '/' )) . '/' . basename ( $docroot );
} else {
$rootdir = '/' ;
}
$rootdir = rtrim ( $rootdir , '/' );
$rootdir = str_replace ( '%2F' , '/' , rawurlencode ( $rootdir ));
// Individuazione di $baseurl
$baseurl = ( isHTTPS ( true ) ? 'https' : 'http' ) . '://' . $_SERVER [ 'HTTP_HOST' ] . $rootdir ;
// Impostazione delle variabili globali
define ( 'DOCROOT' , $docroot );
define ( 'ROOTDIR' , $rootdir );
define ( 'BASEURL' , $baseurl );
}
/**
* Restituisce la configurazione dell ' installazione .
*
* @ return array
*/
public static function getPaths ()
{
$assets = ROOTDIR . '/assets/dist' ;
return [
'assets' => $assets ,
'css' => $assets . '/css' ,
'js' => $assets . '/js' ,
'img' => $assets . '/img' ,
];
}
/**
* Restituisce la configurazione dell ' installazione .
*
* @ return array
*/
public static function getAssets ()
{
// Assets aggiuntivi
$config = self :: getConfig ();
// Impostazione dei percorsi
$paths = self :: getPaths ();
$lang = Translator :: getInstance () -> getCurrentLocale ();
2018-06-23 15:41:32 +02:00
// Sezioni: nome - percorso
$sections = [
'css' => 'css' ,
'print' => 'css' ,
'js' => 'js' ,
];
2018-02-14 11:10:03 +01:00
2018-06-23 15:41:32 +02:00
$assets = [];
2018-02-14 11:10:03 +01:00
2018-06-23 15:41:32 +02:00
foreach ( $sections as $section => $dir ) {
$result = array_unique ( array_merge ( self :: $assets [ $section ], $config [ 'assets' ][ $section ]));
2018-02-14 11:10:03 +01:00
2018-06-23 15:41:32 +02:00
foreach ( $result as $key => $element ) {
$element = $paths [ $dir ] . '/' . $element ;
$element = str_replace ( '|lang|' , $lang , $element );
2018-02-14 11:10:03 +01:00
2018-06-23 15:41:32 +02:00
$result [ $key ] = $element ;
}
2018-02-14 11:10:03 +01:00
2018-06-23 15:41:32 +02:00
$assets [ $section ] = $result ;
2018-02-14 11:10:03 +01:00
}
// JS aggiuntivi per gli utenti connessi
if ( Auth :: check ()) {
2018-06-23 15:41:32 +02:00
$assets [ 'js' ][] = ROOTDIR . '/lib/functions.js' ;
$assets [ 'js' ][] = ROOTDIR . '/lib/init.js' ;
2018-02-14 11:10:03 +01:00
}
2018-06-23 15:41:32 +02:00
return $assets ;
2018-02-14 11:10:03 +01:00
}
/**
* Restituisce un ' insieme di array comprendenti le informazioni per la costruzione della query del modulo indicato .
*
2018-06-26 16:33:15 +02:00
* @ param array $element
2018-02-14 11:10:03 +01:00
*
* @ return array
*/
public static function readQuery ( $element )
{
if ( str_contains ( $element [ 'option' ], '|select|' )) {
$result = self :: readNewQuery ( $element );
} else {
$result = self :: readOldQuery ( $element );
}
return $result ;
}
protected static function readNewQuery ( $element )
{
$fields = [];
$summable = [];
$search_inside = [];
$search = [];
2018-06-26 16:33:15 +02:00
$format = [];
2018-02-14 11:10:03 +01:00
$slow = [];
$order_by = [];
$query = $element [ 'option' ];
$views = self :: getViews ( $element );
$select = [];
foreach ( $views as $view ) {
$select [] = $view [ 'query' ] . ( ! empty ( $view [ 'name' ]) ? " AS ' " . $view [ 'name' ] . " ' " : '' );
if ( $view [ 'enabled' ]) {
$view [ 'name' ] = trim ( $view [ 'name' ]);
$view [ 'search_inside' ] = trim ( $view [ 'search_inside' ]);
$view [ 'order_by' ] = trim ( $view [ 'order_by' ]);
$fields [] = trim ( $view [ 'name' ]);
$search_inside [] = ! empty ( $view [ 'search_inside' ]) ? $view [ 'search_inside' ] : $view [ 'name' ];
2018-02-22 12:35:22 +01:00
$order_by [] = ! empty ( $view [ 'order_by' ]) ? $view [ 'order_by' ] : '`' . $view [ 'name' ] . '`' ;
2018-02-14 11:10:03 +01:00
$search [] = $view [ 'search' ];
$slow [] = $view [ 'slow' ];
$format [] = $view [ 'format' ];
if ( $view [ 'summable' ]) {
$summable [] = 'SUM(`' . trim ( $view [ 'name' ] . " `) AS 'sum_ " . ( count ( $fields ) - 1 ) . " ' " );
}
}
}
$select = empty ( $select ) ? '*' : implode ( ', ' , $select );
$query = str_replace ( '|select|' , $select , $query );
return [
2018-02-17 14:32:40 +01:00
'query' => self :: replacePlaceholder ( $query ),
2018-02-14 11:10:03 +01:00
'fields' => $fields ,
'search_inside' => $search_inside ,
'order_by' => $order_by ,
'search' => $search ,
'slow' => $slow ,
'format' => $format ,
2018-04-20 15:50:57 +02:00
'summable' => $summable ,
2018-02-14 11:10:03 +01:00
];
}
protected static function readOldQuery ( $element )
{
$options = str_replace ([ " \r " , " \n " , " \t " ], ' ' , $element [ 'option' ]);
$options = json_decode ( $options , true );
$options = $options [ 'main_query' ][ 0 ];
2018-03-02 19:01:30 +01:00
$fields = [];
$order_by = [];
$search = [];
$slow = [];
$format = [];
2018-02-14 11:10:03 +01:00
$query = $options [ 'query' ];
2018-03-02 19:01:30 +01:00
$views = explode ( ',' , $options [ 'fields' ]);
foreach ( $views as $view ) {
$fields [] = trim ( $view );
$order_by [] = '`' . trim ( $view ) . '`' ;
2018-02-14 11:10:03 +01:00
$search [] = 1 ;
$slow [] = 0 ;
$format [] = 0 ;
}
$search_inside = $fields ;
return [
2018-02-17 14:32:40 +01:00
'query' => self :: replacePlaceholder ( $query ),
2018-02-14 11:10:03 +01:00
'fields' => $fields ,
'search_inside' => $search_inside ,
'order_by' => $order_by ,
'search' => $search ,
'slow' => $slow ,
'format' => $format ,
'summable' => [],
];
}
protected static function getViews ( $element )
{
$database = Database :: getConnection ();
$user = Auth :: user ();
$views = $database -> fetchArray ( 'SELECT * FROM `zz_views` WHERE `id_module`=' . prepare ( $element [ 'id' ]) . ' AND
`id` IN (
SELECT `id_vista` FROM `zz_group_view` WHERE `id_gruppo` = (
SELECT `idgruppo` FROM `zz_users` WHERE `id` = '.prepare($user[' id ']).'
))
ORDER BY `order` ASC ' );
return $views ;
}
public static function replacePlaceholder ( $query , $custom = null )
{
2018-03-22 15:40:20 +01:00
$id_module = filter ( 'id_module' );
2018-02-14 11:10:03 +01:00
$user = Auth :: user ();
2018-02-17 14:32:40 +01:00
// Sostituzione degli identificatori
2018-02-14 11:10:03 +01:00
$id = empty ( $custom ) ? $user [ 'idanagrafica' ] : $custom ;
$query = str_replace ([ '|idagente|' , '|idtecnico|' , '|idanagrafica|' ], prepare ( $id ), $query );
2018-02-17 14:32:40 +01:00
// Sostituzione delle date
2018-02-14 11:10:03 +01:00
$query = str_replace ([ '|period_start|' , '|period_end|' ], [ $_SESSION [ 'period_start' ], $_SESSION [ 'period_end' ]], $query );
2018-03-21 19:33:33 +01:00
// Sostituzione dei segmenti
2018-03-22 15:40:20 +01:00
$query = str_replace ( '|segment|' , ! empty ( $_SESSION [ 'm' . $id_module ][ 'id_segment' ]) ? ' AND id_segment = ' . prepare ( $_SESSION [ 'm' . $id_module ][ 'id_segment' ]) : '' , $query );
2018-02-14 11:10:03 +01:00
return $query ;
2018-02-10 10:53:00 +01:00
}
2018-02-19 10:52:42 +01:00
public static function load ( $file , $result , $options , $directory = null )
{
$module = self :: getCurrentModule ();
$id_module = filter ( 'id_module' );
$id_record = filter ( 'id_record' );
$directory = empty ( $directory ) ? 'include|custom|/common/' : $directory ;
$directory = str_contains ( $directory , DOCROOT ) ? $directory : DOCROOT . '/' . $directory ;
ob_start ();
2018-06-26 09:41:43 +02:00
$require = self :: filepath ( $directory , 'form.php' );
require $require ;
2018-02-19 10:52:42 +01:00
$form = ob_get_clean ();
$response = self :: internalLoad ( $file , $result , $options , $directory );
$form = str_replace ( '|response|' , $response , $form );
return $form ;
}
protected static function internalLoad ( $file , $result , $options , $directory = null )
{
$module = self :: getCurrentModule ();
$id_module = filter ( 'id_module' );
$id_record = filter ( 'id_record' );
$directory = empty ( $directory ) ? 'include|custom|/common/' : $directory ;
ob_start ();
2018-06-26 09:41:43 +02:00
$require = self :: filepath ( $directory , $file );
require $require ;
2018-02-19 10:52:42 +01:00
$response = ob_get_clean ();
return $response ;
}
2018-06-26 09:41:43 +02:00
/**
* Individua il percorso per il file da includere considerando gli eventuali custom .
*
* @ param string $path
*
* @ return string | null
*/
public static function filepath ( $path , $file = null )
{
$path = str_contains ( $path , DOCROOT ) ? $path : DOCROOT . '/' . ltrim ( $path , '/' );
$path = empty ( $file ) ? $path : rtrim ( $path , '/' ) . '/' . $file ;
$original_file = str_replace ( '|custom|' , '' , $path );
$custom_file = str_replace ( '|custom|' , '/custom' , $path );
2018-06-26 16:33:15 +02:00
$result = '' ;
2018-06-26 09:41:43 +02:00
if ( file_exists ( $custom_file )) {
$result = $custom_file ;
} elseif ( file_exists ( $original_file )) {
$result = $original_file ;
}
2018-06-26 14:26:40 +02:00
return slashes ( $result );
2018-06-26 09:41:43 +02:00
}
2018-02-10 10:53:00 +01:00
}