<?php

class KataoSupplierProfileForm extends BaseKataoSupplierForm {
    public function configure() {
        unset($this->validatorSchema['accounting_code'], $this->validatorSchema['accounting_code_sol'], $this->validatorSchema['katao_supplier_node_list'], $this->validatorSchema['is_global'], $this->validatorSchema['referer_id'], $this->validatorSchema['member_id'], $this->validatorSchema['katao_index'], $this->validatorSchema['url_identifier'], $this->validatorSchema['created_at'], $this->validatorSchema['updated_at']);
        unset($this->widgetSchema['accounting_code'], $this->widgetSchema['accounting_code_sol'], $this->widgetSchema['katao_supplier_node_list'], $this->widgetSchema['referer_id'], $this->widgetSchema['member_id'], $this->widgetSchema['katao_index'], $this->widgetSchema['url_identifier'], $this->widgetSchema['created_at'], $this->widgetSchema['updated_at']);

        $this->widgetSchema['name'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['email'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['address1'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['address2'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['zip'] = new sfWidgetFormInput(array(), array('style' => 'width: 43px'));
        $this->widgetSchema['city'] = new sfWidgetFormInput(array(), array('style' => 'width: 200px'));
        $this->widgetSchema['phone'] = new sfWidgetFormInput(array(), array('style' => 'width: 85px'));
        $this->widgetSchema['fax'] = new sfWidgetFormInput(array(), array('style' => 'width: 85px'));
        $this->widgetSchema['website'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['order_min_amount'] = new sfWidgetFormInput(array(), array('style' => 'width: 50px; text-align: right'));

        $this->widgetSchema['presentation'] = new wpWidgetFormRichText();

        $this->widgetSchema->setLabels(array(
                'name' => 'Nom',
                'email' => 'Email',
                'phone' => 'Téléphone',
                'fax' => 'Fax',
                'website' => 'Site internet',
                'address1' => 'Adresse',
                'address2' => 'Complément',
                'zip' => 'Code postal',
                'city' => 'Ville',
                'siret_number' => 'N° SIRET',
                'order_min_amount' => 'Montant min. commande',
                ));

        $katao_supplier = $this->getObject();
        $katao_user = KataoUserPeer::retrieveByKataoSupplierId($katao_supplier->getId());

        $this->widgetSchema['user_login'] = new sfWidgetFormInput(array(), array('autocomplete' => 'off'));
        $this->validatorSchema['user_login'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_login', 'Login');
        $this->setDefault('user_login', $katao_user->getLogin());

        $this->widgetSchema['user_password'] = new sfWidgetFormInputPassword(array(), array('autocomplete' => 'off'));
        $this->validatorSchema['user_password'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_password', 'Mot de passe');
        $this->setDefault('user_password', '');

        $this->widgetSchema['user_password_confirmation'] = new sfWidgetFormInputPassword(array(), array('autocomplete' => 'off'));
        $this->validatorSchema['user_password_confirmation'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_password_confirmation', 'Confirmation');
        $this->setDefault('user_password_confirmation', '');

        $this->widgetSchema['update_gmap'] = new wpWidgetFormInputCheckbox();
        $this->widgetSchema['update_gmap']->setOption('value_attribute_value', 1);
        $this->widgetSchema->setLabel('update_gmap', 'Mettre à jour les coordonnées Google Map automatiquement');
        $this->validatorSchema['update_gmap'] = new sfValidatorPass();

        $this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('user_password', sfValidatorSchemaCompare::EQUAL, 'user_password_confirmation'));
    }

    protected function doSave($con = null) {
        $katao_user = KataoUserPeer::retrieveByKataoSupplierId($this->getObject()->getId());
        $katao_user->setLogin($this->getValue('user_login'));
        if ($this->getValue('user_password') && $this->getValue('user_password_confirmation') && $this->getValue('user_password') == $this->getValue('user_password_confirmation')) {
            $katao_user->setPassword($this->getValue('user_password'));
        }
        $katao_user->save($con);

        $katao_supplier_id = $this->getObject()->getId();
        $return = parent::doSave();

        if ($this->getValue('update_gmap')) {
            $katao_supplier =/*(KataoSupplier)*/ $this->getObject();
            $katao_supplier->initGmapCoordinates(true);
            $katao_supplier->save($con);
        }

        $katao_user->setKataoSupplierId($katao_supplier_id);
        $katao_user->save($con);

        return $return;
    }
}