document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. abstract class BaseFormPropel extends sfFormPropel {
  2.    
  3.     public function setup() {
  4.     }
  5.  
  6.     /**
  7.      * Load the 1:n Relations in the form as embebed. The relation
  8.      * must by defined in a standar form.
  9.      *
  10.      * This method use ysJQueryRevolutions for the sfWidgetFormInputAjaxDelete
  11.      *
  12.      * Supouse we have main table a and child table b, a as id field and b to,
  13.      * the relation must be implemented trought b.a_id. In otherwise you
  14.      * must reimplement this method to work with your fields.
  15.      *
  16.      * @param string $model : Model name of the child table, like items, files etc.
  17.      * @param string $label_field : Name of the fild to show as label for the embebed form, like name, text etc.
  18.      * @param string $ajax_delete_url  : Internal URI to action that make the delete of the asocied object, ej (demo/delete).
  19.      *    
  20.      */
  21.     protected function loadAsEmbebedForm($model,$label_field, $ajax_delete_url) {
  22.  
  23.         $model = ucfirst($model);
  24.         $model_field = strtolower($model[0]).substr($model,1);
  25.         $parent_model = $this->getModelName();
  26.         $parent_form_field = strtolower($parent_model[0]).substr($parent_model,1);
  27.  
  28.         $form = $model.\'Form\';
  29.         $getAsociedModel = \'get\'.$model.\'s\';
  30.         $getLabelField = \'get\'.ucfirst($label_field);
  31.  
  32.         if(!$this->isNew()) {
  33.             foreach($this->getObject()->$getAsociedModel() as $element) {
  34.  
  35.                 $element_form = new $form($element);
  36.                 $element_form_name = $model.\'_\'.$element->getId();
  37.                 unset($element_form[\'id\'],$element_form[$parent_form_field.\'_id\']);
  38.  
  39.                 $this->embedForm($model.\'_\'.$element->getId(), $element_form);
  40.                 $this->widgetSchema[$element_form_name]->setLabel($element->$getLabelField());
  41.                 $this->widgetSchema[$element_form_name][$label_field] = new sfWidgetFormInputAjaxDelete(array(
  42.                                 \'url\' => $ajax_delete_url,            // required
  43.                                 \'model_id\' => $element->getId(),      // required
  44.                                 \'update\' => \'sf_admin_form_field_\'.$element_form_name,//required
  45.                                 \'selector\' => \'.\',                    //Must Change # for class
  46.                                 \'confirm\' => \'Esta Seguro!\',          // optional
  47.                 ));
  48.             }
  49.         }
  50.  
  51.         $nform = new $form();
  52.         unset($nform[\'id\'],$nform[$parent_form_field.\'_id\']);
  53.         $this->embedForm($model,$nform);
  54.         $this->widgetSchema[$model]->setLabel(\'Nuevo\');
  55.     }
  56.  
  57.     /**
  58.      * If the embebed form has changes, bind the main object
  59.      * to the child objects
  60.      *
  61.      * @param array $taintedValues : Taintes Values from Bind method
  62.      */
  63.     protected function bindObjectToEmbebed($taintedValues) {
  64.  
  65.         $parent_model = $this->getModelName();
  66.         $set_parent_model = \'set\'.$parent_model;
  67.         foreach($this->embeddedForms as $key => $form) {
  68.             if($this->is_seted_value($taintedValues[$key])) {
  69.                 $this->embeddedForms[$key]->getObject()->$set_parent_model($this->getObject());
  70.             }else {
  71.                 unset($this->widgetSchema[$key]);
  72.                 $this->validatorSchema[$key]  = new sfValidatorPass();
  73.             }
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Check if some value as changed in the embebed forms
  79.      *
  80.      * @param array $tainted : The Tainted Values
  81.      *
  82.      * @return boolean: True if some changes, false otherwise
  83.      */
  84.     protected function is_seted_value($tainted) {
  85.         if(count($tainted) ==  0) {
  86.             return false;
  87.         }
  88.         foreach($tainted as $value) {
  89.             if(strlen($value) > 0 && !is_null($value) ) {
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     public function bind(array $taintedValues = null, array $taintedFiles = null) {
  97.         $this->bindObjectToEmbebed($taintedValues);
  98.         parent::bind($taintedValues, $taintedFiles);
  99.     }
  100. }
');