trunk /sally/core/lib/sly/Form/ElementBase.php

Language PHP Lines 491
MD5 Hash 513e2a439bfbb271452f9b32e7c24f98 Estimated Cost $4,229 (why?)
Repository https://bitbucket.org/SallyCMS/trunk View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Base class for elements
 *
 * This class wraps some common functionality for all form elements.
 *
 * @ingroup form
 * @author  Christoph
 */
abstract class sly_Form_ElementBase extends sly_Viewable {
	protected $label;          ///< string
	protected $attributes;     ///< array
	protected $helpText;       ///< string
	protected $outerClass;     ///< string
	protected $formRowClass;   ///< string
	protected $multilingual;   ///< boolean
	protected $helpTextIsHtml; ///< boolean

	/**
	 * Constructor
	 *
	 * @param string  $name   the element's name
	 * @param string  $label  the label
	 * @param mixed   $value  the value
	 * @param string  $id     optional ID (if it should differ from $name)
	 */
	public function __construct($name, $label, $value, $id = null) {
		$this->attributes     = array();
		$this->label          = $label;
		$this->outerClass     = '';
		$this->formRowClass   = '';
		$this->multilingual   = false;
		$this->helpTextIsHtml = false;

		$this->setAttribute('name',  $name);
		$this->setAttribute('value', $value);
		$this->setAttribute('id',    $id === null ? $name : $id);
	}

	public function getID()    { return $this->getAttribute('id', '');    }  ///< @return string
	public function getName()  { return $this->getAttribute('name', '');  }  ///< @return string
	public function getValue() { return $this->getAttribute('value', ''); }  ///< @return string

	/**
	 * Returns the label
	 *
	 * @return string  the label
	 */
	public function getLabel() {
		return $this->label;
	}

	/**
	 * Returns an attribute
	 *
	 * @param  string $name     the attribute's name
	 * @param  mixed  $default  the default value
	 * @return mixed            the value or the default value
	 */
	public function getAttribute($name, $default = null) {
		return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
	}

	/**
	 * Sets an attribute
	 *
	 * @param  string $name          the attribute's name
	 * @param  mixed  $value         the new value
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setAttribute($name, $value) {
		$this->attributes[$name] = $value;
		return $this;
	}

	/**
	 * Removes an attribute
	 *
	 * @param  string $name          the attribute's name
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function removeAttribute($name) {
		unset($this->attributes[$name]);
		return $this;
	}

	/**
	 * Adds a new CSS class
	 *
	 * This method will add a new CSS class to the element. Classes are
	 * automatically made unique.
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function addClass($className) {
		$newClasses = $this->addClassHelper($className, $this->getAttribute('class'));
		$this->setAttribute('class', $newClasses);

		return $this;
	}

	/**
	 * Removes a CSS class
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function removeClass($className) {
		$newClasses = $this->removeClassHelper($className, $this->getAttribute('class'));
		$this->setAttribute('class', $newClasses);

		return $this;
	}

	/**
	 * Adds a new CSS style
	 *
	 * This method will add a new CSS style to the element. Styles are
	 * automatically made unique.
	 *
	 * @param  string $style         the CSS style
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function addStyle($style) {
		$styles = strval($this->getAttribute('style'));
		$styles = empty($styles) ? array() : explode(';', $styles);

		if (!in_array($style, $styles)) {
			$styles[] = $style;
		}

		return $this->setAttribute('style', implode(';', array_unique($styles)));
	}

	/**
	 * Returns the attributes as a HTML string
	 *
	 * @param  array $exclude  list of attribute names to exclude
	 * @return string          string like 'foo="bar" name="sly"'
	 */
	protected function getAttributeString($exclude = array()) {
		$exclude    = sly_makeArray($exclude);
		$attributes = array();

		foreach ($this->attributes as $name => &$value) {
			if (!is_array($value) && strlen($value) > 0 && !in_array($name, $exclude)) {
				$attributes[] = $name.'="'.sly_html($value).'"';
			}
		}

		return implode(' ', $attributes);
	}

	/**
	 * Enables or disables the element
	 *
	 * This method is just a wrapper for setting/removing the disabled attribute.
	 *
	 * @param  boolean $disabled     true to disable the element, else false
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setDisabled($disabled = true) {
		if ($disabled) $this->setAttribute('disabled', 'disabled');
		else $this->removeAttribute('disabled');
	}

	/**
	 * Marks the element as (not) required
	 *
	 * This method is just a wrapper for setting/removing the required attribute.
	 *
	 * @param  boolean $required     true to require a value, else false
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setRequired($required = true) {
		if ($required) return $this->setAttribute('required', 'required');
		else return $this->removeAttribute('required');
	}

	/**
	 * Sets the help text
	 *
	 * The help text will be displayed below the element in a smaller font. HTML
	 * is not allowed.
	 *
	 * @param  string $helpText      the new help text
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setHelpText($helpText) {
		$this->helpText = $helpText;
		return $this;
	}

	/**
	 * Toggle whether or not the helptext may contain HTML
	 *
	 * @param  boolean $flag         true to allow HTML, else false
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setHelpTextIsHTML($flag) {
		$this->helpTextIsHtml = (boolean) $flag;
		return $this;
	}

	/**
	 * @return boolean
	 */
	public function isHelpTextHTML() {
		return $this->helpTextIsHtml;
	}

	/**
	 * Sets the value
	 *
	 * @param  mixed $value          the new value
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setValue($value) {
		return $this->setAttribute('value', $value);
	}

	/**
	 * Sets the name
	 *
	 * @param  string $name          the new name
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setName($name) {
		return $this->setAttribute('name', $name);
	}

	/**
	 * Sets the ID
	 *
	 * @param  string $id            the new id
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setID($id) {
		return $this->setAttribute('id', $id);
	}

	/**
	 * Sets the label
	 *
	 * The label will be displayed left to the element. HTML is not allowed. Use
	 * Spaces to indent the label (leading spaces will be converted to &nbsp;).
	 *
	 * @param  string $label         the new label
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function setLabel($label) {
		$this->label = $label;
		return $this;
	}

	/**
	 * Returns the help text
	 *
	 * @return string  the help text
	 */
	public function getHelpText() {
		return $this->helpText;
	}

	/**
	 * Returns the outer row class
	 *
	 * @return string  the outer class
	 */
	public function getOuterClass() {
		return $this->outerClass;
	}

	/**
	 * Returns the form row class
	 *
	 * @return string  the form row class
	 */
	public function getFormRowClass() {
		return $this->formRowClass;
	}

	/**
	 * Container check
	 *
	 * This method checks whether an element is rendering a complete form row
	 * (including the label part, if needed) or if it's just the raw element
	 * (in this case, the form instance will render the label).
	 *
	 * @return boolean  always false
	 */
	public function isContainer() {
		return false;
	}

	/**
	 * Return language status
	 *
	 * @return boolean  true if the element is multilingual, else false
	 */
	public function isMultilingual() {
		return $this->multilingual;
	}

	/**
	 * Sets the elements multilingual status
	 *
	 * Set the element to multilingual if you want Sally to automatically create
	 * X versions of the element for each language in your project. If so, you
	 * have to give the value of this element in form of an array (clang =>
	 * value).
	 *
	 * @param  boolean $multilingual  the new status
	 * @return sly_Form_ElementBase   the object itself
	 */
	public function setMultilingual($multilingual = true) {
		$this->multilingual = (boolean) $multilingual;
		return $this;
	}

	/**
	 * Returns the value to be displayed
	 *
	 * This method will return the values that shall be displayed in the form.
	 * This is mostly useful when a form is submitted and the POST data will be
	 * shown instead of those that were given when the form elements are
	 * initialized.
	 *
	 * @param  string      $type     the variable type to cast to
	 * @param  boolean     $asArray  true to get an array, or else false
	 * @param  sly_Request $request  the request to use or null for the global one
	 * @return mixed                 the value(s) to display
	 */
	public function getDisplayValueHelper($type = 'string', $asArray = false, sly_Request $request = null) {
		// Pr?fen, ob das Formular bereits abgeschickt und noch einmal angezeigt
		// werden soll. Falls ja, ?bernehmen wir den Wert aus den POST-Daten.

		$name    = $this->attributes['name'];
		$request = $request ? $request : sly_Core::getRequest();
		$isSet   = $request->post->has($name);

		if ($isSet && !$asArray) {
			return $request->post($name, $type);
		}

		if ($isSet && $asArray) {
			return $request->postArray($name, $type);
		}

		return $this->attributes['value'];
	}

	/**
	 * Get the form element name
	 *
	 * @return string  the element name
	 */
	public function getDisplayName() {
		return $this->getAttribute('name');
	}

	/**
	 * Renders a form template
	 *
	 * @param  string $filename  the file to render, relative to include/views/_form
	 * @return string            the HTML code
	 */
	protected function renderFilename($filename) {
		return $this->renderView($filename);
	}

	/**
	 * Get the full path for a view
	 *
	 * This methods prepends the filename of a specific view with its path. If
	 * the view is not found inside the core, an exception is thrown.
	 *
	 * @throws sly_Form_Exception  if the view could not be found
	 * @param  string $file        the relative filename
	 * @return string              the full path to the view file
	 */
	protected function getViewFile($file) {
		$full = SLY_COREFOLDER.'/views/form/'.$file;
		if (file_exists($full)) return $full;

		throw new sly_Form_Exception(t('view_not_found', $file));
	}

	/**
	 * Adds a new outer row class
	 *
	 * This method will add a new CSS class to the element. Classes are
	 * automatically made unique.
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function addOuterClass($className) {
		$this->outerClass = $this->addClassHelper($className, $this->outerClass);
		return $this;
	}

	/**
	 * Removes a CSS class
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function removeOuterClass($className) {
		$this->outerClass = $this->removeClassHelper($className, $this->outerClass);
		return $this;
	}

	/**
	 * Adds a new form row class
	 *
	 * This method will add a new CSS class to the element. Classes are
	 * automatically made unique.
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function addFormRowClass($className) {
		$this->formRowClass = $this->addClassHelper($className, $this->formRowClass);
		return $this;
	}

	/**
	 * Removes a CSS class
	 *
	 * @param  string $className     the CSS class
	 * @return sly_Form_ElementBase  the object itself
	 */
	public function removeFormRowClass($className) {
		$this->formRowClass = $this->removeClassHelper($className, $this->formRowClass);
		return $this;
	}

	/**
	 * Adds a new form row class
	 *
	 * This method will add a new CSS class to the element. Classes are
	 * automatically made unique.
	 *
	 * @param  string $className  the CSS class
	 * @return string             string of all current classes
	 */
	protected function addClassHelper($toAdd, $current) {
		$classes = $this->getClassList($current.' '.$toAdd);
		return implode(' ', $classes);
	}

	/**
	 * @param  string $className  the CSS class
	 * @return string             string of all current classes
	 */
	protected function removeClassHelper($toRemove, $current) {
		$toRemove = $this->getClassList($toRemove);
		$classes  = $this->getClassList($current);

		foreach ($toRemove as $removeMe) {
			$pos = array_search($removeMe, $classes);
			if ($pos !== false) unset($classes[$pos]);
		}

		return implode(' ', $classes);
	}

	/**
	 * Normalize class string
	 *
	 * Removes duplicates and extra spaces.
	 *
	 * @param  string $classString
	 * @return array
	 */
	private function getClassList($classString) {
		return array_unique(array_filter(array_map('trim', explode(' ', $classString))));
	}
}
Back to Top