<?php

/**
 * KataoSupplier form.
 *
 * @package katao
 * @subpackage form
 * @author Your name here
 * @version SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class KataoSupplierForm extends BaseKataoSupplierForm {
    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['presentation'], $this->validatorSchema['url_identifier'], $this->validatorSchema['created_at'], $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['presentation'], $this->widgetSchema['url_identifier'], $this->widgetSchema['created_at'], $this->widgetSchema['updated_at']);

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

        $members = array('' => '&mdash;&nbsp;aucun&nbsp;&mdash;');
        foreach (KataoMemberPeer::getAllGroupedByNode() as $member_id => $member_name) {
            $members[$member_id] = $member_name;
        }

        $referers = array('' => '&mdash;&nbsp;aucun&nbsp;&mdash;');
        foreach (KataoMemberPeer::getAllReferers() as $referer_id => $referer_name) {
            $referers[$referer_id] = $referer_name;
        }

        $this->widgetSchema['member_id'] = new sfWidgetFormSelect(array('choices' => $members));
        $this->widgetSchema['referer_id'] = new sfWidgetFormSelect(array('choices' => $referers));

        $this->widgetSchema['is_global'] = new sfWidgetFormSelect(array('choices' => array(0 => 'Local', 1 => 'National')), array('onchange' => 'if("0" == $("katao_supplier_is_global").value) { $("katao_supplier_node_list_row").show(); } else { $("katao_supplier_node_list_row").hide(); }'));
        $this->widgetSchema['katao_supplier_node_list'] = new sfWidgetFormPropelSelectMany(array('model' => 'KataoNode'));
        $this->widgetSchema['katao_supplier_product_category_list'] = new sfWidgetFormPropelSelectMany(array('model' => 'KataoProductCategory', 'order_by' => array('SortOrder', 'ASC')));
        $this->widgetSchema['siret_number'] = new sfWidgetFormInput(array(), array('maxlength' => 14, 'onchange' => $sf_user->isJustDelegate()?'checkSiretNumberAvailability();':''));

        $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->setLabels(array(
                'member_id' => 'Compte adhérent lié',
                'referer_id' => 'Référent',
                'name' => 'Nom',
                'email' => 'Email',
                'address1' => 'Adresse',
                'address2' => 'Complément',
                'zip' => 'Code postal',
                'city' => 'Ville',
                'phone' => 'Téléphone',
                'fax' => 'Fax',
                'website' => 'Site internet',
                'siret_number' => 'N° SIRET',
                'is_global' => 'Type',
                'order_min_amount' => 'Montant min. commande',
                'katao_supplier_node_list' => 'Noeuds autorisés',
                'katao_supplier_product_category_list' => 'Rayons proposés',
                ));

        $katao_supplier = $this->getObject();

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

        $this->widgetSchema['user_status'] = new sfWidgetFormSelect(array('choices' => KataoUserPeer::getAllStatuses(true)));
        $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();

        if ($sf_user->isJustDelegate()) {
            $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkSiretNumber')), array('invalid' => 'Un fournisseur national existe déjà avec ce n° SIRET.')));
        }
    }

    public function checkSiretNumber($validator, $value) {
        $sf_request = sfContext::getInstance()->getRequest();
        $id = $sf_request->getParameter('katao_supplier[id]');
        $siret_number = $sf_request->getParameter('katao_supplier[siret_number]');
        if (null != $katao_supplier = KataoSupplierPeer::retrieveBySiretNumber($siret_number, $id)) {
            if ($katao_supplier->getIsGlobal()) {
                throw new sfValidatorError($validator, 'invalid');
            }
        }
        return $value;
    }

    protected function doSave($con = null) {
        $katao_user =/*(KataoUser)*/ $this->getObject()->isNew()?new KataoUser():KataoUserPeer::retrieveByKataoSupplierId($this->getObject()->getId());
        $katao_user->setStatus($this->getValue('user_status'));
        $katao_user->setLogin($this->getValue('user_login'));
        $katao_user->setPassword($this->getValue('user_password'));
        $katao_user->save($con);

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

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

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

        return $return;
    }
}