1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-27 00:06:14 +01:00
openstamanager/src/Modules.php

363 lines
10 KiB
PHP
Raw Normal View History

<?php
use Models\Module;
/**
* Classe per la gestione delle informazioni relative ai moduli installati.
*
* @since 2.3
*/
class Modules
{
/** @var array Elenco delle condizioni aggiuntive disponibili */
protected static $additionals = [];
2018-05-11 14:48:46 +02:00
/** @var array Elenco dei segmenti disponibili */
protected static $segments = [];
/** @var array Elenco gerarchico dei moduli */
protected static $hierarchy;
/**
* Restituisce tutte le informazioni di tutti i moduli installati.
*
* @return array
*/
public static function getModules()
{
$results = Module::getAll();
2018-06-23 18:35:08 +02:00
// Caricamento dei plugin
if (!$results->first()->relationLoaded('plugins')) {
2018-08-11 15:37:38 +02:00
$results->load('plugins');
}
return $results;
}
/**
* 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
foreach ($modules as $key => $module) {
if ($module->permission == '-') {
unset($modules[$key]);
}
}
return $modules;
}
/**
* Restituisce le informazioni relative a un singolo modulo specificato.
*
* @param string|int $module
*
* @return Module
*/
public static function get($module)
{
self::getModules();
return Module::get($module);
}
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);
}
/**
* 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;
}
/**
* 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)
{
$module = self::get($module);
2018-05-31 11:36:14 +02:00
$user = Auth::user();
2018-06-23 15:41:32 +02:00
if (!isset(self::$additionals[$module['id']])) {
2018-09-20 12:05:22 +02:00
$database = database();
$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']));
foreach ($results as $result) {
if (!empty($result['clause'])) {
2019-02-01 17:44:31 +01:00
$result['clause'] = Util\Query::replacePlaceholder($result['clause']);
$additionals[$result['position']][] = $result['clause'];
}
}
// Aggiunta dei segmenti
2019-11-02 15:41:04 +01:00
if ($include_segments) {
$segments = self::getSegments($module['id']);
$id_segment = $_SESSION['module_'.$module['id']]['id_segment'];
foreach ($segments as $result) {
if (!empty($result['clause']) && $result['id'] == $id_segment) {
$result['clause'] = Util\Query::replacePlaceholder($result['clause']);
$additionals[$result['position']][] = $result['clause'];
}
}
}
self::$additionals[$module['id']] = $additionals;
}
return (array) self::$additionals[$module['id']];
}
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
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];
}
/**
* 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)
{
2019-11-02 15:41:04 +01:00
$array = self::getAdditionals($module, $include_segments);
if (!empty($type) && isset($array[$type])) {
2017-09-20 10:33:22 +02:00
$result = (array) $array[$type];
} else {
2017-09-20 10:33:22 +02:00
$result = array_merge((array) $array['WHR'], (array) $array['HVN']);
}
$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()
{
if (!isset(self::$hierarchy)) {
2018-09-06 09:45:49 +02:00
self::$hierarchy = Module::getHierarchy()->toArray();
}
return self::$hierarchy;
}
/**
* Restituisce il menu principale del progetto.
*
* @param int $depth Profondità del menu
*
* @return string
*/
public static function getMainMenu($depth = 3)
{
$menus = self::getHierarchy();
2018-09-05 10:36:41 +02:00
$module = Modules::getCurrent();
$module_name = isset($module) ? $module->name : '';
$result = '';
foreach ($menus as $menu) {
$result .= self::sidebarMenu($menu, $module_name, $depth)[0];
}
return $result;
}
2018-12-29 12:03:22 +01:00
/**
* Costruisce un link HTML per il modulo e il record indicati.
*
* @param string|int $modulo
* @param int $id_record
* @param string $testo
* @param string $alternativo
* @param string $extra
2019-07-05 10:47:45 +02:00
* @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;
}
/**
* Restituisce l'insieme dei menu derivato da un'array strutturato ad albero.
*
* @param array $element
* @param int $actual
* @param int $max_depth
* @param int $actual_depth
*
* @return string
*/
protected static function sidebarMenu($element, $actual = null, $max_depth = 3, $actual_depth = 0)
{
if ($actual_depth >= $max_depth) {
return '';
}
$link = (!empty($element['option']) && $element['option'] != 'menu') ? ROOTDIR.'/controller.php?id_module='.$element['id'] : 'javascript:;';
$title = $element['title'];
2018-06-23 15:41:32 +02:00
$target = '_self'; // $target = ($element['new'] == 1) ? '_blank' : '_self';
$active = ($actual == $element['name']);
$show = (self::getPermission($element['id']) != '-' && !empty($element['enabled'])) ? true : false;
$submenus = $element['all_children'];
if (!empty($submenus)) {
$temp = '';
foreach ($submenus as $submenu) {
$r = self::sidebarMenu($submenu, $actual, $actual_depth + 1);
$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];
}
}