added 'array' type for config manager

This allows to use simple arrays in the config file but have a comma
separated list in the config manager.
This commit is contained in:
Andreas Gohr 2012-11-10 18:18:05 +01:00
parent b8f41ef0ba
commit 60dd32d957
2 changed files with 107 additions and 0 deletions

View File

@ -435,6 +435,110 @@ if (!class_exists('setting')) {
}
}
if (!class_exists('setting_array')) {
class setting_array extends setting {
/**
* Create an array from a string
*
* @param $string
* @return array
*/
protected function _from_string($string){
$array = explode(',', $string);
$array = array_map('trim', $array);
$array = array_filter($array);
$array = array_unique($array);
return $array;
}
/**
* Create a string from an array
*
* @param $array
* @return string
*/
protected function _from_array($array){
return join(', ', (array) $array);
}
/**
* update setting with user provided value $input
* if value fails error check, save it
*
* @param string $input
* @return bool true if changed, false otherwise (incl. on error)
*/
function update($input) {
if (is_null($input)) return false;
if ($this->is_protected()) return false;
$input = $this->_from_string($input);
$value = is_null($this->_local) ? $this->_default : $this->_local;
if ($value == $input) return false;
foreach($input as $item){
if ($this->_pattern && !preg_match($this->_pattern,$item)) {
$this->_error = true;
$this->_input = $input;
return false;
}
}
$this->_local = $input;
return true;
}
protected function _escape($string) {
$tr = array("\\" => '\\\\', "'" => '\\\'');
return "'".strtr( cleanText($string), $tr)."'";
}
/**
* generate string to save setting value to file according to $fmt
*/
function out($var, $fmt='php') {
if ($this->is_protected()) return '';
if (is_null($this->_local) || ($this->_default == $this->_local)) return '';
$out = '';
if ($fmt=='php') {
$vals = array_map(array($this, '_escape'), $this->_local);
$out = '$'.$var."['".$this->_out_key()."'] = array(".join(', ',$vals).");\n";
}
return $out;
}
function html(&$plugin, $echo=false) {
$value = '';
$disable = '';
if ($this->is_protected()) {
$value = $this->_protected;
$disable = 'disabled="disabled"';
} else {
if ($echo && $this->_error) {
$value = $this->_input;
} else {
$value = is_null($this->_local) ? $this->_default : $this->_local;
}
}
$key = htmlspecialchars($this->_key);
$value = htmlspecialchars($this->_from_array($value));
$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<input id="config___'.$key.'" name="config['.$key.']" type="text" class="edit" value="'.$value.'" '.$disable.'/>';
return array($label,$input);
}
}
}
if (!class_exists('setting_string')) {
class setting_string extends setting {
function html(&$plugin, $echo=false) {

View File

@ -32,6 +32,9 @@
* separated list of checked choices
* 'fieldset' - used to group configuration settings, but is not itself a setting. To make this clear in
* the language files the keys for this type should start with '_'.
* 'array' - a simple (one dimensional) array of string values, shown as comma separated list in the
* config manager but saved as PHP array(). Values may not contain commas themselves.
* _pattern matching on the array values supported.
*
* Single Setting (source: settings/extra.class.php)
* -------------------------------------------------