<?php

/**
 * Product filter form base class.
 *
 * @package    sf
 * @subpackage filter
 * @author     Atma
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseProductFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'inner_id'       => new sfWidgetFormFilterInput(),
      'alias'          => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'product_img'    => new sfWidgetFormFilterInput(),
      'supplier_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Suppliers'), 'add_empty' => true)),
      'supplier_price' => new sfWidgetFormFilterInput(),
      'certificate'    => new sfWidgetFormFilterInput(),
      'offer_count'    => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'is_detail'      => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'is_delete'      => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'is_main'        => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'show_main'      => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'sort'           => new sfWidgetFormFilterInput(),
      'free_shipping'  => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'the_weight'     => new sfWidgetFormFilterInput(),
      'created_at'     => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'updated_at'     => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      '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(
      'inner_id'       => new sfValidatorPass(array('required' => false)),
      'alias'          => new sfValidatorPass(array('required' => false)),
      'product_img'    => new sfValidatorPass(array('required' => false)),
      'supplier_id'    => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Suppliers'), 'column' => 'id')),
      'supplier_price' => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'certificate'    => new sfValidatorPass(array('required' => false)),
      'offer_count'    => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'is_detail'      => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'is_delete'      => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'is_main'        => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'show_main'      => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'sort'           => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'free_shipping'  => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'the_weight'     => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'created_at'     => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'updated_at'     => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      '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->widgetSchema->setNameFormat('product_filters[%s]');

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

    $this->setupInheritance();

    parent::setup();
  }

  public function addPropListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->leftJoin('r.ProductPropValue ProductPropValue')
          ->andWhereIn('ProductPropValue.product_id', $values);
  }

  public function addCatsListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->leftJoin('r.CatProduct CatProduct')
          ->andWhereIn('CatProduct.cat_id', $values);
  }

  public function addTagsListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->leftJoin('r.ProductTags ProductTags')
          ->andWhereIn('ProductTags.tag_id', $values);
  }

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

  public function getFields()
  {
    return array(
      'id'             => 'Number',
      'inner_id'       => 'Text',
      'alias'          => 'Text',
      'product_img'    => 'Text',
      'supplier_id'    => 'ForeignKey',
      'supplier_price' => 'Number',
      'certificate'    => 'Text',
      'offer_count'    => 'Number',
      'is_detail'      => 'Boolean',
      'is_delete'      => 'Boolean',
      'is_main'        => 'Boolean',
      'show_main'      => 'Boolean',
      'sort'           => 'Number',
      'free_shipping'  => 'Boolean',
      'the_weight'     => 'Number',
      'created_at'     => 'Date',
      'updated_at'     => 'Date',
      'prop_list'      => 'ManyKey',
      'cats_list'      => 'ManyKey',
      'tags_list'      => 'ManyKey',
    );
  }
}
