2017-08-04 16:28:16 +02:00
< ? php
2018-08-10 17:14:09 +02:00
use Models\Module ;
2017-08-04 16:28:16 +02:00
/**
* Classe per la gestione delle informazioni relative ai moduli installati .
*
* @ since 2.3
*/
class Modules
{
2017-08-31 11:32:49 +02:00
/** @var array Elenco delle condizioni aggiuntive disponibili */
2017-08-04 16:28:16 +02:00
protected static $additionals = [];
2018-05-11 14:48:46 +02:00
/** @var array Elenco dei segmenti disponibili */
protected static $segments = [];
2017-08-04 16:28:16 +02:00
2017-08-31 11:32:49 +02:00
/** @var array Elenco gerarchico dei moduli */
2018-08-10 17:14:09 +02:00
protected static $hierarchy ;
2017-08-04 16:28:16 +02:00
/**
* Restituisce tutte le informazioni di tutti i moduli installati .
*
* @ return array
*/
public static function getModules ()
{
2018-08-21 11:06:37 +02:00
$results = Module :: getAll ();
2018-06-23 18:35:08 +02:00
2018-08-21 11:06:37 +02:00
// Caricamento dei plugin
if ( ! $results -> first () -> relationLoaded ( 'plugins' )) {
2018-08-11 15:37:38 +02:00
$results -> load ( 'plugins' );
2017-08-04 16:28:16 +02:00
}
2018-08-21 11:06:37 +02:00
return $results ;
2017-08-04 16:28:16 +02:00
}
2018-02-03 16:29:53 +01:00
/**
* Restituisce l ' elenco dei moduli con permessi di accesso accordati .
*
* @ return array
*/
public static function getAvailableModules ()
{
// Individuazione dei moduli con permesso di accesso
$modules = self :: getModules ();
2018-06-23 15:41:32 +02:00
2018-02-03 16:29:53 +01:00
foreach ( $modules as $key => $module ) {
2018-08-10 17:14:09 +02:00
if ( $module -> permission == '-' ) {
2018-02-03 16:29:53 +01:00
unset ( $modules [ $key ]);
}
}
return $modules ;
}
2017-08-04 16:28:16 +02:00
/**
* Restituisce le informazioni relative a un singolo modulo specificato .
*
* @ param string | int $module
*
2018-08-10 17:14:09 +02:00
* @ return Module
2017-08-04 16:28:16 +02:00
*/
2017-09-22 15:19:59 +02:00
public static function get ( $module )
2017-08-04 16:28:16 +02:00
{
2018-08-21 11:06:37 +02:00
self :: getModules ();
2017-08-04 16:28:16 +02:00
2018-08-21 11:06:37 +02:00
return Module :: get ( $module );
2017-08-04 16:28:16 +02:00
}
2018-09-05 10:36:41 +02:00
/**
* Restituisce il modulo attualmente in utilizzo .
*
* @ return Module
*/
public static function getCurrent ()
{
return Module :: getCurrent ( $id );
}
/**
* Imposta il modulo attualmente in utilizzo .
*
* @ param int $id
*/
public static function setCurrent ( $id )
{
Module :: setCurrent ( $id );
}
2017-08-04 16:28:16 +02:00
/**
* Restituisce i permessi accordati all ' utente in relazione al modulo specificato .
*
* @ param string | int $module
*
* @ return string
*/
public static function getPermission ( $module )
{
2018-08-11 15:37:38 +02:00
return self :: get ( $module ) -> permission ;
2017-08-04 16:28:16 +02:00
}
/**
* Restituisce i filtri aggiuntivi dell ' utente in relazione al modulo specificato .
*
* @ param int $id
*
* @ return string
*/
public static function getAdditionals ( $module )
{
2018-05-11 15:34:54 +02:00
$module = self :: get ( $module );
2018-05-31 11:36:14 +02:00
$user = Auth :: user ();
2018-05-11 15:34:54 +02:00
2018-06-23 15:41:32 +02:00
if ( ! isset ( self :: $additionals [ $module [ 'id' ]])) {
2018-05-11 15:34:54 +02:00
$database = Database :: getConnection ();
$additionals [ 'WHR' ] = [];
$additionals [ 'HVN' ] = [];
$results = $database -> fetchArray ( 'SELECT * FROM `zz_group_module` WHERE `idgruppo` = (SELECT `idgruppo` FROM `zz_users` WHERE `id` = ' . prepare ( $user [ 'id_utente' ]) . ') AND `enabled` = 1 AND `idmodule` = ' . prepare ( $module [ 'id' ]));
foreach ( $results as $result ) {
if ( ! empty ( $result [ 'clause' ])) {
$result [ 'clause' ] = App :: replacePlaceholder ( $result [ 'clause' ]);
$additionals [ $result [ 'position' ]][] = $result [ 'clause' ];
}
}
// Aggiunta dei segmenti
$segments = self :: getSegments ( $module [ 'id' ]);
foreach ( $segments as $result ) {
2018-07-19 09:58:28 +02:00
if ( ! empty ( $result [ 'clause' ]) && $result [ 'id' ] == $_SESSION [ 'module_' . $module [ 'id' ]][ 'id_segment' ]) {
2018-05-11 15:34:54 +02:00
$result [ 'clause' ] = App :: replacePlaceholder ( $result [ 'clause' ]);
$additionals [ $result [ 'position' ]][] = $result [ 'clause' ];
}
}
self :: $additionals [ $module [ 'id' ]] = $additionals ;
}
return ( array ) self :: $additionals [ $module [ 'id' ]];
2017-08-04 16:28:16 +02:00
}
2018-05-11 14:48:46 +02:00
/**
* Restituisce i filtri aggiuntivi dell ' utente in relazione al modulo specificato .
*
* @ param int $id
*
* @ return string
*/
public static function getSegments ( $module )
{
if ( Update :: isUpdateAvailable ()) {
return [];
}
$module = self :: get ( $module )[ 'id' ];
if ( ! isset ( self :: $segments [ $module ])) {
$database = Database :: getConnection ();
2018-05-11 15:34:54 +02:00
self :: $segments [ $module ] = $database -> fetchArray ( 'SELECT * FROM `zz_segments` WHERE `id_module` = ' . prepare ( $module ) . ' ORDER BY `predefined` DESC, `id` ASC' );
2018-05-11 14:48:46 +02:00
}
return ( array ) self :: $segments [ $module ];
}
2017-08-04 16:28:16 +02:00
/**
* Restituisce le condizioni SQL aggiuntive del modulo .
*
* @ param string $name
*
* @ return array
*/
public static function getAdditionalsQuery ( $module , $type = null )
{
$array = self :: getAdditionals ( $module );
if ( ! empty ( $type ) && isset ( $array [ $type ])) {
2017-09-20 10:33:22 +02:00
$result = ( array ) $array [ $type ];
2017-08-04 16:28:16 +02:00
} else {
2017-09-20 10:33:22 +02:00
$result = array_merge (( array ) $array [ 'WHR' ], ( array ) $array [ 'HVN' ]);
2017-08-04 16:28:16 +02:00
}
$result = implode ( ' AND ' , $result );
$result = empty ( $result ) ? $result : ' AND ' . $result ;
return $result ;
}
public static function replaceAdditionals ( $id_module , $query )
{
$result = $query ;
// Aggiunta delle condizione WHERE
$result = str_replace ( '1=1' , '1=1' . self :: getAdditionalsQuery ( $id_module , 'WHR' ), $result );
// Aggiunta delle condizione HAVING
$result = str_replace ( '2=2' , '2=2' . self :: getAdditionalsQuery ( $id_module , 'HVN' ), $result );
return $result ;
}
/**
* Restituisce tutte le informazioni dei moduli installati in una scala gerarchica fino alla profondità indicata .
*
*
* @ param int $depth
*
* @ return array
*/
2018-08-10 17:14:09 +02:00
public static function getHierarchy ()
2017-08-04 16:28:16 +02:00
{
2018-08-10 17:14:09 +02:00
if ( ! isset ( self :: $hierarchy )) {
2018-09-06 09:45:49 +02:00
self :: $hierarchy = Module :: getHierarchy () -> toArray ();
2017-08-04 16:28:16 +02:00
}
return self :: $hierarchy ;
}
/**
* Restituisce il menu principale del progetto .
*
* @ param int $depth Profondità del menu
*
* @ return string
*/
public static function getMainMenu ( $depth = 3 )
{
2018-08-10 17:14:09 +02:00
$menus = self :: getHierarchy ();
2017-08-04 16:28:16 +02:00
2018-09-05 10:36:41 +02:00
$module = Modules :: getCurrent ();
2018-08-10 17:14:09 +02:00
$module_name = isset ( $module ) ? $module -> name : '' ;
2017-08-04 16:28:16 +02:00
2018-08-10 17:14:09 +02:00
$result = '' ;
foreach ( $menus as $menu ) {
$result .= self :: sidebarMenu ( $menu , $module_name , $depth )[ 0 ];
2017-08-04 16:28:16 +02:00
}
2018-08-10 17:14:09 +02:00
return $result ;
2017-08-04 16:28:16 +02:00
}
/**
* Restituisce l 'insieme dei menu derivato da un' array strutturato ad albero .
*
* @ param array $element
* @ param int $actual
2018-08-10 17:14:09 +02:00
* @ param int $max_depth
* @ param int $actual_depth
2017-08-04 16:28:16 +02:00
*
* @ return string
*/
2018-08-10 17:14:09 +02:00
protected static function sidebarMenu ( $element , $actual = null , $max_depth = 3 , $actual_depth = 0 )
2017-08-04 16:28:16 +02:00
{
2018-08-10 17:14:09 +02:00
if ( $actual_depth >= $max_depth ) {
return '' ;
}
$link = ( ! empty ( $element [ 'option' ]) && $element [ 'option' ] != 'menu' ) ? ROOTDIR . '/controller.php?id_module=' . $element [ 'id' ] : 'javascript:;' ;
2017-08-04 16:28:16 +02:00
$title = $element [ 'title' ];
2018-06-23 15:41:32 +02:00
$target = '_self' ; // $target = ($element['new'] == 1) ? '_blank' : '_self';
2017-08-04 16:28:16 +02:00
$active = ( $actual == $element [ 'name' ]);
$show = ( self :: getPermission ( $element [ 'id' ]) != '-' && ! empty ( $element [ 'enabled' ])) ? true : false ;
2018-08-10 17:14:09 +02:00
$submenus = $element [ 'all_children' ];
2017-08-04 16:28:16 +02:00
if ( ! empty ( $submenus )) {
$temp = '' ;
foreach ( $submenus as $submenu ) {
2018-08-10 17:14:09 +02:00
$r = self :: sidebarMenu ( $submenu , $actual , $actual_depth + 1 );
2017-08-04 16:28:16 +02:00
$active = $active || $r [ 1 ];
if ( ! $show && $r [ 2 ]) {
$link = 'javascript:;' ;
}
$show = $show || $r [ 2 ];
$temp .= $r [ 0 ];
}
}
$result = '' ;
if ( $show ) {
$result .= '<li class="treeview' ;
if ( $active ) {
$result .= ' active actual' ;
}
$result .= '" id="' . $element [ 'id' ] . ' " >
< a href = " '. $link .' " target = " '. $target .' " >
< i class = " '. $element['icon'] .' " ></ i >
< span > '.$title.' </ span > ' ;
if ( ! empty ( $submenus ) && ! empty ( $temp )) {
$result .= '
< span class = " pull-right-container " >
< i class = " fa fa-angle-left pull-right " ></ i >
</ span >
</ a >
< ul class = " treeview-menu " >
'.$temp.'
</ ul > ' ;
} else {
$result .= '
</ a > ' ;
}
$result .= '
</ li > ' ;
}
return [ $result , $active , $show ];
}
/**
2018-06-26 09:41:43 +02:00
* Costruisce un link HTML per il modulo e il record indicati .
2017-08-04 16:28:16 +02:00
*
* @ param string | int $modulo
* @ param int $id_record
* @ param string $testo
* @ param string $alternativo
* @ param string $extra
*
* @ return string
*/
2017-09-19 18:14:23 +02:00
public static function link ( $modulo , $id_record = null , $testo = null , $alternativo = true , $extra = null , $blank = true )
2017-08-04 16:28:16 +02:00
{
2017-09-14 10:27:49 +02:00
$testo = isset ( $testo ) ? nl2br ( $testo ) : tr ( 'Visualizza scheda' );
2017-08-04 16:28:16 +02:00
$alternativo = is_bool ( $alternativo ) && $alternativo ? $testo : $alternativo ;
2017-09-14 10:27:49 +02:00
// Aggiunta automatica dell'icona di riferimento
if ( ! str_contains ( $testo , '<i ' )) {
$testo = $testo . ' <i class="fa fa-external-link"></i>' ;
}
2017-09-22 15:19:59 +02:00
$module = self :: get ( $modulo );
2017-08-04 16:28:16 +02:00
$extra .= ! empty ( $blank ) ? ' target="_blank"' : '' ;
2018-08-10 17:14:09 +02:00
if ( ! empty ( $module ) && in_array ( $module -> permission , [ 'r' , 'rw' ])) {
2017-09-19 18:14:23 +02:00
$link = ! empty ( $id_record ) ? 'editor.php?id_module=' . $module [ 'id' ] . '&id_record=' . $id_record : 'controller.php?id_module=' . $module [ 'id' ];
return '<a href="' . ROOTDIR . '/' . $link . '" ' . $extra . '>' . $testo . '</a>' ;
2017-08-04 16:28:16 +02:00
} else {
return $alternativo ;
}
}
2018-06-26 09:41:43 +02:00
/**
* Individua il percorso per il file .
*
2018-09-19 10:44:32 +02:00
* @ param string | int $element
2018-06-26 09:41:43 +02:00
* @ param string $file
*
* @ return string | null
*/
2018-09-19 10:44:32 +02:00
public static function filepath ( $element , $file )
2018-06-26 09:41:43 +02:00
{
2018-09-19 10:44:32 +02:00
return self :: get ( $element ) -> filepath ( $file );
2018-06-26 09:41:43 +02:00
}
2017-08-04 16:28:16 +02:00
}