1 <?php
 2 
 3 /**
 4  * Create forms for rex_config data.
 5  *
 6  * @package redaxo\core\form
 7  */
 8 class rex_config_form extends rex_form_base
 9 {
10     use rex_factory_trait;
11 
12     private $namespace;
13 
14     protected function __construct($namespace, $fieldset = null, $debug = false)
15     {
16         parent::__construct($fieldset, md5($this->namespace.$fieldset), 'post', $debug);
17 
18         $this->namespace = $namespace;
19 
20         // --------- Load Env
21         if (rex::isBackend()) {
22             $this->loadBackendConfig();
23         }
24     }
25 
26     /**
27      * @param string      $namespace `rex_config` namespace, usually the package key
28      * @param null|string $fieldset
29      * @param bool        $debug
30      *
31      * @return static
32      */
33     public static function factory($namespace, $fieldset = null, $debug = false)
34     {
35         $class = static::getFactoryClass();
36         return new $class($namespace, $fieldset, $debug);
37     }
38 
39     protected function loadBackendConfig()
40     {
41         parent::loadBackendConfig();
42 
43         $attr = ['type' => 'submit', 'internal::useArraySyntax' => false, 'internal::fieldSeparateEnding' => true];
44         $this->addControlField(
45             null,
46             $this->addField('button', 'save', rex_i18n::msg('form_save'), $attr, false)
47         );
48     }
49 
50     protected function getValue($name)
51     {
52         return rex_config::get($this->namespace, $name);
53     }
54 
55     protected function save()
56     {
57         foreach ($this->getSaveElements() as $fieldsetName => $fieldsetElements) {
58             foreach ($fieldsetElements as $element) {
59                 // read-only-fields nicht speichern
60                 if (strpos($element->getAttribute('class'), 'form-control-static') !== false) {
61                     continue;
62                 }
63 
64                 $fieldName = $element->getFieldName();
65                 $fieldValue = $element->getSaveValue();
66 
67                 if (is_string($fieldValue)) {
68                     $fieldValue = trim($fieldValue);
69                 }
70 
71                 rex_config::set($this->namespace, $fieldName, $fieldValue);
72             }
73         }
74 
75         return true;
76     }
77 }
78