<?php

class KataoMemberProfileForm extends BaseKataoMemberForm {
    public function configure() {
        parent::configure();



        unset($this->validatorSchema['katao_assoc_member_group_list'], $this->validatorSchema['katao_node_id'], $this->validatorSchema['accounting_code'], $this->validatorSchema['accounting_code_sol'], $this->validatorSchema['initial_amount_euro'], $this->validatorSchema['initial_amount_sol'], $this->validatorSchema['is_referer'], $this->validatorSchema['is_delegate'], $this->validatorSchema['katao_member_id'], $this->validatorSchema['user_status'], $this->validatorSchema['katao_index'], $this->validatorSchema['created_at'], $this->validatorSchema['updated_at']);
        unset($this->widgetSchema['katao_assoc_member_group_list'], $this->widgetSchema['katao_node_id'], $this->widgetSchema['accounting_code'], $this->widgetSchema['accounting_code_sol'], $this->widgetSchema['initial_amount_euro'], $this->widgetSchema['initial_amount_sol'], $this->widgetSchema['is_referer'], $this->widgetSchema['is_delegate'], $this->widgetSchema['katao_member_id'], $this->widgetSchema['user_status'], $this->widgetSchema['katao_index'], $this->widgetSchema['created_at'], $this->widgetSchema['updated_at']);

        $this->widgetSchema->setLabels(array(
                'first_name' => 'Prénom',
                'last_name' => 'Nom',
                'is_anonymous' => 'Anonyme ?',
                'card_number_sol' => 'N° carte ' . wpConfig::getAdditionalCurrencyName(),
                ));

        $this->widgetSchema['is_anonymous'] = new wpWidgetFormInputCheckbox();
        $this->widgetSchema['is_anonymous']->setOption('value_attribute_value', 1);
        if ((bool)$this->getObject()->getIsAnonymous()) {
            $this->widgetSchema['is_anonymous']->setAttribute('checked', 'checked');
        }

        $katao_member = $this->getObject();
        $katao_user = KataoUserPeer::retrieveByKataoMemberId($katao_member->getId());

        $this->widgetSchema['user_email'] = new sfWidgetFormInput();
        $this->validatorSchema['user_email'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_email', 'Email');
        $this->setDefault('user_email', $katao_user->getEmail());

        $this->widgetSchema['user_phone'] = new sfWidgetFormInput();
        $this->validatorSchema['user_phone'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_phone', 'Téléphone');
        $this->setDefault('user_phone', $katao_user->getPhone());

        $this->widgetSchema['user_fax'] = new sfWidgetFormInput();
        $this->validatorSchema['user_fax'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_fax', 'Fax');
    	$this->setDefault('user_fax', $katao_user->getFax());

        $this->widgetSchema['user_address1'] = new sfWidgetFormInput();
        $this->validatorSchema['user_address1'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_address1', 'Adresse');
        $this->setDefault('user_address1', $katao_user->getAddress1());

        $this->widgetSchema['user_address2'] = new sfWidgetFormInput();
        $this->validatorSchema['user_address2'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_address2', 'Complément');
        $this->setDefault('user_address2', $katao_user->getAddress2());

        $this->widgetSchema['user_zip'] = new sfWidgetFormInput();
        $this->validatorSchema['user_zip'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_zip', 'Code postal');
        $this->setDefault('user_zip', $katao_user->getZip());

        $this->widgetSchema['user_city'] = new sfWidgetFormInput();
        $this->validatorSchema['user_city'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_city', 'Ville');
        $this->setDefault('user_city', $katao_user->getCity());

        $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::retrieveByKataoMemberId($this->getObject()->getId());
        $katao_user->setEmail($this->getValue('user_email'));
        $katao_user->setPhone($this->getValue('user_phone'));
        $katao_user->setFax($this->getValue('user_fax'));
        $katao_user->setAddress1($this->getValue('user_address1'));
        $katao_user->setAddress2($this->getValue('user_address2'));
        $katao_user->setZip($this->getValue('user_zip'));
        $katao_user->setCity($this->getValue('user_city'));
        $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'));
        }
        if ($this->getValue('update_gmap')) {
            $katao_user->initGmapCoordinates(true);
        }
        $katao_user->save($con);

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

        $katao_user->setKataoMemberId($katao_member_id);
        $katao_user->save($con);

        return $return;
    }
}