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