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