862 |
aurelien |
1 |
<?php
|
906 |
aurelien |
2 |
/**
|
|
|
3 |
* PHP Version 5
|
|
|
4 |
*
|
|
|
5 |
* @category PHP
|
|
|
6 |
* @package jrest
|
|
|
7 |
* @author Aurélien Peronnet <aurelien@tela-botania.org>
|
|
|
8 |
* @copyright 2010 Tela-Botanica
|
|
|
9 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
10 |
* @version SVN: <svn_id>
|
|
|
11 |
* @link /doc/jrest/
|
862 |
aurelien |
12 |
*/
|
|
|
13 |
|
|
|
14 |
/**
|
906 |
aurelien |
15 |
* in : utf8
|
|
|
16 |
* out : utf8
|
|
|
17 |
*
|
|
|
18 |
* Librairie recherche d'observations a partir de divers critères
|
|
|
19 |
*
|
862 |
aurelien |
20 |
*/
|
|
|
21 |
class RechercheObservation extends Cel {
|
|
|
22 |
|
930 |
aurelien |
23 |
public function obtenirIdObservationsPourOrdre($id_utilisateur, $ordre) {
|
|
|
24 |
|
|
|
25 |
$requete_selection_id = 'SELECT id_observation FROM cel_obs WHERE ordre ';
|
|
|
26 |
|
|
|
27 |
if(is_array($ordre)) {
|
|
|
28 |
$ordre = array_map(array($this,'proteger'), $ordre);
|
|
|
29 |
$requete_selection_id .= ' IN ('.implode(',',$ordre).') ';
|
|
|
30 |
} else {
|
|
|
31 |
$requete_selection_id .= ' = '.$this->proteger($ordre).' ';
|
|
|
32 |
}
|
|
|
33 |
|
959 |
aurelien |
34 |
$requete_selection_id .= ' AND ce_utilisateur = '.$this->proteger($id_utilisateur);
|
|
|
35 |
$requete_selection_id .= ' ORDER BY id_observation';
|
930 |
aurelien |
36 |
|
|
|
37 |
$resultat_ids = $this->requeter($requete_selection_id);
|
|
|
38 |
|
|
|
39 |
$ids = array();
|
|
|
40 |
if(is_array($resultat_ids)) {
|
|
|
41 |
foreach ($resultat_ids as $resultat) {
|
|
|
42 |
$ids[] = $resultat['id_observation'];
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
return $ids;
|
|
|
47 |
}
|
|
|
48 |
|
959 |
aurelien |
49 |
public function rechercherObservations($id_utilisateur = null, $criteres = array(), $debut = 0, $limite = 50) {
|
862 |
aurelien |
50 |
|
906 |
aurelien |
51 |
$requete_selection_observations = 'SELECT * FROM cel_obs ';
|
862 |
aurelien |
52 |
|
906 |
aurelien |
53 |
if($id_utilisateur != null) {
|
|
|
54 |
$requete_selection_observations .= 'WHERE ce_utilisateur = '.$this->proteger($id_utilisateur).' AND ';
|
|
|
55 |
} else if(count($criteres) > 0) {
|
|
|
56 |
$requete_selection_observations .= 'WHERE ';
|
|
|
57 |
}
|
|
|
58 |
|
862 |
aurelien |
59 |
$sous_requete_recherche = $this->fabriquerSousRequeteRecherche($id_utilisateur, $criteres);
|
|
|
60 |
|
|
|
61 |
$requete_selection_observations .= $sous_requete_recherche;
|
935 |
aurelien |
62 |
$requete_selection_observations = rtrim($requete_selection_observations, 'AND ');
|
862 |
aurelien |
63 |
|
1380 |
aurelien |
64 |
$requete_selection_observations .= ' ORDER BY id_observation, ordre ';
|
|
|
65 |
$requete_selection_observations .= ($debut != 0 && $limite != 0) ? 'LIMIT '.$debut.','.$limite : '' ;
|
862 |
aurelien |
66 |
$resultats_observations = array();
|
|
|
67 |
$resultats_observations = $this->requeter($requete_selection_observations);
|
1332 |
aurelien |
68 |
|
862 |
aurelien |
69 |
return $resultats_observations;
|
|
|
70 |
}
|
|
|
71 |
|
906 |
aurelien |
72 |
public function compterObservations($id_utilisateur = null, $criteres = array()) {
|
862 |
aurelien |
73 |
|
906 |
aurelien |
74 |
$requete_selection_observations = 'SELECT COUNT(*) as nb_obs FROM cel_obs ';
|
862 |
aurelien |
75 |
|
906 |
aurelien |
76 |
if($id_utilisateur != null) {
|
|
|
77 |
$requete_selection_observations .= 'WHERE ce_utilisateur = '.$this->proteger($id_utilisateur).' AND ';
|
|
|
78 |
} else if(count($criteres) > 0) {
|
|
|
79 |
$requete_selection_observations .= 'WHERE ';
|
|
|
80 |
}
|
|
|
81 |
|
862 |
aurelien |
82 |
$sous_requete_recherche = $this->fabriquerSousRequeteRecherche($id_utilisateur, $criteres);
|
|
|
83 |
|
|
|
84 |
$requete_selection_observations .= $sous_requete_recherche;
|
935 |
aurelien |
85 |
$requete_selection_observations = rtrim($requete_selection_observations, 'AND ');
|
862 |
aurelien |
86 |
|
959 |
aurelien |
87 |
$nb_obs = '0';
|
862 |
aurelien |
88 |
$resultat_requete_nombre_observations = $this->requeter($requete_selection_observations);
|
|
|
89 |
|
|
|
90 |
if($resultat_requete_nombre_observations && is_array($resultat_requete_nombre_observations) && count($resultat_requete_nombre_observations) > 0) {
|
|
|
91 |
$nb_obs = $resultat_requete_nombre_observations[0]['nb_obs'];
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $nb_obs;
|
|
|
95 |
}
|
|
|
96 |
|
959 |
aurelien |
97 |
public function formaterPourEnvoiCel($tableau_observations) {
|
|
|
98 |
foreach($tableau_observations as &$observation) {
|
961 |
aurelien |
99 |
$observation['ce_zone_geo'] = $this->convertirCodeZoneGeoVersCodeInsee($observation['ce_zone_geo']);
|
959 |
aurelien |
100 |
$ids_mots_cles = $this->getIdsMotsClesObservation($observation['id_observation']);
|
|
|
101 |
$mots_cles_chaine = '';
|
|
|
102 |
foreach($ids_mots_cles as $id_mot_cle) {
|
1162 |
aurelien |
103 |
$mots_cles_chaine .= $id_mot_cle['id_mot_cle_obs'].';';
|
959 |
aurelien |
104 |
}
|
|
|
105 |
$mots_cles_chaine = rtrim($mots_cles_chaine,';');
|
|
|
106 |
$observation['mots_cles'] = $mots_cles_chaine;
|
1332 |
aurelien |
107 |
|
|
|
108 |
foreach($observation as $champ => $valeur) {
|
|
|
109 |
if($valeur == 'NULL') {
|
|
|
110 |
$observation[$champ] = "";
|
|
|
111 |
}
|
|
|
112 |
}
|
959 |
aurelien |
113 |
}
|
|
|
114 |
|
|
|
115 |
return $tableau_observations;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function getIdsMotsClesObservation($id_observation) {
|
|
|
119 |
|
1162 |
aurelien |
120 |
$requete_selection_mots_cles = 'SELECT DISTINCT id_mot_cle_obs '.
|
959 |
aurelien |
121 |
'FROM cel_obs_mots_cles '.
|
1295 |
aurelien |
122 |
'WHERE id_observation = '.$id_observation;
|
959 |
aurelien |
123 |
|
|
|
124 |
return $this->requeter($requete_selection_mots_cles);
|
|
|
125 |
}
|
|
|
126 |
|
862 |
aurelien |
127 |
// TODO: fonction temporaire
|
|
|
128 |
public function parserRequeteCriteres($chaine_criteres) {
|
|
|
129 |
$criteres_parses = array();
|
|
|
130 |
$criteres = explode("&", $chaine_criteres) ;
|
|
|
131 |
|
959 |
aurelien |
132 |
foreach($criteres as &$critere) {
|
|
|
133 |
$nom_valeur = explode("=",$critere) ;
|
|
|
134 |
if(count($nom_valeur) >= 2) {
|
|
|
135 |
$criteres_parses[$nom_valeur[0]] = $nom_valeur[1];
|
|
|
136 |
}
|
862 |
aurelien |
137 |
}
|
|
|
138 |
|
|
|
139 |
return $criteres_parses;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private function fabriquerSousRequeteRecherche($id_utilisateur, $criteres) {
|
|
|
143 |
|
995 |
aurelien |
144 |
$sous_requete = '';
|
|
|
145 |
|
959 |
aurelien |
146 |
foreach ($criteres as $nom => $valeur)
|
862 |
aurelien |
147 |
{
|
959 |
aurelien |
148 |
if($valeur == null || trim($nom) == "" || trim($valeur) == "") {
|
|
|
149 |
continue;
|
862 |
aurelien |
150 |
}
|
|
|
151 |
|
959 |
aurelien |
152 |
switch ($nom) {
|
862 |
aurelien |
153 |
|
1311 |
aurelien |
154 |
case "mots_cles";
|
|
|
155 |
$sous_requete .= 'mots_cles_texte LIKE '.$this->proteger('%'.$valeur.'%').' AND ';
|
|
|
156 |
break;
|
|
|
157 |
|
862 |
aurelien |
158 |
case 'annee':
|
1332 |
aurelien |
159 |
if($valeur == "NULL") {
|
|
|
160 |
$sous_requete .= "date_observation IS NULL OR year(date_observation) = 0000" ;
|
|
|
161 |
} else {
|
|
|
162 |
$sous_requete .= "year(date_observation) = ".$this->proteger($valeur) ;
|
|
|
163 |
}
|
862 |
aurelien |
164 |
$sous_requete .= ' AND ' ;
|
|
|
165 |
break;
|
|
|
166 |
|
|
|
167 |
case 'mois':
|
1332 |
aurelien |
168 |
if($valeur == "NULL") {
|
|
|
169 |
$sous_requete .= "date_observation IS NULL OR month(date_observation) = 00" ;
|
|
|
170 |
} else {
|
|
|
171 |
$sous_requete .= "month(date_observation) = ".$this->proteger($valeur) ;
|
|
|
172 |
}
|
862 |
aurelien |
173 |
$sous_requete .= ' AND ' ;
|
|
|
174 |
break;
|
|
|
175 |
|
|
|
176 |
case 'jour':
|
1332 |
aurelien |
177 |
if($valeur == "NULL") {
|
|
|
178 |
$sous_requete .= "date_observation IS NULL OR day(date_observation) = 00" ;
|
|
|
179 |
} else {
|
|
|
180 |
$sous_requete .= "day(date_observation) = ".$this->proteger($valeur) ;
|
|
|
181 |
}
|
862 |
aurelien |
182 |
$sous_requete .= ' AND ' ;
|
|
|
183 |
break;
|
959 |
aurelien |
184 |
|
|
|
185 |
case 'departement':
|
1332 |
aurelien |
186 |
if($valeur == "NULL") {
|
|
|
187 |
$sous_requete .= "(ce_zone_geo IS NULL OR ce_zone_geo = '')";
|
|
|
188 |
} else {
|
1378 |
aurelien |
189 |
if(strpos($valeur,',') !== false) {
|
|
|
190 |
$dpts = explode(',',$valeur);
|
|
|
191 |
$sous_requete .= '(';
|
|
|
192 |
foreach($dpts as $dpt) {
|
|
|
193 |
$sous_requete .= "ce_zone_geo LIKE ".$this->proteger('INSEE-C:'.$dpt.'___').' OR ';
|
|
|
194 |
}
|
|
|
195 |
$sous_requete = rtrim($sous_requete,' OR ').') ';
|
|
|
196 |
} else {
|
|
|
197 |
$sous_requete .= "ce_zone_geo LIKE ".$this->proteger('INSEE-C:'.$valeur.'___');
|
|
|
198 |
}
|
|
|
199 |
|
1332 |
aurelien |
200 |
}
|
959 |
aurelien |
201 |
$sous_requete .= ' AND ' ;
|
|
|
202 |
break;
|
|
|
203 |
|
|
|
204 |
case 'commune':
|
1332 |
aurelien |
205 |
if($valeur == "NULL") {
|
|
|
206 |
$sous_requete .= "(zone_geo IS NULL OR zone_geo = '')";
|
|
|
207 |
} else {
|
|
|
208 |
$sous_requete .= "zone_geo = ".$this->proteger($valeur);
|
|
|
209 |
}
|
959 |
aurelien |
210 |
$sous_requete .= ' AND ' ;
|
|
|
211 |
break;
|
919 |
aurelien |
212 |
|
959 |
aurelien |
213 |
case 'id_mots_cles':
|
|
|
214 |
$liste_mc = '"'.str_replace(';','","',$valeur).'"';
|
|
|
215 |
$sous_requete .= '' ;
|
1295 |
aurelien |
216 |
$sous_requete .= 'id_observation IN (SELECT id_observation FROM cel_obs_mots_cles WHERE id_mot_cle_obs IN ('.$liste_mc.'))';
|
959 |
aurelien |
217 |
$sous_requete .= ' AND ' ;
|
862 |
aurelien |
218 |
break;
|
1311 |
aurelien |
219 |
case 'recherche':
|
|
|
220 |
$sous_requete .= $this->fabriquerSousRequeteRechercheGenerale($id_utilisateur, $valeur);
|
|
|
221 |
$sous_requete .= ' AND ';
|
1378 |
aurelien |
222 |
break;
|
|
|
223 |
case 'date_debut':
|
|
|
224 |
$sous_requete .= 'date_observation >= '.$this->proteger($this->formaterEnDateMysql($valeur));
|
|
|
225 |
$sous_requete .= ' AND ';
|
|
|
226 |
break;
|
|
|
227 |
case 'date_fin':
|
|
|
228 |
$sous_requete .= 'date_observation <= '.$this->proteger($this->formaterEnDateMysql($valeur));
|
|
|
229 |
$sous_requete .= ' AND ';
|
|
|
230 |
break;
|
862 |
aurelien |
231 |
default:
|
959 |
aurelien |
232 |
if(trim($nom) != '')
|
862 |
aurelien |
233 |
{
|
959 |
aurelien |
234 |
$sous_requete .= $nom." = ".$this->proteger($valeur) ;
|
862 |
aurelien |
235 |
$sous_requete .= ' AND ' ;
|
1332 |
aurelien |
236 |
}
|
|
|
237 |
if(trim($nom) == "NULL") {
|
|
|
238 |
$sous_requete .= "(".$nom." IS NULL OR ".$nom." = '')" ;
|
|
|
239 |
$sous_requete .= ' AND ' ;
|
862 |
aurelien |
240 |
}
|
|
|
241 |
break;
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
$sous_requete = rtrim($sous_requete,' AND ');
|
|
|
246 |
return $sous_requete;
|
|
|
247 |
}
|
987 |
aurelien |
248 |
|
1378 |
aurelien |
249 |
private function formaterEnDateMysql($date) {
|
|
|
250 |
$annee = substr($date, 6, 4);
|
|
|
251 |
$mois = substr($date, 3, 2);
|
|
|
252 |
$jour = substr($date, 0, 2);
|
|
|
253 |
$date = $annee . '-' . $mois . '-' . $jour;
|
|
|
254 |
return $date;
|
|
|
255 |
}
|
|
|
256 |
|
1311 |
aurelien |
257 |
private function fabriquerSousRequeteRechercheGenerale($id_utilisateur, $valeur) {
|
|
|
258 |
$valeur = str_replace("*","%",$valeur);
|
|
|
259 |
$valeur = $this->proteger('%'.$valeur.'%');
|
|
|
260 |
$sous_requete = "(nom_sel LIKE ".$valeur.
|
|
|
261 |
" OR courriel_utilisateur LIKE ".$valeur.
|
|
|
262 |
" OR prenom_utilisateur LIKE ".$valeur.
|
|
|
263 |
" OR nom_utilisateur LIKE ".$valeur.
|
|
|
264 |
" OR nom_sel LIKE ".$valeur.
|
|
|
265 |
" OR nom_sel_nn LIKE ".$valeur.
|
|
|
266 |
" OR nom_ret LIKE ".$valeur.
|
|
|
267 |
" OR nom_ret_nn LIKE ".$valeur.
|
|
|
268 |
" OR nt LIKE ".$valeur.
|
|
|
269 |
" OR famille LIKE ".$valeur.
|
|
|
270 |
" OR zone_geo LIKE ".$valeur.
|
|
|
271 |
" OR ce_zone_geo LIKE ".$valeur.
|
|
|
272 |
" OR date_observation LIKE ".$valeur.
|
|
|
273 |
" OR lieudit LIKE ".$valeur.
|
|
|
274 |
" OR station LIKE ".$valeur.
|
|
|
275 |
" OR milieu LIKE ".$valeur.
|
|
|
276 |
" OR commentaire LIKE ".$valeur.
|
|
|
277 |
" OR transmission LIKE ".$valeur.
|
|
|
278 |
" OR latitude LIKE ".$valeur.
|
|
|
279 |
" OR longitude LIKE ".$valeur.
|
|
|
280 |
" OR mots_cles_texte LIKE ".$valeur.
|
|
|
281 |
")";
|
|
|
282 |
return $sous_requete;
|
|
|
283 |
}
|
|
|
284 |
|
987 |
aurelien |
285 |
private function estUnvaleurZeroNulle($valeur) {
|
|
|
286 |
return $valeur == "000null";
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
private function traiterRequeteValeurZeroNulle($valeur) {
|
|
|
290 |
$champs = array('annee' => 'date_observation',
|
|
|
291 |
'mois' => 'date_observation',
|
|
|
292 |
'jour' => 'date_observation',
|
|
|
293 |
'departement' => 'ce_zone_geo',
|
|
|
294 |
'commune' => 'zone_geo'
|
|
|
295 |
);
|
|
|
296 |
|
|
|
297 |
return $sous_requete .= $champs[$valeur]." = ".$this->proteger("");
|
1378 |
aurelien |
298 |
}
|
862 |
aurelien |
299 |
}
|
|
|
300 |
?>
|