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