<commit_before><?php
class StatistiqueStatsFilterForm extends BaseForm
{
	protected $config;
	protected $aggregatAppellation = false;
	protected static $rangeFields = array('doc.mouvements.date', 'doc.date_campagne');

	public function __construct($config, $defaults = array(), $options = array(), $CSRFSecret = null)
	{
		$this->config = $config;
		$this->aggregatAppellation = StatistiqueConfiguration::getInstance()->isAggregatAppellation();
		parent::__construct($defaults, $options, $CSRFSecret);
	}

	public function configure()
	{
		$this->setWidgets(array(
				'doc.declarant.famille' => new bsWidgetFormChoice(array('multiple' => true, 'expanded' => true, 'choices' => self::getFamilles())),
				'lastyear' => new bsWidgetFormInputCheckbox(),
				'pdf' => new bsWidgetFormInputCheckbox(),
				'doc.mouvements.date/from' => new bsWidgetFormInputDate(),
				'doc.mouvements.date/to' => new bsWidgetFormInputDate(),
				'statistiques' => new bsWidgetFormChoice(array('multiple' => false, 'expanded' => true, 'choices' => $this->getStatistiques())),
				'doc.type_transaction' => new bsWidgetFormChoice(array('multiple' => true, 'expanded' => true, 'choices' => self::getTransactions())),
				'doc.date_campagne/from' => new bsWidgetFormInputDate(),
				'doc.date_campagne/to' => new bsWidgetFormInputDate(),
		));

		$this->widgetSchema->setLabels(array(
				'doc.declarant.famille' => 'Catégorie',
				'lastyear' => 'Stat N/N-1',
				'pdf' => 'PDF',
				'statistiques' => 'Statistiques',
				'doc.type_transaction' => 'Conditionnement',
		));

		$this->setValidators(array(
				'doc.declarant.famille' => new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getFamilles()))),
				'doc.mouvements.date/from' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'doc.mouvements.date/to' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'lastyear' => new ValidatorBoolean(array('required' => false)),
				'pdf' => new ValidatorBoolean(array('required' => false)),
				'statistiques' => new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getStatistiques()))),
				'doc.type_transaction' => new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getTransactions()))),
				'doc.date_campagne/from' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'doc.date_campagne/to' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
		));

		if($this->aggregatAppellation){
			$this->setWidget('doc.mouvements.appellation' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getLibelles('appellation')), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.mouvements.appellation' , 'Appellation');
			$this->setValidator('doc.mouvements.appellation' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getLibelles('appellation')))));

			$this->setWidget('doc.appellation' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getLibelles('appellation')), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.appellation' , 'Appellation');
			$this->setValidator('doc.appellation' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getLibelles('appellation')))));
		}else{
			$this->setWidget('doc.mouvements.produit_hash' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getProduitsCepage()), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.mouvements.produit_hash' , 'Produit');
			$this->setValidator('doc.mouvements.produit_hash' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getProduitsCepage()))));

			$this->setWidget('doc.produit' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getProduitsCepage()), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.produit' , 'Produit');
			$this->setValidator('doc.produit' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getProduitsCepage()))));
		}


		$this->setDefault('pdf', true);
        $this->widgetSchema->setNameFormat('statistique_filter[%s]');
    }

    public function getStatistiques() {
    	$statistiques = array();
    	foreach ($this->config['statistiques'] as $key => $value) {
				if(!isset($value['hidden']) || !$value['hidden']){
					$statistiques[$key] = $value['libelle'];
				}
    	}
    	return $statistiques;
    }

    public static function getTransactions() {
    	$transactions = VracClient::$types_transaction_vins;
    	$libelles = VracClient::$types_transaction;
    	$result = array();
    	foreach ($transactions as $transaction) {
    		$result[$transaction] = $libelles[$transaction];
    	}
    	return $result;
    }

    public static function getFamilles() {
    	return EtablissementFamilles::getFamilles();
    }

    public static function getRegions() {
    	return array_merge(array(null => null), EtablissementClient::getRegions());
    }

    public static function getLibelles($noeud) {
        $libelles = array();
        $items = self::getItems($noeud);

        foreach($items as $key => $item) {
            $libelles[$key] = $item->getLibelle();
        }

        return $libelles;
    }

    public static function getItems($noeud) {

        return ConfigurationClient::getCurrent()->declaration->getKeys($noeud);
    }

		public static function getProduitsCepage() {
        $libelles = array();
        $items = ConfigurationClient::getCurrent()->declaration->getProduits();

        foreach($items as $key => $item) {
					  if(floatval($item->getDroitCVO(date('Y-m-d'))->taux)){
							$libelles[$key] = $item->getLibelleFormat();
						}
        }

        return $libelles;
    }


    public function canPeriodeCompare()
    {
    	$values = $this->getValues();
    	return ($values['lastyear'] && ($values['doc.mouvements.date/from'] || $values['doc.date_campagne/from']))? true : false;<commit_msg>ajout d'un try catch pour les produits qui n'ont aucun noeud de cvo<commit_after><?php
class StatistiqueStatsFilterForm extends BaseForm
{
	protected $config;
	protected $aggregatAppellation = false;
	protected static $rangeFields = array('doc.mouvements.date', 'doc.date_campagne');

	public function __construct($config, $defaults = array(), $options = array(), $CSRFSecret = null)
	{
		$this->config = $config;
		$this->aggregatAppellation = StatistiqueConfiguration::getInstance()->isAggregatAppellation();
		parent::__construct($defaults, $options, $CSRFSecret);
	}

	public function configure()
	{
		$this->setWidgets(array(
				'doc.declarant.famille' => new bsWidgetFormChoice(array('multiple' => true, 'expanded' => true, 'choices' => self::getFamilles())),
				'lastyear' => new bsWidgetFormInputCheckbox(),
				'pdf' => new bsWidgetFormInputCheckbox(),
				'doc.mouvements.date/from' => new bsWidgetFormInputDate(),
				'doc.mouvements.date/to' => new bsWidgetFormInputDate(),
				'statistiques' => new bsWidgetFormChoice(array('multiple' => false, 'expanded' => true, 'choices' => $this->getStatistiques())),
				'doc.type_transaction' => new bsWidgetFormChoice(array('multiple' => true, 'expanded' => true, 'choices' => self::getTransactions())),
				'doc.date_campagne/from' => new bsWidgetFormInputDate(),
				'doc.date_campagne/to' => new bsWidgetFormInputDate(),
		));

		$this->widgetSchema->setLabels(array(
				'doc.declarant.famille' => 'Catégorie',
				'lastyear' => 'Stat N/N-1',
				'pdf' => 'PDF',
				'statistiques' => 'Statistiques',
				'doc.type_transaction' => 'Conditionnement',
		));

		$this->setValidators(array(
				'doc.declarant.famille' => new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getFamilles()))),
				'doc.mouvements.date/from' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'doc.mouvements.date/to' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'lastyear' => new ValidatorBoolean(array('required' => false)),
				'pdf' => new ValidatorBoolean(array('required' => false)),
				'statistiques' => new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getStatistiques()))),
				'doc.type_transaction' => new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getTransactions()))),
				'doc.date_campagne/from' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
				'doc.date_campagne/to' => new sfValidatorDate(array('date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~', 'required' => false)),
		));

		if($this->aggregatAppellation){
			$this->setWidget('doc.mouvements.appellation' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getLibelles('appellation')), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.mouvements.appellation' , 'Appellation');
			$this->setValidator('doc.mouvements.appellation' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getLibelles('appellation')))));

			$this->setWidget('doc.appellation' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getLibelles('appellation')), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.appellation' , 'Appellation');
			$this->setValidator('doc.appellation' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getLibelles('appellation')))));
		}else{
			$this->setWidget('doc.mouvements.produit_hash' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getProduitsCepage()), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.mouvements.produit_hash' , 'Produit');
			$this->setValidator('doc.mouvements.produit_hash' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getProduitsCepage()))));

			$this->setWidget('doc.produit' , new bsWidgetFormChoice(array('multiple' => true, 'choices' => self::getProduitsCepage()), array('class' => 'select2 form-control')));
			$this->widgetSchema->setLabel('doc.produit' , 'Produit');
			$this->setValidator('doc.produit' , new sfValidatorChoice(array('required' => false, 'multiple' => true, 'choices' => array_keys(self::getProduitsCepage()))));
		}


		$this->setDefault('pdf', true);
        $this->widgetSchema->setNameFormat('statistique_filter[%s]');
    }

    public function getStatistiques() {
    	$statistiques = array();
    	foreach ($this->config['statistiques'] as $key => $value) {
				if(!isset($value['hidden']) || !$value['hidden']){
					$statistiques[$key] = $value['libelle'];
				}
    	}
    	return $statistiques;
    }

    public static function getTransactions() {
    	$transactions = VracClient::$types_transaction_vins;
    	$libelles = VracClient::$types_transaction;
    	$result = array();
    	foreach ($transactions as $transaction) {
    		$result[$transaction] = $libelles[$transaction];
    	}
    	return $result;
    }

    public static function getFamilles() {
    	return EtablissementFamilles::getFamilles();
    }

    public static function getRegions() {
    	return array_merge(array(null => null), EtablissementClient::getRegions());
    }

    public static function getLibelles($noeud) {
        $libelles = array();
        $items = self::getItems($noeud);

        foreach($items as $key => $item) {
            $libelles[$key] = $item->getLibelle();
        }

        return $libelles;
    }

    public static function getItems($noeud) {

        return ConfigurationClient::getCurrent()->declaration->getKeys($noeud);
    }

		public static function getProduitsCepage() {
        $libelles = array();
        $items = ConfigurationClient::getCurrent()->declaration->getProduits();

        foreach($items as $key => $item) {
						try {
							if(floatval($item->getDroitCVO(date('Y-m-d'))->taux)){
								$libelles[$key] = $item->getLibelleFormat();
							}
						} catch (sfException $e) {
							continue;
						}

        }

        return $libelles;
    }


    public function canPeriodeCompare()
    {
    	$values = $this->getValues();
    	return ($values['lastyear'] && ($values['doc.mouvements.date/from'] || $values['doc.date_campagne/from']))? true : false;
{'fraction_non_alphanumeric': 0.19481019481019482, 'fraction_numerical': 0.002618002618002618, 'mean_word_length': 4.985253456221198, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 261, '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': '1376593', 'n_tokens_mistral': 4629, 'n_tokens_neox': 4242, 'n_words': 662}