0337d704 |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2004 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\') 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 | // hide private information if not logged |
40 | if (logged()) |
41 | $globals->search->result_fields .=' |
42 | q.profile_web AS web, |
43 | q.profile_mobile AS mobile, |
44 | q.profile_freetext AS freetext, |
45 | adr.city, gp.pays AS countrytxt, gr.name AS region, |
46 | e.entreprise,'; |
47 | else |
48 | $globals->search->result_fields .=" |
49 | IF(q.profile_web_pub='public', q.profile_web, '') AS web, |
50 | IF(q.profile_mobile_pub='public', q.profile_mobile, '') AS mobile, |
51 | IF(q.profile_freetext_pub='public', q.profile_freetext, '') AS freetext, |
52 | IF(adr.pub='public', adr.city, '') AS city, |
53 | IF(adr.pub='public', gp.pays, '') AS countrytxt, |
54 | IF(adr.pub='public', gr.name, '') AS region, |
55 | IF(e.pub='public', e.entreprise, '') AS entreprise,"; |
56 | $globals->search->result_where_statement = ' |
57 | LEFT JOIN applis_ins AS ai0 ON (u.user_id = ai0.uid AND ai0.ordre = 0) |
58 | LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid) |
59 | LEFT JOIN applis_ins AS ai1 ON (u.user_id = ai1.uid AND ai1.ordre = 1) |
60 | LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid) |
61 | LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = u.user_id) |
62 | LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id) |
63 | LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id) |
64 | LEFT JOIN geoloc_pays AS n ON (u.nationalite = n.a2) |
65 | LEFT JOIN adresses AS adr ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut)) |
66 | LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2) |
67 | LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)'; |
68 | |
69 | // }}} |
70 | // {{{ function display_lines() |
71 | |
72 | /** |
73 | * This function is a heuristic that approximatively tells |
74 | * how many lines of output the contact list will use. |
75 | */ |
76 | function display_lines($text) |
77 | { |
78 | $n = 0; |
79 | $tokens = Array('<tr>', '<div class="nom">'); |
80 | foreach ($tokens as $t) { |
81 | $i = -1; |
82 | while ( ($i = strpos($text,$t,$i+1))!==false) { $n++; } |
83 | } |
84 | return $n; |
85 | } |
86 | |
87 | // }}} |
88 | // {{{ class ThrowError |
89 | |
90 | /** handle errors for end-users queries |
91 | * assign the error message and runs the templates |
92 | * |
93 | * @author Jean-Sebastien Bedo |
94 | */ |
95 | class ThrowError |
96 | { |
97 | /** constuctor |
98 | * @param $explain string the error (in natural language) |
99 | */ |
100 | function ThrowError($explain) |
101 | { |
102 | global $page; |
103 | $page->trig_run('Erreur : '.$explain); |
104 | } |
105 | } |
106 | |
107 | // }}} |
108 | // {{{ class SField [Base class] |
109 | |
110 | /** classe de base représentant un champ de recherche |
111 | * (correspond à un champ du formulaire mais peut être à plusieurs champs de la bdd) |
112 | * interface étendue pour chaque type de champ particulier |
113 | */ |
114 | class SField |
115 | { |
116 | // {{{ properties |
117 | |
118 | /** le nom du champ dans le formulaire HTML */ |
119 | var $fieldFormName; |
120 | /** champs de la bdd correspondant à ce champ sous forme d'un tableau */ |
121 | var $fieldDbName; |
122 | /** champ résultat dans la requête MySQL correspondant à ce champ |
123 | * (alias utilisé pour la clause ORDER BY) */ |
124 | var $fieldResultName; |
125 | /** valeur du champ instanciée par l'utilisateur */ |
126 | var $value; |
127 | |
128 | // }}} |
129 | // {{{ constructor |
130 | |
131 | /** constructeur |
132 | * (récupère la requête de l'utilisateur pour ce champ) */ |
133 | function SField($_fieldFormName, $_fieldDbName='', $_fieldResultName='') |
134 | { |
135 | $this->fieldFormName = $_fieldFormName; |
136 | $this->fieldDbName = $_fieldDbName; |
137 | $this->fieldResultName = $_fieldResultName; |
138 | $this->get_request(); |
139 | } |
140 | |
141 | // }}} |
142 | // {{{ function get_request() |
143 | |
144 | /** récupérer la requête de l'utilisateur |
145 | * on met une chaîne vide si le champ n'a pas été complété */ |
146 | function get_request() |
147 | { |
148 | $this->value = trim(Env::get($this->fieldFormName)); |
149 | } |
150 | |
151 | // }}} |
152 | // {{{ function get_where_statement() |
153 | |
154 | /** récupérer la clause correspondant au champ dans la clause WHERE de la requête |
155 | * on parcourt l'ensemble des champs de la bdd de $fieldDbName et on associe |
156 | * à chacun d'entre eux une clause spécifique |
157 | * la clause totale et la disjonction de ces clauses spécifiques */ |
158 | function get_where_statement() |
159 | { |
160 | if ($this->value=='') { |
161 | return false; |
162 | } |
163 | $res = implode(' OR ', array_filter(array_map(array($this, 'get_single_where_statement'), $this->fieldDbName))); |
164 | return empty($res) ? '' : "($res)"; |
165 | } |
166 | |
167 | // }}} |
168 | // {{{ function get_order_statement() |
169 | |
170 | /** récupérer la clause correspondant au champ dans la clause ORDER BY de la requête |
171 | * utilisé par exemple pour placer d'abord le nom égal à la requête avant les approximations */ |
172 | function get_order_statement() |
173 | { |
174 | return false; |
175 | } |
176 | |
177 | // }}} |
178 | // {{{ function get_select_statement() |
179 | |
180 | function get_select_statement() |
181 | { |
182 | return false; |
183 | } |
184 | |
185 | // }}} |
186 | // {{{ function get_url() |
187 | |
188 | /** récupérer le bout d'URL correspondant aux paramètres permettant d'imiter une requête d'un |
189 | * utilisateur assignant la valeur $this->value à ce champ */ |
190 | function get_url() |
191 | { |
192 | if (empty($this->value)) { |
193 | return false; |
194 | } else { |
195 | return $this->fieldFormName.'='.urlencode($this->value); |
196 | } |
197 | } |
198 | |
199 | // }}} |
200 | } |
201 | |
202 | // }}} |
203 | // {{{ class QuickSearch [Google Like] |
204 | |
205 | class QuickSearch extends SField |
206 | { |
207 | // {{{ properties |
208 | |
209 | /** stores tokens */ |
210 | var $strings; |
211 | /** stores numerical ranges */ |
212 | var $ranges; |
213 | |
214 | // }}} |
215 | // {{{ constructor |
216 | |
217 | function QuickSearch($_fieldFormName) |
218 | { |
219 | $this->fieldFormName = $_fieldFormName; |
220 | $this->get_request(); |
221 | if (preg_match(":[\]\[{}~/§_`|%$^=+]|\*\*:", $this->value)) { |
222 | new ThrowError('Un champ contient un caractère interdit rendant la recherche impossible.'); |
223 | } |
224 | } |
225 | |
226 | // }}} |
227 | // {{{ function isempty() |
228 | |
229 | function isempty() |
230 | { |
231 | return empty($this->strings) && empty($this->ranges); |
232 | } |
233 | |
234 | // }}} |
235 | // {{{ function get_request() |
236 | |
237 | function get_request() |
238 | { |
239 | parent::get_request(); |
240 | $s = replace_accent(trim($this->value)); |
241 | $s = preg_replace('!\d+!', ' ', $s); |
242 | $s = str_replace('*','%',$s); |
243 | $this->strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY); |
244 | |
245 | $s = trim($this->value); |
246 | $s = preg_replace('! *- *!', '-', $s); |
247 | $s = preg_replace('!([<>]) *!', ' \1', $s); |
248 | $s = preg_replace('![^0-9\-><]!', ' ', $s); |
249 | $s = preg_replace('![<>\-] !', '', $s); |
250 | $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY); |
251 | $this->ranges=Array(); |
252 | foreach ($ranges as $r) { |
253 | if (preg_match('!^([<>]\d{4}|\d{4}(-\d{4})?)$!', $r)) $this->ranges[] = $r; |
254 | } |
255 | } |
256 | |
257 | // }}} |
258 | // {{{ function get_where_statement() |
259 | |
260 | function get_where_statement() |
261 | { |
262 | $where = Array(); |
263 | foreach ($this->strings as $i => $s) { |
264 | $t = str_replace('*', '%', $s).'%'; |
265 | $t = str_replace('%%', '%', $t); |
266 | $where[] = "sn$i.token LIKE '$t'"; |
267 | } |
268 | |
269 | $wherep = Array(); |
270 | foreach ($this->ranges as $r) { |
271 | if (preg_match('!^\d{4}$!', $r)) { |
272 | $wherep[] = "u.promo=$r"; |
273 | } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) { |
274 | $p1=min(intval($matches[1]), intval($matches[2])); |
275 | $p2=max(intval($matches[1]), intval($matches[2])); |
276 | $wherep[] = "(u.promo>=$p1 AND u.promo<=$p2)"; |
277 | } elseif (preg_match('!^<(\d{4})!', $r, $matches)) { |
278 | $wherep[] = "u.promo<={$matches[1]}"; |
279 | } elseif (preg_match('!^>(\d{4})!', $r, $matches)) { |
280 | $wherep[] = "u.promo>={$matches[1]}"; |
281 | } |
282 | } |
283 | if (!empty($wherep)) { |
284 | $where[] = '('.join(' OR ',$wherep).')'; |
285 | } |
286 | return join(" AND ", $where); |
287 | } |
288 | |
289 | // }}} |
290 | // {{{ get_select_statement |
291 | function get_select_statement() |
292 | { |
293 | $join = ""; |
294 | foreach ($this->strings as $i => $s) { |
295 | $join .= "INNER JOIN search_name AS sn$i ON (u.user_id = sn$i.uid)\n"; |
296 | } |
297 | return $join; |
298 | } |
299 | // }}} |
300 | // {{{ function get_order_statement() |
301 | |
302 | function get_order_statement() |
303 | { |
304 | return false; |
305 | } |
306 | |
307 | // }}} |
308 | // {{{ function get_score_statement |
309 | |
310 | function get_score_statement() |
311 | { |
312 | $sum = array('0'); |
313 | foreach ($this->strings as $i => $s) { |
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 | |
325 | /** classe de champ numérique entier (offset par exemple) |
326 | */ |
327 | class NumericSField extends SField |
328 | { |
329 | // {{{ constructor |
330 | |
331 | /** constructeur |
332 | * (récupère la requête de l'utilisateur pour ce champ) */ |
333 | function NumericSField($_fieldFormName) |
334 | { |
335 | $this->fieldFormName = $_fieldFormName; |
336 | $this->get_request(); |
337 | } |
338 | |
339 | // }}} |
340 | // {{{ function get_request() |
341 | |
342 | /** récupère la requête de l'utilisateur et échoue s'il ne s'agit pas d'un entier */ |
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)) { |
350 | new ThrowError('Un champ numérique contient des caractères alphanumériques.'); |
351 | } |
352 | } |
353 | |
354 | // }}} |
355 | } |
356 | |
357 | // }}} |
358 | // {{{ class RefSField [ ??? ] |
359 | |
360 | class RefSField extends SField |
361 | { |
362 | // {{{ properties |
363 | |
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() |
385 | |
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 | // }}} |
442 | // {{{ class RefWithSoundexSField [ ??? ] |
443 | |
444 | class RefWithSoundexSField extends RefSField |
445 | { |
446 | // {{{ function compare() |
447 | |
448 | function compare() |
449 | { |
450 | return "='".soundex_fr($this->value)."'"; |
451 | } |
452 | |
453 | // }}} |
454 | } |
455 | |
456 | // }}} |
457 | // {{{ class StringSField [String fields] |
458 | |
459 | /** classe de champ texte (nom par exemple) |
460 | */ |
461 | class StringSField extends SField |
462 | { |
463 | // {{{ function get_request() |
464 | |
465 | /** récupère la requête de l'utilisateur et échoue si la chaîne contient des caractères |
466 | * interdits */ |
467 | function get_request() |
468 | { |
469 | parent::get_request(); |
470 | if (preg_match(":[\]\[<>{}~/§_`|%$^=+]|\*\*:", $this->value)) { |
471 | new ThrowError('Un champ contient un caractère interdit rendant la recherche impossible.'); |
472 | } |
473 | } |
474 | |
475 | // }}} |
476 | // {{{ function length() |
477 | |
478 | /** donne la longueur de la requête de l'utilisateur |
479 | * (au sens strict i.e. pas d'* ni d'espace ou de trait d'union -> les contraintes réellement |
480 | * imposées par l'utilisateur) */ |
481 | function length() |
482 | { |
483 | global $lc_accent,$uc_accent; |
484 | return strlen($this->value) - strlen(ereg_replace('[a-z'.$lc_accent.$uc_accent.']', '', strtolower($this->value))); |
485 | } |
486 | |
487 | // }}} |
488 | // {{{ function too_large() |
489 | |
490 | function too_large() |
491 | { |
492 | return ($this->length()<2); |
493 | } |
494 | |
495 | // }}} |
496 | // {{{ function get_single_where_statement() |
497 | |
498 | /** clause WHERE correspondant à un champ de la bdd et à ce champ de formulaire |
499 | * @param field nom de champ de la bdd concerné par la clause */ |
500 | function get_single_where_statement($field) |
501 | { |
502 | $regexp = strtr(addslashes($this->value), '-*', '_%'); |
503 | return "$field LIKE '$regexp%'"; |
504 | } |
505 | |
506 | // }}} |
507 | // {{{ function get_order_statement() |
508 | |
509 | /** clause ORDER BY correspondant à ce champ de formulaire */ |
510 | function get_order_statement() |
511 | { |
512 | if ($this->value!='' && $this->fieldResultName!='') { |
513 | return "{$this->fieldResultName}!='".addslashes($this->value)."'"; |
514 | } else { |
515 | return false; |
516 | } |
517 | } |
518 | |
519 | // }}} |
520 | } |
521 | |
522 | // }}} |
523 | // {{{ class NameSField [Names : serach 'n%' + '% b'] |
524 | |
525 | /** classe pour les noms : on cherche en plus du like 'foo%' le like '% foo' (particules) |
526 | +*/ |
527 | class NameSField extends StringSField |
528 | { |
529 | // {{{ function get_single_where_statement() |
530 | |
531 | function get_single_where_statement($field) |
532 | { |
533 | $regexp = strtr(addslashes($this->value), '-*', '_%'); |
534 | return "$field LIKE '$regexp%' OR $field LIKE '% $regexp%' OR $field LIKE '%-$regexp%'"; |
535 | } |
536 | |
537 | // }}} |
538 | // {{{ function get_order_statement() |
539 | |
540 | function get_order_statement() |
541 | { |
542 | if ($this->value!='' && $this->fieldResultName!='') { |
543 | return "{$this->fieldResultName} NOT LIKE '".addslashes($this->value)."'"; |
544 | } else { |
545 | return false; |
546 | } |
547 | } |
548 | |
549 | // }}} |
550 | } |
551 | |
552 | // }}} |
553 | // {{{ class StringWithSoundexSField [Strings + soundex] |
554 | |
555 | /** classe de champ texte avec soundex (nom par exemple) |
556 | */ |
557 | class StringWithSoundexSField extends StringSField |
558 | { |
559 | // {{{ function get_single_where_statement() |
560 | |
561 | /** clause WHERE correspondant à un champ de la bdd et à ce champ de formulaire |
562 | * @param field nom de champ de la bdd concerné par la clause */ |
563 | function get_single_where_statement($field) { |
564 | return $field.'="'.soundex_fr($this->value).'"'; |
565 | } |
566 | |
567 | // }}} |
568 | } |
569 | |
570 | // }}} |
571 | // {{{ class PromoSField [Prom field] |
572 | |
573 | /** classe de champ de promotion */ |
574 | class PromoSField extends SField |
575 | { |
576 | // {{{ properties |
577 | |
578 | /** opérateur de comparaison (<,>,=) de la promo utilisé pour ce champ de formulaire */ |
579 | var $compareField; |
580 | |
581 | // }}} |
582 | // {{{ constructor |
583 | |
584 | /** constructeur |
585 | * compareField est un champ de formulaire très simple qui ne sert qu'à la construction de la |
586 | * clause WHERE de la promo */ |
587 | function PromoSField($_fieldFormName, $_compareFieldFormName, $_fieldDbName, $_fieldResultName) |
588 | { |
589 | parent::SField($_fieldFormName, $_fieldDbName, $_fieldResultName); |
590 | $this->compareField = new SField($_compareFieldFormName); |
591 | } |
592 | |
593 | // }}} |
594 | // {{{ function get_request() |
595 | |
596 | /** récupère la requête utilisateur et échoue si le champ du formulaire ne représente pas une |
597 | * promotion (nombre à 4 chiffres) */ |
598 | function get_request() |
599 | { |
600 | parent::get_request(); |
601 | if (preg_match('/^[0-9]{2}$/', $this->value)){ |
602 | $this->value = intval($this->value) + 1900; |
603 | } |
604 | if (!(empty($this->value) or preg_match('/^[0-9]{4}$/', $this->value))) { |
605 | new ThrowError('La promotion est une année à quatre chiffres.'); |
606 | } |
607 | } |
608 | |
609 | // }}} |
610 | // {{{ function is_a_single_promo() |
611 | |
612 | /** teste si la requête est de la forme =promotion -> contrainte forte imposée -> elle suffit |
613 | * pour autoriser un affichage des résultats alors que <promotion est insuffisant */ |
614 | function is_a_single_promo() |
615 | { |
616 | return ($this->compareField->value=='=' && $this->value!=''); |
617 | } |
618 | |
619 | // }}} |
620 | // {{{ function too_large() |
621 | |
622 | function too_large() |
623 | { |
624 | return !$this->is_a_single_promo(); |
625 | } |
626 | |
627 | // }}} |
628 | // {{{ function get_single_where_statement() |
629 | |
630 | /** clause WHERE correspondant à ce champ */ |
631 | function get_single_where_statement($field) |
632 | { |
633 | return $field.$this->compareField->value.$this->value; |
634 | } |
635 | |
636 | // }}} |
637 | // {{{ function get_url() |
638 | |
639 | /** récupérer le bout d'URL correspondant aux paramètres permettant d'imiter une requête |
640 | * d'un utilisateur assignant la valeur $this->value à ce champ et assignant l'opérateur de |
641 | * comparaison adéquat */ |
642 | function get_url() |
643 | { |
644 | if (!($u=parent::get_url())) { |
645 | return false; |
646 | } |
647 | return $u.'&'.$this->compareField->get_url(); |
648 | } |
649 | |
650 | // }}} |
651 | } |
652 | |
653 | // }}} |
654 | // {{{ class SFieldGroup [Group fields] |
655 | |
656 | /** classe groupant des champs de formulaire de recherche */ |
657 | class SFieldGroup |
658 | { |
659 | // {{{ properties |
660 | |
661 | /** tableau des classes correspondant aux champs groupés */ |
662 | var $fields; |
663 | /** type de groupe : ET ou OU */ |
664 | var $and; |
665 | |
666 | // }}} |
667 | // {{{ constuctor |
668 | |
669 | /** constructeur */ |
670 | function SFieldGroup($_and, $_fields) |
671 | { |
672 | $this->fields = $_fields; |
673 | $this->and = $_and; |
674 | } |
675 | |
676 | // }}} |
677 | // {{{ function too_large() |
678 | |
679 | function too_large() |
680 | { |
681 | $b = true; |
682 | for ($i=0; $b && $i<count($this->fields); $i++) { |
683 | $b &= $this->fields[$i]->too_large(); |
684 | } |
685 | return $b; |
686 | } |
687 | |
688 | // }}} |
689 | // {{{ function field_get_select() |
690 | |
691 | function field_get_select($f) |
692 | { |
693 | return $f->get_select_statement(); |
694 | } |
695 | |
696 | // }}} |
697 | // {{{ function field_get_where() |
698 | |
699 | /** récupérer la clause WHERE d'un objet champ de recherche */ |
700 | function field_get_where($f) |
701 | { |
702 | return $f->get_where_statement(); |
703 | } |
704 | |
705 | // }}} |
706 | // {{{ function field_get_order() |
707 | |
708 | /** récupérer la clause ORDER BY d'un objet champ de recherche */ |
709 | function field_get_order($f) |
710 | { |
711 | return $f->get_order_statement(); |
712 | } |
713 | |
714 | // }}} |
715 | // {{{ function field_get_url() |
716 | |
717 | /** récupérer le bout d'URL correspondant à un objet champ de recherche */ |
718 | function field_get_url($f) |
719 | { |
720 | return $f->get_url(); |
721 | } |
722 | |
723 | // }}} |
724 | // {{{ function get_select_statement() |
725 | |
726 | function get_select_statement() |
727 | { |
728 | return implode(' ', array_filter(array_map(array($this, 'field_get_select'), $this->fields))); |
729 | } |
730 | |
731 | // }}} |
732 | // {{{ function get_where_statement() |
733 | |
734 | /** récupérer la clause WHERE du groupe de champs = conjonction (ET) ou disjonction (OU) de |
735 | * clauses des champs élémentaires */ |
736 | function get_where_statement() |
737 | { |
738 | $joinText = $this->and ? ' AND ' : ' OR '; |
739 | $res = implode($joinText, array_filter(array_map(array($this, 'field_get_where'), $this->fields))); |
740 | return $res == '' ? '' : "($res)"; |
741 | } |
742 | |
743 | // }}} |
744 | // {{{ function get_order_statement() |
745 | |
746 | /** récupérer la clause ORDER BY du groupe de champs = conjonction (ET) ou disjonction (OU) de |
747 | * clauses des champs élémentaires */ |
748 | function get_order_statement() |
749 | { |
750 | $order = array_filter(array_map(array($this, 'field_get_order'), $this->fields)); |
751 | return count($order)>0 ? implode(',', $order) : false; |
752 | } |
753 | |
754 | // }}} |
755 | // {{{ function get_url() |
756 | |
757 | /** récupérer le bout d'URL correspondant à ce groupe de champs = concaténation des bouts d'URL |
758 | * des champs élémentaires */ |
759 | function get_url($others=Array()) |
760 | { |
761 | $url = array_filter(array_map(array($this, 'field_get_url'), $this->fields)); |
762 | foreach ($url as $key=>$val) { |
763 | if (empty($val)) { |
764 | unset($url[$key]); |
765 | } |
766 | } |
767 | foreach ($others as $key=>$val) { |
768 | if (!empty($val)) { |
769 | $url[] = "$key=$val"; |
770 | } |
771 | } |
772 | return count($url)>0 ? implode('&', $url) : false; |
773 | } |
774 | |
775 | // }}} |
776 | } |
777 | |
778 | // }}} |
779 | |
780 | ?> |