openstamanager/src/Modules.php

384 lines
11 KiB
PHP
Raw Normal View History

<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* 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/>.
*/
use Models\Module;
2020-09-22 14:54:14 +02:00
use Util\Query;
/**
* 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::pool($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)
{
$result = self::get($module);
return $result ? $result->permission : null;
}
/**
* 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'])) {
2020-09-22 14:54:14 +02:00
$result['clause'] = 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 = 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'];
}
}
}
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.
*
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
* @param string $params
2018-12-29 12:03:22 +01:00
*
* @return string
*/
public static function link($modulo, $id_record = null, $testo = null, $alternativo = true, $extra = null, $blank = true, $anchor = null, $params = 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 (!string_contains($testo, '<i ')) {
2018-12-29 12:03:22 +01:00
$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'];
return '<a href="'.base_path().'/'.$link.$params.'#'.$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') ? base_path().'/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'].'" data-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];
}
}