<?php

/**
 * KataoOrder form base class.
 *
 * @package    form
 * @subpackage katao_order
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoOrderForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                => new sfWidgetFormInputHidden(),
      'katao_supplier_id'                 => new sfWidgetFormPropelSelect(array('model' => 'KataoSupplier', 'add_empty' => false)),
      'katao_period_id'                   => new sfWidgetFormPropelSelect(array('model' => 'KataoPeriod', 'add_empty' => false)),
      'katao_node_id'                     => new sfWidgetFormPropelSelect(array('model' => 'KataoNode', 'add_empty' => false)),
      'status'                            => new sfWidgetFormInput(),
      'is_simulated'                      => new sfWidgetFormInput(),
      'supplier_name'                     => new sfWidgetFormInput(),
      'supplier_email'                    => new sfWidgetFormInput(),
      'supplier_address1'                 => new sfWidgetFormInput(),
      'supplier_address2'                 => new sfWidgetFormInput(),
      'supplier_zip'                      => new sfWidgetFormInput(),
      'supplier_city'                     => new sfWidgetFormInput(),
      'katao_name'                        => new sfWidgetFormInput(),
      'katao_address'                     => new sfWidgetFormTextarea(),
      'katao_siret_number'                => new sfWidgetFormInput(),
      'katao_rcs_number'                  => new sfWidgetFormInput(),
      'katao_capital'                     => new sfWidgetFormInput(),
      'delivery_address'                  => new sfWidgetFormTextarea(),
      'created_at'                        => new sfWidgetFormDateTime(),
      'updated_at'                        => new sfWidgetFormDateTime(),
      'katao_supplier_invoice_order_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoSupplierInvoice')),
    ));

    $this->setValidators(array(
      'id'                                => new sfValidatorPropelChoice(array('model' => 'KataoOrder', 'column' => 'id', 'required' => false)),
      'katao_supplier_id'                 => new sfValidatorPropelChoice(array('model' => 'KataoSupplier', 'column' => 'id')),
      'katao_period_id'                   => new sfValidatorPropelChoice(array('model' => 'KataoPeriod', 'column' => 'id')),
      'katao_node_id'                     => new sfValidatorPropelChoice(array('model' => 'KataoNode', 'column' => 'id')),
      'status'                            => new sfValidatorInteger(),
      'is_simulated'                      => new sfValidatorInteger(array('required' => false)),
      'supplier_name'                     => new sfValidatorString(array('max_length' => 255)),
      'supplier_email'                    => new sfValidatorString(array('max_length' => 255)),
      'supplier_address1'                 => new sfValidatorString(array('max_length' => 255)),
      'supplier_address2'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'supplier_zip'                      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'supplier_city'                     => new sfValidatorString(array('max_length' => 255)),
      'katao_name'                        => new sfValidatorString(array('max_length' => 255)),
      'katao_address'                     => new sfValidatorString(array('required' => false)),
      'katao_siret_number'                => new sfValidatorString(array('max_length' => 14)),
      'katao_rcs_number'                  => new sfValidatorString(array('max_length' => 255)),
      'katao_capital'                     => new sfValidatorInteger(),
      'delivery_address'                  => new sfValidatorString(array('required' => false)),
      'created_at'                        => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                        => new sfValidatorDateTime(array('required' => false)),
      'katao_supplier_invoice_order_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoSupplierInvoice', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['katao_supplier_invoice_order_list']))
    {
      $values = array();
      foreach ($this->object->getKataoSupplierInvoiceOrders() as $obj)
      {
        $values[] = $obj->getKataoSupplierInvoiceId();
      }

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

  }

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

    $this->saveKataoSupplierInvoiceOrderList($con);
  }

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

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

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

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

    $values = $this->getValue('katao_supplier_invoice_order_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoSupplierInvoiceOrder();
        $obj->setKataoOrderId($this->object->getPrimaryKey());
        $obj->setKataoSupplierInvoiceId($value);
        $obj->save();
      }
    }
  }

}
