1 <?php
  2 
  3 /**
  4  * @package redaxo\core\form
  5  */
  6 class rex_form_element
  7 {
  8     protected $value;
  9     protected $defaultSaveValue = '';
 10     protected $label;
 11     protected $tag;
 12     protected $table;
 13     protected $attributes;
 14     protected $separateEnding;
 15     protected $fieldName;
 16     protected $header;
 17     protected $footer;
 18     protected $prefix;
 19     protected $suffix;
 20     protected $notice;
 21     /** @var rex_validator */
 22     protected $validator;
 23 
 24     public function __construct($tag, rex_form_base $table = null, array $attributes = [], $separateEnding = false)
 25     {
 26         $this->value = null;
 27         $this->label = '';
 28         $this->tag = $tag;
 29         $this->table = $table;
 30         $this->setAttributes($attributes);
 31         $this->separateEnding = $separateEnding;
 32         $this->setHeader('');
 33         $this->setFooter('');
 34         $this->setPrefix('');
 35         $this->setSuffix('');
 36         $this->fieldName = '';
 37         $this->validator = rex_validator::factory();
 38     }
 39 
 40     // --------- Attribute setter/getters
 41 
 42     public function setValue($value)
 43     {
 44         if (is_array($value)) {
 45             $value = '|' . implode('|', $value) . '|';
 46         }
 47         $this->value = $value;
 48     }
 49 
 50     public function setDefaultSaveValue($value)
 51     {
 52         $this->defaultSaveValue = $value;
 53     }
 54 
 55     public function getSaveValue()
 56     {
 57         $value = $this->getValue();
 58         return $value !== '' ? $value : $this->defaultSaveValue;
 59     }
 60 
 61     public function getValue()
 62     {
 63         return $this->value;
 64     }
 65 
 66     public function setFieldName($name)
 67     {
 68         $this->fieldName = $name;
 69     }
 70 
 71     public function getFieldName()
 72     {
 73         return $this->fieldName;
 74     }
 75 
 76     public function setLabel($label)
 77     {
 78         $this->label = $label;
 79     }
 80 
 81     public function getLabel()
 82     {
 83         return $this->label;
 84     }
 85 
 86     public function setNotice($notice)
 87     {
 88         $this->notice = $notice;
 89     }
 90 
 91     public function getNotice()
 92     {
 93         return $this->notice;
 94     }
 95 
 96     public function getTag()
 97     {
 98         return $this->tag;
 99     }
100 
101     public function setSuffix($suffix)
102     {
103         $this->suffix = $suffix;
104     }
105 
106     public function getSuffix()
107     {
108         return $this->suffix;
109     }
110 
111     public function setPrefix($prefix)
112     {
113         $this->prefix = $prefix;
114     }
115 
116     public function getPrefix()
117     {
118         return $this->prefix;
119     }
120 
121     public function setHeader($header)
122     {
123         $this->header = $header;
124     }
125 
126     public function getHeader()
127     {
128         return $this->header;
129     }
130 
131     public function setFooter($footer)
132     {
133         $this->footer = $footer;
134     }
135 
136     public function getFooter()
137     {
138         return $this->footer;
139     }
140 
141     public function setAttribute($name, $value)
142     {
143         if ($name == 'value') {
144             $this->setValue($value);
145         } else {
146             if ($name == 'id') {
147                 $value = rex_string::normalize($value, '-');
148             } elseif ($name == 'name') {
149                 $value = rex_string::normalize($value, '_', '[]');
150             }
151 
152             $this->attributes[$name] = $value;
153         }
154     }
155 
156     public function getAttribute($name, $default = null)
157     {
158         if ($name == 'value') {
159             return $this->getValue();
160         }
161         if ($this->hasAttribute($name)) {
162             return $this->attributes[$name];
163         }
164 
165         return $default;
166     }
167 
168     public function setAttributes(array $attributes)
169     {
170         $this->attributes = [];
171 
172         foreach ($attributes as $name => $value) {
173             $this->setAttribute($name, $value);
174         }
175     }
176 
177     public function getAttributes()
178     {
179         return $this->attributes;
180     }
181 
182     public function hasAttribute($name)
183     {
184         return isset($this->attributes[$name]);
185     }
186 
187     public function hasSeparateEnding()
188     {
189         return $this->separateEnding;
190     }
191 
192     /**
193      * @return rex_validator
194      */
195     public function getValidator()
196     {
197         return $this->validator;
198     }
199 
200     // --------- Element Methods
201 
202     protected function formatClass()
203     {
204         return $this->getAttribute('class');
205     }
206 
207     protected function formatLabel()
208     {
209         $s = '';
210         $label = $this->getLabel();
211 
212         if ($label != '') {
213             $s .= '<label class="control-label" for="' . $this->getAttribute('id') . '">' . $label . '</label>';
214         }
215 
216         return $s;
217     }
218 
219     public function formatElement()
220     {
221         $attr = '';
222         $value = $this->getValue();
223         $tag = rex_escape($this->getTag(), 'html_attr');
224 
225         foreach ($this->getAttributes() as $attributeName => $attributeValue) {
226             $attr .= ' ' . rex_escape($attributeName, 'html_attr') . '="' . rex_escape($attributeValue) . '"';
227         }
228 
229         if ($this->hasSeparateEnding()) {
230             if ($tag == 'button') {
231                 $attr .= ' value="1"';
232             }
233             return '<' . $tag . $attr . '>' . rex_escape($value) . '</' . $tag . '>';
234         }
235         $attr .= ' value="' . rex_escape($value) . '"';
236         return '<' . $tag . $attr . ' />';
237     }
238 
239     protected function formatNotice()
240     {
241         $notice = $this->getNotice();
242         if ($notice != '') {
243             return $notice;
244         }
245         return '';
246     }
247 
248     protected function wrapContent($content)
249     {
250         return $content;
251     }
252 
253     protected function getFragment()
254     {
255         return 'core/form/form.php';
256     }
257 
258     protected function _get()
259     {
260         $class = $this->formatClass();
261         $class = $class == '' ? '' : ' ' . $class;
262 
263         $formElements = [];
264         $n = [];
265         $n['header'] = $this->getHeader();
266         $n['id'] = '';
267         //$n['class']     = $class;
268         $n['label'] = $this->formatLabel();
269         $n['before'] = $this->getPrefix();
270         $n['field'] = $this->formatElement();
271         $n['after'] = $this->getSuffix();
272         $n['note'] = $this->formatNotice();
273         $n['footer'] = $this->getFooter();
274         $formElements[] = $n;
275 
276         $fragment = new rex_fragment();
277         $fragment->setVar('elements', $formElements, false);
278         return  $fragment->parse($this->getFragment());
279     }
280 
281     public function get()
282     {
283         $s = $this->wrapContent($this->_get());
284         return $s;
285     }
286 
287     public function show()
288     {
289         echo $this->get();
290     }
291 }
292