openstamanager/src/Settings.php

225 lines
7.5 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/>.
*/
2018-08-11 15:37:38 +02:00
use Models\Setting;
2018-12-29 12:03:22 +01:00
use Respect\Validation\Validator as v;
2018-07-09 10:44:54 +02:00
/**
* Classe per la gestione dell impostazioni del progetto.
*
* @since 2.3
*/
class Settings
{
2018-07-09 10:44:54 +02:00
/** @var array Elenco delle impostazioni disponibili */
protected static $settings = [];
protected static $references = [];
protected static $sections = [];
/**
* Restituisce tutte le informazioni di tutti le impostazioni presenti.
*
* @return array
*/
public static function getSettings()
{
if (empty(self::$settings)) {
$settings = [];
$references = [];
$sections = [];
2018-08-11 15:37:38 +02:00
$results = Setting::all();
2018-07-09 10:44:54 +02:00
foreach ($results as $result) {
2018-08-11 15:37:38 +02:00
$settings[$result->id] = $result;
$references[$result->nome] = $result->id;
2018-07-09 10:44:54 +02:00
if (!isset($sections[$result['sezione']])) {
$sections[$result['sezione']] = [];
}
2018-08-11 15:37:38 +02:00
$sections[$result['sezione']][] = $result->id;
2018-07-09 10:44:54 +02:00
}
self::$settings = $settings;
self::$references = $references;
self::$sections = $sections;
}
return self::$settings;
}
/**
* Restituisce le informazioni relative ad una singola impostazione specificata.
2018-07-09 10:44:54 +02:00
*
* @param string|int $setting
*
* @return array
*/
public static function get($setting)
{
$settings = self::getSettings();
if (!is_numeric($setting) && !empty(self::$references[$setting])) {
$setting = self::$references[$setting];
}
return $settings[$setting];
}
/**
* Restituisce il valore corrente dell'impostazione ricercata.
*
2018-07-09 10:44:54 +02:00
* @param string|int $setting
*
* @return string
*/
2018-07-09 10:44:54 +02:00
public static function getValue($setting)
{
2018-08-11 15:37:38 +02:00
return self::get($setting)->valore;
2018-07-09 10:44:54 +02:00
}
2018-08-11 15:37:38 +02:00
/**
* Imposta il valore dell'impostazione indicata.
*
* @param string|int $setting
* @param mixed $value
*
* @return bool
*/
2018-07-09 10:44:54 +02:00
public static function setValue($setting, $value)
{
2018-07-09 10:44:54 +02:00
$setting = self::get($setting);
2021-07-07 07:57:10 +02:00
$value = (is_array($value) ? implode(',', $value) : $value);
2018-07-09 10:44:54 +02:00
// Trasformazioni
// Boolean (checkbox)
2018-08-11 15:37:38 +02:00
if ($setting->tipo == 'boolean') {
2018-07-09 10:44:54 +02:00
$value = (empty($value) || $value == 'off') ? false : true;
}
// Validazioni
// integer
2018-08-11 15:37:38 +02:00
if ($setting->tipo == 'integer') {
2018-11-02 18:15:50 +01:00
$validator = v::intVal();
}
2018-07-09 10:44:54 +02:00
// list
// verifico che il valore scelto sia nella lista enumerata nel db
2018-08-11 15:37:38 +02:00
elseif (preg_match("/list\[(.+?)\]/", $setting->tipo, $m)) {
2018-07-09 10:44:54 +02:00
$validator = v::in(explode(',', $m[1]));
}
// multiple
// verifico che il valore scelto sia nella lista enumerata nel db
elseif (preg_match("/multiple\[(.+?)\]/", $setting->tipo, $m[0][0])) {
//$validator = v::in(explode(',', $m[0][0][1]));
2018-07-09 10:44:54 +02:00
}
// Boolean (checkbox)
2018-08-11 15:37:38 +02:00
elseif ($setting->tipo == 'boolean') {
2018-07-09 10:44:54 +02:00
$validator = v::boolType();
}
if (empty($validator) || $validator->validate($value)) {
2018-08-11 15:37:38 +02:00
$setting->valore = $value;
$setting->save();
2018-07-09 10:44:54 +02:00
return true;
}
return false;
2018-07-09 10:44:54 +02:00
}
2018-09-07 16:19:16 +02:00
/**
* Genera l'input HTML per la modifica dell'impostazione.
*
* @param string|int $setting
2018-09-19 09:57:30 +02:00
* @param bool $required
*
2018-09-07 16:19:16 +02:00
* @return string
*/
public static function input($setting, $required = false)
2018-07-09 10:44:54 +02:00
{
$setting = self::get($setting);
2018-08-11 15:37:38 +02:00
// Lista predefinita
if (preg_match("/list\[(.+?)\]/", $setting->tipo, $m)) {
2018-09-18 17:58:15 +02:00
$values = explode(',', $m[1]);
$list = [];
foreach ($values as $value) {
$list[] = [
'id' => $value,
'text' => $value,
];
}
2018-09-18 17:58:15 +02:00
2018-07-09 10:44:54 +02:00
$result = '
{[ "type": "select", "multiple": 0, "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.']", "values": '.json_encode($list).', "value": "'.$setting->valore.'", "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
2018-07-09 10:44:54 +02:00
}
// Lista multipla
2020-05-04 18:12:02 +02:00
elseif (preg_match("/multiple\[(.+?)\]/", $setting->tipo, $m)) {
$values = explode(',', $m[1]);
$list = [];
foreach ($values as $value) {
$list[] = [
'id' => $value,
'text' => $value,
];
}
2020-02-05 14:51:39 +01:00
$result = '
{[ "type": "select", "multiple": 1, "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.'][]", "values": '.json_encode($list).', "value": "'.$setting->valore.'", "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
}
2018-08-11 15:37:38 +02:00
// Lista da query
elseif (preg_match('/^query=(.+?)$/', $setting->tipo, $m)) {
2018-07-09 10:44:54 +02:00
$result = '
{[ "type": "select", "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.']", "values": "'.str_replace('"', '\"', $setting->tipo).'", "value": "'.$setting->valore.'", "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
}
2018-07-09 10:44:54 +02:00
// Boolean (checkbox)
2018-08-11 15:37:38 +02:00
elseif ($setting->tipo == 'boolean') {
2018-07-09 10:44:54 +02:00
$result = '
{[ "type": "checkbox", "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.']", "placeholder": "'.tr('Attivo').'", "value": "'.$setting->valore.'", "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
2018-08-11 15:37:38 +02:00
}
2018-09-18 17:58:15 +02:00
// Campi di default
elseif (in_array($setting->tipo, ['textarea', 'ckeditor', 'timestamp', 'date', 'time'])) {
2018-07-09 10:44:54 +02:00
$result = '
{[ "type": "'.$setting->tipo.'", "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.']", "value": '.json_encode($setting->valore).', "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
2018-07-09 10:44:54 +02:00
}
2018-08-11 15:37:38 +02:00
// Campo di testo
2018-07-09 10:44:54 +02:00
else {
2018-08-11 15:37:38 +02:00
$numerico = in_array($setting->tipo, ['integer', 'decimal']);
2018-08-11 15:37:38 +02:00
$tipo = preg_match('/password/i', $setting->nome, $m) ? 'password' : $setting->tipo;
2018-07-09 10:44:54 +02:00
$tipo = $numerico ? 'number' : 'text';
2018-07-09 10:44:54 +02:00
$result = '
{[ "type": "'.$tipo.'", "label": '.json_encode($setting->nome).', "readonly": "'.!$setting->editable.'", "name": "setting['.$setting->id.']", "value": "'.$setting->valore.'"'.($numerico && $setting->tipo == 'integer' ? ', "decimals": 0' : '').', "required": "'.intval($required).'", "help": "'.$setting->help.'" ]}';
2018-07-09 10:44:54 +02:00
}
2018-07-09 10:44:54 +02:00
return $result;
}
}