<?php

/**
 * Credenciales form base class.
 *
 * @package    form
 * @subpackage credenciales
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseCredencialesForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                         => new sfWidgetFormInputHidden(),
      'id_menu'                    => new sfWidgetFormPropelSelect(array('model' => 'Menus', 'add_empty' => true)),
      'name'                       => new sfWidgetFormInput(),
      'descripcion'                => new sfWidgetFormInput(),
      'orden'                      => new sfWidgetFormInput(),
      'update_at'                  => new sfWidgetFormDate(),
      'ruta'                       => new sfWidgetFormInput(),
      'perfiles_credenciales_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Perfiles')),
    ));

    $this->setValidators(array(
      'id'                         => new sfValidatorPropelChoice(array('model' => 'Credenciales', 'column' => 'id', 'required' => false)),
      'id_menu'                    => new sfValidatorPropelChoice(array('model' => 'Menus', 'column' => 'id', 'required' => false)),
      'name'                       => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'descripcion'                => new sfValidatorString(array('max_length' => 30, 'required' => false)),
      'orden'                      => new sfValidatorNumber(array('required' => false)),
      'update_at'                  => new sfValidatorDate(array('required' => false)),
      'ruta'                       => new sfValidatorString(array('max_length' => 30, 'required' => false)),
      'perfiles_credenciales_list' => new sfValidatorPropelChoiceMany(array('model' => 'Perfiles', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('credenciales[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Credenciales';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['perfiles_credenciales_list']))
    {
      $values = array();
      foreach ($this->object->getPerfilesCredencialess() as $obj)
      {
        $values[] = $obj->getIdPerfil();
      }

      $this->setDefault('perfiles_credenciales_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savePerfilesCredencialesList($con);
  }

  public function savePerfilesCredencialesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['perfiles_credenciales_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(PerfilesCredencialesPeer::ID_CREDENCIAL, $this->object->getPrimaryKey());
    PerfilesCredencialesPeer::doDelete($c, $con);

    $values = $this->getValue('perfiles_credenciales_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new PerfilesCredenciales();
        $obj->setIdCredencial($this->object->getPrimaryKey());
        $obj->setIdPerfil($value);
        $obj->save();
      }
    }
  }

}
