<commit_before><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ParcellaireAjoutParcelleForm
 *
 * @author mathurin
 */
abstract class ParcellaireParcelleForm extends acCouchdbObjectForm {

    public function configure() {
        $appellationNode = $this->getAppellationNode();

        $hasLieuEditable = $appellationNode->getConfig()->hasLieuEditable();
        $produits = $this->getProduits();
        $communes = $this->getCommunes();
        $this->setWidget('commune', new sfWidgetFormChoice(array('choices' => $communes)));
        $this->setWidget('section', new sfWidgetFormInput());
        $this->setWidget('numero_parcelle', new sfWidgetFormInput());
        $this->setWidget('superficie', new sfWidgetFormInputFloat(array('float_format' => '%01.2f')));

        if (!$hasLieuEditable) {
            $this->setWidget('lieuCepage', new sfWidgetFormChoice(array('choices' => $produits)));
        } else {
            $this->setWidget('cepage', new sfWidgetFormChoice(array('choices' => $produits)));
            $this->setWidget('lieuDit', new sfWidgetFormInput());
        }

        $this->widgetSchema->setLabel('commune', 'Commune :');
        $this->widgetSchema->setLabel('section', 'Section :');
        $this->widgetSchema->setLabel('numero_parcelle', 'Numéro :');
        if (!$hasLieuEditable) {
            $this->widgetSchema->setLabel('lieuCepage', 'Lieu-dit/cépage :');
        } else {
            $this->widgetSchema->setLabel('lieuDit', 'Lieu Dit:');
            $this->widgetSchema->setLabel('cepage', 'Cépage :');
        }

        $this->setValidator('commune', new sfValidatorChoice(array('required' => true,'choices' => array_keys($communes)), array('required' => "Aucune commune saisie.")));
        $this->setValidator('section', new sfValidatorRegex(array("required" => true, "pattern" => "/^[0-9A-Z]+$/"), array("invalid" => "La section doit être composée de numéro et lettres en majuscules")));
        $this->setValidator('numero_parcelle',new sfValidatorRegex(array("required" => true, "pattern" => "/^[0-9]+$/"), array("invalid" => "Le numéro doit être un nombre")));

        if (!$hasLieuEditable) {
            $this->setValidator('lieuCepage', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($produits)), array('required' => "Aucun cépage saisie.")));
        } else {
            $this->setValidator('cepage', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($produits)), array('required' => "Aucun cépage saisie.")));
            $this->setValidator('lieuDit', new sfValidatorString(array('required' => true)));
        }

        $this->setValidator('superficie', new sfValidatorNumber(array('required' => true)));

        $this->widgetSchema->setNameFormat('parcellaire_parcelle[%s]');
    }

    public function getProduits() {
        $appellationNode = $this->getAppellationNode();

        $this->allCepagesAppellation = array();
        foreach ($appellationNode->getConfig()->getProduitsFilter(_ConfigurationDeclaration::TYPE_DECLARATION_PARCELLAIRE) as $key => $cepage) {
            $keyCepage = str_replace('/', '-', $key);
            $libelleCepage = $cepage->getLibelleLong();
            $lieu = $cepage->getCouleur()->getLieu();
            $libelleLieu = $lieu->getLibelle();
            $this->allCepagesAppellation[$keyCepage] = trim($libelleLieu . ' ' . $libelleCepage);
        }
        
        asort($this->allCepagesAppellation);
        $this->allCepagesAppellation = array_merge(array('' => ''), $this->allCepagesAppellation);
        return $this->allCepagesAppellation;
    }

    public function getCommunes() {
       $config = $this->getObject()->getDocument()->getConfiguration();
       $communes = array();
       foreach($config->communes as $communeName => $dpt) {
       $communes[strtoupper($communeName)] = $communeName;           
       }
       return array_merge(array('' => ''), $communes);
    }

    protected function doUpdateObject($values) {
        
        if ((!isset($values['commune']) || empty($values['commune'])) ||
                (!isset($values['section']) || empty($values['section'])) ||
                (!isset($values['numero_parcelle']) || empty($values['numero_parcelle']))
        ) {
            return;
        }

        $config = $this->getObject()->getDocument()->getConfiguration();
        $commune = $values['commune'];
        $section = preg_replace('/^0*/','',$values['section']);
        $numero_parcelle = preg_replace('/^0*/','',$values['numero_parcelle']);
        $lieu = null;
        $dpt = $config->communes[$commune]; 
        
        if (!$this->getAppellationNode()->getConfig()->hasLieuEditable()) {
            $cepage = $values['lieuCepage'];
        } else {
            $cepage = $values['cepage'];
            $lieu = $values['lieuDit'];
        }
       
        $parcelle = $this->getObject()->getDocument()->addParcelleForAppellation($this->getAppellationNode()->getKey(), $cepage, $commune, $section, $numero_parcelle, $lieu, $dpt);

        $parcelle->superficie = $values['superficie'];

        if($this->getObject() instanceof ParcellaireCepageDetail && $this->getObject()->getHash() != $parcelle->getHash()) {
            $this->getObject()->getCepage()->detail->remove($this->getObject()->getKey());
        }
    }

    public function getLieuDetailForAutocomplete() {
        $lieuxDetail = array();
        foreach ($this->getAppellationNode()->getLieuxEditable() as $libelle) {
            $lieuxDetail[] = $libelle;
        }
        $entries = array();
        foreach($lieuxDetail as $lieu) {
            $entry = new stdClass();
            $entry->id = trim($lieu);
            $entry->text = trim($lieu);                    
            $entries[] = $entry;
        }
        return $entries;
    }
    
}<commit_msg>Fixe bad merge<commit_after><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ParcellaireAjoutParcelleForm
 *
 * @author mathurin
 */
abstract class ParcellaireParcelleForm extends acCouchdbObjectForm {

    public function configure() {
        $appellationNode = $this->getAppellationNode();

        $hasLieuEditable = $appellationNode->getConfig()->hasLieuEditable();
        $produits = $this->getProduits();
        $communes = $this->getCommunes();
        $this->setWidget('commune', new sfWidgetFormChoice(array('choices' => $communes)));
        $this->setWidget('section', new sfWidgetFormInput());
        $this->setWidget('numero_parcelle', new sfWidgetFormInput());
        $this->setWidget('superficie', new sfWidgetFormInputFloat(array('float_format' => '%01.2f')));

        if (!$hasLieuEditable) {
            $this->setWidget('lieuCepage', new sfWidgetFormChoice(array('choices' => $produits)));
        } else {
            $this->setWidget('cepage', new sfWidgetFormChoice(array('choices' => $produits)));
            $this->setWidget('lieuDit', new sfWidgetFormInput());
        }

        $this->widgetSchema->setLabel('commune', 'Commune :');
        $this->widgetSchema->setLabel('section', 'Section :');
        $this->widgetSchema->setLabel('numero_parcelle', 'Numéro :');
        if (!$hasLieuEditable) {
            $this->widgetSchema->setLabel('lieuCepage', 'Lieu-dit/cépage :');
        } else {
            $this->widgetSchema->setLabel('lieuDit', 'L<NAME>:');
            $this->widgetSchema->setLabel('cepage', 'Cépage :');
        }

        $this->setValidator('commune', new sfValidatorChoice(array('required' => true,'choices' => array_keys($communes)), array('required' => "Aucune commune saisie.")));
        $this->setValidator('section', new sfValidatorRegex(array("required" => true, "pattern" => "/^[0-9A-Z]+$/"), array("invalid" => "La section doit être composée de numéro et lettres en majuscules")));
        $this->setValidator('numero_parcelle',new sfValidatorRegex(array("required" => true, "pattern" => "/^[0-9]+$/"), array("invalid" => "Le numéro doit être un nombre")));

        if (!$hasLieuEditable) {
            $this->setValidator('lieuCepage', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($produits)), array('required' => "Aucun cépage saisie.")));
        } else {
            $this->setValidator('cepage', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($produits)), array('required' => "Aucun cépage saisie.")));
            $this->setValidator('lieuDit', new sfValidatorString(array('required' => true)));
        }

        $this->setValidator('superficie', new sfValidatorNumber(array('required' => true)));

        $this->widgetSchema->setNameFormat('parcellaire_parcelle[%s]');
    }

    public function getProduits() {
        $appellationNode = $this->getAppellationNode();
        $this->allCepagesAppellation = array();
        if ($appellationNode == ParcellaireClient::APPELLATION_VTSGN) {
            $this->allCepagesAppellation = $this->getProduitsForVtSGN();
        } else {
            foreach ($appellationNode->getConfig()->getProduitsFilter(_ConfigurationDeclaration::TYPE_DECLARATION_PARCELLAIRE) as $key => $cepage) {
                $keyCepage = str_replace('/', '-', $key);
                $libelleCepage = $cepage->getLibelleLong();
                $lieu = $cepage->getCouleur()->getLieu();
                $libelleLieu = $lieu->getLibelle();
                $this->allCepagesAppellation[$keyCepage] = trim($libelleLieu . ' ' . $libelleCepage);
            }
        }

        asort($this->allCepagesAppellation);
        $this->allCepagesAppellation = array_merge(array('' => ''), $this->allCepagesAppellation);
        return $this->allCepagesAppellation;
    }

    public function getCommunes() {
       $config = $this->getObject()->getDocument()->getConfiguration();
       $communes = array();
       foreach($config->communes as $communeName => $dpt) {
       $communes[strtoupper($communeName)] = $communeName;           
       }
       return array_merge(array('' => ''), $communes);
    }

    protected function doUpdateObject($values) {
        
        if ((!isset($values['commune']) || empty($values['commune'])) ||
                (!isset($values['section']) || empty($values['section'])) ||
                (!isset($values['numero_parcelle']) || empty($values['numero_parcelle']))
        ) {
            return;
        }

        $config = $this->getObject()->getDocument()->getConfiguration();
        $commune = $values['commune'];
        $section = preg_replace('/^0*/','',$values['section']);
        $numero_parcelle = preg_replace('/^0*/','',$values['numero_parcelle']);
        $lieu = null;
        $dpt = $config->communes[$commune]; 
        
        if (!$this->getAppellationNode()->getConfig()->hasLieuEditable()) {
            $cepage = $values['lieuCepage'];
        } else {
            $cepage = $values['cepage'];
            $lieu = $values['lieuDit'];
        }
       
        $parcelle = $this->getObject()->getDocument()->addParcelleForAppellation($this->getAppellationNode()->getKey(), $cepage, $commune, $section, $numero_parcelle, $lieu, $dpt);

        $parcelle->superficie = $values['superficie'];

        if($this->getObject() instanceof ParcellaireCepageDetail && $this->getObject()->getHash() != $parcelle->getHash()) {
            $this->getObject()->getCepage()->detail->remove($this->getObject()->getKey());
        }
    }

    public function getLieuDetailForAutocomplete() {
        $lieuxDetail = array();
        if ($this->getAppellationNode() == ParcellaireClient::APPELLATION_VTSGN) {
            $allAppellationsKeys = array_keys(ParcellaireClient::getInstance()->getAppellationsKeys());
            foreach ($allAppellationsKeys as $appellationKey) {
                $appellationNode = $this->getObject()->getAppellationNodeFromAppellationKey($appellationKey, true);
                foreach ($appellationNode->getLieuxEditable() as $libelle) {
                    $lieuxDetail[] = $libelle;
                }
                $entries = array();
                foreach ($lieuxDetail as $lieu) {
                    $entry = new stdClass();
                    $entry->id = trim($lieu);
                    $entry->text = trim($lieu);
                    $entries[] = $entry;
                }
                return $entries;
            }
        } else {
            foreach ($this->getAppellationNode()->getLieuxEditable() as $libelle) {
                $lieuxDetail[] = $libelle;
            }
            $entries = array();
            foreach ($lieuxDetail as $lieu) {
                $entry = new stdClass();
                $entry->id = trim($lieu);
                $entry->text = trim($lieu);
                $entries[] = $entry;
            }
            return $entries;
        }
    }
    
}
{'fraction_non_alphanumeric': 0.17223453864383456, 'fraction_numerical': 0.0013760415870346305, 'mean_word_length': 2.91442250149611, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 217, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3526660', 'n_tokens_mistral': 3913, 'n_tokens_neox': 3845, 'n_words': 612}