[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  namespace dokuwiki\plugin\config\core\Setting;
   4  
   5  /**
   6   * Class setting_array
   7   */
   8  class SettingArray extends Setting {
   9  
  10      /**
  11       * Create an array from a string
  12       *
  13       * @param string $string
  14       * @return array
  15       */
  16      protected function fromString($string) {
  17          $array = explode(',', $string);
  18          $array = array_map('trim', $array);
  19          $array = array_filter($array);
  20          $array = array_unique($array);
  21          return $array;
  22      }
  23  
  24      /**
  25       * Create a string from an array
  26       *
  27       * @param array $array
  28       * @return string
  29       */
  30      protected function fromArray($array) {
  31          return join(', ', (array) $array);
  32      }
  33  
  34      /**
  35       * update setting with user provided value $input
  36       * if value fails error check, save it
  37       *
  38       * @param string $input
  39       * @return bool true if changed, false otherwise (incl. on error)
  40       */
  41      public function update($input) {
  42          if(is_null($input)) return false;
  43          if($this->isProtected()) return false;
  44  
  45          $input = $this->fromString($input);
  46  
  47          $value = is_null($this->local) ? $this->default : $this->local;
  48          if($value == $input) return false;
  49  
  50          foreach($input as $item) {
  51              if($this->pattern && !preg_match($this->pattern, $item)) {
  52                  $this->error = true;
  53                  $this->input = $input;
  54                  return false;
  55              }
  56          }
  57  
  58          $this->local = $input;
  59          return true;
  60      }
  61  
  62      /** @inheritdoc */
  63      public function html(\admin_plugin_config $plugin, $echo = false) {
  64          $disable = '';
  65  
  66          if($this->isProtected()) {
  67              $value = $this->protected;
  68              $disable = 'disabled="disabled"';
  69          } else {
  70              if($echo && $this->error) {
  71                  $value = $this->input;
  72              } else {
  73                  $value = is_null($this->local) ? $this->default : $this->local;
  74              }
  75          }
  76  
  77          $key = htmlspecialchars($this->key);
  78          $value = htmlspecialchars($this->fromArray($value));
  79  
  80          $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
  81          $input = '<input id="config___' . $key . '" name="config[' . $key .
  82              ']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>';
  83          return array($label, $input);
  84      }
  85  }