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