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