Subversion Repositories eFlore/Applications.coel

Rev

Rev 1829 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/**
 * Classe permettant de traiter plus facilement les champs dégueulasses de la base de données COEL.
 * 
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 * @version $Id$
 * @copyright 2010
 */
class UtilTruck {
        const TYPE_AUTRE = 'AUTRE';
        const TYPE_TOTAL = 'TOTAL';
        const SEPARATEUR_TYPE_VALEUR = '##';
        const SEPARATEUR_VALEURS = ';;';
        const SEPARATEUR_DONNEES = '||';
        const VALEUR_NULL = 'NC';
        
        private $ontologie = null;
        
        public function __construct(Ontologie $ontologie = null) {
                $this->setOntologie($ontologie);
        }
        
        public function setOntologie(Ontologie $ontologie) {
                $this->ontologie = $ontologie;
        }
        
        public function construireTxtTruckSimple($chaine_a_analyser) {
                return $this->construireTxtTruck($chaine_a_analyser, false, false);
        }
        
        public static function construireTxtTruckSansMajuscule($chaine_a_analyser) {
                return $this->construireTxtTruck($chaine_a_analyser, false, true);
        }
        
        public function construireTxtTruckSansPointFinal($chaine_a_analyser) {
                return $this->construireTxtTruck($chaine_a_analyser, true, false);
        }
        
        private function construireTxtTruck($chaine_a_analyser, $majuscule = true, $point_final = true) {
                $termes = $this->traiterTxtTruck($chaine_a_analyser);
                $chaine_a_retourner = self::formaterTableauDeTxt($termes, $majuscule, $point_final);
                return $chaine_a_retourner;
        }
        
        public function traiterTxtTruck($chaine_a_analyser) {
                $termes = array();
                if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
                        $valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
                        $nbre_valeurs = count($valeurs);
                        if ($nbre_valeurs > 0) {
                                for ($i = 0; $i < $nbre_valeurs; $i++)  {
                                        $valeur = trim($valeurs[$i]);
                                        if ($valeur != '') {
                                                $valeur_formatee = $this->formaterValeurTruck($valeur);
                                                $termes[] = $valeur_formatee;
                                        }
                                }
                        }
                }
                return $termes;
        }
        
        public function getTxtTruckParPosition($chaine_a_analyser, $position = 1) {
                $retour = '';
                if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
                        $valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
                        $nbre_valeurs = count($valeurs);
                        if ($nbre_valeurs > 0) {
                                $position = $position - 1;
                                $valeur = trim($valeurs[$position]);
                                if ($valeur != '') {
                                        $retour = $this->formaterValeurTruck($valeur);
                                }
                        }
                }
                return $retour;
        }
        
        public function getTableauTruck($chaine_a_analyser) {
                $tableau_retour = array();
                if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
                        $valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
                        $nbre_valeurs = count($valeurs);
                        if ($nbre_valeurs > 0) {
                                for ($i = 0; $i < $nbre_valeurs; $i++)  {
                                        $valeur = trim($valeurs[$i]);
                                        if ($valeur != '') {
                                                $tableau_retour[] = $valeur;
                                        }
                                }
                        }
                }
                return $tableau_retour;
        }
        
        public function getNbreValeur($chaine_a_analyser) {
                $nbre_valeurs = null;
                if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != '')) {
                        $valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
                        $nbre_valeurs = count($valeurs);
                }
                return $nbre_valeurs;
        }
        
        private function formaterValeurTruck($valeur) {
                $chaine_a_retourner = '';
                
                if (preg_match('/^[^#]+##[^$]+$/', $valeur))    {
                        $cle_valeur = explode(self::SEPARATEUR_TYPE_VALEUR, $valeur);
                        $chaine_a_retourner = (($cle_valeur[1] == '' || $cle_valeur[1] == 'null') ? self::VALEUR_NULL : $cle_valeur[1]);
                        $chaine_a_retourner .= ' '.$this->formaterParenthese($cle_valeur[0]);
                } else if ($valeur != '')       {
                        $chaine_a_retourner = $valeur;
                } else {
                        trigger_error("Valeur truck posant problème :$valeur", E_USER_NOTICE);
                }
                
                return $chaine_a_retourner;
        }
        
        public function formaterParenthese($chaine_a_afficher) {
                if ($chaine_a_afficher != '') {
                        $chaine_a_afficher = '('.$chaine_a_afficher.')';
                }
                return $chaine_a_afficher;
        }
                
        public static function formaterTableauDeTxt(Array $tableau_de_txt, $majuscule = true, $point_final = true) {
                $chaine_a_afficher = '';
                $taille_du_tableau = count($tableau_de_txt);
                if ($taille_du_tableau > 0) {
                        $index_avt_dernier = $taille_du_tableau - 1;
                        for ($i = 0; $i < $taille_du_tableau; $i++)     {
                                $mot = $tableau_de_txt[$i];
                                if ($i != $index_avt_dernier) {
                                        $chaine_a_afficher .= $mot.', ';
                                } else {
                                        $chaine_a_afficher .= self::nettoyerPointFinal($mot);
                                        if ($point_final) {
                                                $chaine_a_afficher .= '.';
                                        }
                                }
                        }
                }
                if ($majuscule) {
                        $chaine_a_afficher = ucfirst($chaine_a_afficher);
                }
                return $chaine_a_afficher;
        }
        
        private static function formaterAutre($chaine_a_afficher) {
                if ($chaine_a_afficher != '') {
                        $chaine_a_afficher = ' [Autre : '.$chaine_a_afficher.']';
                }
                return $chaine_a_afficher;
        }
        
        private static function formaterOuiNon($chaine_a_formater) {
                $txt_a_retourner = '';
                if ($chaine_a_formater == '0') {
                        $txt_a_retourner = 'non';
                } else if ($chaine_a_formater == '1') {
                        $txt_a_retourner = 'oui';
                }
                return $txt_a_retourner;
        }
        
        private static function nettoyerPointFinal($mot) {
                $mot = preg_replace('/[.]$/', '', $mot);
                return $mot;
        }
        
        public function construireTxtListeOntologie($chaineAAnalyser, $valeurEstOntologie = true, $typeEstOntologie = true, $donneeEstOntologie = false) {
                $termes = array();
                $autres = array();
                $chaineAAnalyser = trim($chaineAAnalyser);
                if ($chaineAAnalyser != '') {
                        $valeurs = explode(self::SEPARATEUR_VALEURS, $chaineAAnalyser);
                        $nbreValeurs = count($valeurs);
                        if ($nbreValeurs > 0)   {
                                for ($i = 0; $i < $nbreValeurs; $i++)   {
                                        $valeur = $valeurs[$i];
                                        
                                        // VALEUR SANS TYPE 
                                        // La valeur sans type est une entrée de l'ontologie
                                        if ($valeurEstOntologie && preg_match('/^[0-9]+$/u', $valeur)) {
                                                if ($valeur == '0') {
                                                        $valeur = '';
                                                } else {
                                                        if (isset($this->ontologie)) {
                                                                $valeurOntologie = $this->ontologie->getTableauValeur($valeur);
                                                                if (isset($valeurOntologie)) {
                                                                        $valeur = $valeurOntologie['nom'];
                                                                }
                                                        } else {
                                                                $e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
                                                                trigger_error($e, E_USER_WARNING);
                                                        }
                                                }
                                        }
                                        
                                        // VALEUR AVEC TYPE
                                        // Type : AUTRE
                                        $valeurTypeAutre = self::TYPE_AUTRE.self::SEPARATEUR_TYPE_VALEUR;
                                        if (preg_match('/^'.$valeurTypeAutre.'.+$/u', $valeur)) {
                                                $txtAutre = preg_replace('/^'.$valeurTypeAutre.'/u', '', $valeur);
                                                if ($txtAutre != '') {
                                                        $autres[] = $txtAutre;
                                                }
                                                $valeur = '';
                                        }
                                        // Type correspondant à une entrée de l'ontologie
                                        if ($typeEstOntologie) {
                                                $valeurTypeOntologie = '([0-9]+)'.self::SEPARATEUR_TYPE_VALEUR;
                                                $valeurTypeAutre = '([[:alnum:]]+)'.self::SEPARATEUR_TYPE_VALEUR;
                                                if (preg_match('/^'.$valeurTypeOntologie.'.*$/u', $valeur, $match)) {// Cas type : réf. numérique
                                                        $type = $match[1];
                                                        if (isset($this->ontologie)) {
                                                                $valeurOntologieNom = $this->ontologie->getNom($type);
                                                                if (isset($valeurOntologieNom)) {
                                                                        $valeurOntologieNom .= ' : ';
                                                                        $valeur = preg_replace('/^'.$type.'/u', $valeurOntologieNom, $valeur);
                                                                }
                                                        } else {
                                                                $e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
                                                                trigger_error($e, E_USER_WARNING);
                                                        }
                                                } else if (preg_match('/^'.$valeurTypeAutre.'.*$/u', $valeur, $match)) {// Cas type : AUTRE
                                                        $type = $match[1];
                                                        $valeur = preg_replace('/^'.$type.'/u', $type.' : ', $valeur);
                                                }
                                        }
                                        // Donnée correspondant à une entrée de l'ontologie
                                        if ($donneeEstOntologie) {
                                                $donneeOntologie = self::SEPARATEUR_TYPE_VALEUR.'([0-9]+)';
                                                if (preg_match('/^.+'.$donneeOntologie.'$/u', $valeur, $match)) {
                                                        $donnee = $match[1];
                                                        $donnee = str_replace(self::SEPARATEUR_TYPE_VALEUR, '', $donnee);
                                                        if (isset($this->ontologie)) {
                                                                $valeurOntologieNom = $this->ontologie->getNom($donnee);
                                                                if (isset($valeurOntologieNom)) {
                                                                        $valeur = preg_replace('/'.$donnee.'$/u', $valeurOntologieNom, $valeur);
                                                                }
                                                        } else {
                                                                $e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
                                                                trigger_error($e, E_USER_WARNING);
                                                        }
                                                }
                                        }
                                        
                                        // Nettoyage final
                                        $valeur = preg_replace('/'.self::SEPARATEUR_TYPE_VALEUR.'/', '', $valeur);
                                        
                                        if ($valeur != '') {
                                                $termes[] = $valeur;
                                        }
                                }
                        }
                }
                
                $chaineTermes = self::formaterTableauDeTxt($termes);
                $chaineAutres = self::formaterTableauDeTxt($autres);
                $chaineARetourner = $chaineTermes.self::formaterAutre($chaineAutres);
                
                return $chaineARetourner;
        }
        
        public static function extraireNbrePart($truk_unite_base) {
                $types = explode(self::SEPARATEUR_VALEURS, $truk_unite_base);
                $nbre = 0;
                foreach ($types as $type) {
                        $unite_base = explode(self::SEPARATEUR_TYPE_VALEUR, $type);
                        $nbre_part = 0;
                        if (isset($unite_base[1])) {
                                $unite_base_info = explode('||', $unite_base[1]);
                                $nbre_part = $unite_base_info[3];
                        }
                        $nbre += $nbre_part;
                }
                return $nbre;
        }
}