Commit | Line | Data |
---|---|---|
d9b3d712 RB |
1 | <?php |
2 | /*************************************************************************** | |
21b67462 | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
d9b3d712 RB |
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 | ||
21b67462 | 22 | // {{{ class UserFilterBuilder |
d9b3d712 RB |
23 | class UserFilterBuilder |
24 | { | |
25 | private $envprefix; | |
26 | private $fields; | |
27 | private $valid = true; | |
28 | private $ufc = null; | |
59c6cb70 | 29 | private $orders = array(); |
d9b3d712 RB |
30 | |
31 | /** Constructor | |
32 | * @param $fields An array of UFB_Field objects | |
33 | * @param $envprefix Prefix to use for parts of the query | |
34 | */ | |
35 | public function __construct($fields, $envprefix = '') | |
36 | { | |
37 | $this->fields = $fields; | |
38 | $this->envprefix = $envprefix; | |
39 | } | |
40 | ||
41 | /** Builds the UFC; returns as soon as a field says it is invalid | |
42 | */ | |
43 | private function buildUFC() | |
44 | { | |
45 | if ($this->ufc != null) { | |
46 | return; | |
47 | } | |
48 | $this->ufc = new PFC_And(); | |
49 | ||
50 | foreach ($this->fields as $field) { | |
51 | $this->valid = $field->apply(&$this); | |
52 | if (!$this->valid) { | |
53 | return; | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | public function addCond(PlFilterCondition &$cond) | |
59 | { | |
60 | $this->ufc->addChild($cond); | |
61 | } | |
62 | ||
59c6cb70 RB |
63 | public function addOrder(PlFilterOrder &$order) |
64 | { | |
65 | $this->order[] = $order; | |
66 | } | |
67 | ||
d9b3d712 RB |
68 | public function isValid() |
69 | { | |
70 | $this->buildUFC(); | |
71 | return $this->valid; | |
72 | } | |
73 | ||
6faa0186 RB |
74 | public function isEmpty() |
75 | { | |
76 | $this->buildUFC(); | |
77 | foreach ($this->fields as $field) { | |
78 | if (! $field->isEmpty()) { | |
79 | return false; | |
80 | } | |
81 | } | |
82 | return true; | |
83 | } | |
84 | ||
d9b3d712 RB |
85 | /** Returns the built UFC |
86 | * @return The UFC, or PFC_False() if an error happened | |
87 | */ | |
3314838e | 88 | public function getUFC() |
d9b3d712 RB |
89 | { |
90 | $this->buildUFC(); | |
91 | if ($this->valid) { | |
92 | return $this->ufc; | |
93 | } else { | |
94 | return new PFC_False(); | |
95 | } | |
96 | } | |
97 | ||
59c6cb70 RB |
98 | /** Returns adequate orders |
99 | */ | |
100 | public function getOrders() | |
101 | { | |
102 | $this->buildUFC(); | |
103 | return $this->orders; | |
104 | } | |
105 | ||
d9b3d712 RB |
106 | /** Wrappers around Env::i/s/..., to add envprefix |
107 | */ | |
0f17825b FB |
108 | public function s($key, $def = '') |
109 | { | |
110 | return Env::s($this->envprefix . $key, $def); | |
111 | } | |
112 | ||
113 | public function t($key, $def = '') | |
114 | { | |
115 | return Env::t($this->envprefix . $key, $def); | |
d9b3d712 RB |
116 | } |
117 | ||
0f17825b FB |
118 | public function i($key, $def = 0) |
119 | { | |
05fa89a5 | 120 | return Env::i($this->envprefix . $key, $def); |
d9b3d712 RB |
121 | } |
122 | ||
0f17825b FB |
123 | public function v($key, $def = null) |
124 | { | |
d9b3d712 RB |
125 | return Env::v($this->envprefix . $key, $def); |
126 | } | |
127 | ||
0f17825b FB |
128 | public function b($key, $def = false) |
129 | { | |
130 | return Env::b($this->envprefix . $key, $def); | |
131 | } | |
132 | ||
133 | public function has($key) | |
134 | { | |
135 | return Env::has($this->envprefix . $key); | |
136 | } | |
137 | ||
138 | public function blank($key, $strict = false) | |
139 | { | |
140 | return Env::blank($key, $strict); | |
d9b3d712 | 141 | } |
d9696b0a | 142 | |
78be0329 FB |
143 | public function hasAlnum($key) |
144 | { | |
145 | $str = $this->s($key); | |
146 | return preg_match('/[a-z0-9]/i', $str); | |
147 | } | |
148 | ||
149 | public function hasAlpha($key) | |
150 | { | |
151 | $str = $this->s($key); | |
152 | return preg_match('/[a-z]/i', $str); | |
153 | } | |
154 | ||
0f17825b FB |
155 | public function isOn($key) |
156 | { | |
157 | return $this->has($key) && $this->t($key) == 'on'; | |
d9696b0a | 158 | } |
d9b3d712 | 159 | } |
21b67462 | 160 | // }}} |
d9b3d712 | 161 | |
59c6cb70 RB |
162 | // {{{ class UFB_QuickSearch |
163 | class UFB_QuickSearch extends UserFilterBuilder | |
164 | { | |
165 | public function __construct($envprefix = '') | |
166 | { | |
167 | $fields = array( | |
168 | new UFBF_Quick('quick', 'Recherche rapide'), | |
169 | ); | |
170 | parent::__construct($fields, $envprefix); | |
171 | } | |
172 | } | |
173 | // }}} | |
174 | ||
21b67462 RB |
175 | // {{{ class UFB_AdvancedSearch |
176 | class UFB_AdvancedSearch extends UserFilterBuilder | |
177 | { | |
178 | public function __construct($envprefix = '') | |
179 | { | |
180 | $fields = array( | |
181 | new UFBF_Name('name', 'Nom'), | |
182 | new UFBF_Promo('promo1', 'Promotion', 'egal1'), | |
183 | new UFBF_Promo('promo2', 'Promotion', 'egal2'), | |
184 | new UFBF_Sex('woman', 'Sexe'), | |
185 | new UFBF_Registered('subscriber', 'Inscrit'), | |
186 | new UFBF_Dead('alive', 'En vie'), | |
187 | ||
188 | new UFBF_Town('city', 'Ville / Code Postal'), | |
189 | new UFBF_Country('countryTxt', 'country', 'Pays'), | |
190 | new UFBF_AdminArea('region', 'Région'), | |
191 | ||
192 | new UFBF_JobCompany('entreprise', 'Entreprise'), | |
21b67462 RB |
193 | new UFBF_JobDescription('jobdescription', 'Fonction'), |
194 | new UFBF_JobCv('cv', 'CV'), | |
3ac45f10 | 195 | new UFBF_JobTerms('jobterm', 'Mots-clefs'), |
21b67462 RB |
196 | |
197 | new UFBF_Nationality('nationaliteTxt', 'nationalite', 'Nationalité'), | |
198 | new UFBF_Binet('binetTxt', 'binet', 'Binet'), | |
199 | new UFBF_Group('groupexTxt', 'groupex', 'Groupe X'), | |
200 | new UFBF_Section('sectionTxt', 'section', 'Section'), | |
201 | ||
fb3b6547 RB |
202 | new UFBF_EducationSchool('schoolTxt', 'school', "École d'application"), |
203 | new UFBF_EducationDegree('diplomaTxt', 'diploma', 'Diplôme'), | |
204 | new UFBF_EducationField('fieldTxt', 'field', "Domaine d'études"), | |
21b67462 RB |
205 | |
206 | new UFBF_Comment('free', 'Commentaire'), | |
3ed7556a RB |
207 | new UFBF_Phone('phone_number', 'Téléphone'), |
208 | new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'), | |
96f01fba RB |
209 | |
210 | new UFBF_Mentor('only_referent', 'Référent'), | |
21b67462 RB |
211 | ); |
212 | parent::__construct($fields, $envprefix); | |
213 | } | |
214 | } | |
215 | // }}} | |
216 | ||
6faa0186 RB |
217 | // {{{ class UFB_MentorSearch |
218 | class UFB_MentorSearch extends UserFilterBuilder | |
219 | { | |
220 | public function __construct($envprefix = '') | |
221 | { | |
222 | $fields = array( | |
459e6f81 PC |
223 | new UFBF_MentorCountry('country'), |
224 | new UFBF_MentorTerm('jobterm', 'jobtermText'), | |
6faa0186 RB |
225 | new UFBF_MentorExpertise('expertise'), |
226 | ); | |
227 | parent::__construct($fields, $envprefix); | |
228 | } | |
229 | } | |
230 | // }}} | |
231 | ||
21b67462 | 232 | // {{{ class UFB_Field |
d9b3d712 RB |
233 | abstract class UFB_Field |
234 | { | |
235 | protected $envfield; | |
236 | protected $formtext; | |
237 | ||
238 | protected $empty = false; | |
239 | protected $val = null; | |
240 | ||
241 | /** Constructor | |
242 | * @param $envfield Name of the field in the environment | |
243 | * @param $formtext User-friendly name of that field | |
244 | */ | |
245 | public function __construct($envfield, $formtext = '') | |
246 | { | |
247 | $this->envfield = $envfield; | |
248 | if ($formtext != '') { | |
249 | $this->formtext = $formtext; | |
250 | } else { | |
251 | $formtext = ucfirst($envfield); | |
252 | } | |
253 | } | |
254 | ||
255 | /** Prints the given error message to the user, and returns false | |
256 | * in order to be used as return $this->raise('ERROR'); | |
257 | * | |
258 | * All %s in the $msg will be replaced with the formtext. | |
259 | */ | |
260 | protected function raise($msg) | |
261 | { | |
262 | Platal::page()->trigError(str_replace('%s', $this->formtext, $msg)); | |
263 | return false; | |
264 | } | |
265 | ||
266 | public function apply(UserFilterBuilder &$ufb) { | |
267 | if (!$this->check($ufb)) { | |
268 | return false; | |
269 | } | |
270 | ||
271 | if (!$this->empty) { | |
272 | $ufc = $this->buildUFC($ufb); | |
273 | if ($ufc != null) { | |
274 | $ufb->addCond($ufc); | |
275 | } | |
276 | } | |
277 | return true; | |
278 | } | |
279 | ||
6faa0186 RB |
280 | public function isEmpty() |
281 | { | |
282 | return $this->empty; | |
283 | } | |
284 | ||
d9b3d712 RB |
285 | /** Create the UFC associated to the field; won't be called |
286 | * if the field is "empty" | |
287 | * @param &$ufb UFB to which fields must be added | |
288 | * @return UFC | |
289 | */ | |
290 | abstract protected function buildUFC(UserFilterBuilder &$ufb); | |
291 | ||
292 | /** This function is intended to run consistency checks on the value | |
293 | * @return boolean Whether the input is valid | |
294 | */ | |
295 | abstract protected function check(UserFilterBuilder &$ufb); | |
296 | } | |
21b67462 | 297 | // }}} |
d9b3d712 | 298 | |
21b67462 | 299 | // {{{ class UFBF_Text |
d9b3d712 RB |
300 | abstract class UFBF_Text extends UFB_Field |
301 | { | |
d9b3d712 RB |
302 | private $minlength; |
303 | private $maxlength; | |
304 | ||
f3f800d8 | 305 | public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255) |
d9b3d712 RB |
306 | { |
307 | parent::__construct($envfield, $formtext); | |
d9b3d712 RB |
308 | $this->minlength = $minlength; |
309 | $this->maxlength = $maxlength; | |
310 | } | |
311 | ||
312 | protected function check(UserFilterBuilder &$ufb) | |
313 | { | |
0f17825b | 314 | if ($ufb->blank($this->envfield)) { |
d9b3d712 RB |
315 | $this->empty = true; |
316 | return true; | |
317 | } | |
318 | ||
0f17825b | 319 | $this->val = $ufb->t($this->envfield); |
d9b3d712 RB |
320 | if (strlen($this->val) < $this->minlength) { |
321 | return $this->raise("Le champ %s est trop court (minimum {$this->minlength})."); | |
322 | } else if (strlen($this->val) > $this->maxlength) { | |
323 | return $this->raise("Le champ %s est trop long (maximum {$this->maxlength})."); | |
5a0b9531 | 324 | } else if (preg_match(":[\]\[<>{}~§_`|%$^=]|\*\*:u", $this->val)) { |
f3f800d8 | 325 | return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.'); |
d9b3d712 | 326 | } |
f3f800d8 | 327 | |
d9b3d712 RB |
328 | return true; |
329 | } | |
330 | } | |
21b67462 | 331 | // }}} |
d9b3d712 | 332 | |
21b67462 | 333 | // {{{ class UFBF_Range |
d9b3d712 RB |
334 | /** Subclass to use for fields which only allow integers within a range |
335 | */ | |
336 | abstract class UFBF_Range extends UFB_Field | |
337 | { | |
338 | ||
339 | private $min; | |
340 | private $max; | |
341 | ||
342 | public function __construct($envfield, $formtext = '', $min = 0, $max = 65535) | |
343 | { | |
344 | parent::__construct($envfield, $formtext); | |
345 | $this->min = $min; | |
346 | $this->max = $max; | |
347 | } | |
348 | ||
349 | protected function check(UserFilterBuilder &$ufb) | |
350 | { | |
0f17825b | 351 | if ($ufb->blank($this->envfield)) { |
d9b3d712 RB |
352 | $this->empty = true; |
353 | return true; | |
354 | } | |
355 | ||
356 | $this->val = $ufb->i($this->envfield); | |
357 | if ($this->val < $this->min) { | |
358 | return $this->raise("Le champs %s est inférieur au minimum ({$this->min})."); | |
359 | } else if ($this->val > $this->max) { | |
360 | return $this->raise("Le champ %s est supérieur au maximum ({$this->max})."); | |
361 | } | |
362 | return true; | |
363 | } | |
364 | } | |
21b67462 | 365 | // }}} |
d9b3d712 | 366 | |
21b67462 | 367 | // {{{ class UFBF_Index |
d9b3d712 RB |
368 | /** Subclass to use for indexed fields |
369 | */ | |
370 | abstract class UFBF_Index extends UFB_Field | |
371 | { | |
372 | protected function check(UserFilterBuilder &$ufb) | |
373 | { | |
0f17825b | 374 | if ($ufb->blank($this->envfield)) { |
d9b3d712 RB |
375 | $this->empty = true; |
376 | } | |
6faa0186 | 377 | $this->val = $ufb->i($this->envfield); |
d9b3d712 RB |
378 | return true; |
379 | } | |
380 | } | |
21b67462 | 381 | // }}} |
d9b3d712 | 382 | |
21b67462 | 383 | // {{{ class UFBF_Enum |
d9b3d712 RB |
384 | /** Subclass to use for fields whose value must belong to a specific set of values |
385 | */ | |
386 | abstract class UFBF_Enum extends UFB_Field | |
387 | { | |
21b67462 RB |
388 | protected $allowedvalues; |
389 | ||
390 | public function __construct($envfield, $formtext = '', $allowedvalues = array(), $strict = false) | |
d9b3d712 RB |
391 | { |
392 | parent::__construct($envfield, $formtext); | |
393 | $this->allowedvalues = $allowedvalues; | |
21b67462 | 394 | $this->strict = $strict; |
d9b3d712 RB |
395 | } |
396 | ||
397 | protected function check(UserFilterBuilder &$ufb) | |
398 | { | |
0f17825b | 399 | if ($ufb->blank($this->envfield)) { |
d9b3d712 RB |
400 | $this->empty = true; |
401 | return true; | |
402 | } | |
403 | ||
404 | $this->val = $ufb->v($this->envfield); | |
405 | if (! in_array($this->val, $this->allowedvalues)) { | |
21b67462 RB |
406 | if ($this->strict) { |
407 | return $this->raise("La valeur {$this->val} n'est pas valide pour le champ %s."); | |
408 | } else { | |
409 | $this->empty = true; | |
410 | } | |
d9b3d712 RB |
411 | } |
412 | return true; | |
413 | } | |
414 | } | |
21b67462 | 415 | // }}} |
d9b3d712 | 416 | |
21b67462 RB |
417 | // {{{ class UFBF_Bool |
418 | abstract class UFBF_Bool extends UFB_Field | |
d9b3d712 | 419 | { |
21b67462 RB |
420 | protected function check(UserFilterBuilder &$ufb) |
421 | { | |
0f17825b | 422 | if ($ufb->blank($this->envfield)) { |
21b67462 RB |
423 | $this->empty = true; |
424 | return true; | |
425 | } | |
426 | ||
0f17825b | 427 | $this->val = $ufb->b($this->envfield); |
21b67462 RB |
428 | return true; |
429 | } | |
430 | } | |
431 | // }}} | |
d9b3d712 | 432 | |
21b67462 RB |
433 | // {{{ class UFBF_Mixed |
434 | /** A class for building UFBFs when the user can input either a text or an ID | |
435 | */ | |
436 | abstract class UFBF_Mixed extends UFB_Field | |
437 | { | |
438 | /** Name of the DirEnum on which class is based | |
439 | */ | |
440 | protected $direnum; | |
441 | ||
442 | protected $envfieldindex; | |
443 | ||
444 | public function __construct($envfieldtext, $envfieldindex, $formtext = '') | |
d9b3d712 | 445 | { |
21b67462 RB |
446 | parent::__construct($envfieldtext, $formtext); |
447 | $this->envfieldindex = $envfieldindex; | |
d9b3d712 RB |
448 | } |
449 | ||
21b67462 | 450 | protected function check(UserFilterBuilder &$ufb) |
d9b3d712 | 451 | { |
78be0329 | 452 | if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) { |
21b67462 RB |
453 | $this->empty = true; |
454 | return true; | |
455 | } | |
456 | ||
0f17825b | 457 | if (!$ufb->blank($this->envfieldindex)) { |
21b67462 RB |
458 | $index = $ufb->v($this->envfieldindex); |
459 | if (is_int($index)) { | |
460 | $index = intval($index); | |
461 | } else { | |
462 | $index = strtoupper($index); | |
463 | } | |
464 | $this->val = array($index); | |
d9b3d712 | 465 | } else { |
aab2ffdd | 466 | $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield), |
0f17825b | 467 | $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS); |
21b67462 RB |
468 | if (count($indexes) == 0) { |
469 | return false; | |
470 | } | |
471 | $this->val = $indexes; | |
d9b3d712 | 472 | } |
21b67462 | 473 | return true; |
d9b3d712 RB |
474 | } |
475 | } | |
21b67462 | 476 | // }}} |
d9b3d712 | 477 | |
f3f800d8 | 478 | // {{{ class UFBF_Quick |
59c6cb70 RB |
479 | class UFBF_Quick extends UFB_Field |
480 | { | |
481 | protected function check(UserFilterBuilder &$ufb) | |
482 | { | |
0f17825b | 483 | if ($ufb->blank($this->envfield)) { |
59c6cb70 RB |
484 | $this->empty = true; |
485 | return true; | |
486 | } | |
487 | ||
0f17825b | 488 | $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield))); |
4b2e2074 RB |
489 | |
490 | return true; | |
59c6cb70 RB |
491 | } |
492 | ||
493 | protected function buildUFC(UserFilterBuilder &$ufb) | |
494 | { | |
59c6cb70 | 495 | |
4b2e2074 | 496 | $r = $s = $this->val; |
59c6cb70 RB |
497 | |
498 | /** Admin: Email, IP | |
499 | */ | |
500 | if (S::admin() && strpos($s, '@') !== false) { | |
4b2e2074 | 501 | return new UFC_Email($s); |
59c6cb70 | 502 | } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) { |
0c457792 | 503 | return new UFC_Ip($s); |
59c6cb70 RB |
504 | } |
505 | ||
4b2e2074 RB |
506 | $conds = new PFC_And(); |
507 | ||
59c6cb70 RB |
508 | /** Name |
509 | */ | |
510 | $s = preg_replace('!\d+!', ' ', $s); | |
511 | $strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY); | |
512 | if (count($strings) > 5) { | |
513 | Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte"); | |
514 | $strings = array_slice($strings, 0, 5); | |
515 | } | |
516 | ||
517 | if (count($strings)) { | |
518 | if (S::logged()) { | |
519 | $flags = array(); | |
520 | } else { | |
521 | $flags = array('public'); | |
522 | } | |
0f17825b | 523 | if ($ufb->b('with_soundex')) { |
59c6cb70 RB |
524 | $soundex = true; |
525 | $st = array(); | |
526 | foreach ($strings as $string) { | |
527 | $st[] = soundex_fr($string); | |
528 | } | |
529 | } else { | |
530 | $soundex = false; | |
531 | $st = $strings; | |
532 | } | |
0f17825b | 533 | $exact =$ufb->b('exact'); |
59c6cb70 RB |
534 | $conds->addChild(new UFC_NameTokens($st, $flags, $soundex, $exact)); |
535 | ||
4b2e2074 | 536 | $ufb->addOrder(new UFO_Score()); |
59c6cb70 RB |
537 | } |
538 | ||
539 | /** Promo ranges | |
540 | */ | |
541 | $s = preg_replace('! *- *!', '-', $r); | |
542 | $s = preg_replace('!([<>]) *!', ' \1', $s); | |
543 | $s = preg_replace('![^0-9\-><]!', ' ', $s); | |
544 | $s = preg_replace('![<>\-] !', '', $s); | |
545 | $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY); | |
546 | foreach ($ranges as $r) { | |
547 | if (preg_match('!^\d{4}$!', $r)) { | |
548 | $conds->addChild(new UFC_Promo('=', UserFilter::DISPLAY, 'X' . $r)); | |
549 | } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) { | |
550 | $p1=min(intval($matches[1]), intval($matches[2])); | |
551 | $p2=max(intval($matches[1]), intval($matches[2])); | |
552 | $conds->addChild(new PFC_And( | |
553 | new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $p1), | |
554 | new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $p2) | |
555 | )); | |
556 | } elseif (preg_match('!^<(\d{4})!', $r, $matches)) { | |
557 | $conds->addChild(new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $matches[1])); | |
558 | } elseif (preg_match('!^>(\d{4})!', $r, $matches)) { | |
4b2e2074 | 559 | $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1])); |
59c6cb70 RB |
560 | } |
561 | } | |
562 | ||
563 | /** Phone number | |
564 | */ | |
565 | $t = preg_replace('!(\d{4}-\d{4}|>\d{4}|<\d{4})!', '', $s); | |
566 | $t = preg_replace('![<>\- ]!', '', $t); | |
567 | if (strlen($t) > 4) { | |
568 | $conds->addChild(new UFC_Phone($t)); | |
569 | } | |
570 | ||
571 | return $conds; | |
572 | } | |
573 | } | |
574 | // }}} | |
575 | ||
21b67462 RB |
576 | // {{{ class UFBF_Name |
577 | class UFBF_Name extends UFBF_Text | |
578 | { | |
579 | protected function check(UserFilterBuilder &$ufb) | |
580 | { | |
581 | if (!parent::check($ufb)) { | |
582 | return false; | |
583 | } | |
584 | ||
585 | $this->val = preg_split('/[[:space:]]/', $this->val); | |
586 | if (count($this->val) == 0) { | |
587 | $this->empty = true; | |
588 | } | |
589 | return true; | |
590 | } | |
591 | ||
592 | protected function buildUFC(UserFilterBuilder &$ufb) | |
593 | { | |
0f17825b | 594 | return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact')); |
21b67462 RB |
595 | } |
596 | } | |
597 | // }}} | |
598 | ||
599 | // {{{ class UFBF_Promo | |
d9b3d712 RB |
600 | class UFBF_Promo extends UFB_Field |
601 | { | |
602 | private static $validcomps = array('<', '<=', '=', '>=', '>'); | |
603 | private $comp; | |
604 | private $envfieldcomp; | |
605 | ||
606 | public function __construct($envfield, $fromtext = '', $envfieldcomp) | |
607 | { | |
608 | parent::__construct($envfield, $fromtext); | |
609 | $this->envfieldcomp = $envfieldcomp; | |
610 | } | |
611 | ||
612 | protected function check(UserFilterBuilder &$ufb) | |
613 | { | |
0f17825b | 614 | if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp)) { |
d9b3d712 RB |
615 | $this->empty = true; |
616 | return true; | |
617 | } | |
618 | ||
9e6b7376 RB |
619 | $this->val = $ufb->i($this->envfield); |
620 | $this->comp = $ufb->v($this->envfieldcomp); | |
d9b3d712 RB |
621 | |
622 | if (!in_array($this->comp, self::$validcomps)) { | |
623 | return $this->raise("Le critère {$this->comp} n'est pas valide pour le champ %s"); | |
624 | } | |
625 | ||
626 | if (preg_match('/^[0-9]{2}$/', $this->val)) { | |
627 | $this->val += 1900; | |
628 | } | |
629 | if ($this->val < 1900 || $this->val > 9999) { | |
630 | return $this->raise("Le champ %s doit être une année à 4 chiffres."); | |
631 | } | |
632 | return true; | |
633 | } | |
634 | ||
635 | protected function buildUFC(UserFilterBuilder &$ufb) { | |
999db5aa | 636 | return new UFC_Promo($this->comp, UserFilter::GRADE_ING, $this->val); |
d9b3d712 RB |
637 | } |
638 | } | |
21b67462 RB |
639 | // }}} |
640 | ||
641 | // {{{ class UFBF_Sex | |
642 | class UFBF_Sex extends UFBF_Enum | |
643 | { | |
644 | public function __construct($envfield, $formtext = '') | |
645 | { | |
646 | parent::__construct($envfield, $formtext, array(1, 2)); | |
647 | } | |
648 | ||
649 | private static function getVal($id) | |
650 | { | |
651 | switch($id) { | |
652 | case 1: | |
653 | return User::GENDER_MALE; | |
654 | break; | |
655 | case 2: | |
656 | return User::GENDER_FEMALE; | |
657 | break; | |
658 | } | |
659 | } | |
660 | ||
661 | protected function buildUFC(UserFilterBuilder &$ufb) | |
662 | { | |
663 | return new UFC_Sex(self::getVal($this->val)); | |
664 | } | |
665 | } | |
666 | // }}} | |
667 | ||
668 | // {{{ class UFBF_Registered | |
669 | class UFBF_Registered extends UFBF_Enum | |
670 | { | |
671 | public function __construct($envfield, $formtext = '') | |
672 | { | |
673 | parent::__construct($envfield, $formtext, array(1, 2)); | |
674 | } | |
675 | ||
676 | protected function buildUFC(UserFilterBuilder &$ufb) | |
677 | { | |
678 | if ($this->val == 1) { | |
679 | return new UFC_Registered(); | |
680 | } else if ($this->val == 2) { | |
e637cb69 | 681 | return new PFC_Not(new UFC_Registered()); |
21b67462 RB |
682 | } |
683 | } | |
684 | } | |
685 | // }}} | |
d9b3d712 | 686 | |
21b67462 RB |
687 | // {{{ class UFBF_Dead |
688 | class UFBF_Dead extends UFBF_Enum | |
689 | { | |
690 | public function __construct($envfield, $formtext = '') | |
691 | { | |
692 | parent::__construct($envfield, $formtext, array(1, 2)); | |
693 | } | |
694 | ||
695 | protected function buildUFC(UserFilterBuilder &$ufb) | |
696 | { | |
697 | if ($this->val == 1) { | |
2d9951d8 | 698 | return new PFC_Not(new UFC_Dead()); |
21b67462 RB |
699 | } else if ($this->val == 2) { |
700 | return new UFC_Dead(); | |
701 | } | |
702 | } | |
703 | } | |
704 | // }}} | |
705 | ||
706 | // {{{ class UFBF_Town | |
707 | /** Retrieves a town, either from a postal code or a town name | |
708 | */ | |
709 | class UFBF_Town extends UFBF_Text | |
710 | { | |
711 | const TYPE_TEXT = 1; | |
712 | const TYPE_ZIP = 2; | |
713 | const TYPE_ANY = 3; | |
714 | ||
715 | private $type; | |
d9696b0a RB |
716 | private $onlycurrentfield; |
717 | ||
718 | public function __construct($envfield, $formtext = '', $type = self::TYPE_ANY, $onlycurrentfield = 'only_current') | |
21b67462 RB |
719 | { |
720 | $this->type = $type; | |
d9696b0a | 721 | $this->onlycurrentfield = $onlycurrentfield; |
f3f800d8 | 722 | parent::__construct($envfield, $formtext, 2, 30); |
21b67462 RB |
723 | } |
724 | ||
725 | protected function buildUFC(UserFilterBuilder &$ufb) | |
726 | { | |
d9696b0a RB |
727 | if ($ufb->isOn($this->onlycurrentfield)) { |
728 | $flags = UFC_Address::FLAG_CURRENT; | |
729 | } else { | |
730 | $flags = UFC_Address::FLAG_ANY; | |
731 | } | |
732 | ||
21b67462 RB |
733 | if (preg_match('/[0-9]/', $this->val)) { |
734 | if ($this->type & self::TYPE_ZIP) { | |
d9696b0a | 735 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags); |
21b67462 RB |
736 | } else { |
737 | return new PFC_False(); | |
738 | } | |
739 | } else { | |
1f8dfc60 | 740 | $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val); |
d9696b0a | 741 | $byzip = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags); |
21b67462 RB |
742 | if ($this->type & self::TYPE_ANY) { |
743 | return new PFC_Or($byname, $byzip); | |
744 | } else if ($this->type & self::TYPE_TEXT) { | |
745 | return $byname; | |
746 | } else { | |
747 | return $byzip; | |
748 | } | |
749 | } | |
750 | } | |
751 | } | |
752 | // }}} | |
753 | ||
754 | // {{{ class UFBF_Country | |
755 | class UFBF_Country extends UFBF_Mixed | |
756 | { | |
757 | protected $direnum = DirEnum::COUNTRIES; | |
d9696b0a RB |
758 | protected $onlycurrentfield; |
759 | ||
760 | public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current') | |
761 | { | |
762 | parent::__construct($envfieldtext, $envfieldindex, $formtext); | |
763 | $this->onlycurrentfield = $onlycurrentfield; | |
764 | } | |
21b67462 RB |
765 | |
766 | protected function buildUFC(UserFilterBuilder &$ufb) | |
767 | { | |
d9696b0a RB |
768 | if ($ufb->isOn($this->onlycurrentfield)) { |
769 | $flags = UFC_Address::FLAG_CURRENT; | |
770 | } else { | |
771 | $flags = UFC_Address::FLAG_ANY; | |
772 | } | |
773 | ||
774 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_COUNTRY, UFC_Address::TYPE_ANY, $flags); | |
21b67462 RB |
775 | } |
776 | } | |
777 | // }}} | |
778 | ||
779 | // {{{ class UFBF_AdminArea | |
32283a1a | 780 | class UFBF_AdminArea extends UFBF_Index |
21b67462 RB |
781 | { |
782 | protected $direnum = DirEnum::ADMINAREAS; | |
d9696b0a RB |
783 | protected $onlycurrentfield; |
784 | ||
32283a1a | 785 | public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current') |
d9696b0a | 786 | { |
32283a1a | 787 | parent::__construct($envfield, $formtext); |
d9696b0a RB |
788 | $this->onlycurrentfield = $onlycurrentfield; |
789 | } | |
790 | ||
21b67462 RB |
791 | |
792 | protected function buildUFC(UserFilterBuilder &$ufb) | |
793 | { | |
d9696b0a RB |
794 | if ($ufb->isOn($this->onlycurrentfield)) { |
795 | $flags = UFC_Address::FLAG_CURRENT; | |
796 | } else { | |
797 | $flags = UFC_Address::FLAG_ANY; | |
798 | } | |
799 | ||
800 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ADMAREA, UFC_Address::TYPE_ANY, $flags); | |
21b67462 RB |
801 | } |
802 | } | |
803 | // }}} | |
804 | ||
805 | // {{{ class UFBF_JobCompany | |
806 | class UFBF_JobCompany extends UFBF_Text | |
807 | { | |
d9696b0a RB |
808 | private $onlymentorfield; |
809 | ||
810 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') | |
811 | { | |
812 | parent::__construct($envfield, $formtext); | |
813 | $this->onlymentorfield = $onlymentorfield; | |
814 | } | |
815 | ||
816 | public function check(UserFilterBuilder &$ufb) { | |
817 | if (parent::check($ufb)) { | |
818 | # No company check for mentors | |
819 | if ($ufb->isOn($this->onlymentorfield)) { | |
820 | $this->empty = true; | |
821 | } | |
822 | return true; | |
823 | } else { | |
824 | return false; | |
825 | } | |
826 | } | |
827 | ||
21b67462 RB |
828 | protected function buildUFC(UserFilterBuilder &$ufb) |
829 | { | |
830 | return new UFC_Job_Company(UFC_Job_Company::JOBNAME, $this->val); | |
831 | } | |
832 | } | |
833 | // }}} | |
834 | ||
3ac45f10 PC |
835 | // {{{ class UFBF_JobTerms |
836 | class UFBF_JobTerms extends UFBF_Index | |
837 | { | |
838 | protected function buildUFC(UserFilterBuilder &$ufb) | |
839 | { | |
840 | return new UFC_Job_Terms($this->val); | |
841 | } | |
842 | } | |
843 | // }}} | |
844 | ||
21b67462 RB |
845 | // {{{ class UFBF_JobDescription |
846 | class UFBF_JobDescription extends UFBF_Text | |
847 | { | |
d9696b0a RB |
848 | private $onlymentorfield; |
849 | ||
850 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') | |
851 | { | |
852 | parent::__construct($envfield, $formtext); | |
853 | $this->onlymentorfield = $onlymentorfield; | |
854 | } | |
855 | ||
21b67462 RB |
856 | protected function buildUFC(UserFilterBuilder &$ufb) |
857 | { | |
d9696b0a RB |
858 | if ($ufb->isOn($this->onlymentorfield)) { |
859 | return new UFC_Mentor_Expertise($this->val); | |
860 | } else { | |
861 | return new UFC_Job_Description($this->val, UserFilter::JOB_USERDEFINED); | |
862 | } | |
21b67462 RB |
863 | } |
864 | } | |
865 | // }}} | |
866 | ||
867 | // {{{ class UFBF_JobCv | |
868 | class UFBF_JobCv extends UFBF_Text | |
869 | { | |
d9696b0a RB |
870 | private $onlymentorfield; |
871 | ||
872 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') | |
873 | { | |
874 | parent::__construct($envfield, $formtext); | |
875 | $this->onlymentorfield = $onlymentorfield; | |
876 | } | |
877 | ||
21b67462 RB |
878 | protected function buildUFC(UserFilterBuilder &$ufb) |
879 | { | |
d9696b0a RB |
880 | if ($ufb->isOn($this->onlymentorfield)) { |
881 | return new UFC_Mentor_Expertise($this->val); | |
882 | } else { | |
883 | return new UFC_Job_Description($this->val, UserFilter::JOB_CV); | |
884 | } | |
21b67462 RB |
885 | } |
886 | } | |
887 | // }}} | |
888 | ||
889 | // {{{ class UFBF_Nationality | |
890 | class UFBF_Nationality extends UFBF_Mixed | |
891 | { | |
892 | protected $direnum = DirEnum::NATIONALITIES; | |
893 | ||
894 | protected function buildUFC(UserFilterBuilder &$ufb) | |
895 | { | |
896 | return new UFC_Nationality($this->val); | |
897 | } | |
898 | } | |
899 | // }}} | |
900 | ||
901 | // {{{ class UFBF_Binet | |
902 | class UFBF_Binet extends UFBF_Mixed | |
903 | { | |
904 | protected $direnum = DirEnum::BINETS; | |
905 | ||
906 | protected function buildUFC(UserFilterBuilder &$ufb) | |
907 | { | |
908 | return new UFC_Binet($this->val); | |
909 | } | |
910 | } | |
911 | // }}} | |
912 | ||
913 | // {{{ class UFBF_Group | |
914 | class UFBF_Group extends UFBF_Mixed | |
915 | { | |
916 | protected $direnum = DirEnum::GROUPESX; | |
917 | ||
918 | protected function buildUFC(UserFilterBuilder &$ufb) | |
919 | { | |
920 | if (count($this->val) == 1) { | |
921 | return new UFC_Group($this->val[0]); | |
922 | } | |
923 | ||
924 | $or = new PFC_Or(); | |
925 | foreach ($this->val as $grp) { | |
926 | $or->addChild(new UFC_Group($grp)); | |
927 | } | |
928 | return $or; | |
929 | } | |
930 | } | |
931 | // }}} | |
932 | ||
933 | // {{{ class UFBF_Section | |
934 | class UFBF_Section extends UFBF_Index | |
935 | { | |
936 | protected $direnum = DirEnum::SECTIONS; | |
937 | ||
938 | protected function buildUFC(UserFilterBuilder &$ufb) | |
939 | { | |
940 | return new UFC_Section($this->val); | |
941 | } | |
942 | } | |
943 | // }}} | |
944 | ||
fb3b6547 RB |
945 | // {{{ class UFBF_EducationSchool |
946 | class UFBF_EducationSchool extends UFBF_Mixed | |
21b67462 | 947 | { |
fb3b6547 | 948 | protected $direnum = DirEnum::EDUSCHOOLS; |
21b67462 RB |
949 | |
950 | protected function buildUFC(UserFilterBuilder &$ufb) | |
951 | { | |
fb3b6547 | 952 | return new UFC_EducationSchool($this->val); |
21b67462 RB |
953 | } |
954 | } | |
955 | // }}} | |
956 | ||
fb3b6547 RB |
957 | // {{{ class UFBF_EducationDegree |
958 | class UFBF_EducationDegree extends UFBF_Mixed | |
21b67462 | 959 | { |
fb3b6547 | 960 | protected $direnum = DirEnum::EDUDEGREES; |
21b67462 RB |
961 | |
962 | protected function buildUFC(UserFilterBuilder &$ufb) | |
963 | { | |
fb3b6547 | 964 | return new UFC_EducationDegree($this->val); |
21b67462 RB |
965 | } |
966 | } | |
967 | // }}} | |
968 | ||
fb3b6547 RB |
969 | // {{{ class UFBF_EducationField |
970 | class UFBF_EducationField extends UFBF_Mixed | |
21b67462 | 971 | { |
fb3b6547 | 972 | protected $direnum = DirEnum::EDUFIELDS; |
21b67462 RB |
973 | |
974 | protected function buildUFC(UserFilterBuilder &$ufb) | |
975 | { | |
fb3b6547 | 976 | return new UFC_EducationField($this->val); |
21b67462 RB |
977 | } |
978 | } | |
979 | // }}} | |
980 | ||
981 | // {{{ class UFBF_Comment | |
982 | class UFBF_Comment extends UFBF_Text | |
983 | { | |
984 | protected function buildUFC(UserFilterBuilder &$ufb) | |
985 | { | |
986 | return new UFC_Comment($this->val); | |
987 | } | |
988 | } | |
989 | // }}} | |
3ed7556a RB |
990 | |
991 | // {{{ class UFBF_Phone | |
992 | class UFBF_Phone extends UFBF_Text | |
993 | { | |
994 | protected function buildUFC(UserFilterBuilder &$ufb) | |
995 | { | |
996 | return new UFC_Phone($this->val); | |
997 | } | |
998 | } | |
999 | // }}} | |
1000 | ||
1001 | // {{{ class UFBF_Networking | |
1002 | class UFBF_Networking extends UFBF_Text | |
1003 | { | |
1004 | private $networktypefield; | |
1005 | private $nwtype; | |
1006 | ||
1007 | public function __construct($envfield, $networktypefield, $formtext = '') | |
1008 | { | |
1009 | parent::__construct($envfield, $formtext); | |
1010 | $this->networktypefield = $networktypefield; | |
1011 | } | |
1012 | ||
1013 | public function check(UserFilterBuilder &$ufb) | |
1014 | { | |
1015 | if (parent::check($ufb)) { | |
1016 | $this->nwtype = $ufb->i($this->networktypefield); | |
1017 | return true; | |
1018 | } else { | |
1019 | return false; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | public function buildUFC(UserFilterBuilder &$ufb) | |
1024 | { | |
1025 | return new UFC_Networking($this->nwtype, $this->val); | |
1026 | } | |
1027 | } | |
1028 | // }}} | |
6faa0186 | 1029 | |
96f01fba RB |
1030 | // {{{ class UFBF_Mentor |
1031 | class UFBF_Mentor extends UFBF_Bool | |
1032 | { | |
1033 | protected function buildUFC(UserFilterBuilder &$ufb) | |
1034 | { | |
1035 | return new UFC_Mentor(); | |
1036 | } | |
1037 | } | |
1038 | // }}} | |
1039 | ||
6faa0186 | 1040 | // {{{ class UFBF_MentorCountry |
459e6f81 | 1041 | class UFBF_MentorCountry extends UFBF_Text |
6faa0186 RB |
1042 | { |
1043 | protected function buildUFC(UserFilterBuilder &$ufb) | |
1044 | { | |
1045 | return new UFC_Mentor_Country($this->val); | |
1046 | } | |
1047 | } | |
1048 | // }}} | |
1049 | ||
459e6f81 PC |
1050 | // {{{ class UFBF_Mentorterm |
1051 | class UFBF_MentorTerm extends UFBF_Index | |
1052 | { | |
1053 | protected function buildUFC(UserFilterBuilder &$ufb) | |
1054 | { | |
1055 | return new UFC_Mentor_Terms($this->val); | |
1056 | } | |
1057 | } | |
1058 | // }}} | |
1059 | ||
6faa0186 RB |
1060 | // {{{ class UFBF_MentorExpertise |
1061 | class UFBF_MentorExpertise extends UFBF_Text | |
1062 | { | |
1063 | protected function buildUFC(UserFilterBuilder &$ufb) | |
1064 | { | |
1065 | return new UFC_Mentor_Expertise($this->val); | |
1066 | } | |
1067 | } | |
1068 | // }}} | |
05fa89a5 FB |
1069 | |
1070 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
d9b3d712 | 1071 | ?> |