Default page on install + Setting::setValue() dot notation + cleanup

This commit is contained in:
Jonathan Labreuille
2016-01-15 13:37:37 +01:00
parent a574733217
commit 3689545589
5 changed files with 127 additions and 111 deletions

View File

@@ -48,14 +48,44 @@ class Setting extends Model {
}
public static function setValue($key, $value) {
if(is_array($value)) {
$value = serialize($value);
}
$keys = explode('.', $key);
return Setting::createOrUpdate(array(
'name' => $key,
'value' => $value
));
if(count($keys) === 1) {
if(is_array($value)) {
$value = serialize($value);
}
return Setting::createOrUpdate(array(
'name' => $key,
'value' => $value
));
} else {
$main_key = array_shift($keys);
$setting_value = static::getValue($main_key, array());
$current_value = &$setting_value;
$last_key = array_pop($keys);
for($i = 0, $count = count($keys); $i < $count; $i++) {
if($i < $count) {
if(!is_array($current_value)) {
$current_value = array();
}
if(!array_key_exists($keys[$i], $current_value)) {
$current_value = array($keys[$i] => array());
}
$current_value =& $current_value[$keys[$i]];
}
}
if(is_scalar($current_value)) {
$current_value = array();
}
$current_value[$last_key] = $value;
return static::setValue($main_key, $setting_value);
}
}
public static function getAll() {