Development

Changeset 16355

You must first sign up to be able to contribute.

Changeset 16355

Show
Ignore:
Timestamp:
03/17/09 11:45:07 (4 years ago)
Author:
lombardot
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/spyFormBuilderInterface2Plugin/config/spy_form_actions.yml

    r16349 r16355  
    7878                cols: 105 
    7979                rows: 20 
     80      conditionnal_actions_end: 
     81        name: End a group of action do with conditionnals 
     82        type: spyActionConditionnalEnd 
    8083               
  • plugins/spyFormBuilderInterface2Plugin/lib/action/spyFormActionBase.class.php

    r16349 r16355  
    99  protected $context; 
    1010   
     11  /** Liste des actions à vérifier avant l'exec */ 
     12  static $_checks=array(); 
     13   
    1114  public function __construct($options, $datas, spyForm $context){ 
    1215    $this->setDatas($datas); 
     
    1720  } 
    1821   
     22  /** 
     23   * Ajoute une vérification 
     24   *  
     25   * @param string $cond PHP CODE condition 
     26   */ 
     27  protected static function addCheck($cond){ 
     28    spyFormActionBase::$_checks[]=$cond; 
     29  } 
     30   
     31  /** 
     32   * Supprimme le dernier check de la liste 
     33   */ 
     34  protected static function removeCheck(){ 
     35    unset(spyFormActionBase::$_checks[(sizeof(spyFormActionBase::$_checks)-1)]); 
     36  } 
     37  /** 
     38   * Verifie l'ensemble des checks et autorise ou non l'exec  
     39   *  
     40   * @return boolean  
     41   */ 
     42  public function checkAll(){ 
     43    $t=true; 
     44    if(sizeof(spyFormActionBase::$_checks)>0){ 
     45      foreach(spyFormActionBase::$_checks as $check){ 
     46         
     47        if(eval('return '.$check.' ?>')){ 
     48          $t=$t; 
     49        }else{ 
     50          $t=false; 
     51        } 
     52      } 
     53    } 
     54    return $t; 
     55  } 
    1956   
    2057  /** 
     
    2360  public function getContext(){ 
    2461    return $this->context; 
     62  } 
     63   
     64  /** 
     65   * Retourne un parametre de la requette 
     66   *  
     67   * @param string parameter  
     68   * @param mixed default Value 
     69   * @return mixed result of the parameter 
     70   */  
     71  public function getRequestParameter($parameter,$default=null){ 
     72    $request=sfContext::getInstance()->getRequest(); 
     73    return ($request->getParameter($parameter))?$request->getParameter($parameter):$default; 
    2574  } 
    2675   
     
    60109  } 
    61110   
    62   /* To set default Options */ 
     111  /** To set default Options */ 
    63112  abstract public function configure($options); 
    64113   
    65   /* Execute the action */ 
     114  /** Execute the action */ 
    66115  abstract public function execute(); 
    67116   
  • plugins/spyFormBuilderInterface2Plugin/lib/spyForm.class.php

    r16349 r16355  
    66class spyForm { 
    77   
    8    
     8  /** Liste of widgets class **/ 
     9  protected   $widgets=array(); 
     10   
     11  /** Liste of labels **/ 
     12  protected   $labels=array(); 
     13   
     14  /** Liste of helps **/ 
     15  protected   $helps=array(); 
     16   
     17  /** Liste of validators **/ 
     18  protected   $valids=array(); 
     19   
     20  /** 
     21   * Constructeur 
     22   *  
     23   * @param integer $form_id Number of the form to retrieve fields ... 
     24   * @param array $datas Array of datas for propel store $datas['id']=ID of the data 
     25   */ 
    926  public function __construct($form_id,$datas=array()){ 
    1027    $this->datas=$datas; 
    11      
     28    $this->form_id=$form_id; 
     29    $this->getConfig(); 
     30    $this->form_object=$this->retrieveForm(); 
     31    $this->init(); 
     32  } 
     33   
     34  /** 
     35   * getConfig() 
     36   * Include the config files for fields validators and actions 
     37   */ 
     38  protected function getConfig(){ 
    1239    require_once(sfContext::getInstance()->getConfigCache()->checkConfig('config/spy_form_widgets.yml')); 
    13      
    1440    require_once(sfContext::getInstance()->getConfigCache()->checkConfig('config/spy_form_validators.yml')); 
    15      
    1641    require_once(sfContext::getInstance()->getConfigCache()->checkConfig('config/spy_form_actions.yml')); 
    1742     
     
    1944    $this->all_validators=sfConfig::get('sfv_validators_rules'); 
    2045    $this->all_actions=sfConfig::get('sfa_actions_post'); 
    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(){ 
    2584    //Construit la class basé sur le sfForm 
    2685    $this->formulaire=new spyFormBuilderForm(); 
     
    3089    $c->addAscendingOrderByColumn(SpyFormBuilderFieldsPeer::RANK); 
    3190    $this->fields=$this->form_object->getSpyFormBuilderFieldss($c); 
    32  
    33      
    34     $widgets=array(); 
    35     $labels=array(); 
    36     $helps=array(); 
    37     $valids=array(); 
    38      
    39     $app=sfContext::getInstance()->getConfiguration()->getApplication(); 
     91     
    4092    foreach($this->fields as $field){ 
    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    } 
    9197    //Insert les champs 
    92     $this->formulaire->setWidgets($widgets); 
     98    $this->formulaire->setWidgets($this->widgets); 
    9399     
    94100    //Insert les labels 
    95     $this->formulaire->getWidgetSchema()->setLabels($labels); 
     101    $this->formulaire->getWidgetSchema()->setLabels($this->labels); 
    96102     
    97103    //Messages d'aide 
    98     if(sizeof($helps)>0) 
    99       $this->formulaire->getWidgetSchema()->setHelps($helps); 
    100      
     104    if(sizeof($this->helps)>0) 
     105      $this->formulaire->getWidgetSchema()->setHelps($this->helps); 
     106     
    101107    //TO retrieve Datas 
    102     $this->doPreActions();         
    103    
    104     //Edit Mode 
    105     if($this->isInEditMode()){ 
    106       $this->formulaire->setDefaults($this->datas); 
    107     } 
    108      
     108    $this->doPreActions();   
     109 
    109110    //Validators 
    110     $this->formulaire->setValidators($valids); 
    111        
    112     $this->formulaire->getWidgetSchema()->setNameFormat('spy_form_builder[%s]'); 
    113      
    114      
     111    $this->formulaire->setValidators($this->valids); 
     112     
     113    $this->formulaire->getWidgetSchema()->setNameFormat($this->form_object->getName().'[%s]'); 
    115114     
    116115    /** 
     
    118117     */ 
    119118    if(sfContext::getInstance()->getRequest()->isMethod('post')){ 
    120       $this->formulaire->bind(sfContext::getInstance()->getRequest()->getParameter('spy_form_builder')); 
     119      $this->formulaire->bind(sfContext::getInstance()->getRequest()->getParameter($this->form_object->getName())); 
    121120      if ($this->formulaire->isValid()) 
    122121      { 
     
    125124    } 
    126125  } 
    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   */ 
    128153  public function isInEditMode(){ 
    129154    return (sizeof($this->datas)>0); 
    130155  } 
    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)) 
    137237      $options=(array_key_exists('options',$params))?$params['options']:array(); 
    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   */ 
    144280  protected function doPreActions(){ 
    145281    $this->actions=$this->form_object->getSpyFormBuilderActions(); 
     
    153289       
    154290      $myAction=new $action_class($options, $this->formulaire->getValues(),$this); 
    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 
    160322    $form_submit_url=sfContext::getInstance()->getModuleName().'/'.sfContext::getInstance()->getActionName(); 
     323     
     324    //Id of form part 
    161325    //SI le module contient le parametre id 
    162326    if(sfContext::getInstance()->getRequest()->getParameter('id')){ 
     
    165329      $form_submit_url.='?form_name='.sfContext::getInstance()->getRequest()->getParameter('form_name'); 
    166330    } 
     331     
     332    //Edit 
    167333    if(sfContext::getInstance()->getRequest()->getParameter('edit')){ 
    168334      $form_submit_url.='&edit='.sfContext::getInstance()->getRequest()->getParameter('edit'); 
    169335    } 
     336     
     337    return $form_submit_url; 
     338  } 
     339   
     340  /** 
     341   * Render the form calcultae the URL to render and execute the template 
     342   */ 
     343  public function render(){ 
     344    $form=$this->formulaire; 
     345    $form_submit_url=$this->getUrlForForm(); 
    170346    eval('?>'.$this->form_object->getTemplate()); 
    171347  } 
    172   protected function retrieveForm($form_id){ 
    173     if(is_numeric($form_id)){ 
    174       try{ 
    175         $this->form_object=SpyFormBuilderPeer::retrieveByPK($form_id); 
    176         if(!$this->form_object instanceof SpyFormBuilder){ 
    177           throw new Exception('Impossible to find Form number '.$form_id); 
    178         } 
    179       }catch(Exception $e){ 
    180         throw  $e; 
    181       } 
    182     }else{ 
    183       try{ 
    184         $this->form_object=SpyFormBuilderPeer::retrieveByName($form_id); 
    185         if(!$this->form_object instanceof SpyFormBuilder){ 
    186           throw new Exception('Impossible to find Form by name '.$form_id); 
    187         } 
    188       }catch(Exception $e){ 
    189         throw  $e; 
    190       } 
    191     } 
    192   } 
     348   
    193349 
    194350}