2017-08-04 16:28:16 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
* Copyright ( C ) DevCode s . n . c .
*
* 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 />.
*/
2017-08-04 16:28:16 +02:00
2018-08-10 17:14:09 +02:00
use Models\Module ;
2020-09-22 14:54:14 +02:00
use Util\Query ;
2018-08-10 17:14:09 +02:00
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 ()
{
2019-01-02 14:15:16 +01:00
return Module :: getCurrent ();
2018-09-05 10:36:41 +02:00
}
/**
* 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 )
{
2020-09-22 20:28:37 +02:00
$result = self :: get ( $module );
return $result ? $result -> permission : null ;
2017-08-04 16:28:16 +02:00
}
/**
* Restituisce i filtri aggiuntivi dell ' utente in relazione al modulo specificato .
*
* @ param int $id
*
* @ return string
*/
2019-11-02 15:41:04 +01:00
public static function getAdditionals ( $module , $include_segments = true )
2017-08-04 16:28:16 +02:00
{
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-09-20 12:05:22 +02:00
$database = database ();
2018-05-11 15:34:54 +02:00
$additionals [ 'WHR' ] = [];
$additionals [ 'HVN' ] = [];
2018-10-19 18:08:11 +02:00
$results = $database -> fetchArray ( 'SELECT * FROM `zz_group_module` WHERE `idgruppo` = (SELECT `idgruppo` FROM `zz_users` WHERE `id` = ' . prepare ( $user [ 'id' ]) . ') AND `enabled` = 1 AND `idmodule` = ' . prepare ( $module [ 'id' ]));
2018-05-11 15:34:54 +02:00
foreach ( $results as $result ) {
if ( ! empty ( $result [ 'clause' ])) {
2020-09-22 14:54:14 +02:00
$result [ 'clause' ] = Query :: replacePlaceholder ( $result [ 'clause' ]);
2018-05-11 15:34:54 +02:00
$additionals [ $result [ 'position' ]][] = $result [ 'clause' ];
}
}
// Aggiunta dei segmenti
2019-11-02 15:41:04 +01:00
if ( $include_segments ) {
$segments = self :: getSegments ( $module [ 'id' ]);
2020-09-22 20:28:37 +02:00
$id_segment = isset ( $_SESSION [ 'module_' . $module [ 'id' ]]) ? $_SESSION [ 'module_' . $module [ 'id' ]][ 'id_segment' ] : null ;
2019-11-02 15:41:04 +01:00
foreach ( $segments as $result ) {
if ( ! empty ( $result [ 'clause' ]) && $result [ 'id' ] == $id_segment ) {
2020-09-22 14:54:14 +02:00
$result [ 'clause' ] = Query :: replacePlaceholder ( $result [ 'clause' ]);
2019-11-02 15:41:04 +01:00
$additionals [ $result [ 'position' ]][] = $result [ 'clause' ];
}
2018-05-11 15:34:54 +02:00
}
}
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 .
*
2019-01-03 11:04:28 +01:00
* @ param int $module
2018-05-11 14:48:46 +02:00
*
2019-01-03 11:04:28 +01:00
* @ return array
2018-05-11 14:48:46 +02:00
*/
public static function getSegments ( $module )
{
if ( Update :: isUpdateAvailable ()) {
return [];
}
$module = self :: get ( $module )[ 'id' ];
if ( ! isset ( self :: $segments [ $module ])) {
2018-09-20 12:05:22 +02:00
$database = database ();
2018-05-11 14:48:46 +02:00
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
*/
2019-11-02 15:41:04 +01:00
public static function getAdditionalsQuery ( $module , $type = null , $include_segments = true )
2017-08-04 16:28:16 +02:00
{
2019-11-02 15:41:04 +01:00
$array = self :: getAdditionals ( $module , $include_segments );
2017-08-04 16:28:16 +02:00
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
}
2018-12-29 12:03:22 +01:00
/**
* Costruisce un link HTML per il modulo e il record indicati .
*
2020-09-22 14:54:14 +02:00
* @ param string | int $modulo
* @ param int $id_record
* @ param string $testo
* @ param bool | string $alternativo
* @ param string $extra
* @ param bool $blank
* @ param string $anchor
2018-12-29 12:03:22 +01:00
*
* @ return string
*/
2019-07-05 10:47:45 +02:00
public static function link ( $modulo , $id_record = null , $testo = null , $alternativo = true , $extra = null , $blank = true , $anchor = null )
2018-12-29 12:03:22 +01:00
{
$testo = isset ( $testo ) ? nl2br ( $testo ) : tr ( 'Visualizza scheda' );
$alternativo = is_bool ( $alternativo ) && $alternativo ? $testo : $alternativo ;
// Aggiunta automatica dell'icona di riferimento
if ( ! str_contains ( $testo , '<i ' )) {
$testo = $testo . ' <i class="fa fa-external-link"></i>' ;
}
$module = self :: get ( $modulo );
$extra .= ! empty ( $blank ) ? ' target="_blank"' : '' ;
if ( ! empty ( $module ) && in_array ( $module -> permission , [ 'r' , 'rw' ])) {
$link = ! empty ( $id_record ) ? 'editor.php?id_module=' . $module [ 'id' ] . '&id_record=' . $id_record : 'controller.php?id_module=' . $module [ 'id' ];
2019-07-05 10:47:45 +02:00
return '<a href="' . ROOTDIR . '/' . $link . '#' . $anchor . '" ' . $extra . '>' . $testo . '</a>' ;
2018-12-29 12:03:22 +01:00
} else {
return $alternativo ;
}
}
/**
* Individua il percorso per il file .
*
* @ param string | int $element
* @ param string $file
*
* @ return string | null
*/
public static function filepath ( $element , $file )
{
$element = self :: get ( $element );
return $element ? $element -> filepath ( $file ) : null ;
}
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 ];
}
}