Subversion Repositories eFlore/Applications.moissonnage

Rev

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

<?php

abstract class Formateur {
        
        protected $criteresRecherche;
        protected $bdd = null;
        protected $nomSource = '';
        
        
        public function __construct($criteresRecherche, $source) {
                $this->criteresRecherche = $criteresRecherche;
                $this->nomSource = $source;
        }
        
        protected function getBdd() {
                if (is_null($this->bdd)) {
                        $this->bdd = new Bdd();
                        $nomBdd = $this->nomSource == 'floradata' ? Config::get('bdd_nom_floradata') : Config::get('bdd_nom_eflore');
                        $this->bdd->requeter("USE {$nomBdd}");
                }
                return $this->bdd;
        }
        
        public function recupererStations() {
                $stations = array();
                if ($this->nomSource == 'floradata' || $this->calculerNombreCriteresNonSpatiaux() > 0) {
                        $requeteSql = $this->construireRequeteStations();
                        $stations = $this->getBdd()->recupererTous($requeteSql);
                        if ($this->determinerFormatRetour(count($stations)) == 'maille') {
                                $maillage = new Maillage($this->criteresRecherche->bbox,
                                $this->criteresRecherche->zoom, $this->nomSource);
                                $maillage->genererMaillesVides();
                                $maillage->ajouterStations($stations);
                                $stations = $maillage->formaterSortie();
                        }
                } else {
                        $nombreStations = $this->obtenirNombreStationsDansBbox();
                        if ($this->determinerFormatRetour($nombreStations) == 'maille') {
                                $stations = $this->recupererMaillesDansBbox();
                        } else {
                                $requeteSql = $this->construireRequeteStations();
                                $stations = $this->getBdd()->recupererTous($requeteSql);
                        }
                }
                return $stations;
        }
        
        public function recupererWfs() {
                $requeteSql = $this->construireRequeteWfs();
                return $this->getBdd()->recupererTous($requeteSql);
        }
        
        protected function determinerFormatRetour($nombreStations) {
                $formatRetour = 'point';
                $zoomMaxMaillage = Config::get('zoom_maximal_maillage');
                if (isset($this->criteresRecherche->format) && $this->criteresRecherche->format == 'maille'
                        && $this->criteresRecherche->zoom <= $zoomMaxMaillage) {
                        $formatRetour = 'maille';
                } else {
                        $seuilMaillage   = Config::get('seuil_maillage');
                        if ($this->criteresRecherche->zoom <= $zoomMaxMaillage && $nombreStations > $seuilMaillage) {
                                $formatRetour = 'maille';
                        }
                }
                return $formatRetour;
        }
        
        protected function calculerNombreCriteresNonSpatiaux() {
                $nombreParametresNonSpatiaux = 0;
                $criteresAIgnorer = array('zoom', 'bbox', 'stations', 'referentiel', 'format');
                foreach ($this->criteresRecherche as $nomCritere => $valeur) {
                        if (!in_array($nomCritere, $criteresAIgnorer)) {
                                $nombreParametresNonSpatiaux ++;
                        }
                }
                return $nombreParametresNonSpatiaux;
        }
        
        abstract protected function construireRequeteStations();
        
        protected function obtenirNombreStationsDansBbox() {}
        
        protected function recupererMaillesDansBbox() {}
        
        public function recupererObservations() {
                $requeteSql = $this->construireRequeteObservations();
                $observations = $this->getBdd()->recupererTous($requeteSql);
                if ($this->nomSource != 'floradata') {
                        $this->recupererNumeroNomenclaturauxTaxons($observations);
                }
                $nomStation = $this->obtenirNomsStationsSurPoint();
                if (strlen($nomStation) == 0) {
                        $nomStation = 'station sans nom';
                }
                for ($index = 0; $index < count($observations);  $index ++) {
                        $observations[$index]['nom_station'] = $nomStation;     
                }
                return $observations;
        }
        
        abstract protected function construireRequeteObservations();
        
        protected function recupererNumeroNomenclaturauxTaxons(& $observations) {
                for ($index = 0; $index < count($observations); $index ++) {
                        if (strlen (trim($observations[$index]['nomSci'])) == 0) {
                                continue;
                        }
                        $numeroNomenclatural = isset($observations[$index]['nn']) ? $observations[$index]['nn'] : null;
                        $referentiels = Referentiel::recupererListeReferentielsDisponibles();
                        $taxon = null;
                        $indexRef = 0;
                        while ($indexRef < count($referentiels) && is_null($taxon)) {
                                $referentiel = new Referentiel($referentiels[$indexRef]);
                                $taxon = $referentiel->obtenirNumeroNomenclatural($observations[$index]['nomSci'],
                                        $numeroNomenclatural);
                                $indexRef ++;
                        }
                        if (!is_null($taxon)) {
                                $observations[$index]['nn'] = $taxon['nn'];
                                $observations[$index]['nom_referentiel'] = $taxon['referentiel'];
                        } else {
                                $observations[$index]['nn'] = '';
                        }
                }
        }
        
        abstract protected function obtenirNomsStationsSurPoint();
        
}

?>