<?php

/**
 * Terceros form base class.
 *
 * @package    form
 * @subpackage terceros
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseTercerosForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'nitocc'                     => new sfWidgetFormInputHidden(),
      'tipter'                     => new sfWidgetFormInput(),
      'razsoc'                     => new sfWidgetFormInput(),
      'ciudad'                     => new sfWidgetFormInput(),
      'telefo'                     => new sfWidgetFormInput(),
      'telfax'                     => new sfWidgetFormInput(),
      'direcc'                     => new sfWidgetFormInput(),
      'estado'                     => new sfWidgetFormInput(),
      'email'                      => new sfWidgetFormInput(),
      'sueldo'                     => new sfWidgetFormInput(),
      'cargo'                      => new sfWidgetFormInput(),
      'codreg'                     => new sfWidgetFormInput(),
      'comisionados_contrato_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Comisiones')),
      'destinos_comisionados_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Comisiones')),
      'comisionados_objs_list'     => new sfWidgetFormPropelSelectMany(array('model' => 'Comisiones')),
      'tarifas_destino_list'       => new sfWidgetFormPropelSelectMany(array('model' => 'Comisiones')),
      'comisionados_planta_list'   => new sfWidgetFormPropelSelectMany(array('model' => 'Comisiones')),
    ));

    $this->setValidators(array(
      'nitocc'                     => new sfValidatorPropelChoice(array('model' => 'Terceros', 'column' => 'nitocc', 'required' => false)),
      'tipter'                     => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'razsoc'                     => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'ciudad'                     => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'telefo'                     => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'telfax'                     => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'direcc'                     => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'estado'                     => new sfValidatorNumber(array('required' => false)),
      'email'                      => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'sueldo'                     => new sfValidatorNumber(array('required' => false)),
      'cargo'                      => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'codreg'                     => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'comisionados_contrato_list' => new sfValidatorPropelChoiceMany(array('model' => 'Comisiones', 'required' => false)),
      'destinos_comisionados_list' => new sfValidatorPropelChoiceMany(array('model' => 'Comisiones', 'required' => false)),
      'comisionados_objs_list'     => new sfValidatorPropelChoiceMany(array('model' => 'Comisiones', 'required' => false)),
      'tarifas_destino_list'       => new sfValidatorPropelChoiceMany(array('model' => 'Comisiones', 'required' => false)),
      'comisionados_planta_list'   => new sfValidatorPropelChoiceMany(array('model' => 'Comisiones', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['comisionados_contrato_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosContratos() as $obj)
      {
        $values[] = $obj->getIdComision();
      }

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

    if (isset($this->widgetSchema['destinos_comisionados_list']))
    {
      $values = array();
      foreach ($this->object->getDestinosComisionadoss() as $obj)
      {
        $values[] = $obj->getIdComision();
      }

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

    if (isset($this->widgetSchema['comisionados_objs_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosObjss() as $obj)
      {
        $values[] = $obj->getIdComision();
      }

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

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

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

    if (isset($this->widgetSchema['comisionados_planta_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosPlantas() as $obj)
      {
        $values[] = $obj->getIdComision();
      }

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

  }

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

    $this->saveComisionadosContratoList($con);
    $this->saveDestinosComisionadosList($con);
    $this->saveComisionadosObjsList($con);
    $this->saveTarifasDestinoList($con);
    $this->saveComisionadosPlantaList($con);
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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::NITOCC, $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->setNitocc($this->object->getPrimaryKey());
        $obj->setIdComision($value);
        $obj->save();
      }
    }
  }

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

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

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

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

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

}
