[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/config/core/Setting/ -> SettingMulticheckbox.php (source)

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