* @license GPL v3 * @license CECILL v2 * @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; } } ?>