PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/FSN/library/Yab/Form/Element.php

https://gitlab.com/r.collas/site_central
PHP | 360 lines | 195 code | 154 blank | 11 comment | 47 complexity | 3c137e5578e0823db6441f9646226d99 MD5 | raw file
  1. <?php
  2. /**
  3. * Yab Framework
  4. *
  5. * @category Yab_Form
  6. * @package Yab_Form_Element
  7. * @author Yann BELLUZZI
  8. * @copyright (c) 2010 YBellu
  9. * @license http://www.ybellu.com/yab-framework/license.html
  10. * @link http://www.ybellu.com/yab-framework
  11. */
  12. class Yab_Form_Element extends Yab_Object {
  13. private $_form = null;
  14. private $_errors = null;
  15. public function __construct(Yab_Form $form, array $attributes = array()) {
  16. $this->_form = $form;
  17. $this->populate($attributes);
  18. if(!$this->has('type'))
  19. $this->set('type', 'text');
  20. if($this->get('type') == 'file')
  21. $this->_form->set('enctype', 'multipart/form-data');
  22. if(!$this->has('value'))
  23. $this->set('value', '');
  24. if(!$this->has('check_value'))
  25. $this->set('check_value', 1);
  26. if(!$this->has('needed'))
  27. $this->set('needed', true);
  28. if(!$this->has('name'))
  29. throw new Yab_Exception('a form element must have a name attribute');
  30. if(!$this->has('id'))
  31. $this->set('id', $this->get('name'));
  32. if($this->isSubmitted()) {
  33. $name = $this->get('name');
  34. $datas = $this->_requestDatas();
  35. $this->set('value', $datas->has($name) ? $datas->get($name) : ($this->isMultiple() ? array() : ($this->get('type') == 'checkbox')?0:null));
  36. }
  37. }
  38. private function _requestDatas() {
  39. $request = Yab_Loader::getInstance()->getRequest();
  40. if($this->get('type') == 'file')
  41. return $request->getFile();
  42. if(preg_match('#post#i', $this->_form->get('method')))
  43. return $request->getPost();
  44. return $request->getGet();
  45. }
  46. public function isMultiple() {
  47. return $this->has('multiple') && $this->get('multiple', 'Bool');
  48. }
  49. public function isValid($value = null) {
  50. if($value !== null)
  51. return ((bool) (count($this->getValueErrors($value)) === 0));
  52. return ((bool) (count($this->getErrors()) === 0));
  53. }
  54. public function isSubmitted() {
  55. $name = $this->get('name');
  56. $datas = $this->_requestDatas();
  57. return $datas->has($name) || $datas->has('___'.$name.'___');
  58. }
  59. public function getErrors($filters = null) {
  60. if($this->_errors !== null)
  61. return $this->_errors;
  62. $this->_errors = $this->getValueErrors($this->getValue(), $filters);
  63. return $this->_errors;
  64. }
  65. public function getValueErrors($value, $filters = null) {
  66. $errors = array();
  67. $validators = $this->has('validators') ? $this->get('validators', 'Array') : array();
  68. $override_errors = $this->has('errors') ? $this->get('errors', 'Array') : array();
  69. if($this->has('options')) {
  70. $options = array();
  71. $options['options'] = $this->get('options');
  72. if($this->has('fake_options'))
  73. $options['fake_options'] = $this->get('fake_options');
  74. if($this->has('min_options')) {
  75. $options['min_options'] = $this->get('min_options', 'Int');
  76. } elseif($this->get('needed')) {
  77. $options['min_options'] = 1;
  78. }
  79. if($this->has('max_options'))
  80. $options['max_options'] = $this->get('max_options', 'Int');
  81. $validators['Options'] = $options;
  82. }
  83. $validator = Yab_Loader::getInstance('Yab_Validator_Factory');
  84. $validator->set('validators', $validators);
  85. $validator->set('errors', $override_errors);
  86. $validator->set('field', $this->has('label')?$this->get('label'):$this->get('id'));
  87. if($filters !== null)
  88. $validator->set('filters', $filters);
  89. $validator->validate($value);
  90. if($this->get('type') == 'file') {
  91. if($this->get('needed'))
  92. $errors = $validator->getErrors();
  93. } elseif($value || $this->get('needed')) {
  94. $errors = $validator->getErrors();
  95. }
  96. return $errors;
  97. }
  98. final public function getValue($filters = true) {
  99. return $this->get('value', $filters && $this->has('filters') ? $this->get('filters') : null);
  100. }
  101. final public function getForm() {
  102. return $this->_form;
  103. }
  104. final public function addClass($class) {
  105. $classes = $this->has('class') ? $this->get('class') : 'form-control';
  106. $classes = trim($classes);
  107. $classes = $classes ? preg_split('#\s+#', trim($classes)) : array();
  108. if(!in_array($class, $classes))
  109. array_push($classes, $class);
  110. return $this->set('class', implode(' ', $classes));
  111. }
  112. public function getHtml() {
  113. $filter = new Yab_Filter_Html();
  114. $type = $this->get('type');
  115. $this->addClass($type);
  116. if($this->isSubmitted() && !$this->isValid())
  117. $this->addClass('error');
  118. $render = '';
  119. switch($type) {
  120. case 'text' :
  121. case 'password' :
  122. case 'hidden' :
  123. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="'.$type.'"'.$this->getAttributesHtml().' value="'.$this->get('value', 'Html').'" /></div>';
  124. break;
  125. case 'date' :
  126. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><div class="input-group input-append date">';
  127. $render .= '<input type="'.$type.'"'.$this->getAttributesHtml().' value="'.$this->get('value', 'Html').'" />';
  128. $render .= '<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>';
  129. $render .= '</div></div>';
  130. break;
  131. case 'file' :
  132. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="'.$type.'"'.$this->getAttributesHtml().' value="" /></div>';
  133. break;
  134. case 'textarea' :
  135. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><textarea'.$this->getAttributesHtml().'>'.$this->get('value', 'Html').'</textarea></div>';
  136. break;
  137. case 'checkbox' :
  138. $id = $this->has('id') ? $this->get('id') : $this->get('name');
  139. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="hidden" name="___'.$this->get('name').'___" value="'.$this->get('check_value').'" /></div>';
  140. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="'.$type.'" id="'.$id.'" value="'.$this->get('check_value').'"'.$this->getAttributesHtml().($this->get('value') == $this->get('check_value') ? ' checked="checked"' : '').' /></div>';
  141. break;
  142. case 'groupbox' :
  143. case 'checkboxes' :
  144. case 'radio' :
  145. if($type != 'radio')
  146. $type = 'checkbox';
  147. $id = $this->has('id') ? $this->get('id') : $this->get('name');
  148. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="hidden" name="___'.$this->get('name').'___" value="1" /></div>';
  149. if($this->has('fake_options')) {
  150. foreach($this->get('fake_options') as $key => $value)
  151. $render .= '<span><label for="'.$id.$filter->filter($key).'">'.$filter->filter($value).'</label><input type="'.$type.'" id="'.$id.$filter->filter($key).'" value="'.$filter->filter($key).'"'.$this->getAttributesHtml().($this->_isSelected($key) ? ' checked="checked"' : '').' /></span>';
  152. }
  153. if($this->has('options')) {
  154. foreach($this->get('options') as $key => $value)
  155. $render .= '<span><label for="'.$id.$filter->filter($key).'">'.$filter->filter($value).'</label><input type="'.$type.'" id="'.$id.$filter->filter($key).'" value="'.$filter->filter($key).'"'.$this->getAttributesHtml().($this->_isSelected($key) ? ' checked="checked"' : '').' /></span>';
  156. }
  157. break;
  158. case 'select' :
  159. $render .= $this->isMultiple() ? '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><input type="hidden" name="___'.$this->get('name').'___" value="1" /></div>' : '';
  160. $render .= '<div class="col-lg-9 col-md-9 col-sm-6 col-xs-6"><select'.$this->getAttributesHtml().($this->isMultiple() ? ' multiple' : '').'>';
  161. $render .= $this->has('fake_options') ? $this->getOptionsHtml($this->get('fake_options')) : '';
  162. $render .= $this->has('options') ? $this->getOptionsHtml($this->get('options')) : '';
  163. $render .= '</select></div>';
  164. break;
  165. default :
  166. throw new Yab_Exception('"'.$type.'" is not a valid Yab_Form_Element type');
  167. break;
  168. }
  169. $pre_html = $this->has('pre_html') ? $this->get('pre_html') : '';
  170. $post_html = $this->has('post_html') ? $this->get('post_html') : '';
  171. return $pre_html.$render.$post_html;
  172. }
  173. public function getAttributesHtml() {
  174. $attributes = '';
  175. foreach($this->_attributes as $key => $value) {
  176. $key = strtolower(trim($key));
  177. if(is_object($value) || is_array($value) || strpos($key, '_') === 0 || in_array($key, array('needed', 'label', 'filters', 'validators', 'value', 'errors', 'check_value', 'tooltip', 'pre_html', 'post_html')))
  178. continue;
  179. if(in_array($key, array('readonly')) && !$value)
  180. continue;
  181. if($key == 'name' && $this->isMultiple())
  182. $value .= '[]';
  183. $html = new Yab_Filter_Html();
  184. $attributes .= ' '.$key.'="'.$html->filter($value).'"';
  185. }
  186. return $attributes;
  187. }
  188. protected function getOptionsHtml($options) {
  189. $filter = new Yab_Filter_Html();
  190. $render_options = '';
  191. foreach($options as $key => $value) {
  192. if(is_array($value)) {
  193. $render_options .= '<optgroup label="'.$filter->filter($key).'">'.$this->getOptionsHtml($value).'</optgroup>';
  194. } else {
  195. $render_options .= '<option value="'.$filter->filter($key).'"'.($this->_isSelected($key) ? ' selected="selected"' : '').'>'.$filter->filter($value).'</option>';
  196. }
  197. }
  198. return $render_options;
  199. }
  200. protected function _isSelected($value) {
  201. if($this->isMultiple())
  202. return in_array($value, $this->get('value', 'Array'));
  203. return (string )$value === (string) $this->get('value');
  204. }
  205. }
  206. // Do not clause PHP tags unless it is really necessary