<?php

/**
 * KataoMember form.
 *
 * @package form
 * @subpackage katao_member
 * @version SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class KataoMemberForm extends BaseKataoMemberForm {
    public function configure() {
        unset($this->validatorSchema['gmap_lng'], $this->validatorSchema['gmap_lat'], $this->validatorSchema['accounting_code'], $this->validatorSchema['accounting_code_sol'], $this->validatorSchema['katao_index'], $this->validatorSchema['updated_at']);
        unset($this->widgetSchema['gmap_lng'], $this->widgetSchema['gmap_lat'], $this->widgetSchema['accounting_code'], $this->widgetSchema['accounting_code_sol'], $this->widgetSchema['katao_index'], $this->widgetSchema['updated_at']);

        $sf_user = sfContext::getInstance()->getUser();

        if ($this->getObject()->isNew() && $sf_user->isJustDelegate()) {
            $this->setDefault('katao_node_id', $sf_user->getInstance()->getKataoMember()->getKataoNodeId());
        }

        $this->widgetSchema['next_katao_node_id'] = new sfWidgetFormPropelSelect(array('model' => 'KataoNode', 'add_empty' => '&mdash;&nbsp;aucun&nbsp;&mdash;'));

    	$this->widgetSchema['created_at'] = new wpWidgetFormRichDate();
    	$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('date_format' => '@(?P<day>\d{2}).(?P<month>\d{2}).(?P<year>\d{4})@'));

    	//region labels
    	$this->widgetSchema->setLabels(array(
                'created_at' => 'Date d\'adhésion',
                'katao_node_id' => 'période active',
                'next_katao_node_id' => 'suivante',
                'first_name' => 'Prénom',
                'last_name' => 'Nom',
                'is_admin' => 'Administrateur',
                'is_referer' => 'Référent',
                'is_delegate' => 'Délégué',
                'is_anonymous' => 'Anonyme ?',
                'katao_member_id' => 'Parrain',
                'is_member' => 'Adhérent ?',
                'initial_amount_euro' => 'Sole initial (€)',
                'initial_amount_sol' => 'Solde initial (' . wpConfig::getAdditionalCurrencyName() . ')',
                'card_number_sol' => 'N° carte ' . wpConfig::getAdditionalCurrencyName(),
                'member_group' => 'Groupe d\'utilisateur',
                ));
    	//endregion


        $this->widgetSchema['first_name'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->widgetSchema['last_name'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));

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

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

        $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');
        }

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

        $this->widgetSchema['katao_member_id'] = new sfWidgetFormSelect(array('choices' => KataoMemberPeer::getAllGroupedByNode(false, false, true)));

        $katao_member =/*(KataoMember)*/ $this->getObject();

		if (!$katao_member->getKataoMemberId()) {
    		$katao_member->setKataoMemberId(wpConfig::getKataoMemberId());
    	}

        $katao_user = null;
        if (!$katao_member->isNew()) {
            $katao_user = KataoUserPeer::retrieveByKataoMemberId($katao_member->getId());
        }
        if (is_null($katao_user)) {
            $katao_user = new KataoUser();
        }

        $this->widgetSchema['is_admin'] = new wpWidgetFormInputCheckbox();
        $this->widgetSchema['is_admin']->setOption('value_attribute_value', 1);
        if ((bool)$katao_user->getIsAdmin()) {
            $this->widgetSchema['is_admin']->setAttribute('checked', 'checked');
        }
        $this->validatorSchema['is_admin'] = new sfValidatorPass();

        $this->widgetSchema['user_email'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $this->validatorSchema['user_email'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
        $this->widgetSchema->setLabel('user_email', 'Email');
        $this->setDefault('user_email', $katao_user->getEmail());

        $this->widgetSchema['user_address1'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
        $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(array(), array('style' => 'width: 250px'));
        $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(array(), array('style' => 'width: 43px'));
        $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(array(), array('style' => 'width: 200px'));
        $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_phone'] = new sfWidgetFormInput(array(), array('style' => 'width: 85px'));
        $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(array(), array('style' => 'width: 85px'));
        $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_status'] = new sfWidgetFormSelect(array('choices' => KataoUserPeer::getAllStatuses()));
        $this->validatorSchema['user_status'] = new sfValidatorInteger();
        $this->widgetSchema->setLabel('user_status', 'Etat');
        $this->setDefault('user_status', $katao_user->getStatus());

        $this->widgetSchema['user_login'] = new sfWidgetFormInput();
        $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 sfWidgetFormInput();
        $this->validatorSchema['user_password'] = new sfValidatorString(array('max_length' => 255));
        $this->widgetSchema->setLabel('user_password', 'Mot de passe');
        $this->setDefault('user_password', $katao_user->getPassword());

        $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 sfValidatorCallback(array('callback' => array($this, 'checkGodfatherWithoutEmail')), array('invalid' => 'Cet adhérent est parrain. Vous devez saisir une adresse email.')));
    }

    public function checkGodfatherWithoutEmail($validator, $value) {
        $email = sfContext::getInstance()->getRequest()->getParameter('katao_member[user_email]');
        if (!$email && 0 < $this->getObject()->countKataoMembersRelatedByKataoMemberId()) {
            throw new sfValidatorError($validator, 'invalid');
        }
        return $value;
    }

    protected function doSave($con = null) {
        $katao_user =/*(KataoUser)*/ $this->getObject()->isNew()?new KataoUser():KataoUserPeer::retrieveByKataoMemberId($this->getObject()->getId());
        $katao_user->setIsAdmin($this->getValue('is_admin'));
        $katao_user->setEmail($this->getValue('user_email'));
        $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->setPhone($this->getValue('user_phone'));
        $katao_user->setFax($this->getValue('user_fax'));
        $katao_user->setStatus($this->getValue('user_status'));
        $katao_user->setLogin($this->getValue('user_login'));
        $katao_user->setPassword($this->getValue('user_password'));
        if ($this->getValue('update_gmap')) {
            $katao_user->initGmapCoordinates(true);
        }
        $katao_user->save($con);

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

        $katao_user->setKataoMember($this->getObject());
        $katao_user->save();

        return $return;
    }
}