<?php

/**
 * KataoProductCategory form base class.
 *
 * @package    form
 * @subpackage katao_product_category
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoProductCategoryForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                   => new sfWidgetFormInputHidden(),
      'name'                                 => new sfWidgetFormInput(),
      'url_identifier'                       => new sfWidgetFormInput(),
      'accounting_code_suffix'               => new sfWidgetFormInput(),
      'sort_order'                           => new sfWidgetFormInput(),
      'created_at'                           => new sfWidgetFormDateTime(),
      'updated_at'                           => new sfWidgetFormDateTime(),
      'katao_supplier_product_category_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoSupplier')),
    ));

    $this->setValidators(array(
      'id'                                   => new sfValidatorPropelChoice(array('model' => 'KataoProductCategory', 'column' => 'id', 'required' => false)),
      'name'                                 => new sfValidatorString(array('max_length' => 255)),
      'url_identifier'                       => new sfValidatorString(array('max_length' => 255)),
      'accounting_code_suffix'               => new sfValidatorString(array('max_length' => 12)),
      'sort_order'                           => new sfValidatorInteger(array('required' => false)),
      'created_at'                           => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                           => new sfValidatorDateTime(array('required' => false)),
      'katao_supplier_product_category_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoSupplier', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

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

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

  }

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

    $this->saveKataoSupplierProductCategoryList($con);
  }

  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_PRODUCT_CATEGORY_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->setKataoProductCategoryId($this->object->getPrimaryKey());
        $obj->setKataoSupplierId($value);
        $obj->save();
      }
    }
  }

}
