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