2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
use Respect\Validation\Validator as v;
|
2018-08-11 15:37:38 +02:00
|
|
|
use Models\Setting;
|
2018-07-09 10:44:54 +02:00
|
|
|
|
2017-08-04 16:28:16 +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];
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il valore corrente dell'impostazione ricercata.
|
|
|
|
*
|
2018-07-09 10:44:54 +02:00
|
|
|
* @param string|int $setting
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
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-07-09 10:44:54 +02:00
|
|
|
$validator = v::intVal()->validate($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
public static function input($setting)
|
|
|
|
{
|
|
|
|
$setting = self::get($setting);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-08-11 15:37:38 +02:00
|
|
|
// Lista predefinita
|
|
|
|
if (preg_match("/list\[(.+?)\]/", $setting->tipo, $m)) {
|
2018-07-09 10:44:54 +02:00
|
|
|
$m = explode(',', $m[1]);
|
|
|
|
$list = '';
|
|
|
|
for ($j = 0; $j < count($m); ++$j) {
|
|
|
|
if ($j != 0) {
|
|
|
|
$list .= ',';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
2018-07-09 10:44:54 +02:00
|
|
|
$list .= '\\"'.$m[$j].'\\": \\"'.$m[$j].'\\"';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
2018-07-09 10:44:54 +02:00
|
|
|
$result = '
|
2018-08-11 15:37:38 +02:00
|
|
|
{[ "type": "select", "label": "'.$setting->nome.'", "name": "setting['.$setting->id.']", "values": "list='.$list.'", "value": "'.$setting->valore.'" ]}';
|
2018-07-09 10:44:54 +02:00
|
|
|
}
|
2017-08-04 16:28:16 +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 = '
|
2018-08-11 15:37:38 +02:00
|
|
|
{[ "type": "select", "label": "'.$setting->nome.'", "name": "setting['.$setting->id.']", "values": "'.$setting->tipo.'", "value": "'.$setting->valore.'" ]}';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
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 = '
|
2018-08-11 15:37:38 +02:00
|
|
|
{[ "type": "checkbox", "label": "'.$setting->nome.'", "name": "setting['.$setting->id.']", "placeholder": "'.tr('Attivo').'", "value": "'.$setting->valore.'" ]}';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Textarea
|
|
|
|
elseif ($setting->tipo == 'textarea') {
|
2018-07-09 10:44:54 +02:00
|
|
|
$result = '
|
2018-08-11 15:37:38 +02:00
|
|
|
{[ "type": "textarea", "label": "'.$setting->nome.'", "name": "setting['.$setting->id.']", "value": '.json_encode($setting->valore).' ]}';
|
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-07-03 11:12:32 +02:00
|
|
|
|
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-03 11:12:32 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
$result = '
|
2018-08-11 15:37:38 +02:00
|
|
|
{[ "type": "'.$tipo.'", "label": "'.$setting->nome.'", "name": "setting['.$setting->id.']", "value": "'.$setting->valore.'"'.($numerico && $setting->tipo == 'integer' ? ', "decimals": 0' : '').' ]}';
|
2018-07-09 10:44:54 +02:00
|
|
|
}
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
return $result;
|
2018-07-03 11:12:32 +02:00
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|