New file |
0,0 → 1,165 |
<?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) |
* |
*/ |
|
define("RANG_FAMILLE", 180); |
|
|
|
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($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) { |
$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']."={$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; |
} |
|
|
public function recupererSousTaxons($listeNn = null) { |
$sousTaxons = array(); |
if (!is_null($this->taxon) && $this->taxon['rang'] >= RANG_FAMILLE) { |
if (is_null($listeNn)) { |
$listeNn = $this->taxon['nn']; |
} |
$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 IN ({$listeNn}) AND num_nom=".$this->champs['nn']; |
$resultats = $this->getBdd()->recupererTous($requete); |
|
$nouvelleListeNn = ''; |
foreach ($resultats as $sousTaxon) { |
$sousTaxons[] = $sousTaxon; |
$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn']; |
} |
// recherche de sous taxons au niveau inferieur (parcours recursif de la methode) |
if (count($resultats) > 0) { |
$sousTaxons = array_merge($sousTaxons, $this->recupererSousTaxons($nouvelleListeNn)); |
} |
} |
|
return $sousTaxons; |
} |
|
|
private function getBdd() { |
if (is_null($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$nomBdd = Config::get('bdd_eflore'); |
if (is_null($nomBdd)) { |
$nomBdd = Config::get('bdd_nom'); |
} |
$this->bdd->requeter("USE ".$nomBdd); |
return $this->bdd; |
} |
|
|
|
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) { |
$aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%'); |
$requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ". |
"WHERE ".$this->champs['ns']." LIKE ".$aProteger; |
$taxon = $this->getBdd()->recuperer($requete); |
if ($taxon == false) { |
$taxon = null; |
} else { |
$taxon['referentiel'] = $this->nomComplet; |
} |
return $taxon; |
} |
|
} |
?> |