2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
use Respect\Validation\Validator as v;
|
|
|
|
|
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)) {
|
|
|
|
$database = Database::getConnection();
|
|
|
|
|
|
|
|
$settings = [];
|
|
|
|
$references = [];
|
|
|
|
$sections = [];
|
|
|
|
|
|
|
|
$results = $database->select('zz_settings', '*');
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$settings[$result['id']] = $result;
|
|
|
|
$references[$result['nome']] = $result['id'];
|
|
|
|
|
|
|
|
if (!isset($sections[$result['sezione']])) {
|
|
|
|
$sections[$result['sezione']] = [];
|
|
|
|
}
|
|
|
|
$sections[$result['sezione']][] = $result['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return self::get($setting)['valore'];
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if ($setting['tipo'] == 'boolean') {
|
|
|
|
$value = (empty($value) || $value == 'off') ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validazioni
|
|
|
|
// integer
|
|
|
|
if ($setting['tipo'] == 'integer') {
|
|
|
|
$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
|
|
|
|
elseif (preg_match("/list\[(.+?)\]/", $setting['tipo'], $m)) {
|
|
|
|
$validator = v::in(explode(',', $m[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Boolean (checkbox)
|
|
|
|
elseif ($setting['tipo'] == 'boolean') {
|
|
|
|
$validator = v::boolType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($validator) || $validator->validate($value)) {
|
2017-08-04 16:28:16 +02:00
|
|
|
$database = Database::getConnection();
|
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
$database->update('zz_settings', [
|
|
|
|
'valore' => $value,
|
|
|
|
], [
|
|
|
|
'id' => $setting['id'],
|
|
|
|
]);
|
|
|
|
self::$settings[$setting['id']]['valore'] = $value;
|
|
|
|
|
|
|
|
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-07-09 10:44:54 +02:00
|
|
|
if (preg_match("/list\[(.+?)\]/", $setting['tipo'], $m)) {
|
|
|
|
$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 = '
|
|
|
|
{[ "type": "select", "label": "'.$setting['nome'].'", "name": "'.$setting['id'].'", "values": "list='.$list.'", "value": "'.$setting['valore'].'" ]}';
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
// query
|
|
|
|
elseif (preg_match('/^query=(.+?)$/', $setting['tipo'], $m)) {
|
|
|
|
$result = '
|
|
|
|
{[ "type": "select", "label": "'.$setting['nome'].'", "name": "'.$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)
|
|
|
|
elseif ($setting['tipo'] == 'boolean') {
|
|
|
|
$result = '
|
|
|
|
{[ "type": "checkbox", "label": "'.$setting['nome'].'", "name": "'.$setting['id'].'", "placeholder": "'.tr('Attivo').'", "value": "'.$setting['valore'].'" ]}';
|
|
|
|
} elseif ($setting['tipo'] == 'textarea') {
|
|
|
|
$result = '
|
|
|
|
{[ "type": "textarea", "label": "'.$setting['nome'].'", "name": "'.$setting['id'].'", "value": '.json_encode($setting['valore']).' ]}';
|
|
|
|
}
|
|
|
|
// Campo di testo normale
|
|
|
|
else {
|
|
|
|
$numerico = in_array($setting['tipo'], ['integer', 'decimal']);
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
$tipo = preg_match('/password/i', $setting['nome'], $m) ? 'password' : $setting['tipo'];
|
|
|
|
$tipo = $numerico ? 'number' : 'text';
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2018-07-09 10:44:54 +02:00
|
|
|
$result = '
|
|
|
|
{[ "type": "'.$tipo.'", "label": "'.$setting['nome'].'", "name": "'.$setting['id'].'", "value": "'.$setting['valore'].'"'.($numerico && $setting['tipo'] == 'integer' ? ', "decimals": 0' : '').' ]}';
|
|
|
|
}
|
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
|
|
|
}
|