summaryrefslogtreecommitdiff
path: root/lib/plugins/config/settings/config.class.php
diff options
context:
space:
mode:
authorchris <chris@jalakai.co.uk>2006-07-05 03:21:42 +0200
committerchris <chris@jalakai.co.uk>2006-07-05 03:21:42 +0200
commit306ca8aa54954ea2623d9e4927a2c07b9efdfea3 (patch)
treed2870987b37ea76662ff7ae7d1fdd10a29d3e100 /lib/plugins/config/settings/config.class.php
parent409d7af7c9c6c97c8c00cada876a2bf967fa1526 (diff)
downloadrpg-306ca8aa54954ea2623d9e4927a2c07b9efdfea3.tar.gz
rpg-306ca8aa54954ea2623d9e4927a2c07b9efdfea3.tar.bz2
disabled actions addition to config plugin
This patch adds a new generic settings class, setting_multicheckbox. The class displays each of a list of possible choices as a checkbox and an additional text input field for "other" values. The final result is saved in the config file as a comma separated list. It also supports an additional, optional, parameter, "_combine" which can be used to group complimentary values, allowing them to be set or unset together with one checkbox. darcs-hash:20060705012142-9b6ab-b92daddd1393bfc5e9b650a57348237726957c71.gz
Diffstat (limited to 'lib/plugins/config/settings/config.class.php')
-rw-r--r--lib/plugins/config/settings/config.class.php128
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/plugins/config/settings/config.class.php b/lib/plugins/config/settings/config.class.php
index 105d6c83a..10c9f4616 100644
--- a/lib/plugins/config/settings/config.class.php
+++ b/lib/plugins/config/settings/config.class.php
@@ -660,6 +660,134 @@ if (!class_exists('setting_no_default')) {
}
}
+if (!class_exists('setting_multicheckbox')) {
+ class setting_multicheckbox extends setting_string {
+
+ var $_choices = array();
+ var $_combine = array();
+
+ function update($input) {
+ if ($this->is_protected()) return false;
+
+ // split any combined values + convert from array to comma separated string
+ $input = ($input) ? $input : array();
+ $input = $this->_array2str($input);
+
+ $value = is_null($this->_local) ? $this->_default : $this->_local;
+ if ($value == $input) return false;
+
+ if ($this->_pattern && !preg_match($this->_pattern,$input)) {
+ $this->_error = true;
+ $this->_input = $input;
+ return false;
+ }
+
+ $this->_local = $input;
+ return true;
+ }
+
+ 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);
+
+ // convert from comma separated list into array + combine complimentary actions
+ $value = $this->_str2array($value);
+
+ $input = '';
+ foreach ($this->_choices as $choice) {
+ $idx = array_search($choice, $value);
+ $checked = ($idx !== false) ? 'checked="checked"' : '';
+
+ $prompt = ($plugin->getLang($this->_key.'_'.$choice) ?
+ $plugin->getLang($this->_key.'_'.$choice) : htmlspecialchars($choice));
+
+ $input .= '<div class="selection">'."\n";
+ $input .= '<label for="config__'.$key.'_'.$choice.'">'.$prompt."</label>\n";
+ $input .= '<input id="config__'.$key.'_'.$choice.'" name="config['.$key.'][]" type="checkbox" class="checkbox" value="'.$choice.'" '.$disable.' '.$checked."/>\n";
+ $input .= "</div>\n";
+
+ // remove this action from the disabledactions array
+ if ($idx !== false) unset($value[$idx]);
+ }
+
+ // handle any remaining values
+ $other = join(',',$value);
+
+ $input .= '<div class="other">'."\n";
+ $input .= '<label for="config__'.$key.'_other">'.$plugin->getLang($key.'_other')."</label>\n";
+ $input .= '<input id="config__'.$key.'_other" name="config['.$key.'][other]" type="text" class="edit" value="'.htmlspecialchars($other).'" '.$disable." />\n";
+ $input .= "</div>\n";
+
+ $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>';
+ return array($label,$input);
+ }
+
+ /**
+ * convert comma separated list to an array and combine any complimentary values
+ */
+ function _str2array($str) {
+ $array = explode(',',$str);
+
+ if (!empty($this->_combine)) {
+ foreach ($this->_combine as $key => $combinators) {
+ $idx = array();
+ foreach ($combinators as $val) {
+ if (($idx[] = array_search($val, $array)) === false) break;
+ }
+
+ if (count($idx) && $idx[count($idx)-1] !== false) {
+ foreach ($idx as $i) unset($array[$i]);
+ $array[] = $key;
+ }
+ }
+ }
+
+ return $array;
+ }
+
+ /**
+ * convert array of values + other back to a comma separated list, incl. splitting any combined values
+ */
+ function _array2str($input) {
+
+ // handle other
+ $other = trim($input['other']);
+ $other = !empty($other) ? explode(',',str_replace(' ','',$input['other'])) : array();
+ unset($input['other']);
+
+ $array = array_unique(array_merge($input, $other));
+
+ // deconstruct any combinations
+ if (!empty($this->_combine)) {
+ foreach ($this->_combine as $key => $combinators) {
+
+ $idx = array_search($key,$array);
+ if ($idx !== false) {
+ unset($array[$idx]);
+ $array = array_merge($array, $combinators);
+ }
+ }
+ }
+
+ return join(',',array_unique($array));
+ }
+ }
+}
+
/**
* Provide php_strip_whitespace (php5 function) functionality