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