Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 798 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

<?php
class DeterminationVernaFormateur implements Formateur {

        const TPL_VUE = 'determination_verna';

        private $parametres = null;
        private $surligneur = null;
        private $trieur = null;
        private $urls = null;
        private $fusioneur = null;
        private $manipulateurDeChaine = null;
        private $imagesService = null;

        private $motsASurligner = array();
        private $noms = array();
        private $infosPourTpl = array();
        
        private $conteneur = null;
        private $apiCartes = null;

        public function __construct(ParametresResultats $parametres, Array $resultats,
                Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
                ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {

                $this->parametres = $parametres;
                $this->noms = $resultats['resultat'];
                $this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
                $this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
                $this->urls = (is_null($urls)) ? new AppUrls() : $urls;
                $this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
                $this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
                
                $this->conteneur = new Conteneur();
                $this->apiCartes = $this->conteneur->getApiCartes();
        }

        private function chargerRepartition() {
                $this->apiCartes->setProjet('chorodep');
                $this->apiCartes->setLargeur('108x101');
                $urls = array();
                foreach ($this->noms as $nom) {
                        if (array_key_exists('nom_retenu.code', $nom)) {
                                $id = $nom['nom_retenu.code'];
                                $id = $this->supprimerCodeReftaxAvecNn($id);
                                $this->apiCartes->setId("nn:$id");
                                if (array_key_exists($id, $urls) == false) {
                                        $urls[$id] = $this->apiCartes->getUrlPng();
                                }
                        }
                }
                $this->infosPourTpl['repartition']['urls'] = $urls;
        }

        public function getTplInfos() {
                return $this->infosPourTpl;
        }

        public function getTplNom() {
                return self::TPL_VUE;
        }

        public function formater() {
                $this->obtenirUrlsPhotos();
                $this->extraireInfosNomsPourTplDetermination();
                $this->obtenirUrlsImagesCoste();
                $this->chargerRepartition();
        }
        
        private function obtenirUrlsImagesCoste() {
                $this->extraireInfosTaxons();
                $this->imagesService->setProjet('coste');
                $tax = implode(',', $this->infosPourTpl['taxons']);
                $this->imagesService->setNnTaxon($tax);
                $costeImg = $this->imagesService->getInfosImagesTaxons();
                if (!empty($costeImg)) {
                        foreach ($costeImg as  $infos) {
                                $num_taxon = $infos['num_taxonomique'];
                                $images[$num_taxon][] = $infos['binaire.href'];
                                $this->infosPourTpl['imagesCoste'] = $images;
                        }
                }
        }
        
        private function extraireInfosTaxons() {
                foreach ($this->noms as $id => $nom ) {
                        if (array_key_exists('num_taxon', $nom) 
                                && array_key_exists('taxon', $nom)) {
                                $this->infosPourTpl['taxons'][$nom['taxon']] = $nom['num_taxon'];
                        }
                }
        }

        private function obtenirUrlsPhotos() {
                $nns = $this->extraireNnDesNoms();
                $urls = $this->imagesService->getUrlsImagesParIdsNoms($nns);
                $this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesUrls($urls);
        }

        private function extraireNnDesNoms() {
                $nns = array();
                foreach ($this->noms as $id => $nom) {
                        if (array_key_exists('nom_retenu.code', $nom)) {
                                if (in_array($nom['nom_retenu.code'], $nns) == false) {
                                        $idAAjouter = $this->supprimerCodeReftaxAvecNn($nom['nom_retenu.code']);
                                        if (is_numeric($idAAjouter)) {
                                                $nns[] = $idAAjouter;
                                        }
                                }
                        }
                }
                return $nns;
        }

        private function supprimerCodeReftaxDesUrls($urls) {
                $urlsNettoyees = array();
                foreach ($urls as $id => $url) {
                        $id = $this->supprimerCodeReftax($id);
                        $urlsNettoyees[$id] = $url;
                }
                return $urlsNettoyees;
        }

        private function supprimerCodeReftax($chaine) {
                $codeReftax = $this->parametres->reftaxCourant.'.';
                $chaine = str_replace($codeReftax, '', $chaine);
                return $chaine;
        }
        
        function supprimerAccents($chaine){
                return strtr($chaine,array('à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a',
                                                                                'ç' => 'c',
                                                                                'è' => 'e','é' => 'e','ê' => 'e','ë' => 'e',
                                                                                'ì' => 'i','í' => 'i','î' => 'i','ï' => 'i',
                                                                                'ñ' => 'n',
                                                                                'ò' => 'o', 'ó' => 'o' , 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 
                                                                                'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 
                                                                                'ý' => 'y', 'ÿ' => 'y'));
        }
        

        private function extraireInfosNomsPourTplDetermination() {
                $taxons = array();
                foreach ($this->noms as $idNomCourant => $nom) {
                        $nn = $this->supprimerCodeReftaxAvecNn($nom['nom_retenu.code']);
                        $nom_min = strtolower($nom['nom_vernaculaire']);
                        $nom_ss_accent = $this->supprimerAccents($nom_min);
                        if (preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', $nom_ss_accent )) {
                                //au moins un debute par la requête
                                if (isset($taxons) && array_key_exists($nn, $taxons[0]) == false) { 
                                        $taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
                                }
                                $taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
                        } else { 
                                //contient
                                if (isset($taxons) && array_key_exists($nn, $taxons[1]) == false) {
                                        $taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
                                }
                                $taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
                        }
                }
                $this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
        }
        
        private function renvoyerInfosNomVerna($valeurs) {
                $nom_verna = array();
                $nom_verna['nn'] = $valeurs['id'];
                $nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
                return $nom_verna;
        }
        
        private function renvoyerInfosTaxon($nomSci, $nn) {
                $taxon = array();
                $taxon['nomSci'] = $nomSci;
                $taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
                $taxon['repartition_vignette'] = $this->chargerRepartition($nn);
                return $taxon;
        }

        private function supprimerCodeReftaxAvecNn($nn) {
                $codeReftax = $this->parametres->reftaxCourant.'.nn:';
                return str_replace($codeReftax, '', $nn);
        }

        //tri alphabétique des noms scientifiques par catégorie (débute par , contient )
        public function trier() {
                $verna = array();
                foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
                        $verna += $this->classerAlphabetiquement('nomSci', $valeurs);
                }
                $this->infosPourTpl['noms'] = $verna;
        }
        
        private function classerAlphabetiquement($champs, $valeurs) {
                $this->trieur->setTableau($valeurs);
                $this->trieur->setChampsEtOrdres(array($champs => SORT_NATURAL));
                return $this->trieur->trier();
        }
        
        

        public function surligner() {
                $this->definirMotsASurligner();
                foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
                        foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
                                $nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
                        }
                        $this->infosPourTpl['noms'][$idNom] = $nom;
                }
        }

        private function definirMotsASurligner() {
                $this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);

        }
        
        private function surlignerMotsMasqueRecherche($nom) {
                $this->surligneur->setTexte($nom);
                $nom = $this->surligneur->surlignerMots($this->motsASurligner);
                return $nom;
        }
}
?>