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