<?php

/**
 * Perfiles form base class.
 *
 * @package    form
 * @subpackage perfiles
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BasePerfilesForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                         => new sfWidgetFormInputHidden(),
      'descripcion'                => new sfWidgetFormInput(),
      'username'                   => new sfWidgetFormInput(),
      'created_at'                 => new sfWidgetFormDate(),
      'update_at'                  => new sfWidgetFormInput(),
      'estado'                     => new sfWidgetFormInput(),
      'perfiles_credenciales_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Credenciales')),
    ));

    $this->setValidators(array(
      'id'                         => new sfValidatorPropelChoice(array('model' => 'Perfiles', 'column' => 'id', 'required' => false)),
      'descripcion'                => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'username'                   => new sfValidatorNumber(array('required' => false)),
      'created_at'                 => new sfValidatorDate(array('required' => false)),
      'update_at'                  => new sfValidatorNumber(array('required' => false)),
      'estado'                     => new sfValidatorNumber(array('required' => false)),
      'perfiles_credenciales_list' => new sfValidatorPropelChoiceMany(array('model' => 'Credenciales', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

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

      $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_PERFIL, $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->setIdPerfil($this->object->getPrimaryKey());
        $obj->setIdCredencial($value);
        $obj->save();
      }
    }
  }

}
