Adds search on broken redirections (Closes #990).
[platal.git] / include / ufbuilder.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 StoredUserFilterBuilder
23 class StoredUserFilterBuilder
24 {
25 // Possible stored types (currently only 'ufb' exists)
26 const TYPE_UFB = 'ufb';
27
28 protected $ufb;
29 protected $env;
30 protected $ufc;
31
32 public function __construct(UserFilterBuilder $ufb, PlFilterCondition $ufc = null, array $env = array())
33 {
34 $this->ufb = $ufb;
35 $this->ufc = $ufc;
36 $this->env = $env;
37 }
38
39 public function export()
40 {
41 $export = new PlDict();
42 $export->set('type', self::TYPE_UFB);
43 $export->set('condition', $this->ufc->export());
44 $export->set('env', $this->env);
45 return $export;
46 }
47
48 public function getEnv()
49 {
50 return $this->env;
51 }
52
53 public function fillFromExport($export)
54 {
55 $export = new PlDict($export);
56 if (!$export->has('type')) {
57 throw new Exception("Missing 'type' field in export.");
58 }
59 if ($export->s('type') != self::TYPE_UFB) {
60 throw new Exception("Unknown type '$type' in export.");
61 }
62 $this->ufc = UserFilterCondition::fromExport($export->v('condition'));
63 $this->env = $export->v('env', array());
64 }
65
66 public function updateFromEnv($env)
67 {
68 $this->ufb->setFakeEnv($env);
69 if ($this->ufb->isValid()) {
70 $this->env = $env;
71 $this->ufc = $this->ufb->getUFC();
72 return true;
73 } else {
74 $this->ufb->clearFakeEnv();
75 return false;
76 }
77 }
78
79 public function refresh()
80 {
81 if ($this->isValid()) {
82 $this->ufc = $this->ufb->getUFC();
83 }
84 }
85
86 public function getUFC()
87 {
88 return $this->ufc;
89 }
90
91 public function isValid()
92 {
93 $this->ufb->setFakeEnv($this->env);
94 return $this->ufb->isValid();
95 }
96
97 public function isEmpty()
98 {
99 $this->ufb->setFakeEnv($this->env);
100 return $this->ufb->isEmpty();
101 }
102 }
103 // }}}
104
105 // {{{ class UserFilterBuilder
106 class UserFilterBuilder
107 {
108 private $envprefix;
109 private $fields;
110 private $valid = true;
111 private $ufc = null;
112 private $orders = array();
113 private $fake_env = null;
114
115 /** Constructor
116 * @param $fields An array of UFB_Field objects
117 * @param $envprefix Prefix to use for parts of the query
118 */
119 public function __construct($fields, $envprefix = '')
120 {
121 $this->fields = $fields;
122 $this->envprefix = $envprefix;
123 }
124
125 public function setFakeEnv($env)
126 {
127 $this->fake_env = new PlDict($env);
128 }
129
130 public function clearFakeEnv()
131 {
132 $this->fake_env = null;
133 }
134
135 /** Builds the UFC; returns as soon as a field says it is invalid
136 */
137 private function buildUFC()
138 {
139 if ($this->ufc != null) {
140 return;
141 }
142 $this->ufc = new PFC_And();
143
144 foreach ($this->fields as $field) {
145 $this->valid = $field->apply($this);
146 if (!$this->valid) {
147 return;
148 }
149 }
150 }
151
152 public function addCond(PlFilterCondition $cond)
153 {
154 $this->ufc->addChild($cond);
155 }
156
157 public function addOrder(PlFilterOrder $order)
158 {
159 $this->order[] = $order;
160 }
161
162 public function isValid()
163 {
164 $this->buildUFC();
165 return $this->valid;
166 }
167
168 public function isEmpty()
169 {
170 $this->buildUFC();
171 foreach ($this->fields as $field) {
172 if (! $field->isEmpty()) {
173 return false;
174 }
175 }
176 return true;
177 }
178
179 /** Returns the built UFC
180 * @return The UFC, or PFC_False() if an error happened
181 */
182 public function getUFC()
183 {
184 $this->buildUFC();
185 if ($this->valid) {
186 if ($this->isEmpty()) {
187 return new PFC_True();
188 } else {
189 return $this->ufc;
190 }
191 } else {
192 return new PFC_False();
193 }
194 }
195
196 /** Returns adequate orders
197 */
198 public function getOrders()
199 {
200 $this->buildUFC();
201 return $this->orders;
202 }
203
204 public function getEnvFieldNames()
205 {
206 $fields = array();
207 foreach ($this->fields as $ufbf) {
208 $fields = array_merge($fields, $ufbf->getEnvFieldNames());
209 }
210 return array_unique($fields);
211 }
212
213 public function getEnv()
214 {
215 $values = array();
216 foreach ($this->getEnvFieldNames() as $field) {
217 if ($this->has($field)) {
218 $values[$field] = $this->v($field);
219 }
220 }
221 return $values;
222 }
223
224 public function setEnv($values)
225 {
226 foreach ($this->getEnvFieldNames() as $field) {
227 if (array_key_exists($field, $values)) {
228 Env::set($this->envprefix . $field, $values[$field]);
229 }
230 }
231 }
232
233 /** Wrappers around Env::i/s/..., to add envprefix
234 */
235 public function s($key, $def = '')
236 {
237 if ($this->fake_env) {
238 return $this->fake_env->s($key, $def);
239 } else {
240 return Env::s($this->envprefix . $key, $def);
241 }
242 }
243
244 public function t($key, $def = '')
245 {
246 if ($this->fake_env) {
247 return $this->fake_env->t($key, $def);
248 } else {
249 return Env::t($this->envprefix . $key, $def);
250 }
251 }
252
253 public function i($key, $def = 0)
254 {
255 if ($this->fake_env) {
256 return $this->fake_env->i($key, $def);
257 } else {
258 return Env::i($this->envprefix . $key, $def);
259 }
260 }
261
262 public function v($key, $def = null)
263 {
264 if ($this->fake_env) {
265 return $this->fake_env->v($key, $def);
266 } else {
267 return Env::v($this->envprefix . $key, $def);
268 }
269 }
270
271 public function b($key, $def = false)
272 {
273 if ($this->fake_env) {
274 return $this->fake_env->b($key, $def);
275 } else {
276 return Env::b($this->envprefix . $key, $def);
277 }
278 }
279
280 public function has($key)
281 {
282 if ($this->fake_env) {
283 return $this->fake_env->has($key);
284 } else {
285 return Env::has($this->envprefix . $key);
286 }
287 }
288
289 public function blank($key, $strict = false)
290 {
291 if ($this->fake_env) {
292 return $this->fake_env->blank($key, $strict);
293 } else {
294 return Env::blank($key, $strict);
295 }
296 }
297
298 public function hasAlnum($key)
299 {
300 $str = $this->s($key);
301 return preg_match('/[a-z0-9]/i', $str);
302 }
303
304 public function hasAlpha($key)
305 {
306 $str = $this->s($key);
307 return preg_match('/[a-z]/i', $str);
308 }
309
310 public function isOn($key)
311 {
312 return $this->has($key) && $this->t($key) == 'on';
313 }
314 }
315 // }}}
316
317 // {{{ class UFB_QuickSearch
318 class UFB_QuickSearch extends UserFilterBuilder
319 {
320 public function __construct($envprefix = '')
321 {
322 $fields = array(
323 new UFBF_Quick('quick', 'Recherche rapide'),
324 new UFBF_NotRegistered('nonins', 'Non inscrits'),
325 );
326 parent::__construct($fields, $envprefix);
327 }
328 }
329 // }}}
330
331 // {{{ class UFB_AdvancedSearch
332 class UFB_AdvancedSearch extends UserFilterBuilder
333 {
334 /** Create a UFB_AdvancedSearch.
335 * @param $include_admin Whether to include 'admin-only' fields
336 * @param $include_ax Whether to include 'ax-only' fields
337 * @param $envprefix Optional prefix for form field names.
338 */
339 public function __construct($include_admin = false, $include_ax = false, $envprefix = '')
340 {
341 $fields = array(
342 new UFBF_Name('name', 'Nom'),
343 new UFBF_Promo('promo1', 'Promotion', 'egal1'),
344 new UFBF_Promo('promo2', 'Promotion', 'egal2'),
345 new UFBF_Sex('woman', 'Sexe'),
346 new UFBF_Registered('subscriber', 'Inscrit'),
347 new UFBF_HasEmailRedirect('has_email_redirect', 'A une redirection active'),
348 new UFBF_Dead('alive', 'En vie'),
349
350 new UFBF_Town('city', 'Ville / Code Postal'),
351 new UFBF_Country('countryTxt', 'country', 'Pays'),
352 new UFBF_AdminArea('administrativearea', 'Région'),
353 new UFBF_SubAdminArea('subadministrativearea', 'Département'),
354
355 new UFBF_JobCompany('entreprise', 'Entreprise'),
356 new UFBF_JobDescription('jobdescription', 'Fonction'),
357 new UFBF_JobCv('cv', 'CV'),
358 new UFBF_JobTerms('jobterm', 'Mots-clefs'),
359
360 new UFBF_Nationality('nationaliteTxt', 'nationalite', 'Nationalité'),
361 new UFBF_Binet('binetTxt', 'binet', 'Binet'),
362 new UFBF_Group('groupexTxt', 'groupex', 'Groupe X'),
363 new UFBF_Section('sectionTxt', 'section', 'Section'),
364
365 new UFBF_EducationSchool('schoolTxt', 'school', "École d'application"),
366 new UFBF_EducationDegree('diplomaTxt', 'diploma', 'Diplôme'),
367 new UFBF_EducationField('fieldTxt', 'field', "Domaine d'études"),
368
369 new UFBF_Comment('free', 'Commentaire'),
370 new UFBF_Phone('phone_number', 'Téléphone'),
371 new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'),
372
373 new UFBF_Mentor('only_referent', 'Référent'),
374 );
375
376 if ($include_admin || $include_ax) {
377 $fields[] = new UFBF_SchoolIds('schoolid_ax', 'Matricule AX', UFC_SchoolId::AX);
378 }
379
380 parent::__construct($fields, $envprefix);
381 }
382 }
383 // }}}
384
385 // {{{ class UFB_MentorSearch
386 class UFB_MentorSearch extends UserFilterBuilder
387 {
388 public function __construct($envprefix = '')
389 {
390 $fields = array(
391 new UFBF_MentorCountry('country'),
392 new UFBF_MentorTerm('jobterm', 'jobtermText'),
393 new UFBF_MentorExpertise('expertise'),
394 );
395 parent::__construct($fields, $envprefix);
396 }
397 }
398 // }}}
399
400 // {{{ class UFB_NewsLetter
401 class UFB_NewsLetter extends UserFilterBuilder
402 {
403 const FIELDS_PROMO = 'promo';
404 const FIELDS_AXID = 'axid';
405 const FIELDS_GEO = 'geo';
406
407 public function __construct($flags, $envprefix = '')
408 {
409 $fields = array();
410 if ($flags->hasFlag(self::FIELDS_PROMO)) {
411 $fields[] = new UFBF_Promo('promo1', 'Promotion', 'egal1');
412 $fields[] = new UFBF_Promo('promo2', 'Promotion', 'egal2');
413 }
414 if ($flags->hasFlag(self::FIELDS_AXID)) {
415 $fields[] = new UFBF_SchoolIds('axid', 'Matricule AX', UFC_SchoolId::AX);
416 }
417 parent::__construct($fields, $envprefix);
418 }
419 }
420 // }}}
421
422 // {{{ class UFB_Field
423 abstract class UFB_Field
424 {
425 protected $envfield;
426 protected $formtext;
427
428 protected $empty = false;
429 protected $val = null;
430
431 /** Constructor
432 * @param $envfield Name of the field in the environment
433 * @param $formtext User-friendly name of that field
434 */
435 public function __construct($envfield, $formtext = '')
436 {
437 $this->envfield = $envfield;
438 if ($formtext != '') {
439 $this->formtext = $formtext;
440 } else {
441 $formtext = ucfirst($envfield);
442 }
443 }
444
445 /** Prints the given error message to the user, and returns false
446 * in order to be used as return $this->raise('ERROR');
447 *
448 * All %s in the $msg will be replaced with the formtext.
449 */
450 protected function raise($msg)
451 {
452 Platal::page()->trigError(str_replace('%s', $this->formtext, $msg));
453 return false;
454 }
455
456 public function apply(UserFilterBuilder $ufb) {
457 if (!$this->check($ufb)) {
458 return false;
459 }
460
461 if (!$this->isEmpty()) {
462 $ufc = $this->buildUFC($ufb);
463 if ($ufc != null) {
464 $ufb->addCond($ufc);
465 }
466 }
467 return true;
468 }
469
470 public function isEmpty()
471 {
472 return $this->empty;
473 }
474
475 /** Create the UFC associated to the field; won't be called
476 * if the field is "empty"
477 * @param $ufb UFB to which fields must be added
478 * @return UFC
479 */
480 abstract protected function buildUFC(UserFilterBuilder $ufb);
481
482 /** This function is intended to run consistency checks on the value
483 * @return boolean Whether the input is valid
484 */
485 abstract protected function check(UserFilterBuilder $ufb);
486
487 // Simple form interface
488
489 /** Retrieve a list of env field names used by that field
490 * their values will be recorded when saving the 'search' and used to prefill the form
491 * when needed.
492 */
493 public function getEnvFieldNames()
494 {
495 return array($this->envfield);
496 }
497 }
498 // }}}
499
500 // {{{ class UFBF_Text
501 abstract class UFBF_Text extends UFB_Field
502 {
503 private $minlength;
504 private $maxlength;
505
506 public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255)
507 {
508 parent::__construct($envfield, $formtext);
509 $this->minlength = $minlength;
510 $this->maxlength = $maxlength;
511 }
512
513 protected function check(UserFilterBuilder $ufb)
514 {
515 if ($ufb->blank($this->envfield)) {
516 $this->empty = true;
517 return true;
518 }
519
520 $this->val = $ufb->t($this->envfield);
521 if (strlen($this->val) < $this->minlength) {
522 return $this->raise("Le champ %s est trop court (minimum {$this->minlength}).");
523 } else if (strlen($this->val) > $this->maxlength) {
524 return $this->raise("Le champ %s est trop long (maximum {$this->maxlength}).");
525 } else if (preg_match(":[\]\[<>{}~§_`|%$^=]|\*\*:u", $this->val)) {
526 return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.');
527 }
528
529 return true;
530 }
531 }
532 // }}}
533
534 // {{{ class UFBF_Range
535 /** Subclass to use for fields which only allow integers within a range
536 */
537 abstract class UFBF_Range extends UFB_Field
538 {
539
540 private $min;
541 private $max;
542
543 public function __construct($envfield, $formtext = '', $min = 0, $max = 65535)
544 {
545 parent::__construct($envfield, $formtext);
546 $this->min = $min;
547 $this->max = $max;
548 }
549
550 protected function check(UserFilterBuilder $ufb)
551 {
552 if ($ufb->blank($this->envfield)) {
553 $this->empty = true;
554 return true;
555 }
556
557 $this->val = $ufb->i($this->envfield);
558 if ($this->val < $this->min) {
559 return $this->raise("Le champs %s est inférieur au minimum ({$this->min}).");
560 } else if ($this->val > $this->max) {
561 return $this->raise("Le champ %s est supérieur au maximum ({$this->max}).");
562 }
563 return true;
564 }
565 }
566 // }}}
567
568 // {{{ class UFBF_Index
569 /** Subclass to use for indexed fields
570 */
571 abstract class UFBF_Index extends UFB_Field
572 {
573 protected function check(UserFilterBuilder $ufb)
574 {
575 if ($ufb->blank($this->envfield)) {
576 $this->empty = true;
577 }
578 $this->val = $ufb->i($this->envfield);
579 return true;
580 }
581 }
582 // }}}
583
584 // {{{ class UFBF_Enum
585 /** Subclass to use for fields whose value must belong to a specific set of values
586 */
587 abstract class UFBF_Enum extends UFB_Field
588 {
589 protected $allowedvalues;
590
591 public function __construct($envfield, $formtext = '', $allowedvalues = array(), $strict = false)
592 {
593 parent::__construct($envfield, $formtext);
594 $this->allowedvalues = $allowedvalues;
595 $this->strict = $strict;
596 }
597
598 protected function check(UserFilterBuilder $ufb)
599 {
600 if ($ufb->blank($this->envfield)) {
601 $this->empty = true;
602 return true;
603 }
604
605 $this->val = $ufb->v($this->envfield);
606 if (! in_array($this->val, $this->allowedvalues)) {
607 if ($this->strict) {
608 return $this->raise("La valeur {$this->val} n'est pas valide pour le champ %s.");
609 } else {
610 $this->empty = true;
611 }
612 }
613 return true;
614 }
615 }
616 // }}}
617
618 // {{{ class UFBF_Bool
619 abstract class UFBF_Bool extends UFB_Field
620 {
621 protected function check(UserFilterBuilder $ufb)
622 {
623 if ($ufb->blank($this->envfield)) {
624 $this->empty = true;
625 return true;
626 }
627
628 $this->val = $ufb->b($this->envfield);
629 return true;
630 }
631 }
632 // }}}
633
634 // {{{ class UFBF_Mixed
635 /** A class for building UFBFs when the user can input either a text or an ID
636 */
637 abstract class UFBF_Mixed extends UFB_Field
638 {
639 /** Name of the DirEnum on which class is based
640 */
641 protected $direnum;
642
643 protected $envfieldindex;
644
645 public function __construct($envfieldtext, $envfieldindex, $formtext = '')
646 {
647 parent::__construct($envfieldtext, $formtext);
648 $this->envfieldindex = $envfieldindex;
649 }
650
651 protected function check(UserFilterBuilder $ufb)
652 {
653 if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) {
654 $this->empty = true;
655 return true;
656 }
657
658 if (!$ufb->blank($this->envfieldindex)) {
659 $index = $ufb->v($this->envfieldindex);
660 if (is_int($index)) {
661 $index = intval($index);
662 } else {
663 $index = strtoupper($index);
664 }
665 $this->val = array($index);
666 } else {
667 $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield),
668 $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS);
669 if (count($indexes) == 0) {
670 return false;
671 }
672 $this->val = $indexes;
673 }
674 return true;
675 }
676
677 public function getEnvFieldNames()
678 {
679 return array($this->envfieldindex, $this->envfield);
680 }
681 }
682 // }}}
683
684 // {{{ class UFBF_Quick
685 class UFBF_Quick extends UFB_Field
686 {
687 protected function check(UserFilterBuilder $ufb)
688 {
689 if ($ufb->blank($this->envfield)) {
690 $this->empty = true;
691 return true;
692 }
693
694 $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield)));
695
696 return true;
697 }
698
699 protected function buildUFC(UserFilterBuilder $ufb)
700 {
701
702 $r = $s = $this->val;
703
704 /** Admin: Email, IP
705 */
706 if (S::admin() && strpos($s, '@') !== false) {
707 return new UFC_Email($s);
708 } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
709 return new UFC_Ip($s);
710 }
711
712 $conds = new PFC_And();
713
714 /** Name
715 */
716 $s = preg_replace('!\d+!', ' ', $s);
717 $strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY);
718 if (count($strings) > 5) {
719 Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte");
720 $strings = array_slice($strings, 0, 5);
721 }
722
723 if (count($strings)) {
724 if (S::user() != null && S::user()->checkPerms('directory_private')) {
725 $flags = array();
726 } else {
727 $flags = array('public');
728 }
729 $exact =$ufb->b('exact');
730 $conds->addChild(new UFC_NameTokens($strings, $flags, $ufb->b('with_soundex'), $exact));
731
732 $ufb->addOrder(new UFO_Score());
733 }
734
735 /** Promo ranges
736 */
737 $s = preg_replace('! *- *!', '-', $r);
738 $s = preg_replace('!([<>]) *!', ' \1', $s);
739 $s = preg_replace('![^0-9\-><]!', ' ', $s);
740 $s = preg_replace('![<>\-] !', '', $s);
741 $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY);
742 foreach ($ranges as $r) {
743 if (preg_match('!^\d{4}$!', $r)) {
744 $conds->addChild(new UFC_Promo('=', UserFilter::DISPLAY, 'X' . $r));
745 } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) {
746 $p1=min(intval($matches[1]), intval($matches[2]));
747 $p2=max(intval($matches[1]), intval($matches[2]));
748 $conds->addChild(new PFC_And(
749 new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $p1),
750 new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $p2)
751 ));
752 } elseif (preg_match('!^<(\d{4})!', $r, $matches)) {
753 $conds->addChild(new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $matches[1]));
754 } elseif (preg_match('!^>(\d{4})!', $r, $matches)) {
755 $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1]));
756 }
757 }
758
759 /** Phone number
760 */
761 $t = preg_replace('!(\d{4}-\d{4}|>\d{4}|<\d{4})!', '', $s);
762 $t = preg_replace('![<>\- ]!', '', $t);
763 if (strlen($t) > 4) {
764 $conds->addChild(new UFC_Phone($t));
765 }
766
767 return $conds;
768 }
769 }
770 // }}}
771
772 // {{{ class UFBF_SchoolIds
773 class UFBF_SchoolIds extends UFB_Field
774 {
775 // One of UFC_SchoolId types
776 protected $type;
777 protected $reversed_envfield;
778 protected $reversed = false;
779
780 public function __construct($envfield, $formtext, $type = UFC_SchoolId::AX, $reversed_envfield = '')
781 {
782 parent::__construct($envfield, $formtext);
783 $this->type = $type;
784 if ($reversed_envfield == '') {
785 $reversed_envfield = $envfield . '_reversed';
786 }
787 $this->reversed_envfield = $reversed_envfield;
788 }
789
790 protected function check(UserFilterBuilder $ufb)
791 {
792 if ($ufb->blank($this->envfield)) {
793 $this->empty = true;
794 return true;
795 }
796
797 $value = $ufb->t($this->envfield);
798 $values = explode("\r\n", $value);
799 $ids = array();
800 foreach ($values as $val) {
801 if (preg_match('/^[0-9A-Z]{0,8}$/', $val)) {
802 $ids[] = $val;
803 }
804 }
805 if (count($ids) == 0) {
806 return $this->raise("Le champ %s ne contient aucune valeur valide.");
807 }
808
809 $this->reversed = $ufb->b($this->reversed_envfield);
810 $this->val = $ids;
811 return true;
812 }
813
814 protected function buildUFC(UserFilterBuilder $ufb)
815 {
816 $ufc = new UFC_SchoolId($this->type, $this->val);
817 if ($this->reversed) {
818 return new PFC_Not($ufc);
819 } else {
820 return $ufc;
821 }
822 }
823 }
824 // }}}
825
826 // {{{ class UFBF_Name
827 class UFBF_Name extends UFBF_Text
828 {
829 protected function check(UserFilterBuilder $ufb)
830 {
831 if (!parent::check($ufb)) {
832 return false;
833 }
834
835 require_once 'name.func.inc.php';
836
837 $this->val = split_name_for_search($this->val);
838 if (count($this->val) == 0) {
839 $this->empty = true;
840 }
841 return true;
842 }
843
844 protected function buildUFC(UserFilterBuilder $ufb)
845 {
846 return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact'));
847 }
848 }
849 // }}}
850
851 // {{{ class UFBF_Promo
852 class UFBF_Promo extends UFB_Field
853 {
854 private static $validcomps = array('<', '<=', '=', '>=', '>');
855 private $comp;
856 private $envfieldcomp;
857
858 public function __construct($envfield, $formtext = '', $envfieldcomp)
859 {
860 parent::__construct($envfield, $formtext);
861 $this->envfieldcomp = $envfieldcomp;
862 }
863
864 protected function check(UserFilterBuilder $ufb)
865 {
866 if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp)) {
867 $this->empty = true;
868 return true;
869 }
870
871 $this->val = $ufb->i($this->envfield);
872 $this->comp = $ufb->v($this->envfieldcomp);
873
874 if (!in_array($this->comp, self::$validcomps)) {
875 return $this->raise("Le critère {$this->comp} n'est pas valide pour le champ %s");
876 }
877
878 if (preg_match('/^[0-9]{2}$/', $this->val)) {
879 $this->val += 1900;
880 }
881 if ($this->val < 1900 || $this->val > 9999) {
882 return $this->raise("Le champ %s doit être une année à 4 chiffres.");
883 }
884 return true;
885 }
886
887 protected function buildUFC(UserFilterBuilder $ufb) {
888 return new UFC_Promo($this->comp, UserFilter::GRADE_ING, $this->val);
889 }
890
891 public function getEnvFieldNames()
892 {
893 return array($this->envfield, $this->envfieldcomp);
894 }
895 }
896 // }}}
897
898 // {{{ class UFBF_Sex
899 class UFBF_Sex extends UFBF_Enum
900 {
901 public function __construct($envfield, $formtext = '')
902 {
903 parent::__construct($envfield, $formtext, array(1, 2));
904 }
905
906 private static function getVal($id)
907 {
908 switch($id) {
909 case 1:
910 return User::GENDER_MALE;
911 break;
912 case 2:
913 return User::GENDER_FEMALE;
914 break;
915 }
916 }
917
918 protected function buildUFC(UserFilterBuilder $ufb)
919 {
920 return new UFC_Sex(self::getVal($this->val));
921 }
922 }
923 // }}}
924
925 // {{{ class UFBF_NotRegistered
926 // Simple field for selecting only alive, not registered users (for quick search)
927 class UFBF_NotRegistered extends UFBF_Bool
928 {
929 protected function buildUFC(UserFilterBuilder $ufb)
930 {
931 if ($this->val) {
932 return new PFC_And(
933 new PFC_Not(new UFC_Dead()),
934 new PFC_Not(new UFC_Registered())
935 );
936 }
937 }
938 }
939 // }}}
940
941 // {{{ class UFBF_Registered
942 class UFBF_Registered extends UFBF_Enum
943 {
944 public function __construct($envfield, $formtext = '')
945 {
946 parent::__construct($envfield, $formtext, array(1, 2));
947 }
948
949 protected function buildUFC(UserFilterBuilder $ufb)
950 {
951 if ($this->val == 1) {
952 return new UFC_Registered();
953 } else if ($this->val == 2) {
954 return new PFC_Not(new UFC_Registered());
955 }
956 }
957 }
958 // }}}
959
960 // {{{ class UFBF_HasEmailRedirect
961 class UFBF_HasEmailRedirect extends UFBF_Enum
962 {
963 public function __construct($envfield, $formtext = '')
964 {
965 parent::__construct($envfield, $formtext, array(1, 2));
966 }
967
968 protected function buildUFC(UserFilterBuilder $ufb)
969 {
970 if ($this->val == 1) {
971 return new UFC_HasEmailRedirect();
972 } else if ($this->val == 2) {
973 return new PFC_Not(new UFC_HasEmailRedirect());
974 }
975 }
976 }
977 // }}}
978
979 // {{{ class UFBF_Dead
980 class UFBF_Dead extends UFBF_Enum
981 {
982 public function __construct($envfield, $formtext = '')
983 {
984 parent::__construct($envfield, $formtext, array(1, 2));
985 }
986
987 protected function buildUFC(UserFilterBuilder $ufb)
988 {
989 if ($this->val == 1) {
990 return new PFC_Not(new UFC_Dead());
991 } else if ($this->val == 2) {
992 return new UFC_Dead();
993 }
994 }
995 }
996 // }}}
997
998 // {{{ class UFBF_Town
999 /** Retrieves a town, either from a postal code or a town name
1000 */
1001 class UFBF_Town extends UFBF_Text
1002 {
1003 const TYPE_TEXT = 1;
1004 const TYPE_ZIP = 2;
1005 const TYPE_ANY = 3;
1006
1007 private $type;
1008 private $onlycurrentfield;
1009
1010 public function __construct($envfield, $formtext = '', $type = self::TYPE_ANY, $onlycurrentfield = 'only_current')
1011 {
1012 $this->type = $type;
1013 $this->onlycurrentfield = $onlycurrentfield;
1014 parent::__construct($envfield, $formtext, 2, 30);
1015 }
1016
1017 protected function buildUFC(UserFilterBuilder $ufb)
1018 {
1019 if ($ufb->isOn($this->onlycurrentfield)) {
1020 $flags = UFC_Address::FLAG_CURRENT;
1021 } else {
1022 $flags = UFC_Address::FLAG_ANY;
1023 }
1024
1025 if (preg_match('/[0-9]/', $this->val)) {
1026 if ($this->type & self::TYPE_ZIP) {
1027 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
1028 } else {
1029 return new PFC_False();
1030 }
1031 } else {
1032 $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val);
1033 $byzip = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
1034 if ($this->type & self::TYPE_ANY) {
1035 return new PFC_Or($byname, $byzip);
1036 } else if ($this->type & self::TYPE_TEXT) {
1037 return $byname;
1038 } else {
1039 return $byzip;
1040 }
1041 }
1042 }
1043
1044 public function getEnvFieldNames()
1045 {
1046 return array($this->envfield, $this->onlycurrentfield);
1047 }
1048 }
1049 // }}}
1050
1051 // {{{ class UFBF_Country
1052 class UFBF_Country extends UFBF_Mixed
1053 {
1054 protected $direnum = DirEnum::COUNTRIES;
1055 protected $onlycurrentfield;
1056
1057 public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current')
1058 {
1059 parent::__construct($envfieldtext, $envfieldindex, $formtext);
1060 $this->onlycurrentfield = $onlycurrentfield;
1061 }
1062
1063 protected function buildUFC(UserFilterBuilder $ufb)
1064 {
1065 if ($ufb->isOn($this->onlycurrentfield)) {
1066 $flags = UFC_Address::FLAG_CURRENT;
1067 } else {
1068 $flags = UFC_Address::FLAG_ANY;
1069 }
1070
1071 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_COUNTRY, UFC_Address::TYPE_ANY, $flags);
1072 }
1073
1074 public function getEnvFieldNames()
1075 {
1076 return array($this->envfield, $this->envfieldindex, $this->onlycurrentfield);
1077 }
1078 }
1079 // }}}
1080
1081 // {{{ class UFBF_AdminArea
1082 class UFBF_AdminArea extends UFBF_Index
1083 {
1084 protected $direnum = DirEnum::ADMINAREAS;
1085 protected $onlycurrentfield;
1086
1087 public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
1088 {
1089 parent::__construct($envfield, $formtext);
1090 $this->onlycurrentfield = $onlycurrentfield;
1091 }
1092
1093
1094 protected function buildUFC(UserFilterBuilder $ufb)
1095 {
1096 if ($ufb->isOn($this->onlycurrentfield)) {
1097 $flags = UFC_Address::FLAG_CURRENT;
1098 } else {
1099 $flags = UFC_Address::FLAG_ANY;
1100 }
1101
1102 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ADMAREA, UFC_Address::TYPE_ANY, $flags);
1103 }
1104
1105 public function getEnvFieldNames()
1106 {
1107 return array($this->envfield, $this->onlycurrentfield);
1108 }
1109 }
1110 // }}}
1111
1112 // {{{ class UFBF_SubAdminArea
1113 class UFBF_SubAdminArea extends UFBF_Index
1114 {
1115 protected $direnum = DirEnum::SUBADMINAREAS;
1116 protected $onlycurrentfield;
1117
1118 public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
1119 {
1120 parent::__construct($envfield, $formtext);
1121 $this->onlycurrentfield = $onlycurrentfield;
1122 }
1123
1124
1125 protected function buildUFC(UserFilterBuilder $ufb)
1126 {
1127 if ($ufb->isOn($this->onlycurrentfield)) {
1128 $flags = UFC_Address::FLAG_CURRENT;
1129 } else {
1130 $flags = UFC_Address::FLAG_ANY;
1131 }
1132
1133 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_SUBADMAREA, UFC_Address::TYPE_ANY, $flags);
1134 }
1135
1136 public function getEnvFieldNames()
1137 {
1138 return array($this->envfield, $this->onlycurrentfield);
1139 }
1140 }
1141 // }}}
1142
1143 // {{{ class UFBF_JobCompany
1144 class UFBF_JobCompany extends UFBF_Text
1145 {
1146 private $onlymentorfield;
1147
1148 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1149 {
1150 parent::__construct($envfield, $formtext);
1151 $this->onlymentorfield = $onlymentorfield;
1152 }
1153
1154 public function check(UserFilterBuilder $ufb) {
1155 if (parent::check($ufb)) {
1156 # No company check for mentors
1157 if ($ufb->isOn($this->onlymentorfield)) {
1158 $this->empty = true;
1159 }
1160 return true;
1161 } else {
1162 return false;
1163 }
1164 }
1165
1166 protected function buildUFC(UserFilterBuilder $ufb)
1167 {
1168 return new UFC_Job_Company(UFC_Job_Company::JOBNAME, $this->val);
1169 }
1170
1171 public function getEnvFieldNames()
1172 {
1173 return array($this->envfield, $this->onlymentorfield);
1174 }
1175 }
1176 // }}}
1177
1178 // {{{ class UFBF_JobTerms
1179 class UFBF_JobTerms extends UFBF_Index
1180 {
1181 protected function buildUFC(UserFilterBuilder $ufb)
1182 {
1183 return new UFC_Job_Terms($this->val);
1184 }
1185 }
1186 // }}}
1187
1188 // {{{ class UFBF_JobDescription
1189 class UFBF_JobDescription extends UFBF_Text
1190 {
1191 private $onlymentorfield;
1192
1193 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1194 {
1195 parent::__construct($envfield, $formtext);
1196 $this->onlymentorfield = $onlymentorfield;
1197 }
1198
1199 protected function buildUFC(UserFilterBuilder $ufb)
1200 {
1201 if ($ufb->isOn($this->onlymentorfield)) {
1202 return new UFC_Mentor_Expertise($this->val);
1203 } else {
1204 return new UFC_Job_Description($this->val, UserFilter::JOB_USERDEFINED);
1205 }
1206 }
1207
1208 public function getEnvFieldNames()
1209 {
1210 return array($this->envfield, $this->onlymentorfield);
1211 }
1212 }
1213 // }}}
1214
1215 // {{{ class UFBF_JobCv
1216 class UFBF_JobCv extends UFBF_Text
1217 {
1218 private $onlymentorfield;
1219
1220 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1221 {
1222 parent::__construct($envfield, $formtext);
1223 $this->onlymentorfield = $onlymentorfield;
1224 }
1225
1226 protected function buildUFC(UserFilterBuilder $ufb)
1227 {
1228 if ($ufb->isOn($this->onlymentorfield)) {
1229 return new UFC_Mentor_Expertise($this->val);
1230 } else {
1231 return new UFC_Job_Description($this->val, UserFilter::JOB_CV);
1232 }
1233 }
1234
1235 public function getEnvFieldNames()
1236 {
1237 return array($this->envfield, $this->onlymentorfield);
1238 }
1239 }
1240 // }}}
1241
1242 // {{{ class UFBF_Nationality
1243 class UFBF_Nationality extends UFBF_Mixed
1244 {
1245 protected $direnum = DirEnum::NATIONALITIES;
1246
1247 protected function buildUFC(UserFilterBuilder $ufb)
1248 {
1249 return new UFC_Nationality($this->val);
1250 }
1251 }
1252 // }}}
1253
1254 // {{{ class UFBF_Binet
1255 class UFBF_Binet extends UFBF_Mixed
1256 {
1257 protected $direnum = DirEnum::BINETS;
1258
1259 protected function buildUFC(UserFilterBuilder $ufb)
1260 {
1261 return new UFC_Binet($this->val);
1262 }
1263 }
1264 // }}}
1265
1266 // {{{ class UFBF_Group
1267 class UFBF_Group extends UFBF_Mixed
1268 {
1269 protected $direnum = DirEnum::GROUPESX;
1270
1271 protected function buildUFC(UserFilterBuilder $ufb)
1272 {
1273 if (count($this->val) == 1) {
1274 return new UFC_Group($this->val[0]);
1275 }
1276
1277 $or = new PFC_Or();
1278 foreach ($this->val as $grp) {
1279 $or->addChild(new UFC_Group($grp));
1280 }
1281 return $or;
1282 }
1283 }
1284 // }}}
1285
1286 // {{{ class UFBF_Section
1287 class UFBF_Section extends UFBF_Mixed
1288 {
1289 protected $direnum = DirEnum::SECTIONS;
1290
1291 protected function buildUFC(UserFilterBuilder $ufb)
1292 {
1293 return new UFC_Section($this->val);
1294 }
1295 }
1296 // }}}
1297
1298 // {{{ class UFBF_EducationSchool
1299 class UFBF_EducationSchool extends UFBF_Mixed
1300 {
1301 protected $direnum = DirEnum::EDUSCHOOLS;
1302
1303 protected function buildUFC(UserFilterBuilder $ufb)
1304 {
1305 return new UFC_EducationSchool($this->val);
1306 }
1307 }
1308 // }}}
1309
1310 // {{{ class UFBF_EducationDegree
1311 class UFBF_EducationDegree extends UFBF_Mixed
1312 {
1313 protected $direnum = DirEnum::EDUDEGREES;
1314
1315 protected function buildUFC(UserFilterBuilder $ufb)
1316 {
1317 return new UFC_EducationDegree($this->val);
1318 }
1319 }
1320 // }}}
1321
1322 // {{{ class UFBF_EducationField
1323 class UFBF_EducationField extends UFBF_Mixed
1324 {
1325 protected $direnum = DirEnum::EDUFIELDS;
1326
1327 protected function buildUFC(UserFilterBuilder $ufb)
1328 {
1329 return new UFC_EducationField($this->val);
1330 }
1331 }
1332 // }}}
1333
1334 // {{{ class UFBF_Comment
1335 class UFBF_Comment extends UFBF_Text
1336 {
1337 protected function buildUFC(UserFilterBuilder $ufb)
1338 {
1339 return new UFC_Comment($this->val);
1340 }
1341 }
1342 // }}}
1343
1344 // {{{ class UFBF_Phone
1345 class UFBF_Phone extends UFBF_Text
1346 {
1347 protected function buildUFC(UserFilterBuilder $ufb)
1348 {
1349 return new UFC_Phone($this->val);
1350 }
1351 }
1352 // }}}
1353
1354 // {{{ class UFBF_Networking
1355 class UFBF_Networking extends UFBF_Text
1356 {
1357 private $networktypefield;
1358 private $nwtype;
1359
1360 public function __construct($envfield, $networktypefield, $formtext = '')
1361 {
1362 parent::__construct($envfield, $formtext);
1363 $this->networktypefield = $networktypefield;
1364 }
1365
1366 public function check(UserFilterBuilder $ufb)
1367 {
1368 if (parent::check($ufb)) {
1369 $this->nwtype = $ufb->i($this->networktypefield);
1370 return true;
1371 } else {
1372 return false;
1373 }
1374 }
1375
1376 public function isEmpty()
1377 {
1378 return parent::isEmpty() || $this->nwtype == 0;
1379 }
1380
1381 public function buildUFC(UserFilterBuilder $ufb)
1382 {
1383 return new UFC_Networking($this->nwtype, $this->val);
1384 }
1385
1386 public function getEnvFieldNames()
1387 {
1388 return array($this->envfield, $this->networktypefield);
1389 }
1390 }
1391 // }}}
1392
1393 // {{{ class UFBF_Mentor
1394 class UFBF_Mentor extends UFBF_Bool
1395 {
1396 protected function buildUFC(UserFilterBuilder $ufb)
1397 {
1398 return new UFC_Mentor();
1399 }
1400 }
1401 // }}}
1402
1403 // {{{ class UFBF_MentorCountry
1404 class UFBF_MentorCountry extends UFBF_Text
1405 {
1406 protected function buildUFC(UserFilterBuilder $ufb)
1407 {
1408 return new UFC_Mentor_Country($this->val);
1409 }
1410 }
1411 // }}}
1412
1413 // {{{ class UFBF_Mentorterm
1414 class UFBF_MentorTerm extends UFBF_Index
1415 {
1416 protected function buildUFC(UserFilterBuilder $ufb)
1417 {
1418 return new UFC_Mentor_Terms($this->val);
1419 }
1420 }
1421 // }}}
1422
1423 // {{{ class UFBF_MentorExpertise
1424 class UFBF_MentorExpertise extends UFBF_Text
1425 {
1426 protected function buildUFC(UserFilterBuilder $ufb)
1427 {
1428 return new UFC_Mentor_Expertise($this->val);
1429 }
1430 }
1431 // }}}
1432
1433 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1434 ?>