summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2012-11-10 18:18:05 +0100
committerAndreas Gohr <andi@splitbrain.org>2012-11-10 18:18:05 +0100
commit60dd32d957e6c4ce2d969c45cc60497f55a39abe (patch)
tree51b56b85739258e873c528e2f6d18af76d3b56e5 /lib
parentb8f41ef0bac4e82cb3b02cd318efaddbaaeb1a78 (diff)
downloadrpg-60dd32d957e6c4ce2d969c45cc60497f55a39abe.tar.gz
rpg-60dd32d957e6c4ce2d969c45cc60497f55a39abe.tar.bz2
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/plugins/config/settings/config.class.php104
-rw-r--r--lib/plugins/config/settings/config.metadata.php3
2 files changed, 107 insertions, 0 deletions
diff --git a/lib/plugins/config/settings/config.class.php b/lib/plugins/config/settings/config.class.php
index 722c8df7b..e4a638eb0 100644
--- a/lib/plugins/config/settings/config.class.php
+++ b/lib/plugins/config/settings/config.class.php
@@ -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) {
diff --git a/lib/plugins/config/settings/config.metadata.php b/lib/plugins/config/settings/config.metadata.php
index 3607f56c6..582452917 100644
--- a/lib/plugins/config/settings/config.metadata.php
+++ b/lib/plugins/config/settings/config.metadata.php
@@ -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)
* -------------------------------------------------