Field.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace Typemill\Models;
  3. class Field
  4. {
  5. private $type;
  6. private $label;
  7. private $name;
  8. private $content;
  9. /* holds all simple attributes for this field like "required" */
  10. private $attributes = array();
  11. /* holds all attribute value pairs for this field like "id=''" */
  12. private $attributeValues = array();
  13. /* holds all options for this field (e.g. select options) */
  14. private $options = array();
  15. /* defines all field types, that are allowed */
  16. private $types = array(
  17. 'checkbox',
  18. 'checkboxlist',
  19. 'color',
  20. 'date',
  21. 'datetime',
  22. 'datetime-local',
  23. 'email',
  24. 'file',
  25. 'hidden',
  26. 'image',
  27. 'month',
  28. 'number',
  29. 'password',
  30. 'radio',
  31. 'range',
  32. 'tel',
  33. 'text',
  34. 'time',
  35. 'url',
  36. 'week',
  37. 'textarea',
  38. 'select',
  39. 'paragraph'
  40. );
  41. /* defines all boolean attributes, that are allowed for fields */
  42. private $attr = array(
  43. 'autofocus',
  44. 'checked',
  45. 'disabled',
  46. 'formnovalidate',
  47. 'multiple',
  48. 'readonly',
  49. 'required'
  50. );
  51. /* defines all attribute value paires, that are allowed for fields */
  52. private $attrValues = array(
  53. 'id',
  54. 'autocomplete',
  55. 'placeholder',
  56. 'maxlength',
  57. 'size',
  58. 'rows',
  59. 'cols',
  60. 'min',
  61. 'max',
  62. 'class',
  63. 'pattern'
  64. );
  65. /* defines additional data, that are allowed for fields */
  66. private $helpers = array(
  67. 'help',
  68. 'description'
  69. );
  70. public function __construct($fieldName, array $fieldConfigs)
  71. {
  72. $this->setName($fieldName);
  73. $type = isset($fieldConfigs['type']) ? $fieldConfigs['type'] : false;
  74. $this->setType($type);
  75. $label = isset($fieldConfigs['label']) ? $fieldConfigs['label'] : false;
  76. $this->setLabel($label);
  77. $checkboxlabel = isset($fieldConfigs['checkboxlabel']) ? $fieldConfigs['checkboxlabel'] : false;
  78. $this->setCheckboxLabel($checkboxlabel);
  79. $options = isset($fieldConfigs['options']) ? $fieldConfigs['options'] : array();
  80. $this->setOptions($options);
  81. $this->setAttributes($fieldConfigs);
  82. $this->setAttributeValues($fieldConfigs);
  83. $this->setHelpers($fieldConfigs);
  84. }
  85. private function setName($name)
  86. {
  87. $this->name = $name;
  88. }
  89. public function getName()
  90. {
  91. return $this->name;
  92. }
  93. private function setType($type)
  94. {
  95. if(in_array($type, $this->types))
  96. {
  97. $this->type = $type;
  98. }
  99. }
  100. public function getType()
  101. {
  102. return $this->type;
  103. }
  104. public function setLabel($label)
  105. {
  106. $this->label = $label;
  107. }
  108. public function getLabel()
  109. {
  110. return $this->label;
  111. }
  112. public function setCheckboxLabel($label)
  113. {
  114. $this->checkboxLabel = $label;
  115. }
  116. public function getCheckboxLabel()
  117. {
  118. return $this->checkboxLabel;
  119. }
  120. public function setContent($content)
  121. {
  122. $this->content = $content;
  123. }
  124. public function getContent()
  125. {
  126. return $this->content;
  127. }
  128. private function setOptions(array $options)
  129. {
  130. foreach($options as $key => $value)
  131. {
  132. $this->options[$key] = $value;
  133. }
  134. }
  135. public function getOptions()
  136. {
  137. if(isset($this->options))
  138. {
  139. return $this->options;
  140. }
  141. return false;
  142. }
  143. private function setAttributes($fieldConfigs)
  144. {
  145. foreach($fieldConfigs as $key => $value)
  146. {
  147. if(is_string($key) && in_array($key, $this->attr))
  148. {
  149. $this->attributes[$key] = $value;
  150. }
  151. }
  152. }
  153. /* get all attributes of the field and return them as a string. For usage in templates */
  154. public function getAttributes()
  155. {
  156. $string = false;
  157. foreach($this->attributes as $key => $attribute)
  158. {
  159. $string .= ' ' . $key;
  160. }
  161. return $string;
  162. }
  163. /* set a single attribute. Used e.g. in controller to change the value */
  164. public function setAttribute($key, $value)
  165. {
  166. $this->attributes[$key] = $value;
  167. }
  168. public function unsetAttribute($key)
  169. {
  170. unset($this->attributes[$key]);
  171. }
  172. /* get a single attribute, if it is defined. For usage in templates like getAttribute('required') */
  173. public function getAttribute($key)
  174. {
  175. if(isset($this->attributes[$key]))
  176. {
  177. return $this->attributes[$key];
  178. }
  179. return false;
  180. }
  181. private function setAttributeValues($fieldConfigs)
  182. {
  183. foreach($fieldConfigs as $key => $value)
  184. {
  185. if(is_string($key) && in_array($key, $this->attrValues))
  186. {
  187. $this->attributeValues[$key] = $value;
  188. }
  189. }
  190. }
  191. /* get all attributes as string. For usage in template */
  192. public function getAttributeValues()
  193. {
  194. $string = false;
  195. foreach($this->attributeValues as $key => $attribute)
  196. {
  197. $string .= ' ' . $key . '="' . $attribute . '"';
  198. }
  199. return $string;
  200. }
  201. public function setAttributeValue($key, $value)
  202. {
  203. /* pretty dirty, but you should not add a value for a simple checkbox */
  204. if($key == 'value' && $this->type == 'checkbox')
  205. {
  206. return;
  207. }
  208. $this->attributeValues[$key] = $value;
  209. }
  210. public function getAttributeValue($key)
  211. {
  212. if(isset($this->attributeValues[$key]))
  213. {
  214. return $this->attributeValues[$key];
  215. }
  216. return false;
  217. }
  218. public function setHelpers($fieldConfigs)
  219. {
  220. foreach($fieldConfigs as $key => $config)
  221. {
  222. if(is_string($key) && in_array($key, $this->helpers))
  223. {
  224. $this->$key = $config;
  225. }
  226. }
  227. }
  228. public function getHelper($helperName)
  229. {
  230. if(isset($this->$helperName))
  231. {
  232. return $this->$helperName;
  233. }
  234. return false;
  235. }
  236. }