<?php

/**
 * KataoConfig form.
 *
 * @package form
 * @subpackage katao_config
 * @version SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class KataoConfigForm extends BaseKataoConfigForm {
    public function configure() {
        $this->widgetSchema['katao_member_id'] = new sfWidgetFormSelect(array('choices' => KataoMemberPeer::getAllGroupedByNode()), array('style' => 'width:250px'));
        $this->widgetSchema['siret_number'] = new sfWidgetFormInput(array(), array('maxlength' => 14));
        $this->widgetSchema['tva_number'] = new sfWidgetFormInput(array(), array('maxlength' => 13));

        $this->widgetSchema['default_product_picture'] = new wpWidgetFormInputFile(array(), array('file_src' => $this->getObject()->getDefaultProductPicture()));
        $this->validatorSchema['default_product_picture'] = new sfValidatorFile(array('required' => false));

        $this->widgetSchema['name'] = new sfWidgetFormInput(array(), array('style' => 'width:250px'));
        $this->widgetSchema['address'] = new sfWidgetFormTextarea(array(), array('style' => 'width:250px; height:50px'));
        $this->widgetSchema['phone'] = new sfWidgetFormInput(array(), array('style' => 'width:250px'));
        $this->widgetSchema['email'] = new sfWidgetFormInput(array(), array('style' => 'width:250px'));

        $this->widgetSchema['default_product_margin'] = new sfWidgetFormInput(array(), array('style' => 'width:25px'));
        $this->widgetSchema['default_product_sol_percent'] = new sfWidgetFormInput(array(), array('style' => 'width:25px'));
        $this->widgetSchema['cart_validation_days_number'] = new sfWidgetFormInput(array(), array('style' => 'width:25px'));

        $this->widgetSchema['initial_transactions_count'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['initial_transactions_sum'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['member_fee'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));

        $this->widgetSchema['accounting_code_prefix_purchase'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_prefix_sell'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_bank'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_bank_sol'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_credit'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_error_adjustment_positive'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_error_adjustment_negative'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_discount'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_sol_discount'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_fees_shipping'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));
        $this->widgetSchema['accounting_code_fees_billing'] = new sfWidgetFormInput(array(), array('style' => 'width:70px'));

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

        $this->widgetSchema->setLabels(array(
                'katao_member_id' => 'Gérant',
                'name' => 'Nom',
                'address' => 'Adresse',
                'phone' => 'Téléphone',
                'email' => 'Email',
                'siret_number' => 'N° SIRET',
                'tva_number' => 'N° TVA',
                'rcs_number' => 'N° RCS',
                'capital' => 'Capital',
                'initial_transactions_count' => 'Nombre initial',
                'initial_transactions_sum' => 'Volume initial',
                'cart_validation_days_number' => 'Délai validation paniers',
                'mailing_list_email' => 'Email newsletter',
                'accounting_code_prefix_purchase' => 'Achat (préfixe)',
                'accounting_code_prefix_sell' => 'Vente (préfixe)',
                'accounting_code_bank' => 'Banque (€)',
                'accounting_code_bank_sol' => 'Banque (' . wpConfig::getAdditionalCurrencyName() . ')',
                'accounting_code_credit' => 'Avoir (€)',
                'accounting_code_error_adjustment_positive' => 'Ecart d\'ajustement (faveur)',
                'accounting_code_error_adjustment_negative' => 'Ecart d\'ajustement (défaveur)',
                'accounting_code_discount' => 'Remise (€)',
                'accounting_code_sol_discount' => 'Remise (' . wpConfig::getAdditionalCurrencyName() . ')',
                'accounting_code_fees_shipping' => 'Frais de port',
                'accounting_code_fees_billing' => 'Frais de facturation',
                'default_product_picture' => 'Photo par défaut',
                'default_product_margin' => 'Marge par défaut',
                'default_product_sol_percent' => 'Part de ' . wpConfig::getAdditionalCurrencyName() . ' par défaut',
                'member_fee' => 'Cotisation annuelle adhérents',
                'additional_currency_name' => 'Nom monnaie complémentaire',
                'debug_email' => 'Email de redirection',
                'bank_name' => 'Nom de la banque',
                'bank_account' => 'N° de compte',
                ));
    }

    public function doSave($con = null) {
        $file = $this->getValue('default_product_picture');
        if ($file && is_object($file)) {
            if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getDefaultProductPicture())) {
                unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getDefaultProductPicture());
            }

            $filename = sha1($file->getOriginalName()) . $file->getExtension($file->getOriginalExtension());
            $file->save(sfConfig::get('sf_upload_dir') . '/repository/produits/' . $filename);
        } elseif (!empty($_REQUEST['RemovePicture'])) {
            if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getDefaultProductPicture())) {
                unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getDefaultProductPicture());
            }
            $this->getObject()->setDefaultProductPicture(null);
        }

        return parent::doSave($con);
    }

    public function updateObject($values = null) {
        $default_product_picture = $this->getObject()->getDefaultProductPicture();
        $object = parent::updateObject($values);

        $file = $this->getValue('default_product_picture');
        if ($file && is_object($file)) {
            $object->setDefaultProductPicture(str_replace(sfConfig::get('sf_web_dir'), '', $object->getDefaultProductPicture()));
        } else {
            $object->setDefaultProductPicture($default_product_picture);
        }

        return $object;
    }
}