2017-08-04 16:28:16 +02:00
< ? php
/**
* Classe per la gestione delle informazioni relative ai moduli installati .
*
* @ since 2.3
*/
class Modules
{
2017-08-31 11:32:49 +02:00
/** @var int Identificativo del modulo corrente */
2017-08-04 16:28:16 +02:00
protected static $current_module ;
2017-08-31 11:32:49 +02:00
/** @var int Identificativo dell'elemento corrente */
2017-08-04 16:28:16 +02:00
protected static $current_element ;
2017-08-31 11:32:49 +02:00
/** @var array Elenco dei moduli disponibili */
2017-08-04 16:28:16 +02:00
protected static $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 = [];
2017-08-31 11:32:49 +02:00
/** @var array Elenco delle query generiche dei moduli */
2017-08-04 16:28:16 +02:00
protected static $queries = [];
2017-08-31 11:32:49 +02:00
/** @var array Elenco gerarchico dei moduli */
2017-08-04 16:28:16 +02:00
protected static $hierarchy = [];
2017-08-31 11:32:49 +02:00
/** @var array Profondità dell'elenco gerarchico */
2017-08-04 16:28:16 +02:00
protected static $depth ;
2017-08-31 11:32:49 +02:00
/** @var array Struttura HTML dedicata al menu principale */
2017-08-04 16:28:16 +02:00
protected static $menu ;
/**
* Restituisce tutte le informazioni di tutti i moduli installati .
*
* @ return array
*/
public static function getModules ()
{
if ( empty ( self :: $modules )) {
$database = Database :: getConnection ();
2017-08-07 13:07:18 +02:00
$user = Auth :: user ();
2017-08-04 16:28:16 +02:00
2018-03-23 21:35:50 +01:00
$results = $database -> fetchArray ( 'SELECT * FROM `zz_modules` LEFT JOIN (SELECT `idmodule`, `permessi` FROM `zz_permissions` WHERE `idgruppo` = (SELECT `idgruppo` FROM `zz_users` WHERE `id` = ' . prepare ( $user [ 'id_utente' ]) . ')) AS `zz_permissions` ON `zz_modules`.`id`=`zz_permissions`.`idmodule` LEFT JOIN (SELECT `idmodule`, `clause`, `position` FROM `zz_group_module` WHERE `idgruppo` = (SELECT `idgruppo` FROM `zz_users` WHERE `id` = ' . prepare ( $user [ 'id_utente' ]) . ') AND `enabled` = 1 UNION SELECT `id_module` AS `idmodule`, `clause`, `position` FROM `zz_segments`) AS `zz_group_module` ON `zz_modules`.`id`=`zz_group_module`.`idmodule`' );
2017-08-04 16:28:16 +02:00
$modules = [];
$additionals = [];
foreach ( $results as $result ) {
if ( empty ( $additionals [ $result [ 'id' ]])) {
$additionals [ $result [ 'id' ]][ 'WHR' ] = [];
$additionals [ $result [ 'id' ]][ 'HVN' ] = [];
}
2018-02-14 11:10:03 +01:00
$result [ 'options' ] = App :: replacePlaceholder ( $result [ 'options' ]);
$result [ 'options2' ] = App :: replacePlaceholder ( $result [ 'options2' ]);
$result [ 'option' ] = empty ( $result [ 'options2' ]) ? $result [ 'options' ] : $result [ 'options2' ];
2017-08-04 16:28:16 +02:00
if ( ! empty ( $result [ 'clause' ])) {
2018-02-14 11:10:03 +01:00
$result [ 'clause' ] = App :: replacePlaceholder ( $result [ 'clause' ]);
2017-08-04 16:28:16 +02:00
$additionals [ $result [ 'id' ]][ $result [ 'position' ]][] = $result [ 'clause' ];
}
if ( empty ( $modules [ $result [ 'id' ]])) {
if ( empty ( $result [ 'permessi' ])) {
2017-08-07 13:07:18 +02:00
if ( Auth :: admin ()) {
2017-08-04 16:28:16 +02:00
$result [ 'permessi' ] = 'rw' ;
} else {
$result [ 'permessi' ] = '-' ;
}
}
unset ( $result [ 'clause' ]);
unset ( $result [ 'position' ]);
unset ( $result [ 'idmodule' ]);
$modules [ $result [ 'id' ]] = $result ;
$modules [ $result [ 'name' ]] = $result [ 'id' ];
}
}
self :: $modules = $modules ;
self :: $additionals = $additionals ;
}
return self :: $modules ;
}
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 ();
foreach ( $modules as $key => $module ) {
if ( $module [ 'permessi' ] == '-' ) {
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
*
* @ return array
*/
2017-09-22 15:19:59 +02:00
public static function get ( $module )
2017-08-04 16:28:16 +02:00
{
if ( ! is_numeric ( $module ) && ! empty ( self :: getModules ()[ $module ])) {
$module = self :: getModules ()[ $module ];
}
return self :: getModules ()[ $module ];
}
/**
* Restituisce i permessi accordati all ' utente in relazione al modulo specificato .
*
* @ param string | int $module
*
* @ return string
*/
public static function getPermission ( $module )
{
2017-09-22 15:19:59 +02:00
return self :: get ( $module )[ 'permessi' ];
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 )
{
2017-09-22 15:19:59 +02:00
return ( array ) self :: $additionals [ self :: get ( $module )[ 'id' ]];
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
*/
public static function getHierarchy ( $depth = 3 )
{
if ( empty ( self :: $hierarchy ) || self :: $depth != $depth ) {
$database = Database :: getConnection ();
$depth = ( $depth < 2 ) ? 2 : $depth ;
$fields = [];
for ( $i = 0 ; $i < $depth ; ++ $i ) {
$fields [] = '`t' . $i . " `.`id` AS 't " . $i . " .id' " ;
}
$query = 'SELECT ' . implode ( ', ' , $fields ) . ' FROM `zz_modules` AS `t0`' ;
for ( $i = 1 ; $i < $depth ; ++ $i ) {
$query .= ' LEFT JOIN `zz_modules` AS `t' . $i . '` ON `t' . $i . '`.`parent` = `t' . ( $i - 1 ) . '`.`id`' ;
}
2018-01-31 11:02:34 +01:00
$query .= ' WHERE `t0`.`parent` IS NULL ORDER BY ' ;
for ( $i = 0 ; $i < $depth ; ++ $i ) {
$query .= '`t' . $i . '`.`order` ASC' ;
if ( $i != $depth - 1 ) {
$query .= ', ' ;
}
}
2017-08-04 16:28:16 +02:00
$modules = $database -> fetchArray ( $query );
$hierarchy = [];
foreach ( $modules as $module ) {
$hierarchy = self :: buildArray ( $module , $hierarchy );
}
self :: $depth = $depth ;
self :: $hierarchy = $hierarchy ;
}
return self :: $hierarchy ;
}
/**
* Restituisce l 'elaborazione dell' array secondo una struttura ad albero ( molteplici root ) .
*
* @ param int $id
* @ param array $data
* @ param int $actual
*
* @ return array
*/
protected static function buildArray ( $module , $data = [], $actual = 0 )
{
if ( ! empty ( $module [ 't' . $actual . '.id' ])) {
$pos = array_search ( $module [ 't' . $actual . '.id' ], array_column ( $data , 'id' ));
if ( $pos === false && ! empty ( $module [ 't' . $actual . '.id' ])) {
2017-09-22 15:19:59 +02:00
$array = self :: get ( $module [ 't' . $actual . '.id' ]);
2017-08-04 16:28:16 +02:00
$array [ 'childrens' ] = [];
$data [] = $array ;
$pos = count ( $data ) - 1 ;
}
if ( ! empty ( $module [ 't' . ( $actual + 1 ) . '.id' ])) {
$data [ $pos ][ 'childrens' ] = self :: buildArray ( $module , $data [ $pos ][ 'childrens' ], $actual + 1 );
}
}
return $data ;
}
/**
* Restituisce il menu principale del progetto .
*
* @ param int $depth Profondità del menu
*
* @ return string
*/
public static function getMainMenu ( $depth = 3 )
{
if ( empty ( self :: $menu ) || self :: $depth != $depth ) {
$menus = self :: getHierarchy ( $depth );
2018-02-14 11:10:03 +01:00
$module_name = App :: getCurrentModule ()[ 'name' ];
2017-08-04 16:28:16 +02:00
$result = '' ;
foreach ( $menus as $menu ) {
$result .= self :: sidebarMenu ( $menu , isset ( $module_name ) ? $module_name : '' )[ 0 ];
}
self :: $menu = $result ;
}
return self :: $menu ;
}
/**
* Restituisce l 'insieme dei menu derivato da un' array strutturato ad albero .
*
* @ param array $element
* @ param int $actual
*
* @ return string
*/
protected static function sidebarMenu ( $element , $actual = null )
{
$options = ( $element [ 'options2' ] != '' ) ? $element [ 'options2' ] : $element [ 'options' ];
2018-03-02 19:01:30 +01:00
$link = ( $options != '' && $options != 'menu' ) ? ROOTDIR . '/controller.php?id_module=' . $element [ 'id' ] : 'javascript:;' ;
2017-08-04 16:28:16 +02:00
$title = $element [ 'title' ];
$target = ( $element [ 'new' ] == 1 ) ? '_blank' : '_self' ;
$active = ( $actual == $element [ 'name' ]);
$show = ( self :: getPermission ( $element [ 'id' ]) != '-' && ! empty ( $element [ 'enabled' ])) ? true : false ;
$submenus = $element [ 'childrens' ];
if ( ! empty ( $submenus )) {
$temp = '' ;
foreach ( $submenus as $submenu ) {
$r = self :: sidebarMenu ( $submenu , $actual );
$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 ];
}
/**
* Undocumented function .
*
* @ 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"' : '' ;
2017-09-18 17:24:25 +02:00
if ( ! empty ( $module ) && in_array ( $module [ 'permessi' ], [ '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 ;
}
}
}