<?php

class UserForm extends PluginUserForm
{
    public function configure()
    {

        $fields = array('name', 'phone', 'email', 'password', 'address', 'is_super_admin', 'user_group_list');
        $this->useFields($fields);

        if(in_array('user_group_list', $fields)){
            $this->widgetSchema['user_group_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => false, 'model' => 'UserGroup', 'expanded' => false, 'add_empty' => true));
        }
        if(in_array('password', $fields)){
            $this->widgetSchema['password'] = new sfWidgetFormInputText(array(), array("size" => 50, "maxlength" => 128, "required" => true, "class" => "gen_pass"));
            $this->validatorSchema['password'] = new sfValidatorString(array('min_length' => 5, 'max_length' => 15, 'required' => false));
        }
        if(in_array('email', $fields)){
            $this->widgetSchema['email'] = new sfWidgetFormInputText(array(), array("size" => 50, "maxlength" => 128, "required" => true));
            $this->validatorSchema['email'] = new sfValidatorEmail();
        }
        if(in_array('phone', $fields)){
            $this->widgetSchema['phone'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off", "size" => 16, "maxlength" => 11, "class" => "phone", "required" => true));
            $this->validatorSchema['phone'] = new sfValidatorNumber(array('phone' => true), array('invalid' => 'Неправильный формат'));
            $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
                new sfValidatorDoctrineUnique(array('model'=> 'User', 'column'=> 'phone', 'query'=> Doctrine_Query::create()
                    ->from("User")->where("deleted_at IS NULL")), array('invalid' => 'Пользователь с данным номером уже зарегистрирован')),
                new sfValidatorDoctrineUnique(array('model'=> 'User', 'column'=> 'email', 'query'=> Doctrine_Query::create()
                    ->from("User")->where("social IS NULL")->andWhere("deleted_at IS NULL")), array('invalid' => 'Пользователь с данной почтой уже зарегистрирован'))
            )));
        }
        $this->widgetSchema['name'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off", "size" => 34, "maxlength" => 255, "required" => true));
        $this->validatorSchema['name'] = new sfValidatorString(array('max_length' => 255, 'required' => true));
    }
}

