| 453 | mathilde | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /**
 | 
        
           |  |  | 4 | * Classe InformationsTaxonsSup.php permet de faire des requetes  pour les rangs superieurs de baseflor
 | 
        
           |  |  | 5 | *  du référentiel BDTFX  et avec un numéro nomenclatural ( différent de 0 ).
 | 
        
           |  |  | 6 | *  fin d'url possibles :
 | 
        
           |  |  | 7 | *
 | 
        
           |  |  | 8 | *  /informations/#bdnt.nn:#num_nomen?champs=ecologie --> retourne champs ecologiques pour un BDNT et un num_nomen
 | 
        
           |  |  | 9 | *
 | 
        
           |  |  | 10 | *
 | 
        
           |  |  | 11 | * Encodage en entrée : utf8
 | 
        
           |  |  | 12 | * Encodage en sortie : utf8
 | 
        
           |  |  | 13 | * @package eflore-projets
 | 
        
           |  |  | 14 | * @author Mathilde SALTHUN-LASSALLE <mathilde@tela-botanica.org>
 | 
        
           |  |  | 15 | * @author Delphine CAUQUIL <delphine@tela-botanica.org>
 | 
        
           |  |  | 16 | * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
 | 
        
           |  |  | 17 | * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 18 | * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 19 | * @version 1.0
 | 
        
           |  |  | 20 | * @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
 | 
        
           |  |  | 21 | */
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | class InformationsTaxonsSup extends Commun{
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | 	protected $table = "";
 | 
        
           |  |  | 26 | 	private $champs_ontologiques = array();
 | 
        
           |  |  | 27 | 	private $requete_condition = "";
 | 
        
           |  |  | 28 | 	private $format_reponse = 'informations';
 | 
        
           |  |  | 29 | 	private $champs_recherches = '*';
 | 
        
           |  |  | 30 | 	protected $serviceNom = 'informations';
 | 
        
           | 517 | mathilde | 31 | 	private $retour_format = 'max';
 | 
        
           | 453 | mathilde | 32 |   | 
        
           |  |  | 33 | 	public function consulter($ressources, $parametres) {
 | 
        
           |  |  | 34 | 		$this->ressources = $ressources;
 | 
        
           |  |  | 35 | 		$this->parametres = $parametres;
 | 
        
           |  |  | 36 | 		$this->traiterParametres();
 | 
        
           | 518 | mathilde | 37 | 		$this->definirTables();
 | 
        
           |  |  | 38 | 		$this->traiterRessources();
 | 
        
           | 453 | mathilde | 39 | 		$resultats = '';
 | 
        
           | 518 | mathilde | 40 | 	foreach ($this->table_version as $version) {
 | 
        
           |  |  | 41 | 			$this->table = $version;
 | 
        
           |  |  | 42 | 			$requete = $this->assemblerLaRequete();
 | 
        
           |  |  | 43 | 			$resultat = $this->getBdd()->recupererTous($requete);
 | 
        
           |  |  | 44 | 			$versionResultat = $this->traiterResultat($resultat, $version);
 | 
        
           |  |  | 45 | 			if (count($this->table_version) > 1) {
 | 
        
           |  |  | 46 | 				$resultats[$version] = $versionResultat;
 | 
        
           |  |  | 47 | 			} else {
 | 
        
           |  |  | 48 | 				$resultats = $versionResultat;
 | 
        
           |  |  | 49 | 			}
 | 
        
           |  |  | 50 | 		}
 | 
        
           | 453 | mathilde | 51 | 		return $versionResultat;
 | 
        
           |  |  | 52 | 	}
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | 	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 | 	public function traiterRessources() {
 | 
        
           |  |  | 58 | 		if (isset($this->ressources) && !empty($this->ressources[0])) {
 | 
        
           |  |  | 59 | 			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
 | 
        
           |  |  | 60 | 				switch ($retour[1]) {
 | 
        
           |  |  | 61 | 					case 'bdtfx' :
 | 
        
           |  |  | 62 | 						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
 | 
        
           |  |  | 63 | 						break;
 | 
        
           |  |  | 64 | 					default :
 | 
        
           |  |  | 65 | 						$e = 'Erreur dans l\'url de votre requête : </br> La ressource " '
 | 
        
           |  |  | 66 | 					.$retour[1].' " n\'existe pas.';
 | 
        
           |  |  | 67 | 					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 68 | 					break;
 | 
        
           |  |  | 69 | 				}
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 | 			}
 | 
        
           |  |  | 72 | 		}
 | 
        
           |  |  | 73 | 	}
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 | 	//+---- paramètres ----+
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 | 	public function traiterParametres() {
 | 
        
           |  |  | 78 | 		if (isset($this->parametres) && !empty($this->parametres)) {
 | 
        
           |  |  | 79 | 			foreach ($this->parametres as $param => $valeur) {
 | 
        
           |  |  | 80 | 				switch ($param) {
 | 
        
           | 516 | mathilde | 81 | 					case 'categorie'  :
 | 
        
           | 453 | mathilde | 82 | 						if ($valeur == "ecologie"){
 | 
        
           |  |  | 83 | 							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
 | 
        
           |  |  | 84 | 							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
 | 
        
           |  |  | 85 | 							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
 | 
        
           |  |  | 86 | 							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
 | 
        
           |  |  | 87 | 							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
 | 
        
           |  |  | 88 | 							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
 | 
        
           |  |  | 89 | 							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
 | 
        
           |  |  | 90 | 						} else {
 | 
        
           | 517 | mathilde | 91 | 							$e = "Valeur de paramètre inconnue pour 'categorie'.";
 | 
        
           | 453 | mathilde | 92 | 							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 93 | 						}
 | 
        
           |  |  | 94 | 						break;
 | 
        
           | 517 | mathilde | 95 | 					case 'retour.format'  :
 | 
        
           |  |  | 96 | 						if ($valeur == 'min' || $valeur == 'max') {
 | 
        
           |  |  | 97 | 							$this->retour_format = $valeur;
 | 
        
           |  |  | 98 | 							break;
 | 
        
           |  |  | 99 | 						} else {
 | 
        
           |  |  | 100 | 							$e = "Valeur de paramètre inconnue pour 'retour.format'.";
 | 
        
           |  |  | 101 | 							$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 102 | 						}
 | 
        
           | 518 | mathilde | 103 | 					case 'version.projet' :
 | 
        
           |  |  | 104 | 						$this->traiterVersion($valeur);
 | 
        
           |  |  | 105 | 						break;
 | 
        
           | 453 | mathilde | 106 | 					default :
 | 
        
           |  |  | 107 | 						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
 | 
        
           |  |  | 108 | 					.$param.' " n\'existe pas.';
 | 
        
           |  |  | 109 | 					$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);	break;
 | 
        
           |  |  | 110 | 				}
 | 
        
           |  |  | 111 | 			}
 | 
        
           |  |  | 112 | 		}
 | 
        
           |  |  | 113 | 	}
 | 
        
           | 518 | mathilde | 114 |   | 
        
           |  |  | 115 | 	//+++------------------------------traitement des versions----------------------------------------++
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 | 	public function traiterVersion($valeur) {
 | 
        
           |  |  | 118 | 		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
 | 
        
           |  |  | 119 | 				$this->version_projet = $valeur;
 | 
        
           |  |  | 120 | 			} else  {
 | 
        
           |  |  | 121 | 				$e = "Erreur : La version est inconnue.";
 | 
        
           |  |  | 122 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 123 | 			}
 | 
        
           |  |  | 124 | 		if ($this->version_projet == '*' && $this->ressources == array()) {
 | 
        
           |  |  | 125 | 			$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
 | 
        
           |  |  | 126 | 			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
 | 
        
           |  |  | 127 | 			throw new Exception($message, $code);
 | 
        
           | 453 | mathilde | 128 | 		}
 | 
        
           |  |  | 129 | 	}
 | 
        
           |  |  | 130 |   | 
        
           | 518 | mathilde | 131 | 	public function DefinirTables() {
 | 
        
           |  |  | 132 | 		$table_num_version = $this->recupererVersionDisponible();
 | 
        
           |  |  | 133 | 		$prefixe_table = config::get('bdd_table_rang_sup');
 | 
        
           |  |  | 134 | 		if ( in_array($this->version_projet,$table_num_version) ) {
 | 
        
           |  |  | 135 | 			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
 | 
        
           |  |  | 136 | 		} elseif ($this->version_projet == '+') {
 | 
        
           |  |  | 137 | 					$derniere_version = $table_num_version[count($table_num_version) - 1];
 | 
        
           |  |  | 138 | 					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
 | 
        
           |  |  | 139 | 		} elseif ($this->version_projet == '*') {
 | 
        
           |  |  | 140 | 			foreach ($table_num_version as $num_version) {
 | 
        
           |  |  | 141 | 				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
 | 
        
           |  |  | 142 | 			}
 | 
        
           |  |  | 143 | 		} else {
 | 
        
           |  |  | 144 | 			$e = "Erreur : La version est inconnue.";
 | 
        
           |  |  | 145 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           | 453 | mathilde | 146 | 		}
 | 
        
           |  |  | 147 | 	}
 | 
        
           |  |  | 148 |   | 
        
           | 518 | mathilde | 149 |   | 
        
           | 453 | mathilde | 150 | 	//+--------------------------formatages de resultats  -------------------------------------------+
 | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 | 	public function retournerResultatFormate($resultat) {
 | 
        
           |  |  | 154 | 		$resultat_json = $resultat[0];
 | 
        
           | 517 | mathilde | 155 | 		if ($this->retour_format == 'max') {
 | 
        
           |  |  | 156 | 			$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
 | 
        
           |  |  | 157 | 			$graphique_presence = false;
 | 
        
           |  |  | 158 | 			foreach ($this->champs_ontologiques as $cle => $valeur){
 | 
        
           |  |  | 159 | 				if ($resultat_json[$cle.'_min'] != ""){
 | 
        
           |  |  | 160 | 					$graphique_presence = true;
 | 
        
           |  |  | 161 | 					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_min'], $cle.'_min');
 | 
        
           |  |  | 162 | 					unset($resultat_json[$cle.'_min']);
 | 
        
           |  |  | 163 | 					$resultat_json= array_merge($resultat_json,$tab_ontologie);
 | 
        
           |  |  | 164 | 				}
 | 
        
           |  |  | 165 | 				if ($resultat_json[$cle.'_max'] != ""){
 | 
        
           |  |  | 166 | 					$tab_ontologie = $this->recupererOntologies($resultat_json[$cle.'_max'], $cle.'_max');
 | 
        
           |  |  | 167 | 					unset($resultat_json[$cle.'_max']);
 | 
        
           |  |  | 168 | 					$resultat_json= array_merge($resultat_json,$tab_ontologie);
 | 
        
           |  |  | 169 | 				}
 | 
        
           | 453 | mathilde | 170 | 			}
 | 
        
           | 517 | mathilde | 171 | 			if ($graphique_presence) {
 | 
        
           |  |  | 172 | 				$resultat_json['graphique_climat']['libelle'] = 'climat';
 | 
        
           |  |  | 173 | 				$resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
 | 
        
           |  |  | 174 | 				$resultat_json['graphique_sol']['libelle'] = 'sol';
 | 
        
           |  |  | 175 | 				$resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol', strtolower($resultat_json['bdnt']).'.nn:'.$resultat_json['num_nomen']);
 | 
        
           | 453 | mathilde | 176 | 			}
 | 
        
           |  |  | 177 | 		}
 | 
        
           |  |  | 178 | 		return $resultat_json ;
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 | 	}
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 | 	//+--------------------------traitement ontologies -------------------------------------------+
 | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 | 	public function recupererOntologies($valeur, $champs){
 | 
        
           |  |  | 185 | 		$chps_sans = preg_replace("/_min|_max/", '', $champs);
 | 
        
           | 518 | mathilde | 186 | 		$url = Config::get('url_service_base').Config::get('nom_projet').
 | 
        
           |  |  | 187 | 			'/ontologies/'.$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur));
 | 
        
           | 453 | mathilde | 188 | 		$val = $this->consulterHref($url);
 | 
        
           |  |  | 189 | 		$resultat_json[$champs.'.libelle'] = $val->nom;
 | 
        
           |  |  | 190 | 		$resultat_json[$champs.'.code'] = $valeur;
 | 
        
           |  |  | 191 | 		$resultat_json[$champs.'.href'] = $url;
 | 
        
           |  |  | 192 | 		return $resultat_json;
 | 
        
           |  |  | 193 | 	}
 | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |   | 
        
           |  |  | 196 | 	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 | 	public function assemblerLaRequete() {
 | 
        
           |  |  | 199 | 		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
 | 
        
           |  |  | 200 | 		.$this->retournerRequeteCondition();
 | 
        
           |  |  | 201 | 		return $requete;
 | 
        
           |  |  | 202 | 	}
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 | 	public  function retournerRequeteCondition() {
 | 
        
           |  |  | 207 | 		$condition = '';
 | 
        
           |  |  | 208 | 		if ($this->requete_condition !== "") {
 | 
        
           |  |  | 209 | 			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
 | 
        
           |  |  | 210 | 		}
 | 
        
           |  |  | 211 | 		return $condition;
 | 
        
           |  |  | 212 | 	}
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 | }
 | 
        
           |  |  | 215 | ?>
 |