<?php

/**
 * KataoSupplier form base class.
 *
 * @package    form
 * @subpackage katao_supplier
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoSupplierForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                   => new sfWidgetFormInputHidden(),
      'katao_index'                          => new sfWidgetFormInput(),
      'member_id'                            => new sfWidgetFormPropelSelect(array('model' => 'KataoMember', 'add_empty' => true)),
      'referer_id'                           => new sfWidgetFormPropelSelect(array('model' => 'KataoMember', 'add_empty' => true)),
      'name'                                 => new sfWidgetFormInput(),
      'url_identifier'                       => new sfWidgetFormInput(),
      'presentation'                         => new sfWidgetFormTextarea(),
      'email'                                => new sfWidgetFormInput(),
      'address1'                             => new sfWidgetFormInput(),
      'address2'                             => new sfWidgetFormInput(),
      'zip'                                  => new sfWidgetFormInput(),
      'city'                                 => new sfWidgetFormInput(),
      'phone'                                => new sfWidgetFormInput(),
      'fax'                                  => new sfWidgetFormInput(),
      'website'                              => new sfWidgetFormInput(),
      'gmap_lng'                             => new sfWidgetFormInput(),
      'gmap_lat'                             => new sfWidgetFormInput(),
      'accounting_code'                      => new sfWidgetFormInput(),
      'accounting_code_sol'                  => new sfWidgetFormInput(),
      'siret_number'                         => new sfWidgetFormInput(),
      'is_global'                            => new sfWidgetFormInput(),
      'order_min_amount'                     => new sfWidgetFormInput(),
      'created_at'                           => new sfWidgetFormDateTime(),
      'updated_at'                           => new sfWidgetFormDateTime(),
      'katao_supplier_node_list'             => new sfWidgetFormPropelSelectMany(array('model' => 'KataoNode')),
      'katao_supplier_product_category_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoProductCategory')),
    ));

    $this->setValidators(array(
      'id'                                   => new sfValidatorPropelChoice(array('model' => 'KataoSupplier', 'column' => 'id', 'required' => false)),
      'katao_index'                          => new sfValidatorInteger(),
      'member_id'                            => new sfValidatorPropelChoice(array('model' => 'KataoMember', 'column' => 'id', 'required' => false)),
      'referer_id'                           => new sfValidatorPropelChoice(array('model' => 'KataoMember', 'column' => 'id', 'required' => false)),
      'name'                                 => new sfValidatorString(array('max_length' => 255)),
      'url_identifier'                       => new sfValidatorString(array('max_length' => 255)),
      'presentation'                         => new sfValidatorString(array('required' => false)),
      'email'                                => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'address1'                             => new sfValidatorString(array('max_length' => 255)),
      'address2'                             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'zip'                                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'city'                                 => new sfValidatorString(array('max_length' => 255)),
      'phone'                                => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'fax'                                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'website'                              => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'gmap_lng'                             => new sfValidatorNumber(array('required' => false)),
      'gmap_lat'                             => new sfValidatorNumber(array('required' => false)),
      'accounting_code'                      => new sfValidatorString(array('max_length' => 6)),
      'accounting_code_sol'                  => new sfValidatorString(array('max_length' => 6)),
      'siret_number'                         => new sfValidatorString(array('max_length' => 14)),
      'is_global'                            => new sfValidatorInteger(array('required' => false)),
      'order_min_amount'                     => new sfValidatorNumber(array('required' => false)),
      'created_at'                           => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                           => new sfValidatorDateTime(array('required' => false)),
      'katao_supplier_node_list'             => new sfValidatorPropelChoiceMany(array('model' => 'KataoNode', 'required' => false)),
      'katao_supplier_product_category_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoProductCategory', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorPropelUnique(array('model' => 'KataoSupplier', 'column' => array('siret_number')))
    );

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['katao_supplier_node_list']))
    {
      $values = array();
      foreach ($this->object->getKataoSupplierNodes() as $obj)
      {
        $values[] = $obj->getKataoNodeId();
      }

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

    if (isset($this->widgetSchema['katao_supplier_product_category_list']))
    {
      $values = array();
      foreach ($this->object->getKataoSupplierProductCategorys() as $obj)
      {
        $values[] = $obj->getKataoProductCategoryId();
      }

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

  }

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

    $this->saveKataoSupplierNodeList($con);
    $this->saveKataoSupplierProductCategoryList($con);
  }

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

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

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

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

    $values = $this->getValue('katao_supplier_node_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoSupplierNode();
        $obj->setKataoSupplierId($this->object->getPrimaryKey());
        $obj->setKataoNodeId($value);
        $obj->save();
      }
    }
  }

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

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

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

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

    $values = $this->getValue('katao_supplier_product_category_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoSupplierProductCategory();
        $obj->setKataoSupplierId($this->object->getPrimaryKey());
        $obj->setKataoProductCategoryId($value);
        $obj->save();
      }
    }
  }

}
