<?php

/**
 * Product form base class.
 *
 * @method Product getObject() Returns the current form's model object
 *
 * @package    sf
 * @subpackage form
 * @author     Atma
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseProductForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'             => new sfWidgetFormInputHidden(array(), array()),
      'inner_id'       => new sfWidgetFormInputText(array(), array("size" => 65)),
      'alias'          => new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 255, "required" => true)),
      'product_img'    => new sfWidgetFormInputText(array(), array("size" => 65)),
      'supplier_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Suppliers'), 'add_empty' => true), array()),
      'supplier_price' => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'certificate'    => new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 255)),
      'offer_count'    => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8, "required" => true)),
      'is_detail'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'is_delete'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'is_main'        => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'show_main'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'sort'           => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'free_shipping'  => new sfWidgetFormInputCheckbox(array(), array()),
      'the_weight'     => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'created_at'     => new sfWidgetFormDateTime(array(), array("required" => true)),
      'updated_at'     => new sfWidgetFormDateTime(array(), array("required" => true)),
      'prop_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'ProductProp')),
      'cats_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Cat')),
      'tags_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Tag')),
    ));

    $this->setValidators(array(
      'id'             => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'inner_id'       => new sfValidatorPass(array('required' => false)),
      'alias'          => new sfValidatorString(array('max_length' => 255)),
      'product_img'    => new sfValidatorPass(array('required' => false)),
      'supplier_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Suppliers'), 'required' => false)),
      'supplier_price' => new sfValidatorInteger(array('required' => false)),
      'certificate'    => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'offer_count'    => new sfValidatorInteger(array('required' => false)),
      'is_detail'      => new sfValidatorBoolean(array('required' => false)),
      'is_delete'      => new sfValidatorBoolean(array('required' => false)),
      'is_main'        => new sfValidatorBoolean(array('required' => false)),
      'show_main'      => new sfValidatorBoolean(array('required' => false)),
      'sort'           => new sfValidatorInteger(array('required' => false)),
      'free_shipping'  => new sfValidatorBoolean(array('required' => false)),
      'the_weight'     => new sfValidatorInteger(array('required' => false)),
      'created_at'     => new sfValidatorDateTime(),
      'updated_at'     => new sfValidatorDateTime(),
      'prop_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'ProductProp', 'required' => false)),
      'cats_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Cat', 'required' => false)),
      'tags_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Tag', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Product', 'column' => array('alias')))
    );

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

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

    $this->setupInheritance();

    parent::setup();
  }

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

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

    if (isset($this->widgetSchema['prop_list']))
    {
      $this->setDefault('prop_list', $this->object->Prop->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['cats_list']))
    {
      $this->setDefault('cats_list', $this->object->Cats->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tags_list']))
    {
      $this->setDefault('tags_list', $this->object->Tags->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savePropList($con);
    $this->saveCatsList($con);
    $this->saveTagsList($con);

    parent::doSave($con);
  }

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

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

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

    $existing = $this->object->Prop->getPrimaryKeys();
    $values = $this->getValue('prop_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Prop', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Prop', array_values($link));
    }
  }

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

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

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

    $existing = $this->object->Cats->getPrimaryKeys();
    $values = $this->getValue('cats_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Cats', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Cats', array_values($link));
    }
  }

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

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

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

    $existing = $this->object->Tags->getPrimaryKeys();
    $values = $this->getValue('tags_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Tags', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Tags', array_values($link));
    }
  }

}
