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