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