<?php

/**
 * TarifasTaxis form base class.
 *
 * @package    form
 * @subpackage tarifas_taxis
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseTarifasTaxisForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                   => new sfWidgetFormInputHidden(),
      'descripcion'          => new sfWidgetFormInput(),
      'tarifa'               => new sfWidgetFormInput(),
      'estado'               => new sfWidgetFormInput(),
      'tarifas_destino_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
    ));

    $this->setValidators(array(
      'id'                   => new sfValidatorPropelChoice(array('model' => 'TarifasTaxis', 'column' => 'id', 'required' => false)),
      'descripcion'          => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'tarifa'               => new sfValidatorNumber(array('required' => false)),
      'estado'               => new sfValidatorNumber(array('required' => false)),
      'tarifas_destino_list' => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['tarifas_destino_list']))
    {
      $values = array();
      foreach ($this->object->getTarifasDestinos() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

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

  }

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

    $this->saveTarifasDestinoList($con);
  }

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

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

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

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

    $values = $this->getValue('tarifas_destino_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new TarifasDestino();
        $obj->setIdTarifa($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

}
