1<?php 2 3namespace dokuwiki\plugin\config\core\Setting; 4 5/** 6 * Class setting_multicheckbox 7 */ 8class SettingMulticheckbox extends SettingString 9{ 10 11 protected $choices = []; 12 protected $combine = []; 13 protected $other = 'always'; 14 15 /** @inheritdoc */ 16 public function update($input) { 17 if($this->isProtected()) return false; 18 19 // split any combined values + convert from array to comma separated string 20 $input = $input ?: []; 21 $input = $this->array2str($input); 22 23 $value = is_null($this->local) ? $this->default : $this->local; 24 if($value == $input) return false; 25 26 if($this->pattern && !preg_match($this->pattern, $input)) { 27 $this->error = true; 28 $this->input = $input; 29 return false; 30 } 31 32 $this->local = $input; 33 return true; 34 } 35 36 /** @inheritdoc */ 37 public function html(\admin_plugin_config $plugin, $echo = false) { 38 39 $disable = ''; 40 41 if ($this->isProtected()) { 42 $value = $this->protected; 43 $disable = 'disabled="disabled"'; 44 } elseif ($echo && $this->error) { 45 $value = $this->input; 46 } else { 47 $value = is_null($this->local) ? $this->default : $this->local; 48 } 49 50 $key = htmlspecialchars($this->key); 51 52 // convert from comma separated list into array + combine complimentary actions 53 $value = $this->str2array($value); 54 $default = $this->str2array($this->default); 55 56 $input = ''; 57 foreach($this->choices as $choice) { 58 $idx = array_search($choice, $value); 59 $idx_default = array_search($choice, $default); 60 61 $checked = ($idx !== false) ? 'checked="checked"' : ''; 62 63 // @todo ideally this would be handled using a second class of "default" 64 $class = (($idx !== false) === (false !== $idx_default)) ? " selectiondefault" : ""; 65 66 $prompt = ($plugin->getLang($this->key . '_' . $choice) ?: htmlspecialchars($choice)); 67 68 $input .= '<div class="selection' . $class . '">' . "\n"; 69 $input .= '<label for="config___' . $key . '_' . $choice . '">' . $prompt . "</label>\n"; 70 $input .= '<input id="config___' . $key . '_' . $choice . '" name="config[' . $key . 71 '][]" type="checkbox" class="checkbox" value="' . $choice . '" ' . $disable . ' ' . $checked . "/>\n"; 72 $input .= "</div>\n"; 73 74 // remove this action from the disabledactions array 75 if($idx !== false) unset($value[$idx]); 76 if($idx_default !== false) unset($default[$idx_default]); 77 } 78 79 // handle any remaining values 80 if($this->other != 'never') { 81 $other = implode(',', $value); 82 // test equivalent to ($this->_other == 'always' || ($other && $this->_other == 'exists') 83 // use != 'exists' rather than == 'always' to ensure invalid values default to 'always' 84 if($this->other != 'exists' || $other) { 85 86 $class = ( 87 (count($default) === count($value)) && 88 (count($value) === count(array_intersect($value, $default))) 89 ) ? 90 " selectiondefault" : ""; 91 92 $input .= '<div class="other' . $class . '">' . "\n"; 93 $input .= '<label for="config___' . $key . '_other">' . 94 $plugin->getLang($key . '_other') . 95 "</label>\n"; 96 $input .= '<input id="config___' . $key . '_other" name="config[' . $key . 97 '][other]" type="text" class="edit" value="' . htmlspecialchars($other) . 98 '" ' . $disable . " />\n"; 99 $input .= "</div>\n"; 100 } 101 } 102 $label = '<label>' . $this->prompt($plugin) . '</label>'; 103 return [$label, $input]; 104 } 105 106 /** 107 * convert comma separated list to an array and combine any complimentary values 108 * 109 * @param string $str 110 * @return array 111 */ 112 protected function str2array($str) { 113 $array = explode(',', $str); 114 115 if(!empty($this->combine)) { 116 foreach($this->combine as $key => $combinators) { 117 $idx = []; 118 foreach($combinators as $val) { 119 if(($idx[] = array_search($val, $array)) === false) break; 120 } 121 122 if(count($idx) && $idx[count($idx) - 1] !== false) { 123 foreach($idx as $i) unset($array[$i]); 124 $array[] = $key; 125 } 126 } 127 } 128 129 return $array; 130 } 131 132 /** 133 * convert array of values + other back to a comma separated list, incl. splitting any combined values 134 * 135 * @param array $input 136 * @return string 137 */ 138 protected function array2str($input) { 139 140 // handle other 141 $other = trim($input['other']); 142 $other = empty($other) ? [] : explode(',', str_replace(' ', '', $input['other'])); 143 unset($input['other']); 144 145 $array = array_unique(array_merge($input, $other)); 146 147 // deconstruct any combinations 148 if(!empty($this->combine)) { 149 foreach($this->combine as $key => $combinators) { 150 151 $idx = array_search($key, $array); 152 if($idx !== false) { 153 unset($array[$idx]); 154 $array = array_merge($array, $combinators); 155 } 156 } 157 } 158 159 return implode(',', array_unique($array)); 160 } 161} 162