<?php

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

        unset($this->validatorSchema['is_anonymous'], $this->validatorSchema['katao_node_id'], $this->validatorSchema['accounting_code'], $this->validatorSchema['accounting_code_sol'], $this->validatorSchema['initial_amount'], $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['is_anonymous'], $this->widgetSchema['katao_node_id'], $this->widgetSchema['accounting_code'], $this->widgetSchema['accounting_code_sol'], $this->widgetSchema['initial_amount'], $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 <sup>*</sup>',
                'last_name' => 'Nom <sup>*</sup>',
                ));

        $katao_member = new KataoMember();
        $katao_user = new KataoUser();

        $this->validatorSchema['first_name'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre prénom'));
        $this->validatorSchema['last_name'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre nom'));

        $this->widgetSchema['user_email'] = new sfWidgetFormInput();
        $this->validatorSchema['user_email'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre email'));
        $this->widgetSchema->setLabel('user_email', 'Email <sup>*</sup>');

        $this->widgetSchema['user_phone'] = new sfWidgetFormInput();
        $this->validatorSchema['user_phone'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre numéro de téléphone'));
        $this->widgetSchema->setLabel('user_phone', 'Téléphone <sup>*</sup>');

        $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->widgetSchema['user_address1'] = new sfWidgetFormInput();
        $this->validatorSchema['user_address1'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre adresse'));
        $this->widgetSchema->setLabel('user_address1', 'Adresse <sup>*</sup>');

        $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->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->widgetSchema['user_city'] = new sfWidgetFormInput();
        $this->validatorSchema['user_city'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre ville'));
        $this->widgetSchema->setLabel('user_city', 'Ville <sup>*</sup>');

        $this->widgetSchema['user_login'] = new sfWidgetFormInput();
        $this->validatorSchema['user_login'] = new sfValidatorString(array('max_length' => 255), array('required' => 'Veuillez entrer votre identifiant'));
        $this->widgetSchema->setLabel('user_login', 'Identifiant <sup>*</sup>');

        $this->widgetSchema['user_situation'] = new sfWidgetFormSelectRadio(array('choices' => sfConfig::get('app_solviolette_situation')));
        $this->validatorSchema['user_situation'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_situation', 'Situation <sup>*</sup>');
        $this->setDefault('user_situation', 'etudiant');

        $years = range(date('Y', strtotime('- 100 year')), date('Y'));
        $this->widgetSchema['user_birthday'] = new sfWidgetFormI18nDate(array('culture' => 'fr', 'years' => array_combine($years, $years)));
        $this->validatorSchema['user_birthday'] = new sfValidatorDate(array('required' => false), array('invalid' => "Veuillez entrer une date valide"));
        $this->widgetSchema->setLabel('user_birthday', 'Date de naissance');

        $this->widgetSchema['user_adhesion'] = new sfWidgetFormSelectRadio(array('choices' => sfConfig::get('app_solviolette_adhesion')));
        $this->validatorSchema['user_adhesion'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_adhesion', 'Situation');
        $this->setDefault('user_adhesion', 'todo');
        // $this->widgetSchema['user_solde'] = new sfWidgetFormInputCheckbox();
        // $this->validatorSchema['user_solde'] = new sfValidatorBoolean();
        // $this->widgetSchema->setLabel('user_solde', 'Je souhaite changer des euros pour un montant de :');
        $this->widgetSchema['user_amount'] = new sfWidgetFormInput();
        $this->validatorSchema['user_amount'] = new sfValidatorInteger(array('required' => false), array('invalid' => 'Le montant doit etre un nombre entier positf.'));
        $this->widgetSchema->setLabel('user_amount', 'Montant');

        $this->widgetSchema['user_payment_mode'] = new sfWidgetFormSelect(array('choices' => array('' => '&mdash;&nbsp;aucun&nbsp;&mdash;', KataoMemberDeposit::PAYMENT_MODE_OTHER => 'Carte Bleue', KataoMemberDeposit::PAYMENT_MODE_CHECK => 'Chèque', KataoMemberDeposit::PAYMENT_MODE_CASH => 'Espèces')));
        $this->validatorSchema['user_payment_mode'] = new sfValidatorPass();
        $this->widgetSchema->setLabel('user_payment_mode', 'Mon moyen de paiement :');
        $this->setDefault('user_payment_mode', KataoMemberDeposit::PAYMENT_MODE_OTHER);

        $this->widgetSchema['parrain_name'] = new sfWidgetFormInput();
        $this->validatorSchema['parrain_name'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('parrain_name', 'Nom du parrain');

        $this->widgetSchema['card_number_sol'] = new sfWidgetFormInput();
        $this->validatorSchema['card_number_sol'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('card_number_sol', 'N° de carte d\'adhérent sol<br /><small>si vous en avez une</small>');

        $this->widgetSchema['no_spam'] = new sfWidgetFormInputHidden();
        $this->setDefault('no_spam', '');

        $this->widgetSchema['user_password'] = new sfWidgetFormInputPassword(array(), array('autocomplete' => 'off'));
        $this->validatorSchema['user_password'] = new sfValidatorString(array('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('required' => false));
        $this->widgetSchema->setLabel('user_password_confirmation', 'Confirmation');
        $this->setDefault('user_password_confirmation', '');

        $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(new sfValidatorSchemaCompare('user_password', sfValidatorSchemaCompare::EQUAL, 'user_password_confirmation', array(), array('invalid' => 'le mot de passe et la confirmation doivent etre les mêmes')),
                    new wpValidatorSolVioletteSubscribe(),
                    )));
    }

    protected function doSave($con = null) {
        $katao_member = /*(KataoMember)*/$this->getObject();
        $katao_member->setIsMember(false);
        $katao_member->setIsAnonymous(true);
        $katao_member->setKataoNodeId(sfConfig::get('app_solviolette_adhesion_node'));

        $katao_user = new KataoUser();
        $katao_user->setStatus(KataoUser::STATUS_PENDING);
        $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->slug($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_member->setKataoNodeRelatedByKataoNodeId(sfContext::getInstance()->getUser()->getActiveNode()?sfContext::getInstance()->getUser()->getActiveNode():KataoNodePeer::getDefaultOne());
        $katao_member->setIsMember(false);

        $return = parent::doSave($con);

        $katao_user->setKataoMember($katao_member);
        $katao_user->save();

        return $return;
    }

    protected function slug($text, $default = '-') {
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
        $text = trim($text, '-');
        if (function_exists('iconv')) {
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }
        $text = strtolower($text);
        $text = preg_replace('~[^-a-zA-Z0-9]+~', '', $text);
        if (empty($text) || ('-' == $text)) {
            $text = $default;
        }
        return $text;
    }
}