abstract class BaseFormPropel extends sfFormPropel {
public function setup() {
}
/**
* Load the 1:n Relations in the form as embebed. The relation
* must by defined in a standar form.
*
* This method use ysJQueryRevolutions for the sfWidgetFormInputAjaxDelete
*
* Supouse we have main table a and child table b, a as id field and b to,
* the relation must be implemented trought b.a_id. In otherwise you
* must reimplement this method to work with your fields.
*
* @param string $model : Model name of the child table, like items, files etc.
* @param string $label_field : Name of the fild to show as label for the embebed form, like name, text etc.
* @param string $ajax_delete_url : Internal URI to action that make the delete of the asocied object, ej (demo/delete).
*
*/
protected function loadAsEmbebedForm($model,$label_field, $ajax_delete_url) {
$model = ucfirst($model);
$model_field = strtolower($model[0]).substr($model,1);
$parent_model = $this->getModelName();
$parent_form_field = strtolower($parent_model[0]).substr($parent_model,1);
$form = $model.\'Form\';
$getAsociedModel = \'get\'.$model.\'s\';
$getLabelField = \'get\'.ucfirst($label_field);
if(!$this->isNew()) {
foreach($this->getObject()->$getAsociedModel() as $element) {
$element_form = new $form($element);
$element_form_name = $model.\'_\'.$element->getId();
unset($element_form[\'id\'],$element_form[$parent_form_field.\'_id\']);
$this->embedForm($model.\'_\'.$element->getId(), $element_form);
$this->widgetSchema[$element_form_name]->setLabel($element->$getLabelField());
$this->widgetSchema[$element_form_name][$label_field] = new sfWidgetFormInputAjaxDelete(array(
\'url\' => $ajax_delete_url, // required
\'model_id\' => $element->getId(), // required
\'update\' => \'sf_admin_form_field_\'.$element_form_name,//required
\'selector\' => \'.\', //Must Change # for class
\'confirm\' => \'Esta Seguro!\', // optional
));
}
}
$nform = new $form();
unset($nform[\'id\'],$nform[$parent_form_field.\'_id\']);
$this->embedForm($model,$nform);
$this->widgetSchema[$model]->setLabel(\'Nuevo\');
}
/**
* If the embebed form has changes, bind the main object
* to the child objects
*
* @param array $taintedValues : Taintes Values from Bind method
*/
protected function bindObjectToEmbebed($taintedValues) {
$parent_model = $this->getModelName();
$set_parent_model = \'set\'.$parent_model;
foreach($this->embeddedForms as $key => $form) {
if($this->is_seted_value($taintedValues[$key])) {
$this->embeddedForms[$key]->getObject()->$set_parent_model($this->getObject());
}else {
unset($this->widgetSchema[$key]);
$this->validatorSchema[$key] = new sfValidatorPass();
}
}
}
/**
* Check if some value as changed in the embebed forms
*
* @param array $tainted : The Tainted Values
*
* @return boolean: True if some changes, false otherwise
*/
protected function is_seted_value($tainted) {
if(count($tainted) == 0) {
return false;
}
foreach($tainted as $value) {
if(strlen($value) > 0 && !is_null($value) ) {
return true;
}
}
return false;
}
public function bind(array $taintedValues = null, array $taintedFiles = null) {
$this->bindObjectToEmbebed($taintedValues);
parent::bind($taintedValues, $taintedFiles);
}
}