123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace Typemill\Models;
- class Field
- {
- private $type;
-
- private $label;
-
- private $name;
-
- private $content;
-
- /* holds all simple attributes for this field like "required" */
- private $attributes = array();
-
- /* holds all attribute value pairs for this field like "id=''" */
- private $attributeValues = array();
-
- /* holds all options for this field (e.g. select options) */
- private $options = array();
-
- /* defines all field types, that are allowed */
- private $types = array(
- 'checkbox',
- 'checkboxlist',
- 'color',
- 'date',
- 'datetime',
- 'datetime-local',
- 'email',
- 'file',
- 'hidden',
- 'image',
- 'month',
- 'number',
- 'password',
- 'radio',
- 'range',
- 'tel',
- 'text',
- 'time',
- 'url',
- 'week',
- 'textarea',
- 'select',
- 'paragraph'
- );
-
- /* defines all boolean attributes, that are allowed for fields */
- private $attr = array(
- 'autofocus',
- 'checked',
- 'disabled',
- 'formnovalidate',
- 'multiple',
- 'readonly',
- 'required'
- );
- /* defines all attribute value paires, that are allowed for fields */
- private $attrValues = array(
- 'id',
- 'autocomplete',
- 'placeholder',
- 'maxlength',
- 'size',
- 'rows',
- 'cols',
- 'min',
- 'max',
- 'class',
- 'pattern'
- );
-
- /* defines additional data, that are allowed for fields */
- private $helpers = array(
- 'help',
- 'description'
- );
-
- public function __construct($fieldName, array $fieldConfigs)
- {
- $this->setName($fieldName);
-
- $type = isset($fieldConfigs['type']) ? $fieldConfigs['type'] : false;
- $this->setType($type);
- $label = isset($fieldConfigs['label']) ? $fieldConfigs['label'] : false;
- $this->setLabel($label);
- $checkboxlabel = isset($fieldConfigs['checkboxlabel']) ? $fieldConfigs['checkboxlabel'] : false;
- $this->setCheckboxLabel($checkboxlabel);
-
- $options = isset($fieldConfigs['options']) ? $fieldConfigs['options'] : array();
- $this->setOptions($options);
-
- $this->setAttributes($fieldConfigs);
-
- $this->setAttributeValues($fieldConfigs);
-
- $this->setHelpers($fieldConfigs);
- }
- private function setName($name)
- {
- $this->name = $name;
- }
- public function getName()
- {
- return $this->name;
- }
-
- private function setType($type)
- {
- if(in_array($type, $this->types))
- {
- $this->type = $type;
- }
- }
-
- public function getType()
- {
- return $this->type;
- }
-
- public function setLabel($label)
- {
- $this->label = $label;
- }
- public function getLabel()
- {
- return $this->label;
- }
- public function setCheckboxLabel($label)
- {
- $this->checkboxLabel = $label;
- }
- public function getCheckboxLabel()
- {
- return $this->checkboxLabel;
- }
-
- public function setContent($content)
- {
- $this->content = $content;
- }
- public function getContent()
- {
- return $this->content;
- }
-
- private function setOptions(array $options)
- {
- foreach($options as $key => $value)
- {
- $this->options[$key] = $value;
- }
- }
-
- public function getOptions()
- {
- if(isset($this->options))
- {
- return $this->options;
- }
- return false;
- }
-
- private function setAttributes($fieldConfigs)
- {
- foreach($fieldConfigs as $key => $value)
- {
- if(is_string($key) && in_array($key, $this->attr))
- {
- $this->attributes[$key] = $value;
- }
- }
- }
-
- /* get all attributes of the field and return them as a string. For usage in templates */
- public function getAttributes()
- {
- $string = false;
-
- foreach($this->attributes as $key => $attribute)
- {
- $string .= ' ' . $key;
- }
-
- return $string;
- }
-
- /* set a single attribute. Used e.g. in controller to change the value */
- public function setAttribute($key, $value)
- {
- $this->attributes[$key] = $value;
- }
-
- public function unsetAttribute($key)
- {
- unset($this->attributes[$key]);
- }
-
- /* get a single attribute, if it is defined. For usage in templates like getAttribute('required') */
- public function getAttribute($key)
- {
- if(isset($this->attributes[$key]))
- {
- return $this->attributes[$key];
- }
-
- return false;
- }
-
- private function setAttributeValues($fieldConfigs)
- {
- foreach($fieldConfigs as $key => $value)
- {
- if(is_string($key) && in_array($key, $this->attrValues))
- {
- $this->attributeValues[$key] = $value;
- }
- }
- }
- /* get all attributes as string. For usage in template */
- public function getAttributeValues()
- {
- $string = false;
-
- foreach($this->attributeValues as $key => $attribute)
- {
- $string .= ' ' . $key . '="' . $attribute . '"';
- }
-
- return $string;
- }
- public function setAttributeValue($key, $value)
- {
- /* pretty dirty, but you should not add a value for a simple checkbox */
- if($key == 'value' && $this->type == 'checkbox')
- {
- return;
- }
- $this->attributeValues[$key] = $value;
- }
-
- public function getAttributeValue($key)
- {
- if(isset($this->attributeValues[$key]))
- {
- return $this->attributeValues[$key];
- }
-
- return false;
- }
-
- public function setHelpers($fieldConfigs)
- {
- foreach($fieldConfigs as $key => $config)
- {
- if(is_string($key) && in_array($key, $this->helpers))
- {
- $this->$key = $config;
- }
- }
- }
-
- public function getHelper($helperName)
- {
- if(isset($this->$helperName))
- {
- return $this->$helperName;
- }
- return false;
- }
- }
|