Field.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. 'size',
  57. 'rows',
  58. 'cols',
  59. 'min',
  60. 'max',
  61. 'class',
  62. 'pattern'
  63. );
  64. /* defines additional data, that are allowed for fields */
  65. private $helpers = array(
  66. 'help',
  67. 'description'
  68. );
  69. public function __construct($fieldName, array $fieldConfigs)
  70. {
  71. $this->setName($fieldName);
  72. $type = isset($fieldConfigs['type']) ? $fieldConfigs['type'] : false;
  73. $this->setType($type);
  74. $label = isset($fieldConfigs['label']) ? $fieldConfigs['label'] : false;
  75. $this->setLabel($label);
  76. $checkboxlabel = isset($fieldConfigs['checkboxlabel']) ? $fieldConfigs['checkboxlabel'] : false;
  77. $this->setCheckboxLabel($checkboxlabel);
  78. $options = isset($fieldConfigs['options']) ? $fieldConfigs['options'] : array();
  79. $this->setOptions($options);
  80. $this->setAttributes($fieldConfigs);
  81. $this->setAttributeValues($fieldConfigs);
  82. $this->setHelpers($fieldConfigs);
  83. }
  84. private function setName($name)
  85. {
  86. $this->name = $name;
  87. }
  88. public function getName()
  89. {
  90. return $this->name;
  91. }
  92. private function setType($type)
  93. {
  94. if(in_array($type, $this->types))
  95. {
  96. $this->type = $type;
  97. }
  98. }
  99. public function getType()
  100. {
  101. return $this->type;
  102. }
  103. public function setLabel($label)
  104. {
  105. $this->label = $label;
  106. }
  107. public function getLabel()
  108. {
  109. return $this->label;
  110. }
  111. public function setCheckboxLabel($label)
  112. {
  113. $this->checkboxLabel = $label;
  114. }
  115. public function getCheckboxLabel()
  116. {
  117. return $this->checkboxLabel;
  118. }
  119. public function setContent($content)
  120. {
  121. $this->content = $content;
  122. }
  123. public function getContent()
  124. {
  125. return $this->content;
  126. }
  127. private function setOptions(array $options)
  128. {
  129. foreach($options as $key => $value)
  130. {
  131. $this->options[$key] = $value;
  132. }
  133. }
  134. public function getOptions()
  135. {
  136. if(isset($this->options))
  137. {
  138. return $this->options;
  139. }
  140. return false;
  141. }
  142. private function setAttributes($fieldConfigs)
  143. {
  144. foreach($fieldConfigs as $key => $value)
  145. {
  146. if(is_string($key) && in_array($key, $this->attr))
  147. {
  148. $this->attributes[$key] = $value;
  149. }
  150. }
  151. }
  152. /* get all attributes of the field and return them as a string. For usage in templates */
  153. public function getAttributes()
  154. {
  155. $string = false;
  156. foreach($this->attributes as $key => $attribute)
  157. {
  158. $string .= ' ' . $key;
  159. }
  160. return $string;
  161. }
  162. /* set a single attribute. Used e.g. in controller to change the value */
  163. public function setAttribute($key, $value)
  164. {
  165. $this->attributes[$key] = $value;
  166. }
  167. public function unsetAttribute($key)
  168. {
  169. unset($this->attributes[$key]);
  170. }
  171. /* get a single attribute, if it is defined. For usage in templates like getAttribute('required') */
  172. public function getAttribute($key)
  173. {
  174. if(isset($this->attributes[$key]))
  175. {
  176. return $this->attributes[$key];
  177. }
  178. return false;
  179. }
  180. private function setAttributeValues($fieldConfigs)
  181. {
  182. foreach($fieldConfigs as $key => $value)
  183. {
  184. if(is_string($key) && in_array($key, $this->attrValues))
  185. {
  186. $this->attributeValues[$key] = $value;
  187. }
  188. }
  189. }
  190. /* get all attributes as string. For usage in template */
  191. public function getAttributeValues()
  192. {
  193. $string = false;
  194. foreach($this->attributeValues as $key => $attribute)
  195. {
  196. $string .= ' ' . $key . '="' . $attribute . '"';
  197. }
  198. return $string;
  199. }
  200. public function setAttributeValue($key, $value)
  201. {
  202. /* pretty dirty, but you should not add a value for a simple checkbox */
  203. if($key == 'value' && $this->type == 'checkbox')
  204. {
  205. return;
  206. }
  207. $this->attributeValues[$key] = $value;
  208. }
  209. public function getAttributeValue($key)
  210. {
  211. if(isset($this->attributeValues[$key]))
  212. {
  213. return $this->attributeValues[$key];
  214. }
  215. return false;
  216. }
  217. public function setHelpers($fieldConfigs)
  218. {
  219. foreach($fieldConfigs as $key => $config)
  220. {
  221. if(is_string($key) && in_array($key, $this->helpers))
  222. {
  223. $this->$key = $config;
  224. }
  225. }
  226. }
  227. public function getHelper($helperName)
  228. {
  229. if(isset($this->$helperName))
  230. {
  231. return $this->$helperName;
  232. }
  233. return false;
  234. }
  235. }