Subversion Repositories eFlore/Applications.moissonnage

Rev

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

<?php


/**
 * Classe qui va effectuer toutes les operations liees aux referentiels ou rechercher
 * des informations sur les taxons dans les tables des referentiels dans la base de donnees
 * 
 * Operations a utiliser :
 *   - recupererListeReferentielsDisponibles (statique) : recherche dans les fichiers de configuration
 *     la liste des referentiels disponibles et les renvoie dans un tableau
 *
 *   - chargerTaxon : recherche dans la table possedant le meme nom que le nom du referentiel en attribut
 *     les informations generales sur un taxon (numero nomenclatural retenu, numero taxonomique,
 *     nom scientifique, rang) a partir de son numero taxonomique
 *     On stockera dans un attribut un tableau associatif contenant ces informations + le nom du referentiel,
 *     ou la valeur nulle si aucun taxon n'a ete trouve
 *
 *   - recupererSousTaxons : a partir du numero nomenclatural d'un taxon, la fonction va recuperer
 *     tous les taxons de rang inferieur de maniere recursive jusqu'en ne plus en trouver de nouveaux
 *     la recherche s'effectuera seulement au niveau des rangs famille, genre, espece et sous-espece
 *     pour limiter le nombre d'informations qui sera a la fin renvoye
 *
 *   - obtenirNumeroNomenclatural : recherche le numero nomenclatural d'un taxon a partir de son nom scientifique
 *
 * @package framework-0.3
 * @author Alexandre GALIBERT <alexandre.galibert@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 2013 Tela Botanica (accueil@tela-botanica.org)
 * 
 */



class Referentiel {
        
        private $nomComplet;
        private $nomCourt;
        private $taxon;
        private $bdd = null;
        
        private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
        private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
        private $champs;
        
        
        public function __construct($nomReferentiel, $taxon = null) {
                $this->nomComplet = $nomReferentiel;
                $this->nomCourt = current(explode('_', $nomReferentiel));
                $proprieteChamps = 'nomsChamps' . ucfirst($this->nomCourt);
                foreach ($this->$proprieteChamps as $nomCourt => $nomChamp) {
                        $this->champs[$nomCourt] = $nomChamp;
                }
                $this->taxon = $taxon;
        }
                
        public function renvoyerNomCompletReferentiel() {
                return $this->nomComplet;
        }
        
        public function renvoyerTaxon() {
                return $this->taxon;
        }
        
        public function chargerTaxon($typeNumero, $numTaxon) {
                $taxon = null;
                // verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
                // que de nombres entiers avant de construire la requete SQL a executer
                if (preg_match('/^\d+$/', $numTaxon) == 1) {
                        $champWhere = $typeNumero == 'nt' ? $this->champs['nt']: 'num_nom';
                        $requete = 
                                "SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
                                $this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
                                $champWhere."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
                                "ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
                        $taxon = $this->getBdd()->recuperer($requete);
                        if ($taxon == false) {
                                $taxon = null;
                        } else {
                                $taxon['referentiel'] = $this->nomComplet;
                        }
                }
                $this->taxon = $taxon;
        }
        
        private function getBdd() {
                if (is_null($this->bdd)) {
                        $this->bdd = new Bdd();
                }
                $nomBdd = Config::get('bdd_nom');
                $this->bdd->requeter("USE ".$nomBdd);
                return $this->bdd;
        }
        
        public function recupererSynonymesEtSousEspeces() {
                $sousTaxons = array();
                if (!is_null($this->taxon) && $this->taxon['rang'] >= Config::get('rang.espece')) {
                        $requete =
                        "SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
                        $this->champs['ns']." AS nom, rang FROM ".$this->nomComplet. " WHERE ".
                        $this->champs['nt']."=".$this->taxon['nt']." OR hierarchie LIKE '%-".$this->champs['nn']."-%'";
                        $sousTaxons = $this->getBdd()->recupererTous($requete);
                }
                return $sousTaxons;
        }
        
        public function recupererGenres() {
                $sousTaxons = array();
                if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.famille')) {
                        $requete =
                        "SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
                        $this->champs['ns']." AS nom, rang FROM ".$this->nomComplet. " WHERE ".
                        "num_tax_sup=".$this->taxon['nn']." AND num_nom=".$this->champs['nn'];
                        $sousTaxons = $this->getBdd()->recupererTous($requete);
                }
                return $sousTaxons;
        }
        
        public static function recupererListeReferentielsDisponibles() {
                $tableau = array();
                $tableauPartiel = explode(',', Config::get('referentiels_dispo'));
                $tableauPartiel = array_map('trim', $tableauPartiel);
                foreach ($tableauPartiel as $champ) {
                        if (strpos($champ, '=') === false) {
                                $tableau[] = $champ;
                        } else {
                                list($cle, $val) = explode('=', $champ);
                                $clePropre = trim($cle);
                                $valeurPropre = trim($val);
                                $tableau[$clePropre] = $valeurPropre;
                        }
                }
                return $tableau;
        }
        
        public function obtenirNumeroNomenclatural($nomScientifique, $numeroNomenclatural = null) {
                $aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%');
                $requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ".
                        "WHERE ".$this->champs['ns']." LIKE ".$aProteger;
                if (!is_null($numeroNomenclatural) || strlen($numeroNomenclatural) > 0) {
                        $requete .= " OR ".$this->champs['nn']."={$numeroNomenclatural}";
                }
                $taxon = $this->getBdd()->recuperer($requete);
                if ($taxon == false) {
                        $taxon = null;
                } else {
                        $taxon['referentiel'] = $this->nomComplet;
                }
                return $taxon;
        }
        
}

?>