1840 |
jpm |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe contenant des méthodes permettant de construire les requêtes SQL complexe concernant les images et obs.
|
1881 |
jpm |
5 |
* Rempli un tableau des clauses "join", "where", "group by" et "oder by" nécessaire à la *recherche* des ids des
|
|
|
6 |
* observations/images correspondantes aux filtres passés dans l'url du web service de recherche
|
|
|
7 |
* (ListeImages et ListeObservations).
|
1840 |
jpm |
8 |
*
|
1881 |
jpm |
9 |
* Attention, cela signifie que toutes les tables ne sont pas *forcément* jointées, par exemple si aucune
|
|
|
10 |
* contrainte ne le nécessite.
|
|
|
11 |
* La requête construite ici est utile pour récupérer la liste des ids d'observations/images qui match.
|
|
|
12 |
* Pour la récupération effective de "toutes" les données nécessaire au retour du web service en json, c'est une autre
|
|
|
13 |
* requête directement dans le web service qui s'en charge. Cette technique en deux étapes est la plus rapide !
|
|
|
14 |
*
|
|
|
15 |
* Note: toujours rajouter les préfixes de table (di, do ou du), en fonction de ce que défini
|
|
|
16 |
* les JOIN qui sont utilisés :
|
|
|
17 |
* - le préfix de del_image est "di"
|
|
|
18 |
* - le préfix de del_observation est "do"
|
|
|
19 |
* - le préfix de del_utilisateur est "du"
|
|
|
20 |
*
|
1840 |
jpm |
21 |
* @category DEL
|
|
|
22 |
* @package Services
|
|
|
23 |
* @package Bibliotheque
|
|
|
24 |
* @version 0.1
|
|
|
25 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
26 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
27 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
28 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
29 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
30 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
31 |
*/
|
|
|
32 |
class Sql {
|
|
|
33 |
|
1871 |
jpm |
34 |
const APPLI_IMG = 'IMG';
|
|
|
35 |
const APPLI_OBS = 'OBS';
|
|
|
36 |
|
1840 |
jpm |
37 |
private $conteneur;
|
|
|
38 |
private $bdd;
|
|
|
39 |
private $parametres = array();
|
1871 |
jpm |
40 |
private $appli;
|
1840 |
jpm |
41 |
private $requete = array(
|
|
|
42 |
'join' => array(),
|
|
|
43 |
'where' => array(),
|
|
|
44 |
'groupby' => array(),
|
|
|
45 |
'orderby' => array());
|
|
|
46 |
|
1871 |
jpm |
47 |
private $champsPrenom = array('du.prenom', 'prenom_utilisateur');
|
|
|
48 |
private $champsNom = array('du.nom', 'nom_utilisateur');
|
1840 |
jpm |
49 |
|
|
|
50 |
|
|
|
51 |
public function __construct(Conteneur $conteneur) {
|
|
|
52 |
$this->conteneur = $conteneur;
|
|
|
53 |
$this->bdd = $this->conteneur->getBdd();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function setParametres(Array $parametres) {
|
|
|
57 |
$this->parametres = $parametres;
|
|
|
58 |
}
|
|
|
59 |
|
1871 |
jpm |
60 |
public function setAppli($appliType) {
|
|
|
61 |
if ($appliType == 'IMG' || $appliType == 'OBS') {
|
|
|
62 |
$this->appli = $appliType;
|
|
|
63 |
} else {
|
|
|
64 |
throw new Exception("Les types d'appli disponible sont : IMG (pour PictoFlora) et OBS (pour IdentiPlante)");
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
private function getPrefixe() {
|
|
|
69 |
return $this->appli === 'IMG' ? 'di' : 'do';
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function etreAppliImg() {
|
|
|
73 |
return $this->appli === 'IMG' ? true : false;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
private function etreAppliObs() {
|
|
|
77 |
return $this->appli === 'OBS' ? true : false;
|
|
|
78 |
}
|
|
|
79 |
|
1840 |
jpm |
80 |
public function getRequeteSql() {
|
|
|
81 |
return $this->requete;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
private function addJoin($join) {
|
1871 |
jpm |
85 |
if (!isset($this->requete['join'][$join])) {
|
|
|
86 |
$this->requete['join'][] = $join;
|
|
|
87 |
}
|
1840 |
jpm |
88 |
}
|
|
|
89 |
|
|
|
90 |
public function getJoin() {
|
|
|
91 |
return ($this->requete['join'] ? implode(' ', array_unique($this->requete['join'])).' ' : '');
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private function addJoinDis($join) {
|
|
|
95 |
$this->requete['join']['dis'] = $join;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private function addWhere($idParam, $where) {
|
|
|
99 |
if (isset($this->parametres['_parametres_condition_or_'])
|
|
|
100 |
&& in_array($idParam, $this->parametres['_parametres_condition_or_'])) {
|
|
|
101 |
$this->requete['where']['OR'][] = $where;
|
|
|
102 |
} else {
|
|
|
103 |
$this->requete['where']['AND'][] = $where;
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
public function getWhere() {
|
|
|
107 |
if (isset($this->requete['where']['OR']) && count($this->requete['where']['OR']) > 0) {
|
|
|
108 |
$this->requete['where']['AND'][] = '('.implode(' OR ', $this->requete['where']['OR']).')';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$where = ' TRUE ';
|
|
|
112 |
if (isset($this->requete['where']['AND']) && count($this->requete['where']['AND']) > 0) {
|
|
|
113 |
$where = implode(' AND ', $this->requete['where']['AND']).' ';
|
|
|
114 |
}
|
|
|
115 |
return $where;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function addGroupBy($groupBy) {
|
|
|
119 |
$this->requete['groupby'][] = $groupBy;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
public function getGroupBy() {
|
|
|
123 |
$groupby = '';
|
|
|
124 |
if (isset($this->requete['groupby']) && count($this->requete['groupby']) > 0) {
|
|
|
125 |
$groupby = 'GROUP BY '.implode(', ', array_unique($this->requete['groupby'])).' ';
|
|
|
126 |
}
|
|
|
127 |
return $groupby;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
private function addOrderBy($orderby) {
|
|
|
131 |
$this->requete['orderby'][] = $orderby;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public function getOrderBy() {
|
|
|
135 |
$orderby = '';
|
|
|
136 |
if (isset($this->requete['orderby']) && count($this->requete['orderby']) > 0) {
|
|
|
137 |
$orderby = 'ORDER BY '.implode(', ', array_unique($this->requete['orderby'])).' ';
|
|
|
138 |
}
|
|
|
139 |
return $orderby;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public function getLimit() {
|
|
|
143 |
return 'LIMIT '.$this->parametres['navigation.depart'].','.$this->parametres['navigation.limite'].' ';
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
1881 |
jpm |
147 |
|
1840 |
jpm |
148 |
*
|
|
|
149 |
* @param $p les paramètres (notamment de masque) passés par l'URL et déjà traités/filtrés (sauf quotes)
|
|
|
150 |
* @param $req le tableau, passé par référence représentant les composants de la requête à bâtir
|
|
|
151 |
*/
|
|
|
152 |
public function ajouterContraintes() {
|
|
|
153 |
$this->ajouterContrainteAuteur();
|
|
|
154 |
$this->ajouterContrainteDate();
|
|
|
155 |
$this->ajouterContrainteDepartement();
|
|
|
156 |
$this->ajouterContrainteIdZoneGeo();
|
|
|
157 |
$this->ajouterContrainteGenre();
|
|
|
158 |
$this->ajouterContrainteFamille();
|
|
|
159 |
$this->ajouterContrainteNs();
|
|
|
160 |
$this->ajouterContrainteNn();
|
|
|
161 |
$this->ajouterContrainteReferentiel();
|
|
|
162 |
$this->ajouterContrainteCommune();
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
private function ajouterContrainteAuteur() {
|
|
|
166 |
if (isset($this->parametres['masque.auteur'])) {
|
|
|
167 |
$auteur = $this->parametres['masque.auteur'];
|
|
|
168 |
// id du poster de l'obs
|
1871 |
jpm |
169 |
$prefixe = $this->getPrefixe();
|
|
|
170 |
$this->addJoin("LEFT JOIN del_utilisateur AS du ON (du.id_utilisateur = $prefixe.ce_utilisateur) ");
|
1840 |
jpm |
171 |
|
|
|
172 |
if (is_numeric($auteur)) {
|
|
|
173 |
$this->ajouterContrainteAuteurId();
|
1871 |
jpm |
174 |
} elseif(preg_match('/@[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,}$/i', $auteur)) {
|
1840 |
jpm |
175 |
$this->ajouterContrainteAuteurEmail();
|
|
|
176 |
} else {
|
|
|
177 |
$this->ajouterContrainteAuteurIntitule();
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
private function ajouterContrainteAuteurId() {
|
|
|
183 |
$id = $this->parametres['masque.auteur'];
|
1871 |
jpm |
184 |
$prefixe = $this->getPrefixe();
|
|
|
185 |
$sqlTpl = "(du.id_utilisateur = %1\$d OR $prefixe.ce_utilisateur = %1\$d)";
|
1840 |
jpm |
186 |
$whereAuteur = sprintf($sqlTpl, $id);
|
|
|
187 |
$this->addWhere('masque.auteur', $whereAuteur);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
private function ajouterContrainteAuteurEmail() {
|
|
|
191 |
$email = $this->parametres['masque.auteur'];
|
1871 |
jpm |
192 |
$prefixe = $this->getPrefixe();
|
|
|
193 |
$sqlTpl = "(du.courriel LIKE %1\$s OR $prefixe.courriel_utilisateur LIKE %1\$s )";
|
1840 |
jpm |
194 |
$emailP = $this->bdd->proteger("$email%");
|
|
|
195 |
$whereAuteur = sprintf($sqlTpl, $emailP);
|
|
|
196 |
$this->addWhere('masque.auteur', $whereAuteur);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Retourne une clause where du style:
|
|
|
201 |
* CONCAT(IF(du.prenom IS NULL, "", du.prenom), [...] vdi.i_nomutilisateur) REGEXP 'xxx'
|
|
|
202 |
*/
|
|
|
203 |
private function ajouterContrainteAuteurIntitule() {
|
|
|
204 |
$auteurExplode = explode(' ', $this->parametres['masque.auteur']);
|
|
|
205 |
$nbreMots = count($auteurExplode);
|
|
|
206 |
|
|
|
207 |
if ($nbreMots == 1) {
|
|
|
208 |
$this->ajouterContrainteAuteurPrenomOuNom();
|
|
|
209 |
} else if ($nbreMots == 2) {
|
|
|
210 |
$this->ajouterContrainteAuteurPrenomEtNom();
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
private function ajouterContrainteAuteurPrenomOuNom() {
|
|
|
215 |
$prenomOuNom = $this->parametres['masque.auteur'];
|
|
|
216 |
|
|
|
217 |
$sqlTpl = 'CONCAT(%s,%s) LIKE %s';
|
1871 |
jpm |
218 |
$prefixe = $this->getPrefixe();
|
|
|
219 |
$champsPrenomSql = self::ajouterIfNullPourConcat($this->champsPrenom, $prefixe);
|
|
|
220 |
$champsNomSql = self::ajouterIfNullPourConcat($this->champsNom, $prefixe);
|
1840 |
jpm |
221 |
$auteurMotif = $this->bdd->proteger("%$prenomOuNom%");
|
|
|
222 |
|
|
|
223 |
$auteurWhere = sprintf($sqlTpl, $champsPrenomSql, $champsNomSql, $auteurMotif);
|
|
|
224 |
$this->addWhere('masque.auteur', $auteurWhere);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
private function ajouterContrainteAuteurPrenomEtNom() {
|
|
|
228 |
list($prenom, $nom) = explode(' ', $this->parametres['masque.auteur']);
|
|
|
229 |
|
|
|
230 |
$sqlTpl = '(CONCAT(%1$s,%2$s) LIKE %3$s AND CONCAT(%1$s,%2$s) LIKE %4$s)';
|
1871 |
jpm |
231 |
$prefixe = $this->getPrefixe();
|
|
|
232 |
$champsPrenomSql = self::ajouterIfNullPourConcat($this->champsPrenom, $prefixe);
|
|
|
233 |
$champsNomSql = self::ajouterIfNullPourConcat($this->champsNom, $prefixe);
|
1840 |
jpm |
234 |
$prenomMotif = $this->bdd->proteger("%$prenom%");
|
|
|
235 |
$nomMotif = $this->bdd->proteger("%$nom%");
|
|
|
236 |
|
|
|
237 |
$auteurWhere = sprintf($sqlTpl, $champsPrenomSql, $champsNomSql, $prenomMotif, $nomMotif);
|
|
|
238 |
$this->addWhere('masque.auteur', $auteurWhere);
|
|
|
239 |
}
|
|
|
240 |
|
1881 |
jpm |
241 |
/**
|
|
|
242 |
* Lorsque l'on concatène des champs, un seul NULL prend le dessus.
|
1840 |
jpm |
243 |
* Il faut donc utiliser la syntaxe IFNULL(%s, "").
|
1881 |
jpm |
244 |
* Cette fonction effectue aussi l'implode() "final".
|
1840 |
jpm |
245 |
*/
|
1871 |
jpm |
246 |
private static function ajouterIfNullPourConcat($champs, $prefixe) {
|
1840 |
jpm |
247 |
$champsProteges = array();
|
|
|
248 |
foreach ($champs as $champ) {
|
1871 |
jpm |
249 |
if (strstr($champ, '.') === false) {
|
|
|
250 |
$champ = "$prefixe.$champ";
|
|
|
251 |
}
|
1840 |
jpm |
252 |
$champsProteges[] = "IFNULL($champ, '')";
|
|
|
253 |
}
|
|
|
254 |
return implode(',', $champsProteges);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
private function ajouterContrainteDate() {
|
|
|
258 |
if (isset($this->parametres['masque.date'])) {
|
|
|
259 |
$date = $this->parametres['masque.date'];
|
1871 |
jpm |
260 |
if (preg_match('/^\d{4}$/', $date) && $date < 2030 && $date > 1600) {
|
|
|
261 |
$sqlTpl = "YEAR(do.date_observation) = %d";
|
1840 |
jpm |
262 |
$dateWhere = sprintf($sqlTpl, $date);
|
|
|
263 |
$this->addWhere('masque.date', $dateWhere);
|
|
|
264 |
} else {
|
1871 |
jpm |
265 |
$sqlTpl = "do.date_observation = %s";
|
|
|
266 |
$dateP = $this->bdd->proteger($date);
|
1840 |
jpm |
267 |
$dateWhere = sprintf($sqlTpl, $dateP);
|
|
|
268 |
$this->addWhere('masque.date', $dateWhere);
|
|
|
269 |
}
|
1871 |
jpm |
270 |
|
|
|
271 |
if ($this->etreAppliImg()) {
|
|
|
272 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
273 |
}
|
1840 |
jpm |
274 |
}
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
private function ajouterContrainteDepartement() {
|
|
|
278 |
if (isset($this->parametres['masque.departement'])) {
|
|
|
279 |
$dept = $this->parametres['masque.departement'];
|
|
|
280 |
$deptMotif = $this->bdd->proteger("INSEE-C:$dept");
|
1871 |
jpm |
281 |
$this->addWhere('masque.departement', "do.ce_zone_geo LIKE $deptMotif");
|
|
|
282 |
|
|
|
283 |
if ($this->etreAppliImg()) {
|
|
|
284 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
285 |
}
|
1840 |
jpm |
286 |
}
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
private function ajouterContrainteIdZoneGeo() {
|
|
|
290 |
if (isset($this->parametres['masque.id_zone_geo'])) {
|
|
|
291 |
$idZgMotif = $this->bdd->proteger($this->parametres['masque.id_zone_geo']);
|
1871 |
jpm |
292 |
$this->addWhere('masque.id_zone_geo', "do.ce_zone_geo = $idZgMotif");
|
|
|
293 |
|
|
|
294 |
if ($this->etreAppliImg()) {
|
|
|
295 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
296 |
}
|
1840 |
jpm |
297 |
}
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
private function ajouterContrainteGenre() {
|
|
|
301 |
if (isset($this->parametres['masque.genre'])) {
|
|
|
302 |
$genre = $this->parametres['masque.genre'];
|
|
|
303 |
$genreMotif = $this->bdd->proteger("%$genre% %");
|
1871 |
jpm |
304 |
$this->addWhere('masque.genre', "do.nom_sel LIKE $genreMotif");
|
|
|
305 |
|
|
|
306 |
if ($this->etreAppliImg()) {
|
|
|
307 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
308 |
}
|
1840 |
jpm |
309 |
}
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
private function ajouterContrainteFamille() {
|
|
|
313 |
if (isset($this->parametres['masque.famille'])) {
|
|
|
314 |
$familleMotif = $this->bdd->proteger($this->parametres['masque.famille']);
|
1871 |
jpm |
315 |
$this->addWhere('masque.famille', "do.famille = $familleMotif");
|
|
|
316 |
|
|
|
317 |
if ($this->etreAppliImg()) {
|
|
|
318 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
319 |
}
|
1840 |
jpm |
320 |
}
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
private function ajouterContrainteNs() {
|
|
|
324 |
if (isset($this->parametres['masque.ns'])) {
|
|
|
325 |
$ns = $this->parametres['masque.ns'];
|
|
|
326 |
$nsMotif = $this->bdd->proteger("$ns%");
|
1871 |
jpm |
327 |
$this->addWhere('masque.ns', "do.nom_sel LIKE $nsMotif");
|
|
|
328 |
|
|
|
329 |
if ($this->etreAppliImg()) {
|
|
|
330 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
331 |
}
|
1840 |
jpm |
332 |
}
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
private function ajouterContrainteNn() {
|
|
|
336 |
if (isset($this->parametres['masque.nn'])) {
|
1871 |
jpm |
337 |
$sqlTpl = '(do.nom_sel_nn = %1$d OR do.nom_ret_nn = %1$d)';
|
1840 |
jpm |
338 |
$nnWhere = sprintf($sqlTpl, $this->parametres['masque.nn']);
|
|
|
339 |
$this->addWhere('masque.nn', $nnWhere);
|
1871 |
jpm |
340 |
|
|
|
341 |
if ($this->etreAppliImg()) {
|
|
|
342 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
343 |
}
|
1840 |
jpm |
344 |
}
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
private function ajouterContrainteReferentiel() {
|
|
|
348 |
if (isset($this->parametres['masque.referentiel'])) {
|
|
|
349 |
$ref = $this->parametres['masque.referentiel'];
|
|
|
350 |
$refMotif = $this->bdd->proteger("$ref%");
|
1871 |
jpm |
351 |
$this->addWhere('masque.referentiel', "do.nom_referentiel LIKE $refMotif");
|
|
|
352 |
|
|
|
353 |
if ($this->etreAppliImg()) {
|
|
|
354 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
355 |
}
|
1840 |
jpm |
356 |
}
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
private function ajouterContrainteCommune() {
|
|
|
360 |
if (isset($this->parametres['masque.commune'])) {
|
|
|
361 |
$commune = $this->parametres['masque.commune'];
|
|
|
362 |
$communeMotif = $this->bdd->proteger("$commune%");
|
1871 |
jpm |
363 |
$this->addWhere('masque.commune', "do.zone_geo LIKE $communeMotif");
|
|
|
364 |
|
|
|
365 |
if ($this->etreAppliImg()) {
|
|
|
366 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
367 |
}
|
1840 |
jpm |
368 |
}
|
|
|
369 |
}
|
|
|
370 |
|
1845 |
jpm |
371 |
public function ajouterConstrainteAppliObs() {
|
|
|
372 |
$this->ajouterContrainteTagCel();
|
|
|
373 |
$this->ajouterContrainteType();
|
|
|
374 |
// TODO : ATTENTION -> vue que l'on utilise une vue basée sur les images, nous devons grouper par obs
|
1871 |
jpm |
375 |
$this->addGroupBy('do.id_observation');
|
1845 |
jpm |
376 |
}
|
|
|
377 |
|
|
|
378 |
private function ajouterContrainteType() {
|
1871 |
jpm |
379 |
if (isset($this->parametres['masque.type'])) {
|
|
|
380 |
if (array_key_exists('adeterminer', $this->parametres['masque.type'])) {
|
|
|
381 |
// Récupèration de toutes les observations qui on le tag "aDeterminer" *ou* qui n'ont pas de nom d'espèce
|
|
|
382 |
// *ou* qui ont la "certitude" à ("aDeterminer" *ou* "douteux")
|
|
|
383 |
$this->addWhere('masque.type', '('.
|
|
|
384 |
'do.certitude = "aDeterminer" '.
|
|
|
385 |
'OR do.certitude = "douteux" '.
|
|
|
386 |
'OR do.mots_cles_texte LIKE "%aDeterminer%" '.
|
|
|
387 |
'OR do.nom_sel_nn IS NULL '.
|
|
|
388 |
'OR do.nom_sel_nn = 0 '.// il ne DEVRAIT pas y avoir d'entrées à 0, mais il y en a quand-même !!
|
|
|
389 |
')');
|
|
|
390 |
}
|
|
|
391 |
if (array_key_exists('validees', $this->parametres['masque.type'])) {
|
|
|
392 |
// Récupèration de toutes les observations ayant un commentaire doté de proposition_retenue = 1
|
|
|
393 |
$this->addJoin('INNER JOIN del_commentaire AS dc '.
|
|
|
394 |
'ON (do.id_observation = dc.ce_observation AND dc.proposition_retenue = 1) ');
|
|
|
395 |
}
|
1845 |
jpm |
396 |
|
1871 |
jpm |
397 |
if (array_key_exists('endiscussion', $this->parametres['masque.type'])) {
|
|
|
398 |
$nbreCommentaire =(int) ($this->conteneur->getParametre('observations.nb_commentaires_discussion'));
|
|
|
399 |
$this->addWhere('masque.type', '(SELECT COUNT(id_commentaire) FROM del_commentaire AS dc '.
|
|
|
400 |
"WHERE ce_observation = id_observation) > $nbreCommentaire ");
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
if ($this->etreAppliImg()) {
|
|
|
404 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
405 |
}
|
1845 |
jpm |
406 |
}
|
|
|
407 |
}
|
|
|
408 |
|
1840 |
jpm |
409 |
public function ajouterConstrainteAppliImg() {
|
|
|
410 |
$this->ajouterContrainteMilieu();
|
|
|
411 |
$this->ajouterContrainteTri();
|
|
|
412 |
$this->ajouterContrainteTagCel();
|
|
|
413 |
$this->ajouterContrainteTagDel();
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
private function ajouterContrainteMilieu() {
|
|
|
417 |
if (isset($this->parametres['masque.milieu'])) {
|
|
|
418 |
$milieu = $this->parametres['masque.milieu'];
|
|
|
419 |
$milieuMotif = $this->bdd->proteger("%$milieu%");
|
1871 |
jpm |
420 |
$this->addWhere('masque.milieu', "do.milieu LIKE $milieuMotif");
|
|
|
421 |
|
|
|
422 |
if ($this->etreAppliImg()) {
|
|
|
423 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
424 |
}
|
1840 |
jpm |
425 |
}
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
private function ajouterContrainteTri() {
|
|
|
429 |
if (isset($this->parametres['tri'])) {
|
|
|
430 |
$tri = $this->parametres['tri'];
|
|
|
431 |
|
1863 |
jpm |
432 |
if (isset($this->parametres['protocole']) && ($tri == 'moyenne-arithmetique' || $tri == 'points')) {
|
1840 |
jpm |
433 |
// $this->parametres['protocole'] *est* défini (cf Outils::filtrerUrlsParams...())
|
1871 |
jpm |
434 |
$sqlTpl = 'LEFT JOIN del_image_stat AS dis ON di.id_image = dis.ce_image AND dis.ce_protocole = %d';
|
1840 |
jpm |
435 |
$triSql = sprintf($sqlTpl, $this->parametres['protocole']);
|
|
|
436 |
$this->addJoinDis($triSql);
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
if (isset($this->parametres['ordre']) && $tri == 'tags') {
|
|
|
440 |
$typeJointure = ($this->parametres['ordre'] == 'desc') ? 'INNER' : 'LEFT';
|
1871 |
jpm |
441 |
$this->addJoin("$typeJointure JOIN del_image_stat AS dis ON di.id_image = dis.ce_image");
|
1840 |
jpm |
442 |
}
|
|
|
443 |
}
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
private function ajouterContrainteTagCel() {
|
1845 |
jpm |
447 |
if (isset($this->parametres['masque.tag_cel'])) {
|
1840 |
jpm |
448 |
if (isset($this->parametres['masque.tag_cel']['AND'])) {
|
|
|
449 |
$tags = $this->parametres['masque.tag_cel']['AND'];
|
|
|
450 |
$clausesWhere = array();
|
|
|
451 |
foreach ($tags as $tag) {
|
|
|
452 |
$tagMotif = $this->bdd->proteger("%$tag%");
|
1913 |
jpm |
453 |
if ($this->etreAppliImg()) {
|
|
|
454 |
$sql = " (do.mots_cles_texte LIKE $tagMotif OR di.mots_cles_texte LIKE $tagMotif) ";
|
|
|
455 |
} else {
|
|
|
456 |
// WARNING : la sous-requête est la meilleure solution trouvée pour contrer le fonctionnement
|
|
|
457 |
// étrange de l'optimiseur de MYSQL 5.6 (à retester avec Mysql 5.7 et suivant).
|
|
|
458 |
$sousRequete = 'SELECT DISTINCT ce_observation '.
|
|
|
459 |
'FROM del_image '.
|
|
|
460 |
"WHERE mots_cles_texte LIKE $tagMotif ".
|
|
|
461 |
'AND ce_observation IS NOT NULL';
|
|
|
462 |
$sql = " (do.mots_cles_texte LIKE $tagMotif OR do.id_observation IN ($sousRequete)) ";
|
|
|
463 |
}
|
1912 |
jpm |
464 |
$clausesWhere[] = $sql;
|
1840 |
jpm |
465 |
}
|
|
|
466 |
$whereTags = implode(' AND ', $clausesWhere);
|
|
|
467 |
$this->addWhere('masque.tag_cel', "($whereTags)");
|
|
|
468 |
} else if (isset($this->parametres['masque.tag_cel']['OR'])) {
|
|
|
469 |
$tags = $this->parametres['masque.tag_cel']['OR'];
|
1871 |
jpm |
470 |
$sqlTpl = "CONCAT(IFNULL(do.mots_cles_texte,''),IFNULL(di.mots_cles_texte,'')) REGEXP %s";
|
1840 |
jpm |
471 |
$tagMotif = $this->bdd->proteger(implode('|', $tags));
|
|
|
472 |
$tagSql = sprintf($sqlTpl, $tagMotif);
|
|
|
473 |
$this->addWhere('masque.tag_cel', $tagSql);
|
|
|
474 |
}
|
1871 |
jpm |
475 |
if ($this->etreAppliImg()) {
|
|
|
476 |
$this->addJoin('LEFT JOIN del_observation AS do ON (di.ce_observation = do.id_observation) ');
|
|
|
477 |
}
|
1840 |
jpm |
478 |
}
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
/**
|
1881 |
jpm |
482 |
* Plusieurs solutions sont disponibles dans les anciennes versions (voir DelTk et l'historique SVN de ce fichier).
|
1840 |
jpm |
483 |
*/
|
|
|
484 |
private function ajouterContrainteTagDel() {
|
|
|
485 |
if (isset($this->parametres['masque.tag_del'])) {
|
1845 |
jpm |
486 |
if (isset($this->parametres['masque.tag_del']['AND'])) {
|
|
|
487 |
$tags = $this->parametres['masque.tag_del']['AND'];
|
|
|
488 |
// optimisation: en cas de "AND" on sort() l'input et le GROUP_CONCAT()
|
|
|
489 |
// donc nous utilisons des ".*" plutôt que de multiples conditions et "|"
|
|
|
490 |
sort($tags);
|
|
|
491 |
$tagsMotif = $this->bdd->proteger(implode('.*', $tags));
|
|
|
492 |
$requete = 'SELECT ce_image '.
|
|
|
493 |
'FROM del_image_tag '.
|
|
|
494 |
'WHERE actif = 1 '.
|
|
|
495 |
'GROUP BY ce_image '.
|
|
|
496 |
"HAVING GROUP_CONCAT(tag_normalise ORDER BY tag_normalise) REGEXP $tagsMotif ".
|
|
|
497 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
498 |
$sql = $this->recupererSqlContrainteTag($requete);
|
|
|
499 |
$this->addWhere('masque.tag_del', $sql);
|
1840 |
jpm |
500 |
|
1845 |
jpm |
501 |
} else if (isset($this->parametres['masque.tag_del']['OR'])) {
|
|
|
502 |
$tags = $this->parametres['masque.tag_del']['OR'];
|
|
|
503 |
$tagsMotif = $this->bdd->proteger(implode('|', $tags));
|
|
|
504 |
$requete = 'SELECT ce_image '.
|
|
|
505 |
'FROM del_image_tag '.
|
|
|
506 |
'WHERE actif = 1 '.
|
|
|
507 |
'GROUP BY ce_image '.
|
|
|
508 |
"HAVING GROUP_CONCAT(tag_normalise) REGEXP $tagsMotif ".
|
|
|
509 |
' -- '.__FILE__.' : '.__LINE__;
|
1871 |
jpm |
510 |
|
1845 |
jpm |
511 |
$sql = $this->recupererSqlContrainteTag($requete);
|
|
|
512 |
$this->addWhere('masque.tag_del', $sql);
|
1840 |
jpm |
513 |
}
|
|
|
514 |
}
|
|
|
515 |
}
|
|
|
516 |
|
|
|
517 |
private function recupererSqlContrainteTag($requete) {
|
|
|
518 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
519 |
$ids = array();
|
|
|
520 |
foreach ($resultats as $resultat) {
|
|
|
521 |
$ids[] = $resultat['ce_image'];
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
if (!empty($ids)) {
|
|
|
525 |
$clauseIn = implode(',', $ids);
|
|
|
526 |
} else {
|
|
|
527 |
$clauseIn = 'SELECT ce_image FROM del_image_tag WHERE false';
|
|
|
528 |
}
|
1871 |
jpm |
529 |
return "di.id_image IN ($clauseIn)";
|
1840 |
jpm |
530 |
}
|
|
|
531 |
|
|
|
532 |
/**
|
|
|
533 |
* Partie spécifique à PictoFlora:
|
1863 |
jpm |
534 |
* Attention : si le critère de tri n'est pas suffisant, les résultats affichés peuvent varier à chaque appel
|
|
|
535 |
* de la même page de résultat de PictoFlora.
|
1840 |
jpm |
536 |
*/
|
|
|
537 |
public function definirOrdreSqlAppliImg() {
|
|
|
538 |
$ordre = $this->parametres['ordre'];
|
|
|
539 |
|
|
|
540 |
switch ($this->parametres['tri']) {
|
1863 |
jpm |
541 |
case 'moyenne-arithmetique' :
|
1888 |
jpm |
542 |
$this->addOrderBy("dis.moyenne $ordre, dis.nb_votes $ordre, id_image $ordre");
|
1840 |
jpm |
543 |
break;
|
|
|
544 |
case 'points' :
|
1888 |
jpm |
545 |
$this->addOrderBy("dis.nb_points $ordre, dis.moyenne $ordre, dis.nb_votes $ordre, id_image $ordre");
|
1840 |
jpm |
546 |
break;
|
|
|
547 |
case 'tags' :
|
1888 |
jpm |
548 |
$this->addOrderBy("dis.nb_tags $ordre, id_image $ordre");
|
1840 |
jpm |
549 |
break;
|
|
|
550 |
case 'date_observation' :
|
1871 |
jpm |
551 |
$this->addOrderBy("date_observation $ordre, ce_observation $ordre");
|
1840 |
jpm |
552 |
break;
|
1863 |
jpm |
553 |
case 'date_transmission' :
|
1840 |
jpm |
554 |
default:
|
1871 |
jpm |
555 |
$this->addOrderBy("di.date_transmission $ordre, ce_observation $ordre");
|
1840 |
jpm |
556 |
}
|
|
|
557 |
}
|
|
|
558 |
|
1845 |
jpm |
559 |
public function definirOrdreSqlAppliObs() {
|
|
|
560 |
$ordre = $this->parametres['ordre'];
|
|
|
561 |
|
|
|
562 |
// parmi self::$tri_possible
|
|
|
563 |
switch ($this->parametres['tri']) {
|
|
|
564 |
case 'date_observation' :
|
|
|
565 |
$this->addOrderBy("date_observation $ordre, id_observation $ordre");
|
|
|
566 |
break;
|
|
|
567 |
default:
|
1871 |
jpm |
568 |
$this->addOrderBy("do.date_transmission $ordre, id_observation $ordre");
|
1845 |
jpm |
569 |
}
|
|
|
570 |
}
|
|
|
571 |
|
1840 |
jpm |
572 |
public function getAliasDesChamps($champsEtAlias, $select = null, $prefix = null) {
|
|
|
573 |
$arr = ($select) ? array_intersect_key($champsEtAlias, array_flip($select)) : $champsEtAlias;
|
|
|
574 |
$keys = array_keys($arr);
|
|
|
575 |
|
|
|
576 |
if ($prefix) {
|
|
|
577 |
array_walk($keys, create_function('&$val, $k, $prefix', '$val = sprintf("%s.`%s`", $prefix, $val);'), $prefix);
|
|
|
578 |
} else {
|
|
|
579 |
array_walk($keys, create_function('&$val, $k', '$val = sprintf("`%s`", $val);'));
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
return implode(', ', array_map(create_function('$v, $k', 'return sprintf("%s AS `%s`", $k, $v);'), $arr, $keys));
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
public function getVotesDesImages($idsImages, $protocole = null) {
|
|
|
586 |
if (!$idsImages) return;
|
|
|
587 |
|
|
|
588 |
$mappingVotes = $this->conteneur->getParametreTableau('votes.mapping');
|
|
|
589 |
$mappingProtocoles = $this->conteneur->getParametreTableau('protocoles.mapping');
|
|
|
590 |
$selectVotes = array('id_vote', 'ce_image', 'ce_protocole', 'ce_utilisateur', 'valeur', 'date');
|
|
|
591 |
$selectProtocole = array('id_protocole', 'intitule', 'descriptif', 'tag');
|
|
|
592 |
$voteChamps = $this->getAliasDesChamps($mappingVotes, $selectVotes, 'v'); // "v": cf alias dans la requête
|
|
|
593 |
$protoChamps = $this->getAliasDesChamps($mappingProtocoles, $selectProtocole, 'p');
|
|
|
594 |
$idImgsConcat = implode(',', $idsImages);
|
|
|
595 |
|
|
|
596 |
$requete = "SELECT $voteChamps, $protoChamps ".
|
|
|
597 |
'FROM del_image_vote AS v '.
|
|
|
598 |
' INNER JOIN del_image_protocole AS p ON (v.ce_protocole = p.id_protocole) '.
|
|
|
599 |
"WHERE v.ce_image IN ($idImgsConcat) ".
|
|
|
600 |
($protocole ? " AND v.ce_protocole = $protocole " : '').
|
|
|
601 |
"ORDER BY FIELD(v.ce_image, $idImgsConcat) ".
|
|
|
602 |
'-- '.__FILE__.':'.__LINE__;
|
|
|
603 |
return $this->bdd->recupererTous($requete);
|
|
|
604 |
}
|
|
|
605 |
|
|
|
606 |
/**
|
|
|
607 |
* Ajoute les informations sur le protocole et les votes aux images.
|
|
|
608 |
*
|
|
|
609 |
* ATTENTION : Subtilité, nous passons ici le tableau d'images indexé par id_image qui est bien
|
|
|
610 |
* plus pratique pour associer les vote à un tableau, puisque nous ne connaissons pas les id d'observation.
|
|
|
611 |
* Mais magiquement (par référence), cela va remplir notre tableau indexé par couple d'id (id_image, id_observation)
|
|
|
612 |
* cf ListeImages::reformateImagesDoubleIndex() à qui revient la tâche de créer ces deux versions
|
|
|
613 |
* simultanément lorsque c'est encore possible.
|
|
|
614 |
*/
|
1881 |
jpm |
615 |
// TODO : supprimer cette "subtilité" source d'erreurs
|
1840 |
jpm |
616 |
public function ajouterInfosVotesProtocoles($votes, &$images) {
|
|
|
617 |
if (!$votes) return;
|
1845 |
jpm |
618 |
|
1840 |
jpm |
619 |
$mappingVotes = $this->conteneur->getParametreTableau('votes.mapping');
|
|
|
620 |
$mappingProtocoles = $this->conteneur->getParametreTableau('protocoles.mapping');
|
|
|
621 |
|
|
|
622 |
// pour chaque vote
|
|
|
623 |
foreach ($votes as $vote) {
|
|
|
624 |
$imgId = $vote['image.id'];
|
|
|
625 |
$protoId = $vote['protocole.id'];
|
|
|
626 |
|
|
|
627 |
if (!array_key_exists('protocoles_votes', $images[$imgId]) ||
|
1863 |
jpm |
628 |
!array_key_exists($protoId, $images[$imgId]['protocoles_votes'])) {
|
1840 |
jpm |
629 |
// extrait les champs spécifique au protocole (le LEFT JOIN de chargerVotesImage les ramène en doublons
|
|
|
630 |
$protocole = array_intersect_key($vote, array_flip($mappingProtocoles));
|
|
|
631 |
$images[$imgId]['protocoles_votes'][$protoId] = $protocole;
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
$chpsVotes = array('id_vote', 'ce_image', 'ce_utilisateur', 'valeur', 'date');
|
|
|
635 |
$voteSelection = array_intersect_key($mappingVotes, array_flip($chpsVotes));
|
|
|
636 |
$vote = array_intersect_key($vote, array_flip($voteSelection));
|
|
|
637 |
$images[$imgId]['protocoles_votes'][$protoId]['votes'][$vote['vote.id']] = $vote;
|
|
|
638 |
}
|
|
|
639 |
}
|
1845 |
jpm |
640 |
|
|
|
641 |
public function getTotalLignesTrouvees() {
|
1888 |
jpm |
642 |
$resultat = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre -- '.__FILE__.':'.__LINE__);
|
1845 |
jpm |
643 |
return intval($resultat['nbre']);
|
|
|
644 |
}
|
1840 |
jpm |
645 |
}
|