| 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 $format_reponse = 'informations';
 | 
        
           |  |  | 28 | 	protected $serviceNom = 'informations';
 | 
        
           | 517 | mathilde | 29 | 	private $retour_format = 'max';
 | 
        
           | 525 | mathilde | 30 | 	private $Bdd;
 | 
        
           | 548 | mathilde | 31 | 	private $requete_condition = "";
 | 
        
           |  |  | 32 | 	private $champs_recherches = '*';
 | 
        
           | 453 | mathilde | 33 |   | 
        
           |  |  | 34 | 	public function consulter($ressources, $parametres) {
 | 
        
           |  |  | 35 | 		$this->ressources = $ressources;
 | 
        
           |  |  | 36 | 		$this->parametres = $parametres;
 | 
        
           |  |  | 37 | 		$this->traiterParametres();
 | 
        
           | 518 | mathilde | 38 | 		$this->definirTables();
 | 
        
           |  |  | 39 | 		$this->traiterRessources();
 | 
        
           | 453 | mathilde | 40 | 		$resultats = '';
 | 
        
           | 518 | mathilde | 41 | 	foreach ($this->table_version as $version) {
 | 
        
           | 548 | mathilde | 42 | 			$this->table = $version;
 | 
        
           |  |  | 43 | 			$requete = $this->assemblerLaRequete();
 | 
        
           | 525 | mathilde | 44 | 			$resultat = $this->Bdd->recupererTous($requete);
 | 
        
           |  |  | 45 | 			$versionResultat = $this->analyserResultat($resultat);
 | 
        
           | 518 | mathilde | 46 | 			if (count($this->table_version) > 1) {
 | 
        
           |  |  | 47 | 				$resultats[$version] = $versionResultat;
 | 
        
           |  |  | 48 | 			} else {
 | 
        
           |  |  | 49 | 				$resultats = $versionResultat;
 | 
        
           |  |  | 50 | 			}
 | 
        
           |  |  | 51 | 		}
 | 
        
           | 548 | mathilde | 52 | 		return $resultats;
 | 
        
           | 453 | mathilde | 53 | 	}
 | 
        
           |  |  | 54 |   | 
        
           | 525 | mathilde | 55 | 	public function analyserResultat($resultat) {
 | 
        
           |  |  | 56 | 		$versionResultat = null;
 | 
        
           |  |  | 57 | 		if ($resultat == '') {
 | 
        
           |  |  | 58 | 			$message = 'La requête SQL formée comporte une erreur!';
 | 
        
           |  |  | 59 | 			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
 | 
        
           |  |  | 60 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 61 | 		} elseif ($resultat) {
 | 
        
           |  |  | 62 | 			$versionResultat = $this->retournerResultatFormate($resultat);
 | 
        
           |  |  | 63 | 		}
 | 
        
           |  |  | 64 | 		return $versionResultat;
 | 
        
           |  |  | 65 | 	}
 | 
        
           | 453 | mathilde | 66 |   | 
        
           | 525 | mathilde | 67 | 	public function __construct(Conteneur $Conteneur) {
 | 
        
           |  |  | 68 | 		$this->Bdd = $Conteneur->getBdd();
 | 
        
           |  |  | 69 | 	}
 | 
        
           |  |  | 70 |   | 
        
           | 453 | mathilde | 71 | 	//+--------------------------traitement ressources ou paramètres  -------------------------------------------+
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 | 	public function traiterRessources() {
 | 
        
           |  |  | 74 | 			if(preg_match('/^(.+)\.nn:([0-9]+)$/', $this->ressources[0], $retour)==1){
 | 
        
           |  |  | 75 | 				switch ($retour[1]) {
 | 
        
           |  |  | 76 | 					case 'bdtfx' :
 | 
        
           | 548 | mathilde | 77 | 						$this->requete_condition[] = "num_nomen = ".$retour[2]." AND bdnt = 'bdtfx' ";
 | 
        
           | 453 | mathilde | 78 | 						break;
 | 
        
           |  |  | 79 | 					default :
 | 
        
           | 525 | mathilde | 80 | 						$e = 'Erreur dans l\'url de votre requête : </br> Le référentiel " '
 | 
        
           | 453 | mathilde | 81 | 					.$retour[1].' " n\'existe pas.';
 | 
        
           | 525 | mathilde | 82 | 					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
 | 
        
           | 453 | mathilde | 83 | 					break;
 | 
        
           |  |  | 84 | 				}
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 | 			}
 | 
        
           | 525 | mathilde | 87 |   | 
        
           | 453 | mathilde | 88 | 	}
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 | 	//+---- paramètres ----+
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 | 	public function traiterParametres() {
 | 
        
           | 525 | mathilde | 93 | 		if (isset($this->parametres) && !empty($this->parametres) ) {
 | 
        
           | 453 | mathilde | 94 | 			foreach ($this->parametres as $param => $valeur) {
 | 
        
           |  |  | 95 | 				switch ($param) {
 | 
        
           | 516 | mathilde | 96 | 					case 'categorie'  :
 | 
        
           | 453 | mathilde | 97 | 						if ($valeur == "ecologie"){
 | 
        
           | 548 | mathilde | 98 | 							$this->champs_recherches = ' num_nomen, bdnt, ve_lumiere_min , ve_lumiere_max,'
 | 
        
           | 453 | mathilde | 99 | 							.' ve_temperature_min, ve_temperature_max, ve_continentalite_min,'
 | 
        
           |  |  | 100 | 							.' ve_continentalite_max, ve_humidite_atmos_min, ve_humidite_atmos_max,'
 | 
        
           |  |  | 101 | 							.' ve_humidite_edaph_min, ve_humidite_edaph_max, ve_reaction_sol_min,'
 | 
        
           |  |  | 102 | 							.' ve_reaction_sol_max, ve_nutriments_sol_min, ve_nutriments_sol_max,'
 | 
        
           |  |  | 103 | 							.' ve_salinite_min, ve_salinite_max, ve_texture_sol_min,ve_texture_sol_max,'
 | 
        
           |  |  | 104 | 							.' ve_mat_org_sol_min, ve_mat_org_sol_max ';
 | 
        
           |  |  | 105 | 						} else {
 | 
        
           | 517 | mathilde | 106 | 							$e = "Valeur de paramètre inconnue pour 'categorie'.";
 | 
        
           | 525 | mathilde | 107 | 							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
 | 
        
           | 453 | mathilde | 108 | 						}
 | 
        
           |  |  | 109 | 						break;
 | 
        
           | 517 | mathilde | 110 | 					case 'retour.format'  :
 | 
        
           |  |  | 111 | 						if ($valeur == 'min' || $valeur == 'max') {
 | 
        
           |  |  | 112 | 							$this->retour_format = $valeur;
 | 
        
           |  |  | 113 | 							break;
 | 
        
           |  |  | 114 | 						} else {
 | 
        
           |  |  | 115 | 							$e = "Valeur de paramètre inconnue pour 'retour.format'.";
 | 
        
           | 525 | mathilde | 116 | 							throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
 | 
        
           | 517 | mathilde | 117 | 						}
 | 
        
           | 518 | mathilde | 118 | 					case 'version.projet' :
 | 
        
           |  |  | 119 | 						$this->traiterVersion($valeur);
 | 
        
           |  |  | 120 | 						break;
 | 
        
           | 453 | mathilde | 121 | 					default :
 | 
        
           |  |  | 122 | 						$e = 'Erreur dans les parametres de votre requête : </br> Le paramètre " '
 | 
        
           |  |  | 123 | 					.$param.' " n\'existe pas.';
 | 
        
           | 525 | mathilde | 124 | 					throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);	break;
 | 
        
           | 453 | mathilde | 125 | 				}
 | 
        
           |  |  | 126 | 			}
 | 
        
           |  |  | 127 | 		}
 | 
        
           |  |  | 128 | 	}
 | 
        
           | 518 | mathilde | 129 |   | 
        
           |  |  | 130 | 	//+++------------------------------traitement des versions----------------------------------------++
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 | 	public function traiterVersion($valeur) {
 | 
        
           |  |  | 133 | 		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '*' || $valeur == '+') {
 | 
        
           |  |  | 134 | 				$this->version_projet = $valeur;
 | 
        
           |  |  | 135 | 			} else  {
 | 
        
           |  |  | 136 | 				$e = "Erreur : La version est inconnue.";
 | 
        
           | 525 | mathilde | 137 | 				throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
 | 
        
           | 518 | mathilde | 138 | 			}
 | 
        
           |  |  | 139 | 		if ($this->version_projet == '*' && $this->ressources == array()) {
 | 
        
           |  |  | 140 | 			$message = "L'affichage de plusieurs versions ne fonctionne que pour les ressources de type /ressources/#id";
 | 
        
           |  |  | 141 | 			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
 | 
        
           |  |  | 142 | 			throw new Exception($message, $code);
 | 
        
           | 453 | mathilde | 143 | 		}
 | 
        
           |  |  | 144 | 	}
 | 
        
           |  |  | 145 |   | 
        
           | 548 | mathilde | 146 | 	public function definirTables() {
 | 
        
           | 518 | mathilde | 147 | 		$table_num_version = $this->recupererVersionDisponible();
 | 
        
           |  |  | 148 | 		$prefixe_table = config::get('bdd_table_rang_sup');
 | 
        
           |  |  | 149 | 		if ( in_array($this->version_projet,$table_num_version) ) {
 | 
        
           |  |  | 150 | 			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
 | 
        
           |  |  | 151 | 		} elseif ($this->version_projet == '+') {
 | 
        
           |  |  | 152 | 					$derniere_version = $table_num_version[count($table_num_version) - 1];
 | 
        
           |  |  | 153 | 					$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
 | 
        
           |  |  | 154 | 		} elseif ($this->version_projet == '*') {
 | 
        
           |  |  | 155 | 			foreach ($table_num_version as $num_version) {
 | 
        
           |  |  | 156 | 				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
 | 
        
           |  |  | 157 | 			}
 | 
        
           |  |  | 158 | 		} else {
 | 
        
           |  |  | 159 | 			$e = "Erreur : La version est inconnue.";
 | 
        
           | 525 | mathilde | 160 | 			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
 | 
        
           | 453 | mathilde | 161 | 		}
 | 
        
           |  |  | 162 | 	}
 | 
        
           |  |  | 163 |   | 
        
           | 518 | mathilde | 164 |   | 
        
           | 453 | mathilde | 165 | 	//+--------------------------formatages de resultats  -------------------------------------------+
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 | 	public function retournerResultatFormate($resultat) {
 | 
        
           | 548 | mathilde | 169 | 		$this->resultat_json = $resultat[0];
 | 
        
           | 517 | mathilde | 170 | 		if ($this->retour_format == 'max') {
 | 
        
           | 548 | mathilde | 171 | 			$graphique_presence = $this->traiterOntologieEcologie() ;
 | 
        
           | 517 | mathilde | 172 | 			if ($graphique_presence) {
 | 
        
           | 548 | mathilde | 173 | 				$this->ajouterLiensGraphique();
 | 
        
           | 453 | mathilde | 174 | 			}
 | 
        
           | 556 | mathilde | 175 | 		}
 | 
        
           | 548 | mathilde | 176 | 		return $this->resultat_json ;
 | 
        
           | 453 | mathilde | 177 |   | 
        
           |  |  | 178 | 	}
 | 
        
           |  |  | 179 |   | 
        
           | 548 | mathilde | 180 | 	public function traiterOntologieEcologie() {
 | 
        
           |  |  | 181 | 		$donnees_presence =  false;
 | 
        
           |  |  | 182 | 		$this->champs_ontologiques = $this->recupererTableauConfig('champs_ontologiques');
 | 
        
           |  |  | 183 | 		foreach ($this->champs_ontologiques as $cle => $valeur){
 | 
        
           |  |  | 184 | 			if ($this->resultat_json[$cle.'_min'] != ""){
 | 
        
           |  |  | 185 | 				$donnees_presence = true;
 | 
        
           |  |  | 186 | 				$tab_ontologie = $this->recupererOntologies($this->resultat_json[$cle.'_min'], $cle.'_min');
 | 
        
           |  |  | 187 | 				unset($this->resultat_json[$cle.'_min']);
 | 
        
           |  |  | 188 | 			}
 | 
        
           |  |  | 189 | 			if ($this->resultat_json[$cle.'_max'] != ""){
 | 
        
           |  |  | 190 | 				$this->recupererOntologies($this->resultat_json[$cle.'_max'], $cle.'_max');
 | 
        
           |  |  | 191 | 				unset($this->resultat_json[$cle.'_max']);
 | 
        
           |  |  | 192 | 			}
 | 
        
           |  |  | 193 | 		}
 | 
        
           |  |  | 194 | 		return $donnees_presence;
 | 
        
           |  |  | 195 | 	}
 | 
        
           |  |  | 196 |   | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 | 	public function ajouterLiensGraphique() {
 | 
        
           |  |  | 199 | 		$this->resultat_json['graphique_climat']['libelle'] = 'climat';
 | 
        
           |  |  | 200 | 		$this->resultat_json['graphique_climat']['href'] = $this->ajouterHref('graphiques/climat',
 | 
        
           |  |  | 201 | 			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
 | 
        
           |  |  | 202 | 		$this->resultat_json['graphique_sol']['libelle'] = 'sol';
 | 
        
           |  |  | 203 | 		$this->resultat_json['graphique_sol']['href'] = $this->ajouterHref('graphiques/sol',
 | 
        
           |  |  | 204 | 			strtolower($this->resultat_json['bdnt']).'.nn:'.$this->resultat_json['num_nomen']);
 | 
        
           |  |  | 205 | 	}
 | 
        
           |  |  | 206 |   | 
        
           | 453 | mathilde | 207 | 	//+--------------------------traitement ontologies -------------------------------------------+
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 | 	public function recupererOntologies($valeur, $champs){
 | 
        
           |  |  | 210 | 		$chps_sans = preg_replace("/_min|_max/", '', $champs);
 | 
        
           | 518 | mathilde | 211 | 		$url = Config::get('url_service_base').Config::get('nom_projet').
 | 
        
           |  |  | 212 | 			'/ontologies/'.$this->champs_ontologiques[$chps_sans].':'.urlencode(urlencode($valeur));
 | 
        
           | 453 | mathilde | 213 | 		$val = $this->consulterHref($url);
 | 
        
           | 548 | mathilde | 214 | 		$this->resultat_json[$champs.'.libelle'] = $val->nom;
 | 
        
           |  |  | 215 | 		$this->resultat_json[$champs.'.code'] = $valeur;
 | 
        
           |  |  | 216 | 		$this->resultat_json[$champs.'.href'] = $url;
 | 
        
           | 453 | mathilde | 217 | 	}
 | 
        
           |  |  | 218 |   | 
        
           |  |  | 219 |   | 
        
           | 548 | mathilde | 220 | 	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
 | 
        
           | 453 | mathilde | 221 |   | 
        
           | 548 | mathilde | 222 | 	public function assemblerLaRequete() {
 | 
        
           |  |  | 223 | 		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
 | 
        
           |  |  | 224 | 		.$this->retournerRequeteCondition();
 | 
        
           |  |  | 225 | 		return $requete;
 | 
        
           |  |  | 226 | 	}
 | 
        
           | 453 | mathilde | 227 |   | 
        
           | 548 | mathilde | 228 |   | 
        
           |  |  | 229 |   | 
        
           |  |  | 230 | 	public  function retournerRequeteCondition() {
 | 
        
           |  |  | 231 | 		$condition = '';
 | 
        
           |  |  | 232 | 		if (empty($this->requete_condition) == false) {
 | 
        
           |  |  | 233 | 			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
 | 
        
           |  |  | 234 | 		}
 | 
        
           |  |  | 235 | 		return $condition;
 | 
        
           |  |  | 236 | 	}
 | 
        
           |  |  | 237 |   | 
        
           | 453 | mathilde | 238 | }
 | 
        
           |  |  | 239 | ?>
 |