Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
0337d704 | 22 | // {{{ Global variables used for the search Queries |
23 | ||
35fa92e8 | 24 | @$globals->search->result_fields = ' |
94f6c381 | 25 | u.user_id, u.promo, u.matricule, u.matricule_ax, |
26 | if(u.nom_usage=\'\', u.nom, u.nom_usage) AS NomSortKey, | |
27 | u.nom_usage,u.date, | |
28 | u.deces!=0 AS dcd,u.deces, | |
29 | u.perms IN (\'admin\',\'user\', \'disabled\') AS inscrit, | |
30 | u.perms != \'pending\' AS wasinscrit, | |
31 | FIND_IN_SET(\'femme\', u.flags) AS sexe, | |
32 | a.alias AS forlife, | |
33 | ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type, | |
34 | ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type, | |
35 | es.label AS secteur, ef.fonction_fr AS fonction, | |
36 | IF(n.nat=\'\',n.pays,n.nat) AS nat, n.a2 AS iso3166, | |
0e5ec860 | 37 | (COUNT(em.email) > 0 OR FIND_IN_SET("googleapps", u.mail_storage) > 0) AS actif,'; |
0337d704 | 38 | // hide private information if not logged |
eaf30d86 | 39 | if (S::logged()) |
94f6c381 | 40 | $globals->search->result_fields .=' |
41 | q.profile_web AS web, | |
42 | q.profile_mobile AS mobile, | |
43 | q.profile_freetext AS freetext, | |
44 | adr.city, gp.pays AS countrytxt, gr.name AS region, | |
45 | e.entreprise,'; | |
0337d704 | 46 | else |
94f6c381 | 47 | $globals->search->result_fields .=" |
48 | IF(q.profile_web_pub='public', q.profile_web, '') AS web, | |
49 | IF(q.profile_mobile_pub='public', q.profile_mobile, '') AS mobile, | |
50 | IF(q.profile_freetext_pub='public', q.profile_freetext, '') AS freetext, | |
51 | IF(adr.pub='public', adr.city, '') AS city, | |
52 | IF(adr.pub='public', gp.pays, '') AS countrytxt, | |
53 | IF(adr.pub='public', gr.name, '') AS region, | |
54 | IF(e.pub='public', e.entreprise, '') AS entreprise,"; | |
35fa92e8 | 55 | @$globals->search->result_where_statement = ' |
94f6c381 | 56 | LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0) |
57 | LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid) | |
58 | LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1) | |
59 | LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid) | |
60 | LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id) | |
61 | LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id) | |
62 | LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id) | |
63 | LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2) | |
64 | LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut)) | |
65 | LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2) | |
66 | LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region) | |
67 | LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = \'active\')'; | |
0337d704 | 68 | |
69 | // }}} | |
0337d704 | 70 | // {{{ class ThrowError |
71 | ||
72 | /** handle errors for end-users queries | |
73 | * assign the error message and runs the templates | |
74 | * | |
75 | * @author Jean-Sebastien Bedo | |
76 | */ | |
77 | class ThrowError | |
78 | { | |
a2aa8436 | 79 | public static $throwHook = array('ThrowError', 'defaultHandler'); |
80 | ||
0337d704 | 81 | /** constuctor |
82 | * @param $explain string the error (in natural language) | |
83 | */ | |
92432704 | 84 | public function __construct($explain) |
0337d704 | 85 | { |
a2aa8436 | 86 | call_user_func(ThrowError::$throwHook, $explain); |
87 | } | |
88 | ||
89 | /** defaut error handler | |
90 | */ | |
91 | private static function defaultHandler($explain) | |
92 | { | |
d7610c35 FB |
93 | global $globals; |
94 | $page =& Platal::page(); | |
bc67c37c | 95 | $page->changeTpl('search/index.tpl'); |
143ba7c9 | 96 | $page->setTitle('Polytechnique.org - Annuaire'); |
bc67c37c | 97 | $page->assign('baseurl', $globals->baseurl); |
a7d35093 | 98 | $page->trigError($explain); |
c9110c6c | 99 | $page->run(); |
0337d704 | 100 | } |
101 | } | |
102 | ||
103 | // }}} | |
104 | // {{{ class SField [Base class] | |
105 | ||
a7de4ef7 | 106 | /** classe de base représentant un champ de recherche |
107 | * (correspond à un champ du formulaire mais peut être à plusieurs champs de la bdd) | |
108 | * interface étendue pour chaque type de champ particulier | |
0337d704 | 109 | */ |
110 | class SField | |
111 | { | |
112 | // {{{ properties | |
94f6c381 | 113 | |
0337d704 | 114 | /** le nom du champ dans le formulaire HTML */ |
115 | var $fieldFormName; | |
a7de4ef7 | 116 | /** champs de la bdd correspondant à ce champ sous forme d'un tableau */ |
0337d704 | 117 | var $fieldDbName; |
a7de4ef7 | 118 | /** champ résultat dans la requête MySQL correspondant à ce champ |
119 | * (alias utilisé pour la clause ORDER BY) */ | |
0337d704 | 120 | var $fieldResultName; |
a7de4ef7 | 121 | /** valeur du champ instanciée par l'utilisateur */ |
0337d704 | 122 | var $value; |
123 | ||
124 | // }}} | |
125 | // {{{ constructor | |
126 | ||
127 | /** constructeur | |
a7de4ef7 | 128 | * (récupère la requête de l'utilisateur pour ce champ) */ |
0337d704 | 129 | function SField($_fieldFormName, $_fieldDbName='', $_fieldResultName='') |
130 | { | |
131 | $this->fieldFormName = $_fieldFormName; | |
132 | $this->fieldDbName = $_fieldDbName; | |
133 | $this->fieldResultName = $_fieldResultName; | |
134 | $this->get_request(); | |
135 | } | |
136 | ||
137 | // }}} | |
138 | // {{{ function get_request() | |
139 | ||
eaf30d86 | 140 | /** récupérer la requête de l'utilisateur |
a7de4ef7 | 141 | * on met une chaîne vide si le champ n'a pas été complété */ |
0337d704 | 142 | function get_request() |
143 | { | |
5e2307dc | 144 | $this->value = trim(Env::v($this->fieldFormName)); |
0337d704 | 145 | } |
146 | ||
147 | // }}} | |
148 | // {{{ function get_where_statement() | |
94f6c381 | 149 | |
a7de4ef7 | 150 | /** récupérer la clause correspondant au champ dans la clause WHERE de la requête |
eaf30d86 | 151 | * on parcourt l'ensemble des champs de la bdd de $fieldDbName et on associe |
a7de4ef7 | 152 | * à chacun d'entre eux une clause spécifique |
153 | * la clause totale et la disjonction de ces clauses spécifiques */ | |
0337d704 | 154 | function get_where_statement() |
155 | { | |
156 | if ($this->value=='') { | |
157 | return false; | |
158 | } | |
159 | $res = implode(' OR ', array_filter(array_map(array($this, 'get_single_where_statement'), $this->fieldDbName))); | |
160 | return empty($res) ? '' : "($res)"; | |
161 | } | |
162 | ||
163 | // }}} | |
164 | // {{{ function get_order_statement() | |
94f6c381 | 165 | |
a7de4ef7 | 166 | /** récupérer la clause correspondant au champ dans la clause ORDER BY de la requête |
167 | * utilisé par exemple pour placer d'abord le nom égal à la requête avant les approximations */ | |
0337d704 | 168 | function get_order_statement() |
169 | { | |
170 | return false; | |
171 | } | |
172 | ||
173 | // }}} | |
174 | // {{{ function get_select_statement() | |
175 | ||
176 | function get_select_statement() | |
177 | { | |
178 | return false; | |
179 | } | |
180 | ||
181 | // }}} | |
182 | // {{{ function get_url() | |
183 | ||
a7de4ef7 | 184 | /** récupérer le bout d'URL correspondant aux paramètres permettant d'imiter une requête d'un |
185 | * utilisateur assignant la valeur $this->value à ce champ */ | |
0337d704 | 186 | function get_url() |
187 | { | |
188 | if (empty($this->value)) { | |
189 | return false; | |
190 | } else { | |
191 | return $this->fieldFormName.'='.urlencode($this->value); | |
192 | } | |
193 | } | |
194 | ||
195 | // }}} | |
196 | } | |
197 | ||
198 | // }}} | |
199 | // {{{ class QuickSearch [Google Like] | |
200 | ||
201 | class QuickSearch extends SField | |
202 | { | |
203 | // {{{ properties | |
94f6c381 | 204 | |
0337d704 | 205 | /** stores tokens */ |
206 | var $strings; | |
207 | /** stores numerical ranges */ | |
208 | var $ranges; | |
bbf610b8 | 209 | /** stores admin searches */ |
210 | var $email; | |
211 | var $ip; | |
0337d704 | 212 | |
213 | // }}} | |
214 | // {{{ constructor | |
94f6c381 | 215 | |
0337d704 | 216 | function QuickSearch($_fieldFormName) |
217 | { | |
218 | $this->fieldFormName = $_fieldFormName; | |
219 | $this->get_request(); | |
a14159bf | 220 | if (preg_match(":[\]\[{}~/§_`|%$^=+]|\*\*:u", $this->value)) { |
a7de4ef7 | 221 | new ThrowError('Un champ contient un caractère interdit rendant la recherche impossible.'); |
0337d704 | 222 | } |
223 | } | |
224 | ||
225 | // }}} | |
226 | // {{{ function isempty() | |
227 | ||
228 | function isempty() | |
229 | { | |
bbf610b8 | 230 | return empty($this->strings) && empty($this->ranges) && empty($this->email) && empty($this->ip); |
0337d704 | 231 | } |
232 | ||
233 | // }}} | |
234 | // {{{ function get_request() | |
94f6c381 | 235 | |
0337d704 | 236 | function get_request() |
237 | { | |
94f6c381 | 238 | parent::get_request(); |
239 | $s = replace_accent(trim($this->value)); | |
bbf610b8 | 240 | $r = $s = str_replace('*','%',$s); |
241 | ||
dd70cd28 | 242 | if (S::admin() && strpos($s, '@') !== false) { |
bbf610b8 | 243 | $this->email = $s; |
dd70cd28 | 244 | } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) { |
bbf610b8 | 245 | $this->ip = $s; |
246 | } | |
247 | if ($this->email || $this->ip) { | |
248 | $this->strings = $this->ranges = array(); | |
249 | return; | |
250 | } | |
251 | ||
94f6c381 | 252 | $s = preg_replace('!\d+!', ' ', $s); |
94f6c381 | 253 | $this->strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY); |
8b035281 | 254 | if (count($this->strings) > 5) { |
d7610c35 | 255 | Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte"); |
8b035281 FB |
256 | $this->strings = array_slice($this->strings, 0, 5); |
257 | } | |
94f6c381 | 258 | |
bbf610b8 | 259 | $s = preg_replace('! *- *!', '-', $r); |
94f6c381 | 260 | $s = preg_replace('!([<>]) *!', ' \1', $s); |
261 | $s = preg_replace('![^0-9\-><]!', ' ', $s); | |
262 | $s = preg_replace('![<>\-] !', '', $s); | |
263 | $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY); | |
264 | $this->ranges=Array(); | |
265 | foreach ($ranges as $r) { | |
266 | if (preg_match('!^([<>]\d{4}|\d{4}(-\d{4})?)$!', $r)) $this->ranges[] = $r; | |
267 | } | |
0337d704 | 268 | } |
269 | ||
270 | // }}} | |
271 | // {{{ function get_where_statement() | |
94f6c381 | 272 | |
0337d704 | 273 | function get_where_statement() |
274 | { | |
94f6c381 | 275 | $where = Array(); |
276 | foreach ($this->strings as $i => $s) { | |
c16b5562 | 277 | if (Env::i('with_soundex') && strlen($s) > 1) { |
94f6c381 | 278 | $t = soundex_fr($s); |
279 | $where[] = "sn$i.soundex = '$t'"; | |
b8c2ada6 SJ |
280 | } elseif (Env::i('exact')) { |
281 | $where[] = "sn$i.token = '$s'"; | |
94f6c381 | 282 | } else { |
283 | $t = str_replace('*', '%', $s).'%'; | |
284 | $t = str_replace('%%', '%', $t); | |
285 | $where[] = "sn$i.token LIKE '$t'"; | |
286 | } | |
287 | } | |
288 | ||
289 | $wherep = Array(); | |
290 | foreach ($this->ranges as $r) { | |
291 | if (preg_match('!^\d{4}$!', $r)) { | |
292 | $wherep[] = "u.promo=$r"; | |
293 | } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) { | |
294 | $p1=min(intval($matches[1]), intval($matches[2])); | |
295 | $p2=max(intval($matches[1]), intval($matches[2])); | |
296 | $wherep[] = "(u.promo>=$p1 AND u.promo<=$p2)"; | |
297 | } elseif (preg_match('!^<(\d{4})!', $r, $matches)) { | |
298 | $wherep[] = "u.promo<={$matches[1]}"; | |
299 | } elseif (preg_match('!^>(\d{4})!', $r, $matches)) { | |
300 | $wherep[] = "u.promo>={$matches[1]}"; | |
301 | } | |
302 | } | |
303 | if (!empty($wherep)) { | |
0337d704 | 304 | $where[] = '('.join(' OR ',$wherep).')'; |
305 | } | |
bbf610b8 | 306 | if (!empty($this->email)) { |
307 | $where[] = 'ems.email = ' . XDB::escape($this->email); | |
308 | } | |
309 | if (!empty($this->ip)) { | |
9797734d | 310 | $ip = ip_to_uint($this->ip); |
8b17e99c VZ |
311 | |
312 | // If the IP address requested for the search cannot be translated, | |
313 | // the predicate should always be valued to false. | |
314 | if ($ip != null) { | |
315 | $where[] = "( ls.ip = $ip OR ls.forward_ip = $ip ) AND ls.suid = 0"; | |
316 | } else { | |
317 | $where[] = "false"; | |
318 | } | |
bbf610b8 | 319 | } |
3b2f9d11 | 320 | |
94f6c381 | 321 | return join(" AND ", $where); |
0337d704 | 322 | } |
323 | ||
324 | // }}} | |
325 | // {{{ get_select_statement | |
326 | function get_select_statement() | |
327 | { | |
328 | $join = ""; | |
c0c9f772 | 329 | $and = ''; |
c16b5562 | 330 | $uniq = ''; |
94f6c381 | 331 | foreach ($this->strings as $i => $s) { |
c0c9f772 | 332 | if (!S::logged()) { |
333 | $and = "AND FIND_IN_SET('public', sn$i.flags)"; | |
c16b5562 | 334 | } |
335 | $myu = str_replace('snv', "sn$i", $uniq); | |
336 | $join .= "INNER JOIN search_name AS sn$i ON (u.user_id = sn$i.uid $and$myu)\n"; | |
337 | $uniq .= " AND sn$i.token != snv.token"; | |
0337d704 | 338 | } |
bbf610b8 | 339 | if (!empty($this->email)) { |
340 | $join .= "LEFT JOIN emails AS ems ON (ems.uid = u.user_id)"; | |
341 | } | |
342 | if (!empty($this->ip)) { | |
343 | $join .= "INNER JOIN logger.sessions AS ls ON (ls.uid = u.user_id)\n"; | |
344 | } | |
0337d704 | 345 | return $join; |
346 | } | |
347 | // }}} | |
348 | // {{{ function get_order_statement() | |
94f6c381 | 349 | |
0337d704 | 350 | function get_order_statement() |
351 | { | |
352 | return false; | |
353 | } | |
354 | ||
355 | // }}} | |
356 | // {{{ function get_score_statement | |
94f6c381 | 357 | |
0337d704 | 358 | function get_score_statement() |
359 | { | |
360 | $sum = array('0'); | |
94f6c381 | 361 | foreach ($this->strings as $i => $s) { |
0337d704 | 362 | $sum[] .= "SUM(sn$i.score + IF('$s'=sn$i.token,5,0))"; |
363 | } | |
364 | return join('+', $sum).' AS score'; | |
365 | } | |
366 | ||
367 | // }}} | |
368 | } | |
369 | ||
370 | // }}} | |
371 | // {{{ class NumericSField [Integer fields] | |
372 | ||
a7de4ef7 | 373 | /** classe de champ numérique entier (offset par exemple) |
0337d704 | 374 | */ |
375 | class NumericSField extends SField | |
376 | { | |
377 | // {{{ constructor | |
94f6c381 | 378 | |
0337d704 | 379 | /** constructeur |
a7de4ef7 | 380 | * (récupère la requête de l'utilisateur pour ce champ) */ |
0337d704 | 381 | function NumericSField($_fieldFormName) |
382 | { | |
383 | $this->fieldFormName = $_fieldFormName; | |
384 | $this->get_request(); | |
385 | } | |
386 | ||
387 | // }}} | |
388 | // {{{ function get_request() | |
94f6c381 | 389 | |
a7de4ef7 | 390 | /** récupère la requête de l'utilisateur et échoue s'il ne s'agit pas d'un entier */ |
0337d704 | 391 | function get_request() |
392 | { | |
393 | parent::get_request(); | |
394 | if (empty($this->value)) { | |
395 | $this->value = 0; | |
396 | } | |
397 | if (!preg_match("/^[0-9]+$/", $this->value)) { | |
a7de4ef7 | 398 | new ThrowError('Un champ numérique contient des caractères alphanumériques.'); |
0337d704 | 399 | } |
400 | } | |
94f6c381 | 401 | |
0337d704 | 402 | // }}} |
403 | } | |
404 | ||
405 | // }}} | |
406 | // {{{ class RefSField [ ??? ] | |
407 | ||
408 | class RefSField extends SField | |
409 | { | |
410 | // {{{ properties | |
94f6c381 | 411 | |
0337d704 | 412 | var $refTable; |
413 | var $refAlias; | |
414 | var $refCondition; | |
415 | var $exact = true; | |
416 | ||
417 | // }}} | |
418 | // {{{ constructor | |
419 | ||
420 | function RefSField($_fieldFormName, $_fieldDbName='', $_refTable, $_refAlias, $_refCondition, $_exact=true) | |
421 | { | |
422 | $this->fieldFormName = $_fieldFormName; | |
423 | $this->fieldDbName = $_fieldDbName; | |
424 | $this->refTable = $_refTable; | |
425 | $this->refAlias = $_refAlias; | |
426 | $this->refCondition = $_refCondition; | |
427 | $this->exact = $_exact; | |
428 | $this->get_request(); | |
429 | } | |
430 | ||
431 | // }}} | |
432 | // {{{ function get_request() | |
94f6c381 | 433 | |
0337d704 | 434 | function get_request() { |
435 | parent::get_request(); | |
436 | if ($this->value=='00' || $this->value=='0') { | |
437 | $this->value=''; | |
438 | } | |
439 | } | |
440 | ||
441 | // }}} | |
442 | // {{{ function too_large() | |
443 | ||
444 | function too_large() | |
445 | { | |
446 | return ($this->value==''); | |
447 | } | |
448 | ||
449 | // }}} | |
450 | // {{{ function compare() | |
451 | ||
452 | function compare() | |
453 | { | |
454 | $val = addslashes($this->value); | |
b8c2ada6 | 455 | if (Env::i('exact')) return "='$val'"; |
0337d704 | 456 | return $this->exact ? "='$val'" : " LIKE '%$val%'"; |
457 | } | |
458 | ||
459 | // }}} | |
460 | // {{{ function get_single_match_statement() | |
461 | ||
462 | function get_single_match_statement($field) | |
463 | { | |
464 | return $field.$this->compare(); | |
465 | } | |
466 | ||
467 | // }}} | |
468 | // {{{ function get_single_where_statement() | |
469 | ||
470 | function get_single_where_statement($field) | |
471 | { | |
472 | return $this->refTable=='' ? $this->get_single_match_statement($field) : false; | |
473 | } | |
474 | ||
475 | // }}} | |
476 | // {{{ function get_select_statement() | |
477 | ||
478 | function get_select_statement() | |
479 | { | |
480 | if ($this->value=='' || $this->refTable=='') { | |
481 | return false; | |
482 | } | |
483 | $res = implode(' OR ', array_filter(array_map(array($this, 'get_single_match_statement'), $this->fieldDbName))); | |
137e819f FB |
484 | if (is_array($this->refTable)) { |
485 | foreach ($this->refTable as $i => $refT) | |
486 | $last = $i; | |
487 | $inner = ""; | |
488 | foreach ($this->refTable as $i => $refT) | |
489 | $inner .= " INNER JOIN {$refT} AS {$this->refAlias[$i]} ON ({$this->refCondition[$i]} ".(($i == $last)?"AND ($res) ":"").")\n"; | |
490 | return $inner; | |
491 | } else { | |
492 | return "INNER JOIN {$this->refTable} AS {$this->refAlias} ON ({$this->refCondition} AND ($res) )"; | |
493 | } | |
0337d704 | 494 | } |
495 | ||
496 | // }}} | |
497 | } | |
498 | ||
499 | // }}} | |
56670b6a | 500 | |
501 | // {{{ class RefSFieldMultipleTable | |
502 | class MapSField extends RefSField | |
503 | { | |
94f6c381 | 504 | var $mapId; |
505 | ||
56670b6a | 506 | function MapSField($_fieldFormName, $_fieldDbName='', $_refTable, $_refAlias, $_refCondition, $_mapId=false) |
507 | { | |
350e6926 | 508 | if ($_mapId === false) |
5e2307dc | 509 | $this->mapId = Env::v($_fieldFormName, ''); |
350e6926 | 510 | else |
511 | $this->mapId = $_mapId; | |
a2aa8436 | 512 | $this->value = $this->mapId; |
350e6926 | 513 | $this->RefSField($_fieldFormName, $_fieldDbName, $_refTable, $_refAlias, $_refCondition, true, false); |
56670b6a | 514 | } |
eaf30d86 | 515 | |
56670b6a | 516 | function get_select_statement() |
517 | { | |
350e6926 | 518 | if ($this->mapId === '') return false; |
56670b6a | 519 | $res = implode(' OR ', array_filter(array_map(array($this, 'get_single_match_statement'), $this->fieldDbName))); |
520 | foreach ($this->refTable as $i => $refT) | |
521 | $last = $i; | |
522 | $inner = ""; | |
523 | foreach ($this->refTable as $i => $refT) | |
524 | $inner .= " INNER JOIN {$refT} AS {$this->refAlias[$i]} ON ({$this->refCondition[$i]} ".(($i == $last)?"AND ($res) ":"").")"; | |
525 | return $inner; | |
526 | } | |
527 | function get_request() | |
528 | { | |
350e6926 | 529 | $this->value = $this->mapId; |
56670b6a | 530 | } |
531 | } | |
532 | ||
0337d704 | 533 | // {{{ class RefWithSoundexSField [ ??? ] |
534 | ||
535 | class RefWithSoundexSField extends RefSField | |
536 | { | |
537 | // {{{ function compare() | |
94f6c381 | 538 | |
0337d704 | 539 | function compare() |
540 | { | |
94f6c381 | 541 | return "='".soundex_fr($this->value)."'"; |
0337d704 | 542 | } |
543 | ||
544 | // }}} | |
545 | } | |
546 | ||
547 | // }}} | |
548 | // {{{ class StringSField [String fields] | |
549 | ||
550 | /** classe de champ texte (nom par exemple) | |
551 | */ | |
552 | class StringSField extends SField | |
553 | { | |
554 | // {{{ function get_request() | |
94f6c381 | 555 | |
a7de4ef7 | 556 | /** récupère la requête de l'utilisateur et échoue si la chaîne contient des caractères |
0337d704 | 557 | * interdits */ |
558 | function get_request() | |
559 | { | |
560 | parent::get_request(); | |
a14159bf | 561 | if (preg_match(":[\]\[<>{}~/§_`|%$^=+]|\*\*:u", $this->value)) { |
a7de4ef7 | 562 | new ThrowError('Un champ contient un caractère interdit rendant la recherche impossible.'); |
0337d704 | 563 | } |
564 | } | |
565 | ||
566 | // }}} | |
567 | // {{{ function length() | |
568 | ||
a7de4ef7 | 569 | /** donne la longueur de la requête de l'utilisateur |
570 | * (au sens strict i.e. pas d'* ni d'espace ou de trait d'union -> les contraintes réellement | |
571 | * imposées par l'utilisateur) */ | |
0337d704 | 572 | function length() |
573 | { | |
a14159bf | 574 | $cleaned = replace_accent(strtolower($this->value)); |
575 | $length = strlen(ereg_replace('[a-z0-9]', '', $cleaned)); | |
576 | return strlen($this->value) - $length; | |
0337d704 | 577 | } |
578 | ||
579 | // }}} | |
580 | // {{{ function too_large() | |
581 | ||
582 | function too_large() | |
583 | { | |
584 | return ($this->length()<2); | |
585 | } | |
586 | ||
587 | // }}} | |
588 | // {{{ function get_single_where_statement() | |
589 | ||
a7de4ef7 | 590 | /** clause WHERE correspondant à un champ de la bdd et à ce champ de formulaire |
591 | * @param field nom de champ de la bdd concerné par la clause */ | |
0337d704 | 592 | function get_single_where_statement($field) |
593 | { | |
b8c2ada6 SJ |
594 | $val = addslashes($this->value); |
595 | if (Env::i('exact')) return "$field = '$val'"; | |
596 | $regexp = strtr($val, '-*', '_%'); | |
0337d704 | 597 | return "$field LIKE '$regexp%'"; |
598 | } | |
599 | ||
600 | // }}} | |
601 | // {{{ function get_order_statement() | |
602 | ||
a7de4ef7 | 603 | /** clause ORDER BY correspondant à ce champ de formulaire */ |
0337d704 | 604 | function get_order_statement() |
605 | { | |
606 | if ($this->value!='' && $this->fieldResultName!='') { | |
607 | return "{$this->fieldResultName}!='".addslashes($this->value)."'"; | |
608 | } else { | |
609 | return false; | |
610 | } | |
611 | } | |
612 | ||
613 | // }}} | |
614 | } | |
615 | ||
616 | // }}} | |
617 | // {{{ class NameSField [Names : serach 'n%' + '% b'] | |
618 | ||
619 | /** classe pour les noms : on cherche en plus du like 'foo%' le like '% foo' (particules) | |
620 | +*/ | |
621 | class NameSField extends StringSField | |
622 | { | |
623 | // {{{ function get_single_where_statement() | |
94f6c381 | 624 | |
0337d704 | 625 | function get_single_where_statement($field) |
626 | { | |
b8c2ada6 SJ |
627 | $val = addslashes($this->value); |
628 | if (Env::i('exact')) return "$field = '$val'"; | |
629 | $regexp = strtr($val, '-*', '_%'); | |
0337d704 | 630 | return "$field LIKE '$regexp%' OR $field LIKE '% $regexp%' OR $field LIKE '%-$regexp%'"; |
631 | } | |
632 | ||
633 | // }}} | |
634 | // {{{ function get_order_statement() | |
94f6c381 | 635 | |
0337d704 | 636 | function get_order_statement() |
637 | { | |
638 | if ($this->value!='' && $this->fieldResultName!='') { | |
639 | return "{$this->fieldResultName} NOT LIKE '".addslashes($this->value)."'"; | |
640 | } else { | |
641 | return false; | |
642 | } | |
643 | } | |
644 | ||
645 | // }}} | |
646 | } | |
647 | ||
648 | // }}} | |
649 | // {{{ class StringWithSoundexSField [Strings + soundex] | |
650 | ||
651 | /** classe de champ texte avec soundex (nom par exemple) | |
652 | */ | |
653 | class StringWithSoundexSField extends StringSField | |
654 | { | |
655 | // {{{ function get_single_where_statement() | |
656 | ||
a7de4ef7 | 657 | /** clause WHERE correspondant à un champ de la bdd et à ce champ de formulaire |
658 | * @param field nom de champ de la bdd concerné par la clause */ | |
0337d704 | 659 | function get_single_where_statement($field) { |
660 | return $field.'="'.soundex_fr($this->value).'"'; | |
661 | } | |
662 | ||
663 | // }}} | |
664 | } | |
665 | ||
666 | // }}} | |
667 | // {{{ class PromoSField [Prom field] | |
668 | ||
669 | /** classe de champ de promotion */ | |
670 | class PromoSField extends SField | |
671 | { | |
672 | // {{{ properties | |
94f6c381 | 673 | |
a7de4ef7 | 674 | /** opérateur de comparaison (<,>,=) de la promo utilisé pour ce champ de formulaire */ |
0337d704 | 675 | var $compareField; |
676 | ||
677 | // }}} | |
678 | // {{{ constructor | |
679 | ||
eaf30d86 | 680 | /** constructeur |
a7de4ef7 | 681 | * compareField est un champ de formulaire très simple qui ne sert qu'à la construction de la |
0337d704 | 682 | * clause WHERE de la promo */ |
683 | function PromoSField($_fieldFormName, $_compareFieldFormName, $_fieldDbName, $_fieldResultName) | |
684 | { | |
685 | parent::SField($_fieldFormName, $_fieldDbName, $_fieldResultName); | |
686 | $this->compareField = new SField($_compareFieldFormName); | |
687 | } | |
688 | ||
689 | // }}} | |
690 | // {{{ function get_request() | |
691 | ||
a7de4ef7 | 692 | /** récupère la requête utilisateur et échoue si le champ du formulaire ne représente pas une |
693 | * promotion (nombre à 4 chiffres) */ | |
0337d704 | 694 | function get_request() |
695 | { | |
696 | parent::get_request(); | |
697 | if (preg_match('/^[0-9]{2}$/', $this->value)){ | |
698 | $this->value = intval($this->value) + 1900; | |
699 | } | |
700 | if (!(empty($this->value) or preg_match('/^[0-9]{4}$/', $this->value))) { | |
a7de4ef7 | 701 | new ThrowError('La promotion est une année à quatre chiffres.'); |
0337d704 | 702 | } |
703 | } | |
704 | ||
705 | // }}} | |
706 | // {{{ function is_a_single_promo() | |
707 | ||
a7de4ef7 | 708 | /** teste si la requête est de la forme =promotion -> contrainte forte imposée -> elle suffit |
709 | * pour autoriser un affichage des résultats alors que <promotion est insuffisant */ | |
0337d704 | 710 | function is_a_single_promo() |
711 | { | |
712 | return ($this->compareField->value=='=' && $this->value!=''); | |
713 | } | |
714 | ||
715 | // }}} | |
716 | // {{{ function too_large() | |
717 | ||
718 | function too_large() | |
719 | { | |
720 | return !$this->is_a_single_promo(); | |
721 | } | |
722 | ||
723 | // }}} | |
724 | // {{{ function get_single_where_statement() | |
725 | ||
a7de4ef7 | 726 | /** clause WHERE correspondant à ce champ */ |
0337d704 | 727 | function get_single_where_statement($field) |
728 | { | |
729 | return $field.$this->compareField->value.$this->value; | |
730 | } | |
731 | ||
732 | // }}} | |
733 | // {{{ function get_url() | |
734 | ||
a7de4ef7 | 735 | /** récupérer le bout d'URL correspondant aux paramètres permettant d'imiter une requête |
736 | * d'un utilisateur assignant la valeur $this->value à ce champ et assignant l'opérateur de | |
737 | * comparaison adéquat */ | |
0337d704 | 738 | function get_url() |
739 | { | |
740 | if (!($u=parent::get_url())) { | |
741 | return false; | |
742 | } | |
743 | return $u.'&'.$this->compareField->get_url(); | |
744 | } | |
745 | ||
746 | // }}} | |
747 | } | |
748 | ||
749 | // }}} | |
750 | // {{{ class SFieldGroup [Group fields] | |
751 | ||
752 | /** classe groupant des champs de formulaire de recherche */ | |
753 | class SFieldGroup | |
754 | { | |
755 | // {{{ properties | |
94f6c381 | 756 | |
a7de4ef7 | 757 | /** tableau des classes correspondant aux champs groupés */ |
0337d704 | 758 | var $fields; |
759 | /** type de groupe : ET ou OU */ | |
760 | var $and; | |
761 | ||
762 | // }}} | |
763 | // {{{ constuctor | |
764 | ||
765 | /** constructeur */ | |
766 | function SFieldGroup($_and, $_fields) | |
767 | { | |
768 | $this->fields = $_fields; | |
769 | $this->and = $_and; | |
63fac48e FB |
770 | foreach ($this->fields as $key=>&$field) { |
771 | if (is_null($field)) { | |
772 | unset($this->fields[$key]); | |
773 | } | |
774 | } | |
0337d704 | 775 | } |
776 | ||
777 | // }}} | |
778 | // {{{ function too_large() | |
779 | ||
780 | function too_large() | |
781 | { | |
782 | $b = true; | |
a2aa8436 | 783 | for ($i=0 ; $b && $i<count($this->fields) ; $i++) { |
63fac48e FB |
784 | if (!is_null($this->fields[$i])) { |
785 | $b = $b && $this->fields[$i]->too_large(); | |
786 | } | |
0337d704 | 787 | } |
788 | return $b; | |
789 | } | |
790 | ||
791 | // }}} | |
792 | // {{{ function field_get_select() | |
793 | ||
794 | function field_get_select($f) | |
795 | { | |
796 | return $f->get_select_statement(); | |
797 | } | |
798 | ||
799 | // }}} | |
800 | // {{{ function field_get_where() | |
801 | ||
a7de4ef7 | 802 | /** récupérer la clause WHERE d'un objet champ de recherche */ |
0337d704 | 803 | function field_get_where($f) |
804 | { | |
805 | return $f->get_where_statement(); | |
806 | } | |
807 | ||
808 | // }}} | |
809 | // {{{ function field_get_order() | |
810 | ||
a7de4ef7 | 811 | /** récupérer la clause ORDER BY d'un objet champ de recherche */ |
0337d704 | 812 | function field_get_order($f) |
813 | { | |
814 | return $f->get_order_statement(); | |
815 | } | |
816 | ||
817 | // }}} | |
818 | // {{{ function field_get_url() | |
819 | ||
a7de4ef7 | 820 | /** récupérer le bout d'URL correspondant à un objet champ de recherche */ |
0337d704 | 821 | function field_get_url($f) |
822 | { | |
823 | return $f->get_url(); | |
824 | } | |
94f6c381 | 825 | |
0337d704 | 826 | // }}} |
827 | // {{{ function get_select_statement() | |
828 | ||
829 | function get_select_statement() | |
830 | { | |
831 | return implode(' ', array_filter(array_map(array($this, 'field_get_select'), $this->fields))); | |
832 | } | |
833 | ||
834 | // }}} | |
835 | // {{{ function get_where_statement() | |
836 | ||
a7de4ef7 | 837 | /** récupérer la clause WHERE du groupe de champs = conjonction (ET) ou disjonction (OU) de |
838 | * clauses des champs élémentaires */ | |
0337d704 | 839 | function get_where_statement() |
840 | { | |
841 | $joinText = $this->and ? ' AND ' : ' OR '; | |
842 | $res = implode($joinText, array_filter(array_map(array($this, 'field_get_where'), $this->fields))); | |
843 | return $res == '' ? '' : "($res)"; | |
844 | } | |
845 | ||
846 | // }}} | |
847 | // {{{ function get_order_statement() | |
848 | ||
a7de4ef7 | 849 | /** récupérer la clause ORDER BY du groupe de champs = conjonction (ET) ou disjonction (OU) de |
850 | * clauses des champs élémentaires */ | |
0337d704 | 851 | function get_order_statement() |
852 | { | |
853 | $order = array_filter(array_map(array($this, 'field_get_order'), $this->fields)); | |
854 | return count($order)>0 ? implode(',', $order) : false; | |
855 | } | |
856 | ||
857 | // }}} | |
858 | // {{{ function get_url() | |
859 | ||
a7de4ef7 | 860 | /** récupérer le bout d'URL correspondant à ce groupe de champs = concaténation des bouts d'URL |
861 | * des champs élémentaires */ | |
0337d704 | 862 | function get_url($others=Array()) |
863 | { | |
864 | $url = array_filter(array_map(array($this, 'field_get_url'), $this->fields)); | |
865 | foreach ($url as $key=>$val) { | |
866 | if (empty($val)) { | |
867 | unset($url[$key]); | |
868 | } | |
869 | } | |
870 | foreach ($others as $key=>$val) { | |
871 | if (!empty($val)) { | |
872 | $url[] = "$key=$val"; | |
873 | } | |
874 | } | |
875 | return count($url)>0 ? implode('&', $url) : false; | |
876 | } | |
877 | ||
878 | // }}} | |
879 | } | |
880 | ||
881 | // }}} | |
882 | ||
a7de4ef7 | 883 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 884 | ?> |