PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/yiiext/components/form/EFormModelBehavior.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 204 lines | 137 code | 5 blank | 62 comment | 11 complexity | d0211720d7d9565d09ce575ad7657c58 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * EFormModelBehavior class file.
  4. *
  5. * @author Veaceslav Medvedev <slavcopost@gmail.com>
  6. * @link http://code.google.com/p/yiiext/
  7. * @license http://www.opensource.org/licenses/mit-license.php
  8. */
  9. /**
  10. * EFormModelBehavior attach instance of EForm for model for easy create form.
  11. *
  12. * Also add helper to generate array of default text elements based of {@link CModel::getAttributes()}.
  13. *
  14. * @author Maxim Furtuna (Ekstazi)
  15. * @version 0.4
  16. * @package yiiext.components.form
  17. */
  18. require_once(dirname(__FILE__).'/EForm.php');
  19. class EFormModelBehavior extends CModelBehavior
  20. {
  21. /**
  22. * @var array initial configuration for form.
  23. */
  24. public $config=array();
  25. /**
  26. * @var EForm main object
  27. */
  28. protected $_form;
  29. protected $_coreButtonTypes=array(
  30. 'htmlButton', // a normal button generated using CHtml::htmlButton
  31. 'htmlReset', // a reset button generated using CHtml::htmlButton
  32. 'htmlSubmit', // a submit button generated using CHtml::htmlButton
  33. 'submit', // a submit button generated using CHtml::submitButton
  34. 'button', // a normal button generated using CHtml::button
  35. 'image', // an image button generated using CHtml::imageButton
  36. 'reset', // a reset button generated using CHtml::resetButton
  37. 'link', // a link button generated using CHtml::linkButton
  38. );
  39. private $_firstButton;
  40. public $id;
  41. public $ajaxValidation=true;
  42. /**
  43. * Get main form.
  44. * @return EForm
  45. */
  46. public function getForm()
  47. {
  48. if($this->_form===null)
  49. {
  50. $o=$this->getOwner();
  51. $this->config=array_merge($this->config,$this->getFormElements());
  52. $this->_form=new EForm($this->config,$o,null);
  53. if(!isset($this->id))
  54. $this->id=sprintf('%x',crc32(serialize(array_keys($this->_form->getElements()->toArray())).$o->scenario));
  55. $this->_form->id=$this->id;
  56. $this->_form->activeForm=array_merge($this->_form->activeForm,array(
  57. 'enableAjaxValidation'=>$this->ajaxValidation,
  58. 'id'=>$this->id
  59. ));
  60. }
  61. return $this->_form;
  62. }
  63. /**
  64. * Render form.
  65. */
  66. public function render()
  67. {
  68. return $this->getForm()->render();
  69. }
  70. /**
  71. * Get array elements based of {@link CModel::getAttributes()}.
  72. * Type of all elements will be text.
  73. * @todo generate other types for inputs
  74. * @return array
  75. */
  76. public function getFormElements()
  77. {
  78. $model=$this->getOwner();
  79. if(method_exists($model,'getFormElements'))
  80. $description=$model->getFormElements();
  81. else
  82. {
  83. $description=array();
  84. foreach($model->getAttributes() as $attribute=>$value)
  85. $description[$attribute]=array(
  86. 'type'=>'text',
  87. );
  88. }
  89. $elements=array();
  90. $buttons=array();
  91. foreach($description as $element=>$config)
  92. {
  93. if(is_array($config))
  94. {
  95. if(!isset($config['type']))
  96. $config['type']='text';
  97. if(preg_match('~^(button|element|input)\.(.+)$~i',$config['type'],$regs))
  98. {
  99. $config['type']=$regs[2];
  100. if(!strcasecmp($regs[1],'button'))
  101. $buttons[$element]=$config;
  102. else
  103. $elements[$element]=$config;
  104. }elseif(in_array($config['type'],$this->_coreButtonTypes))
  105. {
  106. $buttons[$element]=$config;
  107. }else
  108. $elements[$element]=$config;
  109. }else
  110. $elements[$element]=$config;
  111. }
  112. if(empty($buttons))
  113. {
  114. $isBaseAr=($model instanceof CActiveRecord && in_array($model->scenario,array('insert','update','search')));
  115. $buttons=array(
  116. 'submit'=>array(
  117. 'type'=>'submit',
  118. 'label'=>Yii::t('yiiext',$isBaseAr ? 'Save' : 'Submit'),
  119. 'on'=>$isBaseAr ? 'insert,update' : null,
  120. ),
  121. 'reset'=>array(
  122. 'type'=>'reset',
  123. 'label'=>Yii::t('yiiext','Clear'),
  124. 'on'=>$isBaseAr ? 'insert,update' : null,
  125. ),
  126. );
  127. if($isBaseAr)
  128. {
  129. $buttons['search']=array(
  130. 'type'=>'submit',
  131. 'label'=>Yii::t('yiiext','Search'),
  132. 'on'=>'search'
  133. );
  134. }
  135. }
  136. reset($buttons);
  137. $this->_firstButton=key($buttons);
  138. return array(
  139. 'elements'=>$elements,
  140. 'buttons'=>$buttons,
  141. );
  142. }
  143. /**
  144. *
  145. * @return bool status of using ajax validation
  146. */
  147. public function getAjaxValidation()
  148. {
  149. return $this->getForm()->activeForm['enableAjaxValidation'];
  150. }
  151. /**
  152. * Performs check for ajax request and generate responce of validation status
  153. * @return true to allow use in if condition
  154. */
  155. protected function performAjax()
  156. {
  157. $ajaxVar=strcasecmp($this->getForm()->method,'get') ? $_POST['ajax'] : $_GET['ajax'];
  158. if($this->getAjaxValidation()&&isset($ajaxVar)&&$ajaxVar===$this->getForm()->activeForm['id']){
  159. $this->getForm()->loadData();
  160. // because of renderPartial method needs to clean output buffer
  161. ob_get_clean();
  162. echo CActiveForm::validate($this->getOwner(),null,false);
  163. Yii::app()->end();
  164. }
  165. return true;
  166. }
  167. /**
  168. * Performs check for submission, then process ajax request and
  169. * load attributes into model
  170. * @param string $button name of clicked button
  171. * @return boolean validation status
  172. */
  173. public function submitted($button=null)
  174. {
  175. $form=$this->getForm();
  176. $button=isset($button) ? $button : $this->_firstButton;
  177. return $this->performAjax()&&$form->submitted($button);
  178. }
  179. /**
  180. * Performs Performs check for submission, then process ajax request,
  181. * load attributes into model and save it
  182. * @param string $button name of clicked button
  183. * @return boolean saving status
  184. */
  185. public function saved($button=null)
  186. {
  187. return $this->submitted($button)&&$this->getOwner()->save();
  188. }
  189. /**
  190. * Performs Performs check for submission, then process ajax request,
  191. * load attributes into model and validate it
  192. * @param string $button name of clicked button
  193. * @return boolean validation status
  194. */
  195. public function validated($button=null)
  196. {
  197. return $this->submitted($button)&&$this->getOwner()->validate();
  198. }
  199. }