openstamanager/src/Settings.php

185 lines
5.5 KiB
PHP
Raw Normal View History

<?php
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 a una singolo impostazione specificata.
*
* @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);
// 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]));
}
// 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;
}
2018-07-09 10:44:54 +02:00
return false;
}
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 = '
2019-01-22 16:07:22 +01:00
{[ "type": "select", "label": "'.$setting->nome.'", "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
}
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 = '
2019-04-19 20:47:55 +02:00
{[ "type": "select", "label": "'.$setting->nome.'", "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 = '
2019-01-22 16:07:22 +01:00
{[ "type": "checkbox", "label": "'.$setting->nome.'", "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 = '
2019-01-22 16:07:22 +01:00
{[ "type": "'.$setting->tipo.'", "label": "'.$setting->nome.'", "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 = '
2019-01-22 16:07:22 +01:00
{[ "type": "'.$tipo.'", "label": "'.$setting->nome.'", "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;
}
}