<?php

/**
 * Cat form.
 *
 * @package    sf
 * @subpackage form
 * @author     Atma
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CatForm extends BaseCatForm
{
    public function configure()
    {
        $this->embedI18n(array('ru', 'en'));
        $this->useFields(array('ru', 'en', 'photo', 'parent_id', 'sort', 'is_filter', 'is_main', 'alias'));

        $parentId = sfContext::getInstance()->getRequest()->getParameter('parentId');
        if ($parentId) {
            $parentId = str_replace('cat_', '', $parentId);
            if ($parentId) {
                $this->setDefault('parent_id', $parentId);
            }
        }
        $this->widgetSchema['is_filter'] = new sfWidgetFormInput(array('type' => 'checkbox', 'label' => 'Фильтр'), ($this->isNew() || $this->getObject()->getIsFilter() ? array('checked' => 'checked') : array()));
        $this->widgetSchema['is_main'] = new sfWidgetFormInputCheckbox(array('label' => 'Отображать в баннере'), array("required" => true));

        $this->widgetSchema['alias'] = new sfWidgetFormInputText(array('label' => 'Алиас'), array("class" => "translateField", "size" => 32, "maxlength" => 255));
        $this->validatorSchema['alias'] = new sfValidatorString(array('max_length' => 500, 'only_english' => true, 'required' => false));

        $this->widgetSchema['ru']['title'] = new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 1000, "required" => true));
        $this->widgetSchema['en']['title'] = new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 1000, "required" => true));

        $this->widgetSchema['ru']['title']->setLabel('Название');
        $this->widgetSchema['en']['title']->setLabel('Название');

        $otherCat = Doctrine_Query::create()
            ->from("Cat c")
            ->innerJoin("c.Translation t")
            ->where("(SELECT COUNT(*) FROM CatProduct cp WHERE cp.cat_id = c.id) = 0")
            ->andWhere('c.id != ' . $this->getObject()->getId())
            ->execute();

        $this->widgetSchema['photo'] = new sfWidgetFormInputFileUpload(array(
            'label' => 'Изображение',
            'allowedFileTypes' => 'image/png,image/jpeg',
            'script' => '/uploader?key=cat'
        ), array('required' => true));

        $catList = array(
            null => '—'
        );

        if ($otherCat) {
            foreach ($otherCat as $cat) {
                $catList[$cat->getId()] = $cat->getTitle();
            }
        }

        $this->widgetSchema['parent_id'] = new sfWidgetFormChoice(array(
            'choices' => $catList
        ));
        $this->widgetSchema['parent_id'] = new sfWidgetFormInputHidden();
    }
}
