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