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