Subversion Repositories eFlore/Applications.moissonnage

Rev

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

<?php



class Observations {

private $longitude = null;
        private $latitude = null;

        private $utilisateur = '';
        private $taxon = null;
        private $referentiel = '';
        private $sousTaxons = array();
                
        private $bdd;
        
        
        public function __construct() {}
        
        
        public function consulter($ressources, $parametres) {
                extract($parametres);
                $this->utilisateur = $utilisateur != '*' ? $utilisateur : '';
                $this->departement = $dept != '*' ? $dept : '';
                $this->traiterParametresCoordonnees($parametres);
                if (is_null($this->longitude) || is_null($this->latitude)) {
                        $message = "Recherche des observations impossible : coordonnees de la station hors du plan";
                        return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
                }
                        
                // recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite
                if ($referentiel != '*') {
                        $this->referentiel = $this->verifierExistenceReferentiel($referentiel);
                        if (is_null($this->referentiel)) {
                                $message = "Recherche des stations impossible : referentiel inconnu\n"
                                        + "Voici les referentiels disponibles : "
                                        + implode(', ', Referentiel::listeReferentielsDisponibles());
                                return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
                        }
                }
                
                if ($num_taxon != '*') {
                        $this->traiterParametresTaxon($num_taxon);
                        if (is_null($this->taxon)) {
                                $message = "Recherche des stations impossible : taxon non trouve dans les referentiels";
                                return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
                        }
                        // recuperer les sous-taxons
                        $referentiel = new Referentiel($this->referentiel);
                        $this->sousTaxons = $referentiel->recupererSousTaxons();
                }
                
                // recuperer les informations sur les stations repondant a ces parametres
                $observations = $this->recupererObservations();
                $nomSite = $this->obtenirNomsStations();
                
                // mettre en forme les informations au format JSON
                $formateurJSON = new FormateurJson();
                $donneesFormatees = $formateurJSON->formaterObservations($observations, $nomSite);
                return $donneesFormatees;
        }
        



        //-------------------------------------------------------------------------------
        //    Recuperation et verification des parametres
        //-------------------------------------------------------------------------------
        
private function traiterParametresCoordonnees($parametres) {
                $this->longitude = $this->verifierCoordonnees($parametres['longitude'], 'lng');
                $this->latitude  = $this->verifierCoordonnees($parametres['latitude'],  'lat');
        }
        
        private function verifierCoordonnees($valeur, $axe) {
                $limites = array(
                        'ouest' => floatval(Config::get('carte.limite_ouest')),
                        'est'   => floatval(Config::get('carte.limite_est')),
                        'sud'   => floatval(Config::get('carte.limite_sud')),
                        'nord'  => floatval(Config::get('carte.limite_nord'))
                );
                if (($axe=='lng' && floatval($valeur) >= $limites['ouest'] && floatval($valeur) <= $limites['est'])
                        || ($axe=='lat' && floatval($valeur) >= $limites['sud'] && floatval($valeur) <= $limites['nord'])) {
                        return $valeur;
                }
                
                return null;
        }
        
        private function traiterParametresTaxon($numeroTaxon) {
                if ($numeroTaxon != '*') {
                        if ($this->referentiel != '') {
                                $referentiel = new Referentiel($this->referentiel);
                                $this->taxon = $referentiel->recupererTaxon($numeroTaxon);
                        } else {
                                $this->taxon = $this->verifierExistenceTaxon($numeroTaxon);
                        }
                }
        }
        
        private function verifierExistenceReferentiel($referentiel) {
                if ($referentiel == '*') {
                        return '';
                }
                $listeReferentiels = Referentiel::listeReferentielsDisponibles();
                foreach ($listeReferentiels as $nomReferentiel) {
                        if (strstr($nomReferentiel, $referentiel) !== false) {
                                $this->referentiel = $nomReferentiel;
                                break;
                        }
                }
                return null;
        }
        
        private function verifierExistenceTaxon($numTaxon) {
                $listeReferentiels = Referentiel::listeReferentielsDisponibles();
                // tester chaque referentiel jusqu'a trouver un taxon correspondant
                // a tous les criteres demandes dans les masques
                foreach ($listeReferentiels as $nomReferentiel) {
                        $referentiel = new Referentiel($nomReferentiel);
                        $taxon = $referentiel->recupererTaxon($numTaxon);
                        if (!is_null($taxon)) {
                                $this->referentiel = $nomReferentiel;
                                return $taxon;
                        }
                }
                return null;
        }



        //-------------------------------------------------------------------------------
        //    Recuperation des donnees dans la base de donnees
        //    et mise sous forme d'objet geographique en fonction du niveau de zoom
        //-------------------------------------------------------------------------------
        
        private function getBdd() {
                if (!isset($this->bdd)) {
                        $this->bdd = new Bdd();
                }
                $this->bdd->requeter("USE tb_eflore");
                return $this->bdd;
        }
        

        private function recupererObservations() {
                $requete = 'SELECT ' . $this->construireColonnesSelection() . ' FROM sophy_tapir WHERE '
                        . $this->construireCriteresTaxon() . ' lieu_station_latitude=' . $this->latitude
                        . ' AND lieu_station_longitude=' . $this->longitude;
                $observations = $this->getBdd()->recupererTous($requete);
                
                foreach ($observations as $obs) {
                        // recuperer le numero du taxon en parcourant les referentiels
                        // jusqu'a retrouver le taxon 
                        $nn = null;
                        if (!is_null($this->taxon)) {
                                $nn = $this->taxon['nn'];
                        } elseif (strlen(trim($obs['nomSci'])) > 0) {
                                // recuperer le nom du taxon
                                $taxon = $this->obtenirNumeroTaxon($obs['nomSci']);
                                $nn = $taxon['nn'];
                        }
                        $obs['nn'] = $nn;
                        if (!is_null($nn)) {
                                $obs['urlEflore'] = "http://www.tela-botanica.org/bdtfx-nn-{$nn}";
                        }
                }
                
                return $observations;
        }

        private function construireColonnesSelection() {
                $colonnes = array(
                        'idObs' => 'observation_id',
                        'nomSci' => 'nom_scientifique_complet',
                        'date' => 'observation_date',
                        'lieu' => 'lieu_station_nom',
                        'observateur' => "observateur_nom_complet",
                );
                $selectSql = '';
                foreach ($colonnes as $renvoi => $colonne) {
                        $selectSql .= (strlen($selectSql) == 0 ? '' : ', ') . "{$colonne} AS {$renvoi}";
                }
                return $selectSql;
        }
        
        private function construireCriteresTaxon() {
                if (is_null($this->taxon)) {
                        return '';
                }
                $criteres = '(nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom']) . '%\'';
                foreach ($this->sousTaxons as $sousTaxon) {
                        $criteres .= ' OR nom_scientifique_complet LIKE \'' . addslashes($sousTaxon['nom']) . '%\'';
                }
                $criteres .= ') AND ';
                return $criteres;
        }
                
        private function obtenirNomsStations() {
                $critereTaxon = '';
                if (!is_null($this->taxon)) {
                        $critereTaxon = 'nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom_sci'])
                        . '%\' AND ';
                }
                $requete = 'SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE ' . $critereTaxon
                        . ' lieu_station_latitude=' . $this->latitude . ' AND lieu_station_longitude='
                        . $this->longitude;
                $stations = $this->getBdd()->recupererTous($requete);
                $nomsStations = array();
                foreach ($stations as $station) {
                        $nomsStations[] = $station['lieu_station_nom'];
                }
                return implode(', ', $nomsStations);
        }
        
        
        
        private function obtenirNumeroTaxon($nomScientifique) {
                $masque = array('masque.ns' => $nomScientifique);
                $listeReferentiels = Referentiel::listeReferentielsDisponibles();
                // tester chaque referentiel jusqu'a trouver un taxon correspondant
                // a tous les criteres demandes dans les masques
                foreach ($listeReferentiels as $nomReferentiel) {
                        $referentiel = new Referentiel($nomReferentiel);
                        $taxon = $referentiel->recupererTaxon($masque);
                        if (!is_null($taxon)) {
                                return $taxon;
                        }
                }
        }
        
}

?>