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