Rev 548 | Rev 550 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
/**
* Classe InformationsBaseflor.php permet de faire des requetes de baseflor en fonction d'un référentiel
*( BDTFX, BDAFX, BDBFX ) et d'un numéro nomenclatural ( différent de 0 ).
* fin d'url possibles :
* /informations/#bdnt.nn:#num_nomen --> retourne tous les champs pour un BDNT et un num_nomen
* /informations/#bdnt.nn:#num_nomen?champs=description --> retourne champs description pour un BDNT et un num_nomen
* /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
*
*
* Encodage en entrée : utf8
* Encodage en sortie : utf8
* @package eflore-projets
* @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@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 1.0
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
*/
class InformationsBaseflor extends Commun{
protected $table = "";
private $format_reponse = 'informations';
private $retour_format = 'max';
private $champs_ontologiques = array();
protected $serviceNom = 'informations';
private $Bdd;
private $requete_condition = "";
private $champs_recherches = '*';
private $resultat_json;
public function consulter($ressources, $parametres) {
$this->ressources = $ressources;
$this->parametres = $parametres;
$this->traiterParametres();
$this->definirTables();
$this->traiterRessources();
$resultats = null;
foreach ($this->table_version as $version) {
$this->table = $version;
$requete = $this->assemblerLaRequete();
$resultat = $this->Bdd->recupererTous($requete);
$versionResultat = $this->analyserResultat($resultat);
if (count($this->table_version) > 1) {
$resultats[$version] = $versionResultat;
} else {
$resultats = $versionResultat;
}
}
return $resultats;
}
public function __construct(Conteneur $Conteneur) {
$this->Bdd = $Conteneur->getBdd();
}
public function analyserResultat($resultat) {
$versionResultat = null;
if ($resultat == '') {
$message = 'La requête SQL formée comporte une erreur!';
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
} elseif ($resultat) {
$versionResultat = $this->retournerResultatFormate($resultat);
}
return $versionResultat;
}
//+--------------------------traitement ressources ou paramètres -------------------------------------------+
public function traiterParametres() {
if (isset($this->parametres) && !empty($this->parametres)) {
foreach ($this->parametres as $param => $valeur) {
switch ($param) {
case 'categorie' :
if ($valeur == "description") {
$this->champs_recherches = "num_nomen, BDNT, chorologie, inflorescence,"
." sexualite, ordre_maturation, "
."pollinisation, fruit, dissemination, couleur_fleur, macule, floraison, type_bio, "
."form_vegetale ";
$this->requete_condition[] = "( chorologie != '' OR inflorescence != '' ".
"OR sexualite != '' OR ordre_maturation != '' OR pollinisation != '' ".
"OR fruit != '' OR dissemination != '' OR couleur_fleur != '' OR macule != '' ".
"OR floraison != '' OR type_bio != '' OR form_vegetale != '' )";
}elseif ($valeur == "ecologie") {
$this->champs_recherches = " num_nomen, BDNT, ve_lumiere , ve_temperature,"
." ve_continentalite, ve_humidite_atmos, ve_humidite_edaph, ve_reaction_sol,"
." ve_nutriments_sol, ve_salinite, ve_texture_sol, ve_mat_org_sol, "
." vi_lumiere, vi_temperature, vi_continentalite, vi_humidite_edaph,"
." vi_reaction_sol, vi_nutriments_sol, vi_salinite ";
$this->requete_condition[] = "( ve_lumiere != '' OR ve_temperature != '' ".
"OR ve_continentalite != '' OR ve_humidite_atmos != '' ".
"OR ve_humidite_edaph != '' OR ve_reaction_sol != '' ".
"OR ve_nutriments_sol != '' OR ve_salinite != '' ".
"OR ve_texture_sol != '' OR ve_mat_org_sol != '' )";
} else {
$e = "Valeur de paramètre inconnue pour 'categorie'.";
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
}
break;
case 'retour.format' :
if ($valeur == 'min' || $valeur == 'max') {
$this->retour_format = $valeur;
break;
} else {
$e = "Valeur de paramètre inconnue pour 'retour.format'.";
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
}
case 'version.projet' :
$this->traiterVersion($valeur);
break;
default :
$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
.$param.' " n\'existe pas.';
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
break;
}
}
}
}
public function traiterRessources() {
if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)){
switch ($retour[1]) {
case 'bdtfx' :
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDTFX' ";
break;
case 'bdafx' :
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDAFX' ";
break;
case 'bdbfx' :
$this->requete_condition[] = "num_nomen = ".$retour[2]." AND BDNT = 'BDBFX' ";
break;
default :
$e = 'Erreur dans l\'url de votre requête : </br> Le référentiel " '
.$retour[1].' " n\'existe pas.';
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
break;
}
} else {
$e = 'Erreur dans l\'url de votre requête : </br> La ressource demandée n\'existe pas.';
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
break;
}
}
//+++------------------------------traitement des versions----------------------------------------++
public function traiterVersion($valeur) {
if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
$this->version_projet = $valeur;
} else {
$e = "Erreur : La version est inconnue.";
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
}
}
public function definirTables() {
$table_num_version = $this->recupererVersionDisponible();
$prefixe_table = config::get('bdd_table');
if ( in_array($this->version_projet,$table_num_version) ) {
$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
} elseif ($this->version_projet == '+') {
$derniere_version = $table_num_version[count($table_num_version) - 1];
$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
} elseif ($this->version_projet == '*') {
foreach ($table_num_version as $num_version) {
$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
}
} else {
$e = "Erreur : La version est inconnue.";
throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
}
}
//+--------------------------formatages de resultats -------------------------------------------+
public function retournerResultatFormate($resultat) {
$resultat_json = $resultat[0];
if ($this->retour_format == 'max'){
if (!isset($this->parametres['categorie']) ) {
$this->formaterResultatsTousChamps($resultat_json);
} elseif ($this->parametres['categorie'] == 'ecologie') {
$this->formaterResultatsChampsEcologie($resultat_json);
} elseif ($this->parametres['categorie'] == 'description') {
$this->formaterResultatsChampsDescription($resultat_json);
}
}
return $this->resultat_json;
}
public function formaterResultatsTousChamps($resultat) {
$this->resultat_json = $resultat;
$this->traiterTypeBio();
$graphique_presence = $this->traiterOntologieEcologie() ;
if ($graphique_presence) {
$graphique = $this->ajouterLiensGraphique();
}
}
public function formaterResultatsChampsEcologie($resultat) {
$this->resultat_json = $resultat;
$graphique_presence = $this->traiterOntologieEcologie() ;
if ($graphique_presence) {
$graphique = $this->ajouterLiensGraphique();
}
}
public function formaterResultatsChampsDescription($resultat) {
$this->resultat_json = $resultat;
$this->traiterTypeBio();
}
public function traiterOntologieEcologie() {
$donnees_presence = false;
$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
foreach ($this->champs_ontologiques as $cle => $valeur){
if ($this->resultat_json[$cle] != ""){
$donnees_presence = true;
$this->recupererOntologies($this->resultat_json[$cle], $cle);
unset($this->resultat_json[$cle]);
}
}
return $donnees_presence;
}
public function traiterTypeBio() {
if ($this->resultat_json['type_bio'] != ""){
$tab_ontologie = $this->recupererOntologiePrTypeBio($this->resultat_json['type_bio']);
unset($this->resultat_json['type_bio']);
$this->resultat_json= array_merge($this->resultat_json,$tab_ontologie);
}
}
public function ajouterLiensGraphique() {
$this->resultat_json['graphique_climat']['libelle'] = 'climat';
$this->resultat_json['graphique_climat']['href'] =
$this->ajouterHref('graphiques/climat', strtolower($this->resultat_json['BDNT']).'.nn:'.$this->resultat_json['num_nomen']);
$this->resultat_json['graphique_sol']['libelle'] = 'sol';
$this->resultat_json['graphique_sol']['href'] =
$this->ajouterHref('graphiques/sol', strtolower($this->resultat_json['BDNT']).'.nn:'.$this->resultat_json['num_nomen']);
}
//+--------------------------traitement ontologies -------------------------------------------+
public function recupererOntologies($valeur, $champs){
$url = Config::get('url_service_base').Config::get('nom_projet').
'/ontologies/'.$this->champs_ontologiques[$champs].':'.urlencode(urlencode($valeur));
$val = $this->consulterHref($url);
$this->resultat_json[$champs.'.libelle'] = $val->nom;
$this->resultat_json[$champs.'.code'] = $valeur;
$this->resultat_json[$champs.'.href'] = $url;
}
//+-------------ontologie pour le champs type biologique (lire le .txt)---+
public function recupererOntologiePrTypeBio($type_a_traiter){
$resultat_typeBio = "";
if ($type_a_traiter != "") {
$types_prim_sec = $this->separerTypesPrimairesSecondaires($type_a_traiter);
$types_sstypes = $this->classerPrimSecEnTypesSsTypes($types_prim_sec);
$resultat_typeBio = $this->formaterJsonTypeBio($types_sstypes, $type_a_traiter );
}
return $resultat_typeBio;
}
public function classerPrimSecEnTypesSsTypes($types_prim_sec) {
$types_sstypes = '';
foreach ($types_prim_sec as $cle => $tb) {
$types_sstypes[$cle]= $this->separerTypes_SousTypes($tb, $cle);
}
switch (count($types_sstypes)) {
case 1 :
$types_sstypes = $types_sstypes['principal'];
break;
case 2 :
$types_sstypes = array_merge($types_sstypes['principal'],$types_sstypes['secondaire']);
break;
}
return $types_sstypes;
}
public function formaterJsonTypeBio( $types_sstypes, $type_a_traiter) {
$resultat_typeBio['type_bio.code'] = $type_a_traiter;
foreach ($types_sstypes as $cle => $tb) {
if (($cle == 'type.principal') || ($cle == 'type.secondaire')) {
$i = 0;
foreach ($tb as $types) {
$url = Config::get('url_service_base').Config::get('nom_projet').
'/ontologies/TB:'.urlencode(urlencode($types));
$val = $this->consulterHref($url);
$libelle[$cle]= $val->nom;
$detail[$cle][$i]['libelle'] = $val->nom;
$detail[$cle][$i]['code'] = $val->code;
$detail[$cle][$i]['href'] = $url;
$i++;
}
}
if ($cle == 'sous_type.principal' || $cle == 'sous_type.secondaire' ) {
$i = 0;
foreach ($tb as $sstypes) {
$url = Config::get('url_service_base').Config::get('nom_projet').
'/ontologies/ssTT:'.urlencode(urlencode($sstypes));
$val = $this->consulterHref($url);
$libelle[$cle]=$val->nom;
$detail[$cle][$i]['libelle'] = $val->nom;
$detail[$cle][$i]['code'] = $val->code;
$detail[$cle][$i]['href'] = $url;
$i++;
}
}
}
$resultat_typeBio['type_bio.libelle']= $this->formaterTypeBioPrLibelle($libelle);
$resultat_typeBio['type_bio.detail'] = $detail;
return $resultat_typeBio;
}
public function formaterTypeBioPrLibelle($info) {
$libelle = $info['type.principal']." ";
$libelle .= isset($info['sous_type.principal']) ? " (".$info['sous_type.principal'].") " : "";
$libelle .= isset($info['type.secondaire']) ? ", ".$info['type.secondaire']." " : "";
$libelle .= isset($info['sous_type.secondaire']) ? "(".$info['sous_type.secondaire'].") " : "";
return $libelle;
}
//séparation des principaux et des secondaires
public function separerTypesPrimairesSecondaires($type) {
$types = array();
if (preg_match("/(.+)\((.+)\)\-{0,1}(.+){0,1}/", $type,$retour)) {
$types['principal'] = isset($retour[3]) ? $retour[1].'-'. $retour[3] : $retour[1] ;
$types['secondaire'] = $retour[2];
} else {
$types['principal'] = $type;
}
return $types;
}
public function separerTypes_SousTypes($type_a_traiter, $typePrimOuSec) {
$types = array();
if (preg_match("/^([a-zA-Zé]+)\-(.+)$|^([a-zA-Zé,]+[^\-])$/", $type_a_traiter, $retour)) {
//type
$type = array();
$type[0] = (isset($retour[3])) ? $retour[3] : $retour[1] ;
$type = explode(",", $type[0]);
for ($i = 0 ; $i < count($type); $i++) {
$types['type.'.$typePrimOuSec][$i] = $type[$i];
}
//sous types
$ss_types = array();
if (isset($retour[2]) && $retour[2] != '') {
$ss_types[0] = $retour[2] ;
$ss_types = explode("-", $ss_types[0]);
for ($i = 0 ; $i < count($ss_types); $i++) {
$types['sous_type.'.$typePrimOuSec][$i] = $ss_types[$i];
}
}
}
return $types;
}
//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
public function assemblerLaRequete() {
$requete = ' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
.$this->retournerRequeteCondition();
return $requete;
}
public function retournerRequeteCondition() {
$condition = '';
if (empty($this->requete_condition) == false) {
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
}
return $condition;
}
}
?>