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