| 21 | | |
|---|
| 22 | | //recupere le form |
|---|
| 23 | | $this->retrieveForm($form_id); |
|---|
| 24 | | |
|---|
| | 46 | } |
|---|
| | 47 | |
|---|
| | 48 | /** |
|---|
| | 49 | * Retrive the form Object from propel database |
|---|
| | 50 | * |
|---|
| | 51 | * @param integer|string The id or the name of the form |
|---|
| | 52 | * |
|---|
| | 53 | * @return SpyFormBuilder Object for form |
|---|
| | 54 | */ |
|---|
| | 55 | protected function retrieveForm($form_id=null){ |
|---|
| | 56 | if(is_null($form_id)) |
|---|
| | 57 | $form_id=$this->form_id; |
|---|
| | 58 | if(is_numeric($form_id)){ |
|---|
| | 59 | try{ |
|---|
| | 60 | $form_object=SpyFormBuilderPeer::retrieveByPK($form_id); |
|---|
| | 61 | if(!$form_object instanceof SpyFormBuilder){ |
|---|
| | 62 | throw new Exception('Impossible to find Form number '.$form_id); |
|---|
| | 63 | } |
|---|
| | 64 | }catch(Exception $e){ |
|---|
| | 65 | throw $e; |
|---|
| | 66 | } |
|---|
| | 67 | }else{ |
|---|
| | 68 | try{ |
|---|
| | 69 | $form_object=SpyFormBuilderPeer::retrieveByName($form_id); |
|---|
| | 70 | if(!$form_object instanceof SpyFormBuilder){ |
|---|
| | 71 | throw new Exception('Impossible to find Form by name '.$form_id); |
|---|
| | 72 | } |
|---|
| | 73 | }catch(Exception $e){ |
|---|
| | 74 | throw $e; |
|---|
| | 75 | } |
|---|
| | 76 | } |
|---|
| | 77 | return $form_object; |
|---|
| | 78 | } |
|---|
| | 79 | |
|---|
| | 80 | /** |
|---|
| | 81 | * Builde the form |
|---|
| | 82 | */ |
|---|
| | 83 | protected function init(){ |
|---|
| 41 | | if((!$field->getOnlyFor())||(in_array($app,$field->getOnlyFor()))){ |
|---|
| 42 | | if((!$field->getHideOnEdit())||(!$this->isInEditMode())||(!in_array($app,$field->getHideOnEdit()))){ |
|---|
| 43 | | |
|---|
| 44 | | $wclass=$this->all_fields[$field->getWidgetType()]['type']; |
|---|
| 45 | | $params=$field->getWidgetParams(); |
|---|
| 46 | | $options=(array_key_exists('options',$params))?$params['options']:array(); |
|---|
| 47 | | $attributes=(array_key_exists('attributes',$params))?$params['attributes']:array(); |
|---|
| 48 | | |
|---|
| 49 | | $widgets[$field->getName()]= new $wclass($options,$attributes); |
|---|
| 50 | | |
|---|
| 51 | | $labels[$field->getName()]=$field->getLabel(); |
|---|
| 52 | | |
|---|
| 53 | | if($field->getHelp()!=''){ |
|---|
| 54 | | $helps[$field->getName()]=$field->getHelp(); |
|---|
| 55 | | } |
|---|
| 56 | | |
|---|
| 57 | | //Liste des validations pour ce champ |
|---|
| 58 | | $validators=$field->getSpyFormBuilderValidatorss(); |
|---|
| 59 | | if(sizeof($validators)>1){ |
|---|
| 60 | | $valid=array(); |
|---|
| 61 | | foreach($validators as $validator){ |
|---|
| 62 | | $validator_class=$this->all_validators[$validator->getValidatorType()]['type']; |
|---|
| 63 | | $params=$validator->getValidatorParams(); |
|---|
| 64 | | |
|---|
| 65 | | $options=(array_key_exists('options',$params))?$params['options']:array(); |
|---|
| 66 | | $options['trim']=(array_key_exists('trim',$options))?$options['trim']:true; |
|---|
| 67 | | $options['required']=(array_key_exists('required',$options))?$options['required']:false; |
|---|
| 68 | | $valid[]=new $validator_class($options,array('invalid'=>$validator->getInvalidMsg())); |
|---|
| 69 | | } |
|---|
| 70 | | $valids[$field->getName()]=new sfValidatorAnd($valid); |
|---|
| 71 | | }elseif(sizeof($validators)==1){ |
|---|
| 72 | | $validator_class=$this->all_validators[$validators[0]->getValidatorType()]['type']; |
|---|
| 73 | | $params=$validators[0]->getValidatorParams(); |
|---|
| 74 | | if(!is_array($params)) |
|---|
| 75 | | $params=array(); |
|---|
| 76 | | $options=(array_key_exists('options',$params))?$params['options']:array(); |
|---|
| 77 | | $options['trim']=(array_key_exists('trim',$options))?$options['trim']:true; |
|---|
| 78 | | $options['required']=(array_key_exists('required',$options))?$options['required']:false; |
|---|
| 79 | | $attributes=(array_key_exists('attributes',$params))?$params['attributes']:array(); |
|---|
| 80 | | |
|---|
| 81 | | $valids[$field->getName()]=new $validator_class($options,array('invalid'=>$validators[0]->getInvalidMsg())); |
|---|
| 82 | | }else{ |
|---|
| 83 | | $valids[$field->getName()]=new sfValidatorNone(); |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | }//End hideOnEdit |
|---|
| 87 | | }//end only_for |
|---|
| 88 | | |
|---|
| 89 | | } |
|---|
| 90 | | |
|---|
| | 93 | if($this->haveToShowField($field)){ |
|---|
| | 94 | $this->prepareField($field); |
|---|
| | 95 | } |
|---|
| | 96 | } |
|---|
| 127 | | |
|---|
| | 126 | |
|---|
| | 127 | |
|---|
| | 128 | /** |
|---|
| | 129 | * Check the only for Criteria |
|---|
| | 130 | * @param SpyFormBuilderFields the field object |
|---|
| | 131 | * |
|---|
| | 132 | * @return boolean is the field allowed in this context |
|---|
| | 133 | */ |
|---|
| | 134 | protected function checkOnlyFor(SpyFormBuilderFields $field){ |
|---|
| | 135 | return ((!$field->getOnlyFor())||(in_array($this->getCurrentApp(),$field->getOnlyFor()))); |
|---|
| | 136 | } |
|---|
| | 137 | |
|---|
| | 138 | /** |
|---|
| | 139 | * Check the hide on Edit Criteria |
|---|
| | 140 | * @param SpyFormBuilderFields the field object |
|---|
| | 141 | * |
|---|
| | 142 | * @return boolean is the field allowed in this context |
|---|
| | 143 | */ |
|---|
| | 144 | protected function checkHideOnEdit(SpyFormBuilderFields $field){ |
|---|
| | 145 | return ((!$field->getHideOnEdit())||(!$this->isInEditMode())||(!in_array($this->getCurrentApp(),$field->getHideOnEdit()))); |
|---|
| | 146 | } |
|---|
| | 147 | |
|---|
| | 148 | /** |
|---|
| | 149 | * Check if we are in an editing mode |
|---|
| | 150 | * |
|---|
| | 151 | * @return boolean sizeof($this->datas)>0 |
|---|
| | 152 | */ |
|---|
| 131 | | protected function doPostActions(){ |
|---|
| 132 | | $this->actions=$this->form_object->getSpyFormBuilderActions(); |
|---|
| 133 | | foreach($this->actions as $action){ |
|---|
| 134 | | //Construit l'action |
|---|
| 135 | | $action_class=$this->all_actions[$action->getActionType()]['type']; |
|---|
| 136 | | $params=$action->getActionParams(); |
|---|
| | 156 | |
|---|
| | 157 | /** |
|---|
| | 158 | * Check if we need to render the field in this context |
|---|
| | 159 | * @param SpyFormBuilderFields the field object |
|---|
| | 160 | * |
|---|
| | 161 | * @return boolean is the field allowed in this context |
|---|
| | 162 | */ |
|---|
| | 163 | protected function haveToShowField(SpyFormBuilderFields $field){ |
|---|
| | 164 | return ($this->checkOnlyFor($field) && $this->checkHideOnEdit($field)); |
|---|
| | 165 | } |
|---|
| | 166 | /** |
|---|
| | 167 | * Return the name of the current sf application |
|---|
| | 168 | * |
|---|
| | 169 | * @return string name of the app |
|---|
| | 170 | */ |
|---|
| | 171 | public function getCurrentApp(){ |
|---|
| | 172 | return sfContext::getInstance()->getConfiguration()->getApplication(); |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | /** |
|---|
| | 176 | * Prepare to render the field |
|---|
| | 177 | * |
|---|
| | 178 | * @param SpyFormBuilderFields the field object |
|---|
| | 179 | */ |
|---|
| | 180 | protected function prepareField(SpyFormBuilderFields $field){ |
|---|
| | 181 | $wclass=$this->all_fields[$field->getWidgetType()]['type']; |
|---|
| | 182 | |
|---|
| | 183 | $params=$field->getWidgetParams(); |
|---|
| | 184 | |
|---|
| | 185 | $options=(array_key_exists('options',$params))?$params['options']:array(); |
|---|
| | 186 | $attributes=(array_key_exists('attributes',$params))?$params['attributes']:array(); |
|---|
| | 187 | |
|---|
| | 188 | $this->setWidget($field->getName(),new $wclass($options,$attributes)); |
|---|
| | 189 | $this->setLabel($field->getName(),$field->getLabel()); |
|---|
| | 190 | |
|---|
| | 191 | if($field->getHelp()!='') |
|---|
| | 192 | $this->setHelp($field->getName(),$field->getHelp()); |
|---|
| | 193 | |
|---|
| | 194 | //Liste des validations pour ce champ |
|---|
| | 195 | $validators=$field->getSpyFormBuilderValidatorss(); |
|---|
| | 196 | |
|---|
| | 197 | $this->prepareValidators($field,$validators); |
|---|
| | 198 | |
|---|
| | 199 | //Edit Mode |
|---|
| | 200 | if($this->isInEditMode()) |
|---|
| | 201 | $this->formulaire->setDefaults($this->datas); |
|---|
| | 202 | |
|---|
| | 203 | } |
|---|
| | 204 | |
|---|
| | 205 | /** |
|---|
| | 206 | * Prepare to render the validator field |
|---|
| | 207 | * |
|---|
| | 208 | * @param SpyFormBuilderFields the field object |
|---|
| | 209 | * @param Array of SpyFormBuilderValidators the validators Objects |
|---|
| | 210 | */ |
|---|
| | 211 | protected function prepareValidators(SpyFormBuilderFields $field, $validators=array()){ |
|---|
| | 212 | if(sizeof($validators)>1){ |
|---|
| | 213 | $valid=array(); |
|---|
| | 214 | foreach($validators as $validator){ |
|---|
| | 215 | $valid[]=$this->prepareValidator($validator); |
|---|
| | 216 | } |
|---|
| | 217 | $this->valids[$field->getName()]=new sfValidatorAnd($valid); |
|---|
| | 218 | }elseif(sizeof($validators)==1){ |
|---|
| | 219 | $valids[$field->getName()]=$this->prepareValidator($validators[0]); |
|---|
| | 220 | }else{ |
|---|
| | 221 | $valids[$field->getName()]=new sfValidatorNone(); |
|---|
| | 222 | } |
|---|
| | 223 | } |
|---|
| | 224 | |
|---|
| | 225 | /** |
|---|
| | 226 | * Prepare one validator class |
|---|
| | 227 | * |
|---|
| | 228 | * @param SpyFormBuilderValidators the validator Object |
|---|
| | 229 | * |
|---|
| | 230 | * @return sfValidatorBase |
|---|
| | 231 | */ |
|---|
| | 232 | protected function prepareValidator($validator){ |
|---|
| | 233 | $validator_class=$this->all_validators[$validator->getValidatorType()]['type']; |
|---|
| | 234 | $params=$validator->getValidatorParams(); |
|---|
| | 235 | |
|---|
| | 236 | if(is_array($params)) |
|---|
| 138 | | |
|---|
| 139 | | |
|---|
| 140 | | $myAction=new $action_class($options, $this->formulaire->getValues(),$this); |
|---|
| 141 | | $myAction->execute(); |
|---|
| 142 | | } |
|---|
| 143 | | } |
|---|
| | 238 | else |
|---|
| | 239 | $options=array(); |
|---|
| | 240 | |
|---|
| | 241 | $options['trim']=(array_key_exists('trim',$options))?$options['trim']:true; |
|---|
| | 242 | $options['required']=(array_key_exists('required',$options))?$options['required']:false; |
|---|
| | 243 | |
|---|
| | 244 | return new $validator_class($options,array('invalid'=>$validator->getInvalidMsg())); |
|---|
| | 245 | } |
|---|
| | 246 | |
|---|
| | 247 | /** |
|---|
| | 248 | * Set A widget |
|---|
| | 249 | * |
|---|
| | 250 | * @param string name of field |
|---|
| | 251 | * @param sfWidgetForm A widget form element |
|---|
| | 252 | */ |
|---|
| | 253 | protected function setWidget($name, sfWidgetForm $widget){ |
|---|
| | 254 | $this->widgets[$name]=$widget; |
|---|
| | 255 | } |
|---|
| | 256 | |
|---|
| | 257 | /** |
|---|
| | 258 | * Set A Label |
|---|
| | 259 | * |
|---|
| | 260 | * @param string name of field |
|---|
| | 261 | * @param string the label |
|---|
| | 262 | */ |
|---|
| | 263 | protected function setLabel($name, $label){ |
|---|
| | 264 | $this->labels[$name]=$label; |
|---|
| | 265 | } |
|---|
| | 266 | |
|---|
| | 267 | /** |
|---|
| | 268 | * Set Help message for field |
|---|
| | 269 | * |
|---|
| | 270 | * @param string name of field |
|---|
| | 271 | * @param string the help message |
|---|
| | 272 | */ |
|---|
| | 273 | protected function setHelp($name, $help){ |
|---|
| | 274 | $this->helps[$name]=$help; |
|---|
| | 275 | } |
|---|
| | 276 | |
|---|
| | 277 | /** |
|---|
| | 278 | * Some of post Actions have one pré action like storeInPropel to retireve the datas |
|---|
| | 279 | */ |
|---|
| 155 | | $myAction->preExecute(); |
|---|
| 156 | | } |
|---|
| 157 | | } |
|---|
| 158 | | public function render(){ |
|---|
| 159 | | $form=$this->formulaire; |
|---|
| | 291 | if($myAction->checkAll()) |
|---|
| | 292 | $myAction->preExecute(); |
|---|
| | 293 | } |
|---|
| | 294 | } |
|---|
| | 295 | |
|---|
| | 296 | /* |
|---|
| | 297 | * Liste of action do an eecution after the validation of the form |
|---|
| | 298 | */ |
|---|
| | 299 | protected function doPostActions(){ |
|---|
| | 300 | $this->actions=$this->form_object->getSpyFormBuilderActions(); |
|---|
| | 301 | foreach($this->actions as $action){ |
|---|
| | 302 | //Construit l'action |
|---|
| | 303 | $action_class=$this->all_actions[$action->getActionType()]['type']; |
|---|
| | 304 | $params=$action->getActionParams(); |
|---|
| | 305 | if(is_array($params)) |
|---|
| | 306 | $options=(array_key_exists('options',$params))?$params['options']:array(); |
|---|
| | 307 | |
|---|
| | 308 | |
|---|
| | 309 | $myAction=new $action_class($options, $this->formulaire->getValues(),$this); |
|---|
| | 310 | if($myAction->checkAll()) |
|---|
| | 311 | $myAction->execute(); |
|---|
| | 312 | } |
|---|
| | 313 | } |
|---|
| | 314 | |
|---|
| | 315 | /** |
|---|
| | 316 | * Get the url to render the form |
|---|
| | 317 | * |
|---|
| | 318 | * @return string of the url to post |
|---|
| | 319 | */ |
|---|
| | 320 | protected function getUrlForForm(){ |
|---|
| | 321 | //Module part |
|---|