| 6 | jpm | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /**
 | 
        
           |  |  | 4 | * Description :
 | 
        
           |  |  | 5 | * Classe Textes.php est une classe qui contient les méthodes permettant la consultation des textes (clé de détermination
 | 
        
           |  |  | 6 | *  ou description). Les identifiants des textes correspondent au tag de type CleCoste1937TXXPXXNomSci pour le projet
 | 
        
           |  |  | 7 | * coste.
 | 
        
           |  |  | 8 | * Encodage en entrée : utf8
 | 
        
           |  |  | 9 | * Encodage en sortie : utf8
 | 
        
           |  |  | 10 | * @package framework-v3
 | 
        
           |  |  | 11 | * @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
 | 
        
           |  |  | 12 | * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 13 | * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 14 | * @version 1.0
 | 
        
           |  |  | 15 | * @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
 | 
        
           |  |  | 16 | */
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | class Textes extends Commun {
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | 	protected $limite_requete	= array('depart' => 0, 'limite' => 100);
 | 
        
           |  |  | 22 | 	protected $requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
 | 
        
           |  |  | 23 | 	protected $requete_condition = null;
 | 
        
           |  |  | 24 | 	protected $requete_group_by = ' ORDER BY CAST( t1.value AS DECIMAL )';
 | 
        
           |  |  | 25 | 	protected $table_retour;
 | 
        
           |  |  | 26 | 	protected $retour_format = 'max';
 | 
        
           |  |  | 27 | 	protected $format_reponse = 'textes';
 | 
        
           |  |  | 28 | 	protected $table_param = array();
 | 
        
           |  |  | 29 | 	protected $total_resultat;
 | 
        
           |  |  | 30 | 	protected $table_ressources = array();
 | 
        
           |  |  | 31 | 	protected $html = 'txt';
 | 
        
           |  |  | 32 | 	protected $recherche = 'stricte';
 | 
        
           |  |  | 33 | 	protected $service = 'textes';
 | 
        
           |  |  | 34 | 	protected $masque = null;
 | 
        
           | 536 | gduche | 35 | 	protected $config;
 | 
        
           | 6 | jpm | 36 |   | 
        
           | 563 | aurelien | 37 | 	public function __construct($config) {
 | 
        
           | 536 | gduche | 38 | 		$this->config = is_null($config) ? Config::get('Textes') : $config;
 | 
        
           |  |  | 39 | 	}
 | 
        
           | 6 | jpm | 40 |   | 
        
           |  |  | 41 | 	public function consulter($ressources, $parametres) {
 | 
        
           | 536 | gduche | 42 |   | 
        
           | 6 | jpm | 43 | 		$resultat_formate = '';
 | 
        
           |  |  | 44 | 		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_triples")." t2", Config::get("bdd_pages")." p");
 | 
        
           |  |  | 45 | 		$this->traiterParametres($parametres);
 | 
        
           |  |  | 46 | 		$this->traiterRessources($ressources);
 | 
        
           |  |  | 47 | 		if ($this->corps_http == '' && $this->entete_http == '') {
 | 
        
           | 288 | delphine | 48 | 			$requete = $this->assemblerLaRequete();
 | 
        
           | 6 | jpm | 49 | 			$resultat = $this->getBdd()->recupererTous($requete);
 | 
        
           |  |  | 50 | 			$this->testerResultat($resultat, $resultat_formate, $requete);
 | 
        
           |  |  | 51 | 		}
 | 
        
           |  |  | 52 | 		return $this->formerReponseHTTP($resultat_formate);
 | 
        
           |  |  | 53 | 	}
 | 
        
           |  |  | 54 |   | 
        
           | 288 | delphine | 55 | 	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
 | 
        
           |  |  | 56 | 		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
 | 
        
           |  |  | 57 | 		if ($this->corps_http == '' && $this->entete_http == '') {
 | 
        
           |  |  | 58 | 			$this->entete_http = RestServeur::HTTP_CODE_OK;
 | 
        
           |  |  | 59 | 			$this->corps_http  = $resultat_formate;
 | 
        
           |  |  | 60 | 		}
 | 
        
           |  |  | 61 | 		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
 | 
        
           |  |  | 62 | 			$mime = 'text/html';
 | 
        
           |  |  | 63 | 		}
 | 
        
           |  |  | 64 | 		// Gestion du type de contenu
 | 
        
           |  |  | 65 | 		if (!is_null($mime) && !is_null($encodage)) {
 | 
        
           |  |  | 66 | 			header("Content-Type: $mime; charset=$encodage");
 | 
        
           |  |  | 67 | 		} else if (!is_null($mime) && is_null($encodage)) {
 | 
        
           |  |  | 68 | 			header("Content-Type: $mime");
 | 
        
           |  |  | 69 | 		}
 | 
        
           |  |  | 70 | 		// Envoie de l'entête
 | 
        
           |  |  | 71 | 		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
 | 
        
           |  |  | 72 | 		// Envoie du corps
 | 
        
           |  |  | 73 | 		return $this->corps_http;
 | 
        
           |  |  | 74 | 	}
 | 
        
           | 6 | jpm | 75 |   | 
        
           | 288 | delphine | 76 |   | 
        
           | 6 | jpm | 77 | 	public function testerResultat($resultat, &$resultat_formate, $requete) {
 | 
        
           |  |  | 78 | 		if ($resultat == '') { //cas ou coste/services/ ou la requete comporte des erreurs
 | 
        
           |  |  | 79 | 			$e = 'La requête SQL formée comporte une erreur!';
 | 
        
           |  |  | 80 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$e);
 | 
        
           |  |  | 81 | 			Debug::printr($requete);
 | 
        
           |  |  | 82 | 		} elseif ($resultat) {
 | 
        
           |  |  | 83 | 			$resultat_formate = $this->retournerResultatFormate($resultat);
 | 
        
           |  |  | 84 | 		} else {
 | 
        
           | 356 | delphine | 85 | 			if ($this->format_reponse == 'textes/relations') {
 | 
        
           | 6 | jpm | 86 | 				$resultat_formate = 'null';
 | 
        
           |  |  | 87 | 			} else {
 | 
        
           |  |  | 88 | 				$m = 'Données introuvables dans la base';
 | 
        
           |  |  | 89 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$m);
 | 
        
           |  |  | 90 | 				Debug::printr($requete);
 | 
        
           |  |  | 91 | 			}
 | 
        
           |  |  | 92 | 		}
 | 
        
           |  |  | 93 | 	}
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | //---------------------------------TRAITER RESSOURCES-----------------------------------------------------------------
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 | 	public function traiterRessources(&$ressources) {
 | 
        
           |  |  | 99 | 		if (isset($ressources) && !empty($ressources)) {
 | 
        
           |  |  | 100 | 			$this->table = Config::get('bdd_triples');
 | 
        
           |  |  | 101 | 			$this->table_ressources = $ressources;
 | 
        
           |  |  | 102 | 			if ($ressources[0] == 'relations') {
 | 
        
           |  |  | 103 | 				$this->traiterRelationsProjet($ressources);
 | 
        
           |  |  | 104 | 				$this->format_reponse .= '/relations';
 | 
        
           |  |  | 105 | 			} elseif (isset($this->table_ressources[0]) && !empty($this->table_ressources[0])) {
 | 
        
           |  |  | 106 | 				$this->traiterRessourceId($this->table_ressources[0]);
 | 
        
           |  |  | 107 | 				if (isset($this->table_ressources[1]) && !empty($this->table_ressources[1])) {
 | 
        
           |  |  | 108 | 					$this->format_reponse .= '/champ';
 | 
        
           |  |  | 109 | 				}
 | 
        
           |  |  | 110 | 			} else {
 | 
        
           |  |  | 111 | 				$e = "Erreur dans votre requête. <br/> Les ressources disponibles sont :
 | 
        
           |  |  | 112 | 				<li> /textes/#id (id correspondant au tag de la page </li>
 | 
        
           |  |  | 113 | 				<li> /textes/#id/#champ+#champ </li>
 | 
        
           |  |  | 114 | 				<li> /textes/relations/bdtfx/#id (id correspondant au num_nom du projet bdtfx </li>";
 | 
        
           |  |  | 115 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 116 | 			}
 | 
        
           |  |  | 117 | 		} else {
 | 
        
           |  |  | 118 | 			$this->traiterRessourcesListe();
 | 
        
           |  |  | 119 | 		}
 | 
        
           |  |  | 120 | 		$this->requete_condition[] = "p.latest = 'Y'";
 | 
        
           |  |  | 121 | 	}
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 | 	public function traiterRessourceId($id) {
 | 
        
           |  |  | 124 | 		$this->format_reponse .= '/id';
 | 
        
           |  |  | 125 | 		if ($this->recherche == 'etendue') {
 | 
        
           |  |  | 126 | 			$id = '%'.str_replace(' ','%', $id).'%';
 | 
        
           | 289 | delphine | 127 | 		}
 | 
        
           |  |  | 128 | 		if (is_numeric($id)) {
 | 
        
           |  |  | 129 | 			$this->requete_condition[] = 'c.id ='.$this->getBdd()->proteger($id);
 | 
        
           |  |  | 130 | 		} else {
 | 
        
           |  |  | 131 | 			$id = explode('bdtfx.nn:', $id);
 | 
        
           | 641 | aurelien | 132 | 			if (isset($id[1]) && is_numeric($id[1])) {
 | 
        
           | 289 | delphine | 133 | 				$this->requete_condition[] = 'c.num_nom ='.$this->getBdd()->proteger($id[1]);
 | 
        
           | 641 | aurelien | 134 | 			} else {
 | 
        
           |  |  | 135 | 				$e = "Erreur dans votre requête. <br/> Dans le cas d'une ressource bdtfx.nn: le num nom est obligatoire !";
 | 
        
           |  |  | 136 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           | 289 | delphine | 137 | 			}
 | 
        
           |  |  | 138 | 		}
 | 
        
           | 288 | delphine | 139 |   | 
        
           |  |  | 140 | 		$this->requete_champ = 'c.id AS num_nom, i.nom_sci AS titre, t1.resource AS tag, p.body AS texte';
 | 
        
           |  |  | 141 | 		$this->table = array(Config::get("bdd_correspondance_bdnff").' c',
 | 
        
           |  |  | 142 | 			Config::get("bdd_triples").' t1',
 | 
        
           |  |  | 143 | 			Config::get("bdd_index").' i',
 | 
        
           |  |  | 144 | 			Config::get("bdd_pages").' p');
 | 
        
           | 289 | delphine | 145 |   | 
        
           | 288 | delphine | 146 | 		$this->requete_condition[] = 'c.id = i.num_nom';
 | 
        
           |  |  | 147 | 		$this->requete_condition[] = 'i.nom_sci = t1.value';
 | 
        
           |  |  | 148 | 		$this->requete_condition[] = 't1.resource = p.tag';
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 | 		/*$this->requete_champ = ' t1.resource AS tag, t1.value AS num_nom, t2.value AS titre, p.body AS texte';
 | 
        
           | 6 | jpm | 151 | 		$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($id);
 | 
        
           |  |  | 152 | 		$this->requete_condition[] = 't1.property = "num_nom"';
 | 
        
           |  |  | 153 | 		$this->requete_condition[] = '(t1.resource = t2.resource AND t2.property = "titre")';
 | 
        
           |  |  | 154 | 		$this->requete_condition[] = 't1.resource = p.tag';
 | 
        
           |  |  | 155 | 		$this->table = array(Config::get("bdd_triples").' t1',
 | 
        
           |  |  | 156 | 							Config::get("bdd_triples").' t2',
 | 
        
           |  |  | 157 | 							Config::get("bdd_pages").' p');
 | 
        
           | 288 | delphine | 158 | 		$this->ajouterRechercheFloue('t1.resource', $id);*/
 | 
        
           | 6 | jpm | 159 | 	}
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 | 	public function traiterRessourcesListe() {
 | 
        
           |  |  | 162 | 		$this->requete_condition[] = "t1.property = 'num_nom'";
 | 
        
           |  |  | 163 | 		$this->requete_condition[] = "(t1.resource = t2.resource AND t2.property = 'titre')";
 | 
        
           |  |  | 164 | 		$this->requete_condition[] = "t1.resource = p.tag";
 | 
        
           |  |  | 165 | 	}
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 | 	public function traiterRelationsProjet($tab_ress) {
 | 
        
           |  |  | 168 | 		$this->table = array(Config::get("bdd_triples")." t1", Config::get("bdd_pages")." p", Config::get("bdd_table").'_v0_1 c');
 | 
        
           |  |  | 169 | 		$projet = $tab_ress[1];
 | 
        
           |  |  | 170 | 		$num_nom = $tab_ress[2];
 | 
        
           |  |  | 171 | 		$nom_champ_projet = "flore_$projet"."_num";
 | 
        
           |  |  | 172 | 		if (isset($tab_ress[3])) {
 | 
        
           |  |  | 173 | 			$type = $tab_ress[3];
 | 
        
           |  |  | 174 | 			if (!in_array($type, array('cle', 'dsc'))) {
 | 
        
           |  |  | 175 | 				$e = "Le type de texte ne peut etre que clé ou description";
 | 
        
           |  |  | 176 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 177 | 			}
 | 
        
           |  |  | 178 | 		}
 | 
        
           |  |  | 179 | 		$this->requete_condition[] = "c.flore_bdnff_num = ".$this->getBdd()->proteger($num_nom);
 | 
        
           |  |  | 180 | 		$this->requete_condition[] = "t1.value = c.num_nom";
 | 
        
           |  |  | 181 | 		$this->requete_condition[] = "t1.property = 'num_nom'";
 | 
        
           |  |  | 182 | 		$this->requete_condition[] = "t1.resource = p.tag";
 | 
        
           |  |  | 183 | 		$this->requete_champ = 't1.resource AS tag, t1.value AS num_nom, p.body AS texte';
 | 
        
           |  |  | 184 | 		if (isset($type)) $this->requete_condition[] = "t1.resource LIKE ".$this->getBdd()->proteger(ucfirst($type.'%'));
 | 
        
           |  |  | 185 | 	}
 | 
        
           |  |  | 186 |   | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 | 	public function ajouterRechercheFloue($nom_champ, $id) {
 | 
        
           |  |  | 189 | 		if ($this->recherche == 'floue') {
 | 
        
           |  |  | 190 | 			$id = $this->getBdd()->proteger($id);
 | 
        
           |  |  | 191 | 			$this->requete_condition[array_search("$nom_champ LIKE $id",
 | 
        
           |  |  | 192 | 				$this->requete_condition)] =
 | 
        
           |  |  | 193 | 				"($nom_champ LIKE $id)".
 | 
        
           |  |  | 194 | 				" OR ( SOUNDEX($nom_champ) = SOUNDEX($id))".
 | 
        
           |  |  | 195 | 				" OR SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($id))))";
 | 
        
           |  |  | 196 | 		}
 | 
        
           |  |  | 197 | 	}
 | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 | //---------------------------------------TRAITER PARAMETRES-------------------------------------------------------------
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 | 	public function traiterParametres($parametres) {
 | 
        
           |  |  | 202 | 		if (isset($parametres) && !empty($parametres)) {
 | 
        
           |  |  | 203 | 			$this->table_param = $parametres;
 | 
        
           |  |  | 204 | 			if (isset($parametres['recherche']) && $parametres['recherche'] != '') {
 | 
        
           |  |  | 205 | 				$this->recherche = $parametres['recherche'];
 | 
        
           |  |  | 206 | 			}
 | 
        
           |  |  | 207 | 			foreach ($parametres as $param => $val) {
 | 
        
           |  |  | 208 | 				switch ($param) {
 | 
        
           | 288 | delphine | 209 | 					case 'retour.format'	: $this->retour_format = $val;										break;
 | 
        
           |  |  | 210 | 					case 'masque.titre' 	: $this->ajouterLeFiltreMasque('titre', $val);						break;
 | 
        
           |  |  | 211 | 					case 'masque.famille'	: $this->ajouterLeFiltreMasque('famille', $val);					break;
 | 
        
           |  |  | 212 | 					case 'masque.ns'		: $this->ajouterLeFiltreMasque('nom_sci', $val);					break;
 | 
        
           |  |  | 213 | 					case 'masque.type'		: $this->ajouterLeFiltreMasque('type', $val);						break;
 | 
        
           |  |  | 214 | 					case 'masque.txt'		: $this->ajouterLeFiltreMasque('texte', $val);						break;
 | 
        
           |  |  | 215 | 					case 'masque.page'		: $this->ajouterLeFiltreMasque('page',$val);						break;
 | 
        
           |  |  | 216 | 					case 'masque.tome'		: $this->ajouterLeFiltreMasque('tome',$val);						break;
 | 
        
           |  |  | 217 | 					case 'navigation.depart': $this->limite_requete['depart'] = $val;							break;
 | 
        
           |  |  | 218 | 					case 'navigation.limite': $this->limite_requete['limite'] = $val;							break;
 | 
        
           | 6 | jpm | 219 | 					case 'recherche'		: 																	break;
 | 
        
           | 290 | delphine | 220 | 					case 'txt.format'		: 																	break;
 | 
        
           | 6 | jpm | 221 | 					default					:
 | 
        
           |  |  | 222 | 						$e = "Erreur dans les paramètres de recherche de votre requête : </br> Le parametre	$param n\'existe pas.";
 | 
        
           |  |  | 223 | 						$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
 | 
        
           |  |  | 224 | 						break;
 | 
        
           |  |  | 225 | 				}
 | 
        
           |  |  | 226 | 			}
 | 
        
           |  |  | 227 | 		}
 | 
        
           |  |  | 228 | 	}
 | 
        
           |  |  | 229 |   | 
        
           |  |  | 230 |   | 
        
           |  |  | 231 | 	public function ajouterLeFiltreMasque($masque, $valeur) {
 | 
        
           |  |  | 232 | 		if ($this->recherche == 'etendue') {
 | 
        
           |  |  | 233 | 			$valeur = '%'.str_replace(' ','%', $valeur).'%';
 | 
        
           |  |  | 234 | 		}
 | 
        
           |  |  | 235 | 		$valeur = $this->getBdd()->proteger($valeur);
 | 
        
           |  |  | 236 | 		$this->masque[] = "$masque=$valeur";
 | 
        
           |  |  | 237 | 		switch ($masque) {
 | 
        
           |  |  | 238 | 			case 'famille' :
 | 
        
           |  |  | 239 | 				break;
 | 
        
           |  |  | 240 | 			case 'nom_sci' :
 | 
        
           |  |  | 241 | 				$this->requete_condition[] = "t1.value = c.num_nom AND nom_sci LIKE $valeur)";
 | 
        
           |  |  | 242 | 				$this->ajouterRechercheFloue('nom_sci', $valeur);
 | 
        
           |  |  | 243 | 				$this->table[] = Config::get("bdd_table").'_v0_1 c';
 | 
        
           |  |  | 244 | 				break;
 | 
        
           |  |  | 245 | 			case 'type'    :
 | 
        
           |  |  | 246 | 				$type = ($this->getBdd()->proteger($valeur) == '"cle"') ? 'Cle%' : 'Dsc%';
 | 
        
           |  |  | 247 | 				$this->requete_condition[] = 't1.resource LIKE '.$this->getBdd()->proteger($type);
 | 
        
           |  |  | 248 | 				break;
 | 
        
           |  |  | 249 | 			case 'texte'   :
 | 
        
           |  |  | 250 | 				$this->requete_condition[] = "p.body LIKE $valeur";
 | 
        
           |  |  | 251 | 				$this->ajouterRechercheFloue('p.body', $valeur);
 | 
        
           |  |  | 252 | 				break;
 | 
        
           |  |  | 253 | 			case 'titre'   :
 | 
        
           |  |  | 254 | 				$this->requete_condition[] = "t2.value LIKE $valeur";
 | 
        
           |  |  | 255 | 				$this->ajouterRechercheFloue('t2.value', $valeur);
 | 
        
           |  |  | 256 | 				break;
 | 
        
           |  |  | 257 | 			case 'page'	   :
 | 
        
           |  |  | 258 | 				$this->requete_condition[] =  "t1.value = c.num_nom AND c.page = $valeur";
 | 
        
           |  |  | 259 | 				$this->table[] = Config::get("bdd_table").'_v0_1 c';
 | 
        
           |  |  | 260 | 				break;
 | 
        
           |  |  | 261 | 			case 'tome'	   :
 | 
        
           |  |  | 262 | 				$this->requete_condition[] =  "t1.value = c.num_nom AND c.tome = $valeur";
 | 
        
           |  |  | 263 | 				$this->table[] = Config::get("bdd_table").'_v0_1 c';
 | 
        
           |  |  | 264 | 				break;
 | 
        
           |  |  | 265 | 			default 		:
 | 
        
           |  |  | 266 | 				break;
 | 
        
           |  |  | 267 | 		}
 | 
        
           |  |  | 268 | 	}
 | 
        
           |  |  | 269 |   | 
        
           |  |  | 270 |   | 
        
           |  |  | 271 | //-----------------------------FONCTIONS DASSEMBLAGE DE LA REQUETE-----------------------------------------------------
 | 
        
           |  |  | 272 |   | 
        
           |  |  | 273 | 	public function assemblerLaRequete() {
 | 
        
           |  |  | 274 | 		$requete = 	' SELECT '.$this->requete_champ.
 | 
        
           |  |  | 275 | 					' FROM '.implode(', ', $this->table)
 | 
        
           |  |  | 276 | 					.$this->retourneRequeteCondition()
 | 
        
           |  |  | 277 | 					.$this->requete_group_by
 | 
        
           |  |  | 278 | 					.$this->formerRequeteLimite();
 | 
        
           |  |  | 279 | 		return $requete;
 | 
        
           |  |  | 280 | 	}
 | 
        
           |  |  | 281 |   | 
        
           |  |  | 282 |   | 
        
           |  |  | 283 | 	public  function retourneRequeteCondition() {
 | 
        
           |  |  | 284 | 		$condition = '';
 | 
        
           |  |  | 285 | 		if ($this->requete_condition !== null) {
 | 
        
           |  |  | 286 | 			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
 | 
        
           |  |  | 287 | 		}
 | 
        
           |  |  | 288 | 		return $condition;
 | 
        
           |  |  | 289 | 	}
 | 
        
           |  |  | 290 |   | 
        
           |  |  | 291 | 	public function formerRequeteLimite() {
 | 
        
           |  |  | 292 | 		if (in_array($this->format_reponse , array('textes/id', 'textes/id/relations'))) {
 | 
        
           |  |  | 293 | 			$this->requete_limite = '';
 | 
        
           |  |  | 294 | 		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
 | 
        
           |  |  | 295 | 			$this->limite_requete['depart'] =
 | 
        
           |  |  | 296 | 				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
 | 
        
           |  |  | 297 | 			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
 | 
        
           |  |  | 298 | 		} else {
 | 
        
           |  |  | 299 | 			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
 | 
        
           |  |  | 300 | 		}
 | 
        
           |  |  | 301 | 		return $this->requete_limite;
 | 
        
           |  |  | 302 | 	}
 | 
        
           |  |  | 303 |   | 
        
           |  |  | 304 |   | 
        
           |  |  | 305 | 	/**
 | 
        
           |  |  | 306 | 	 * Recupere le nombre total de résultat d'une requete lancée.
 | 
        
           |  |  | 307 | 	 * Est utilisée lors de l'affichage du total dans le résultat retourné et pr déterminer les limites et départ
 | 
        
           |  |  | 308 | 	 * @return nombre total de résultat
 | 
        
           |  |  | 309 | 	 */
 | 
        
           |  |  | 310 | 	public function recupererTotalResultat() {
 | 
        
           |  |  | 311 | 		$total = null;
 | 
        
           |  |  | 312 | 		$requete = 'SELECT count(*) as nombre FROM '
 | 
        
           |  |  | 313 | 					.implode(', ', $this->table).$this->retourneRequeteCondition()
 | 
        
           |  |  | 314 | 					.$this->requete_group_by;
 | 
        
           |  |  | 315 | 		$res = $this->getBdd()->recuperer($requete);
 | 
        
           |  |  | 316 | 		if ($res) {
 | 
        
           |  |  | 317 | 			$total = $res['nombre'];
 | 
        
           |  |  | 318 | 		} else {
 | 
        
           |  |  | 319 | 			$e = 'Données introuvables dans la base ou erreur dans la requête SQL';
 | 
        
           |  |  | 320 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
 | 
        
           |  |  | 321 | 		}
 | 
        
           |  |  | 322 | 		return $total;
 | 
        
           |  |  | 323 | 	}
 | 
        
           |  |  | 324 |   | 
        
           |  |  | 325 |   | 
        
           |  |  | 326 |   | 
        
           |  |  | 327 | //-------------------FONCTIONS POUR LE FORMATAGE EN JSON----------------------------------------------------------------
 | 
        
           |  |  | 328 |   | 
        
           |  |  | 329 | 	public function retournerResultatFormate($resultat) {
 | 
        
           |  |  | 330 | 		$reponse = '';
 | 
        
           |  |  | 331 | 		switch ($this->format_reponse) {
 | 
        
           |  |  | 332 | 			case 'textes' 				: $reponse = $this->formaterTextes($resultat);  		break;
 | 
        
           |  |  | 333 | 			case 'textes/relations'		: $reponse = $this->formaterTextesRelations($resultat); break;
 | 
        
           |  |  | 334 | 			case 'textes/id' 			: $reponse = $this->formaterTextesId($resultat[0]); 	 break;
 | 
        
           |  |  | 335 | 			case 'textes/id/champ' 		: $reponse = $this->formaterTextesIdChamp($resultat[0]); break;
 | 
        
           |  |  | 336 | 			default : break;
 | 
        
           |  |  | 337 | 		}
 | 
        
           |  |  | 338 | 		return $reponse;
 | 
        
           |  |  | 339 | 	}
 | 
        
           |  |  | 340 |   | 
        
           |  |  | 341 |   | 
        
           |  |  | 342 | 	public function formaterTextes($resultat) {
 | 
        
           |  |  | 343 | 		$this->ajouterEnteteResultat($resultat);
 | 
        
           |  |  | 344 | 		$reponse['entete'] = $this->table_retour;
 | 
        
           |  |  | 345 | 		$this->table_retour = array();
 | 
        
           |  |  | 346 | 		foreach ($resultat as $description) {
 | 
        
           |  |  | 347 | 			$tag = $description['tag'];
 | 
        
           |  |  | 348 | 			$this->afficherDonnees('titre', $description['titre']);
 | 
        
           |  |  | 349 | 			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
 | 
        
           |  |  | 350 | 			$this->afficherDonnees('type', $type);
 | 
        
           |  |  | 351 | 			$this->table_retour['texte'] = $description['texte'];
 | 
        
           |  |  | 352 | 			$reponse['resultat'][$tag] = $this->table_retour;
 | 
        
           |  |  | 353 | 			$this->table_retour = array();
 | 
        
           |  |  | 354 | 			$reponse['resultat'][$tag]['href'] = $this->ajouterHref('textes', $tag);
 | 
        
           |  |  | 355 | 		}
 | 
        
           |  |  | 356 | 		return $reponse;
 | 
        
           |  |  | 357 | 	}
 | 
        
           |  |  | 358 |   | 
        
           |  |  | 359 | 	public function formaterTextesRelations($resultat) {
 | 
        
           |  |  | 360 | 		foreach ($resultat as $description) {
 | 
        
           |  |  | 361 | 			$tag = $description['tag'];
 | 
        
           |  |  | 362 | 			$type = (preg_match('/^Cle.*$/', $tag)) ? '1' : '2' ;
 | 
        
           |  |  | 363 | 			$this->afficherDonnees('type', $type);
 | 
        
           |  |  | 364 | 			$this->table_retour['texte'] = $description['texte'];
 | 
        
           |  |  | 365 | 			$reponse[$tag] = $this->table_retour;
 | 
        
           |  |  | 366 | 			$this->table_retour = array();
 | 
        
           |  |  | 367 | 			$reponse[$tag]['href'] = $this->ajouterHref('textes', $tag);
 | 
        
           |  |  | 368 | 		}
 | 
        
           |  |  | 369 | 		return $reponse;
 | 
        
           |  |  | 370 | 	}
 | 
        
           |  |  | 371 |   | 
        
           |  |  | 372 | 	public function ajouterEnteteResultat($resultat) {
 | 
        
           |  |  | 373 | 		if ($this->masque) {
 | 
        
           |  |  | 374 | 			$this->table_retour['masque'] = implode('&', $this->masque);
 | 
        
           |  |  | 375 | 		}
 | 
        
           |  |  | 376 | 		$this->table_retour['depart'] = $this->limite_requete['depart'];
 | 
        
           |  |  | 377 | 		$this->table_retour['limite'] = $this->limite_requete['limite'];
 | 
        
           |  |  | 378 | 		$this->table_retour['total']  = $this->total_resultat;
 | 
        
           |  |  | 379 | 		//formuler les urls
 | 
        
           |  |  | 380 | 		$url = $this->formulerUrl($this->total_resultat, '/'.$this->service);
 | 
        
           |  |  | 381 | 		if ($url['precedent'] != '') { $this->table_retour['href.precedent'] = $url['precedent']; }
 | 
        
           |  |  | 382 | 		if ($url['suivant']   != '') { $this->table_retour['href.suivant']   = $url['suivant']; }
 | 
        
           |  |  | 383 | 	}
 | 
        
           |  |  | 384 |   | 
        
           |  |  | 385 |   | 
        
           |  |  | 386 | 	public function formaterTextesId($reponse) {
 | 
        
           |  |  | 387 |   | 
        
           |  |  | 388 | 		$this->afficherDonnees('titre', $reponse['titre']);
 | 
        
           |  |  | 389 | 		$type = (preg_match('/^Cle.*$/', $reponse['tag'])) ? '2' : '1' ;
 | 
        
           |  |  | 390 | 		$this->afficherDonnees('type', $type);
 | 
        
           |  |  | 391 | 		$this->table_retour['texte'] = $reponse['texte'];
 | 
        
           | 289 | delphine | 392 | 		//$this->afficherDonnees('id', $reponse['num_nom']);
 | 
        
           | 6 | jpm | 393 |   | 
        
           |  |  | 394 | 		return $this->table_retour;
 | 
        
           |  |  | 395 | 	}
 | 
        
           |  |  | 396 |   | 
        
           |  |  | 397 |   | 
        
           |  |  | 398 | 	public function formaterTextesIdChamp($resultat) {
 | 
        
           |  |  | 399 | 		//on recupère tous les resultats possibles
 | 
        
           |  |  | 400 | 		$reponse_id = $this->formaterTextesId($resultat);
 | 
        
           |  |  | 401 | 		$this->table_retour = array();
 | 
        
           |  |  | 402 | 		//on recupère les résultats demandés à partir du tableau de résultat complet
 | 
        
           |  |  | 403 | 		$this->table_retour['id'] = $resultat['tag'];
 | 
        
           |  |  | 404 | 		$champs = explode(' ', $this->table_ressources[1]);
 | 
        
           |  |  | 405 | 		foreach ($champs as $champ) {
 | 
        
           |  |  | 406 | 			if ($this->verifierValiditeChamp($champ, $reponse_id)) {
 | 
        
           |  |  | 407 | 				if (preg_match('/^[^.]+\.\*$/', $champ)) {
 | 
        
           |  |  | 408 | 					$this->afficherPointEtoile($champ, $reponse_id);
 | 
        
           |  |  | 409 | 				} else {
 | 
        
           |  |  | 410 | 					$this->table_retour[$champ] = $reponse_id[$champ];
 | 
        
           |  |  | 411 | 				}
 | 
        
           |  |  | 412 | 			}
 | 
        
           |  |  | 413 | 		}
 | 
        
           |  |  | 414 | 		return $this->table_retour;
 | 
        
           |  |  | 415 | 	}
 | 
        
           |  |  | 416 |   | 
        
           |  |  | 417 |   | 
        
           |  |  | 418 | 	public function afficherPointEtoile($champ, $reponse_id) {
 | 
        
           |  |  | 419 | 		preg_match('/^([^.]+\.)\*$/', $champ, $match);
 | 
        
           |  |  | 420 | 		foreach ($reponse_id as $chp => $valeur) {
 | 
        
           |  |  | 421 | 			if (strrpos($chp, $match[1]) !== false) {
 | 
        
           |  |  | 422 | 				$this->table_retour[$chp] = $valeur;
 | 
        
           |  |  | 423 | 			}
 | 
        
           |  |  | 424 | 		}
 | 
        
           |  |  | 425 | 	}
 | 
        
           |  |  | 426 |   | 
        
           |  |  | 427 |   | 
        
           |  |  | 428 | 	public function verifierValiditeChamp($champ, $reponse_id) {
 | 
        
           |  |  | 429 | 		$validite = false;
 | 
        
           |  |  | 430 | 		preg_match('/^([^.]+)(:?\.([^.]+))?$/', $champ, $match);
 | 
        
           |  |  | 431 | 		if (array_key_exists($match[1], $reponse_id)) {
 | 
        
           |  |  | 432 | 			$validite = true;
 | 
        
           |  |  | 433 | 			if (isset($match[2]) && !empty($match[2])) {
 | 
        
           |  |  | 434 | 				if ($match[1] == 'type') {
 | 
        
           |  |  | 435 | 					$suffixes = array('.code', '.href', '.*');
 | 
        
           |  |  | 436 | 				$validite = (in_array($match[2], $suffixes)) ? true : false;
 | 
        
           |  |  | 437 | 				} else {
 | 
        
           |  |  | 438 | 					$validite = false;
 | 
        
           |  |  | 439 | 				}
 | 
        
           |  |  | 440 | 			}
 | 
        
           |  |  | 441 | 		}
 | 
        
           |  |  | 442 | 		if (!$validite) {
 | 
        
           |  |  | 443 | 			$champs = implode('</li><li>', array_keys($reponse_id));
 | 
        
           |  |  | 444 | 			$e = 'Erreur dans votre requête : </br> Le champ "'.$champ.'" n\'existe pas. Les champs disponibles
 | 
        
           |  |  | 445 | 				 sont : <li>'.$champs.'</li>';
 | 
        
           |  |  | 446 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
 | 
        
           |  |  | 447 | 		}
 | 
        
           |  |  | 448 | 		return $validite;
 | 
        
           |  |  | 449 | 	}
 | 
        
           |  |  | 450 |   | 
        
           |  |  | 451 |   | 
        
           |  |  | 452 | //------------------------------------------------Fonction d'affichage ------------------------------------------------
 | 
        
           |  |  | 453 |   | 
        
           |  |  | 454 | 	public function afficherDonnees($champ, $valeur) {
 | 
        
           |  |  | 455 | 		if ($this->retour_format == 'min') {
 | 
        
           |  |  | 456 | 			$champ = ($champ == 'type') ? $champ.'.code' : $champ;
 | 
        
           |  |  | 457 | 			$this->table_retour[$champ] = $valeur;
 | 
        
           |  |  | 458 | 		} else {
 | 
        
           |  |  | 459 | 			$this->afficherDonneesMax($champ, $valeur);
 | 
        
           |  |  | 460 | 		}
 | 
        
           |  |  | 461 | 	}
 | 
        
           |  |  | 462 |   | 
        
           |  |  | 463 |   | 
        
           |  |  | 464 | 	public function afficherDonneesMax($champ, $valeur) {
 | 
        
           |  |  | 465 | 		if ($champ == 'id') {
 | 
        
           |  |  | 466 | 			$this->table_retour[$champ] = "coste.".$valeur;
 | 
        
           |  |  | 467 | 			$this->table_retour['nom_sci'] = $this->recupererNomSci($valeur);
 | 
        
           |  |  | 468 | 			$this->table_retour['id.href'] = $this->ajouterHref('noms', $valeur);
 | 
        
           |  |  | 469 | 		} elseif (in_array($champ, array('type'))) {
 | 
        
           |  |  | 470 | 			$this->table_retour[$champ.'.code'] = $valeur;
 | 
        
           |  |  | 471 | 			$this->table_retour[$champ] = $this->recupererSignificationCode($valeur);
 | 
        
           | 289 | delphine | 472 | 			$this->table_retour[$champ.'.href'] = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $valeur, 'eflore');
 | 
        
           | 6 | jpm | 473 | 		} else {
 | 
        
           |  |  | 474 | 			$this->table_retour[$champ] = $valeur;
 | 
        
           |  |  | 475 | 		}
 | 
        
           |  |  | 476 | 	}
 | 
        
           |  |  | 477 |   | 
        
           |  |  | 478 |   | 
        
           |  |  | 479 | 	public function recupererSignificationCode($code) {
 | 
        
           | 289 | delphine | 480 | 		$url = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $code.'/nom', 'eflore');
 | 
        
           | 6 | jpm | 481 | 		$res = $this->consulterHref($url);
 | 
        
           |  |  | 482 | 		return $res->nom;
 | 
        
           |  |  | 483 | 	}
 | 
        
           |  |  | 484 |   | 
        
           |  |  | 485 |   | 
        
           |  |  | 486 | 	public function recupererNomSci($id) {
 | 
        
           |  |  | 487 | 		$req = 'SELECT nom_sci FROM coste_v0_1 WHERE num_nom = '.$id;
 | 
        
           |  |  | 488 | 		if ($this->html == 'htm') {
 | 
        
           |  |  | 489 | 			$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
 | 
        
           |  |  | 490 | 		}
 | 
        
           |  |  | 491 | 		$res = $this->getBdd()->recuperer($req);
 | 
        
           |  |  | 492 | 		return $res['nom_sci'];
 | 
        
           |  |  | 493 | 	}
 | 
        
           |  |  | 494 |   | 
        
           |  |  | 495 | }
 | 
        
           |  |  | 496 |   | 
        
           |  |  | 497 | ?>
 |