Adapts promotion quick search for m/d.
[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', 'edu_type'),
344 new UFBF_Promo('promo2', 'Promotion', 'egal2', 'edu_type'),
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_OriginCorps('origin_corps', 'Corps d\'origine'),
361 new UFBF_CurrentCorps('current_corps', 'Corps actuel'),
362 new UFBF_CorpsRank('corps_rank', 'Grade'),
363
364 new UFBF_Nationality('nationaliteTxt', 'nationalite', 'Nationalité'),
365 new UFBF_Binet('binetTxt', 'binet', 'Binet'),
366 new UFBF_Group('groupexTxt', 'groupex', 'Groupe X'),
367 new UFBF_Section('sectionTxt', 'section', 'Section'),
368
369 new UFBF_EducationSchool('schoolTxt', 'school', "École d'application"),
370 new UFBF_EducationDegree('diplomaTxt', 'diploma', 'Diplôme'),
371 new UFBF_EducationField('fieldTxt', 'field', "Domaine d'études"),
372
373 new UFBF_Comment('free', 'Commentaire'),
374 new UFBF_Phone('phone_number', 'Téléphone'),
375 new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'),
376
377 new UFBF_Mentor('only_referent', 'Référent'),
378 );
379
380 if ($include_admin || $include_ax) {
381 $fields[] = new UFBF_SchoolIds('schoolid_ax', 'Matricule AX', UFC_SchoolId::AX);
382 }
383
384 parent::__construct($fields, $envprefix);
385 }
386 }
387 // }}}
388
389 // {{{ class UFB_MentorSearch
390 class UFB_MentorSearch extends UserFilterBuilder
391 {
392 public function __construct($envprefix = '')
393 {
394 $fields = array(
395 new UFBF_MentorCountry('country'),
396 new UFBF_MentorTerm('jobterm', 'jobtermText'),
397 new UFBF_MentorExpertise('expertise'),
398 );
399 parent::__construct($fields, $envprefix);
400 }
401 }
402 // }}}
403
404 // {{{ class UFB_NewsLetter
405 class UFB_NewsLetter extends UserFilterBuilder
406 {
407 const FIELDS_PROMO = 'promo';
408 const FIELDS_AXID = 'axid';
409 const FIELDS_GEO = 'geo';
410
411 public function __construct($flags, $envprefix = '')
412 {
413 $fields = array();
414 if ($flags->hasFlag(self::FIELDS_PROMO)) {
415 $fields[] = new UFBF_Promo('promo1', 'Promotion', 'egal1');
416 $fields[] = new UFBF_Promo('promo2', 'Promotion', 'egal2');
417 }
418 if ($flags->hasFlag(self::FIELDS_AXID)) {
419 $fields[] = new UFBF_SchoolIds('axid', 'Matricule AX', UFC_SchoolId::AX);
420 }
421 parent::__construct($fields, $envprefix);
422 }
423 }
424 // }}}
425
426 // {{{ class UFB_Field
427 abstract class UFB_Field
428 {
429 protected $envfield;
430 protected $formtext;
431
432 protected $empty = false;
433 protected $val = null;
434
435 /** Constructor
436 * @param $envfield Name of the field in the environment
437 * @param $formtext User-friendly name of that field
438 */
439 public function __construct($envfield, $formtext = '')
440 {
441 $this->envfield = $envfield;
442 if ($formtext != '') {
443 $this->formtext = $formtext;
444 } else {
445 $formtext = ucfirst($envfield);
446 }
447 }
448
449 /** Prints the given error message to the user, and returns false
450 * in order to be used as return $this->raise('ERROR');
451 *
452 * All %s in the $msg will be replaced with the formtext.
453 */
454 protected function raise($msg)
455 {
456 Platal::page()->trigError(str_replace('%s', $this->formtext, $msg));
457 return false;
458 }
459
460 public function apply(UserFilterBuilder $ufb) {
461 if (!$this->check($ufb)) {
462 return false;
463 }
464
465 if (!$this->isEmpty()) {
466 $ufc = $this->buildUFC($ufb);
467 if ($ufc != null) {
468 $ufb->addCond($ufc);
469 }
470 }
471 return true;
472 }
473
474 public function isEmpty()
475 {
476 return $this->empty;
477 }
478
479 /** Create the UFC associated to the field; won't be called
480 * if the field is "empty"
481 * @param $ufb UFB to which fields must be added
482 * @return UFC
483 */
484 abstract protected function buildUFC(UserFilterBuilder $ufb);
485
486 /** This function is intended to run consistency checks on the value
487 * @return boolean Whether the input is valid
488 */
489 abstract protected function check(UserFilterBuilder $ufb);
490
491 // Simple form interface
492
493 /** Retrieve a list of env field names used by that field
494 * their values will be recorded when saving the 'search' and used to prefill the form
495 * when needed.
496 */
497 public function getEnvFieldNames()
498 {
499 return array($this->envfield);
500 }
501 }
502 // }}}
503
504 // {{{ class UFBF_Text
505 abstract class UFBF_Text extends UFB_Field
506 {
507 private $minlength;
508 private $maxlength;
509
510 public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255)
511 {
512 parent::__construct($envfield, $formtext);
513 $this->minlength = $minlength;
514 $this->maxlength = $maxlength;
515 }
516
517 protected function check(UserFilterBuilder $ufb)
518 {
519 if ($ufb->blank($this->envfield)) {
520 $this->empty = true;
521 return true;
522 }
523
524 $this->val = $ufb->t($this->envfield);
525 if (strlen($this->val) < $this->minlength) {
526 return $this->raise("Le champ %s est trop court (minimum {$this->minlength}).");
527 } else if (strlen($this->val) > $this->maxlength) {
528 return $this->raise("Le champ %s est trop long (maximum {$this->maxlength}).");
529 } else if (preg_match(":[\]\[<>{}~§_`|%$^=]|\*\*:u", $this->val)) {
530 return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.');
531 }
532
533 return true;
534 }
535 }
536 // }}}
537
538 // {{{ class UFBF_Range
539 /** Subclass to use for fields which only allow integers within a range
540 */
541 abstract class UFBF_Range extends UFB_Field
542 {
543
544 private $min;
545 private $max;
546
547 public function __construct($envfield, $formtext = '', $min = 0, $max = 65535)
548 {
549 parent::__construct($envfield, $formtext);
550 $this->min = $min;
551 $this->max = $max;
552 }
553
554 protected function check(UserFilterBuilder $ufb)
555 {
556 if ($ufb->blank($this->envfield)) {
557 $this->empty = true;
558 return true;
559 }
560
561 $this->val = $ufb->i($this->envfield);
562 if ($this->val < $this->min) {
563 return $this->raise("Le champs %s est inférieur au minimum ({$this->min}).");
564 } else if ($this->val > $this->max) {
565 return $this->raise("Le champ %s est supérieur au maximum ({$this->max}).");
566 }
567 return true;
568 }
569 }
570 // }}}
571
572 // {{{ class UFBF_Index
573 /** Subclass to use for indexed fields
574 */
575 abstract class UFBF_Index extends UFB_Field
576 {
577 protected function check(UserFilterBuilder $ufb)
578 {
579 if ($ufb->blank($this->envfield)) {
580 $this->empty = true;
581 }
582 $this->val = $ufb->i($this->envfield);
583 return true;
584 }
585 }
586 // }}}
587
588 // {{{ class UFBF_Enum
589 /** Subclass to use for fields whose value must belong to a specific set of values
590 */
591 abstract class UFBF_Enum extends UFB_Field
592 {
593 protected $allowedvalues;
594
595 public function __construct($envfield, $formtext = '', $allowedvalues = array(), $strict = false)
596 {
597 parent::__construct($envfield, $formtext);
598 $this->allowedvalues = $allowedvalues;
599 $this->strict = $strict;
600 }
601
602 protected function check(UserFilterBuilder $ufb)
603 {
604 if ($ufb->blank($this->envfield)) {
605 $this->empty = true;
606 return true;
607 }
608
609 $this->val = $ufb->v($this->envfield);
610 if (! in_array($this->val, $this->allowedvalues)) {
611 if ($this->strict) {
612 return $this->raise("La valeur {$this->val} n'est pas valide pour le champ %s.");
613 } else {
614 $this->empty = true;
615 }
616 }
617 return true;
618 }
619 }
620 // }}}
621
622 // {{{ class UFBF_Bool
623 abstract class UFBF_Bool extends UFB_Field
624 {
625 protected function check(UserFilterBuilder $ufb)
626 {
627 if ($ufb->blank($this->envfield)) {
628 $this->empty = true;
629 return true;
630 }
631
632 $this->val = $ufb->b($this->envfield);
633 return true;
634 }
635 }
636 // }}}
637
638 // {{{ class UFBF_Mixed
639 /** A class for building UFBFs when the user can input either a text or an ID
640 */
641 abstract class UFBF_Mixed extends UFB_Field
642 {
643 /** Name of the DirEnum on which class is based
644 */
645 protected $direnum;
646
647 protected $envfieldindex;
648
649 public function __construct($envfieldtext, $envfieldindex, $formtext = '')
650 {
651 parent::__construct($envfieldtext, $formtext);
652 $this->envfieldindex = $envfieldindex;
653 }
654
655 protected function check(UserFilterBuilder $ufb)
656 {
657 if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) {
658 $this->empty = true;
659 return true;
660 }
661
662 if (!$ufb->blank($this->envfieldindex)) {
663 $index = $ufb->v($this->envfieldindex);
664 if (is_int($index)) {
665 $index = intval($index);
666 } else {
667 $index = strtoupper($index);
668 }
669 $this->val = array($index);
670 } else {
671 $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield),
672 $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS);
673 if (count($indexes) == 0) {
674 return false;
675 }
676 $this->val = $indexes;
677 }
678 return true;
679 }
680
681 public function getEnvFieldNames()
682 {
683 return array($this->envfieldindex, $this->envfield);
684 }
685 }
686 // }}}
687
688 // {{{ class UFBF_Quick
689 class UFBF_Quick extends UFB_Field
690 {
691 protected function check(UserFilterBuilder $ufb)
692 {
693 if ($ufb->blank($this->envfield)) {
694 $this->empty = true;
695 return true;
696 }
697
698 $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield)));
699
700 return true;
701 }
702
703 protected function buildUFC(UserFilterBuilder $ufb)
704 {
705
706 $r = $s = $this->val;
707
708 /** Admin: Email, IP
709 */
710 if (S::admin() && strpos($s, '@') !== false) {
711 return new UFC_Email($s);
712 } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
713 return new UFC_Ip($s);
714 }
715
716 $conds = new PFC_And();
717
718 /** Name
719 */
720 $s = preg_replace('!\d+!', ' ', $s);
721 $strings = preg_split("![^a-zA-Z%]+!", $s, -1, PREG_SPLIT_NO_EMPTY);
722 foreach ($strings as $key => $string) {
723 if (strlen($string) < 2) {
724 unset($strings[$key]);
725 }
726 }
727 if (count($strings) > 5) {
728 Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte");
729 $strings = array_slice($strings, 0, 5);
730 }
731
732 if (count($strings)) {
733 if (S::user() != null && S::user()->checkPerms('directory_private')) {
734 $flags = array();
735 } else {
736 $flags = array('public');
737 }
738 $exact =$ufb->b('exact');
739 $conds->addChild(new UFC_NameTokens($strings, $flags, $ufb->b('with_soundex'), $exact));
740
741 $ufb->addOrder(new UFO_Score());
742 }
743
744 /** Promo ranges
745 */
746 $s = preg_replace('! *- *!', '-', $r);
747 $s = preg_replace('!([<>]) *!', ' \1', $s);
748 $s = preg_replace('![^0-9xmdXMD\-><]!', ' ', $s);
749 $s = preg_replace('![<>\-] !', '', $s);
750 $ranges = preg_split('! +!', strtolower($s), -1, PREG_SPLIT_NO_EMPTY);
751 $grades = array('' => UserFilter::GRADE_ING, 'x' => UserFilter::GRADE_ING, 'm' => UserFilter::GRADE_MST, 'd' => UserFilter::GRADE_PHD);
752 foreach ($ranges as $r) {
753 if (preg_match('!^(|x|m|d)(\d{4})$!', $r, $matches)) {
754 $conds->addChild(new UFC_Promo('=', $grades[$matches[1]], $matches[2]));
755 } elseif (preg_match('!^(|x|m|d)(\d{4})-(|x|m|d)(\d{4})$!', $r, $matches)) {
756 if ($matches[1] == $matches[3]) {
757 $p1 = min(intval($matches[2]), intval($matches[4]));
758 $p2 = max(intval($matches[2]), intval($matches[4]));
759 $conds->addChild(new PFC_And(
760 new UFC_Promo('>=', $grades[$matches[1]], $p1),
761 new UFC_Promo('<=', $grades[$matches[1]], $p2)
762 ));
763 }
764 } elseif (preg_match('!^<(|x|m|d)(\d{4})!', $r, $matches)) {
765 $conds->addChild(new UFC_Promo('<=', $grades[$matches[1]], $matches[2]));
766 } elseif (preg_match('!^>(|x|m|d)(\d{4})!', $r, $matches)) {
767 $conds->addChild(new UFC_Promo('>=', $grades[$matches[1]], $matches[2]));
768 }
769 }
770
771 /** Phone number
772 */
773 $t = preg_replace('!(\d{4}-\d{4}|>\d{4}|<\d{4})!', '', $s);
774 $t = preg_replace('![<>\- ]!', '', $t);
775 if (strlen($t) > 5) {
776 $conds->addChild(new UFC_Phone($t));
777 }
778
779 return $conds;
780 }
781 }
782 // }}}
783
784 // {{{ class UFBF_SchoolIds
785 class UFBF_SchoolIds extends UFB_Field
786 {
787 // One of UFC_SchoolId types
788 protected $type;
789 protected $reversed_envfield;
790 protected $reversed = false;
791
792 public function __construct($envfield, $formtext, $type = UFC_SchoolId::AX, $reversed_envfield = '')
793 {
794 parent::__construct($envfield, $formtext);
795 $this->type = $type;
796 if ($reversed_envfield == '') {
797 $reversed_envfield = $envfield . '_reversed';
798 }
799 $this->reversed_envfield = $reversed_envfield;
800 }
801
802 protected function check(UserFilterBuilder $ufb)
803 {
804 if ($ufb->blank($this->envfield)) {
805 $this->empty = true;
806 return true;
807 }
808
809 $value = $ufb->t($this->envfield);
810 $values = explode("\r\n", $value);
811 $ids = array();
812 foreach ($values as $val) {
813 if (preg_match('/^[0-9A-Z]{0,8}$/', $val)) {
814 $ids[] = $val;
815 }
816 }
817 if (count($ids) == 0) {
818 return $this->raise("Le champ %s ne contient aucune valeur valide.");
819 }
820
821 $this->reversed = $ufb->b($this->reversed_envfield);
822 $this->val = $ids;
823 return true;
824 }
825
826 protected function buildUFC(UserFilterBuilder $ufb)
827 {
828 $ufc = new UFC_SchoolId($this->type, $this->val);
829 if ($this->reversed) {
830 return new PFC_Not($ufc);
831 } else {
832 return $ufc;
833 }
834 }
835 }
836 // }}}
837
838 // {{{ class UFBF_Name
839 class UFBF_Name extends UFBF_Text
840 {
841 protected function check(UserFilterBuilder $ufb)
842 {
843 if (!parent::check($ufb)) {
844 return false;
845 }
846
847 require_once 'name.func.inc.php';
848
849 $this->val = split_name_for_search($this->val);
850 if (count($this->val) == 0) {
851 $this->empty = true;
852 }
853 return true;
854 }
855
856 protected function buildUFC(UserFilterBuilder $ufb)
857 {
858 return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact'));
859 }
860 }
861 // }}}
862
863 // {{{ class UFBF_Promo
864 class UFBF_Promo extends UFB_Field
865 {
866 private static $validcomps = array('<', '<=', '=', '>=', '>');
867 private static $validtypes = array(UserFilter::GRADE_ING, UserFilter::GRADE_PHD, UserFilter::GRADE_MST);
868 private $comp;
869 private $type;
870 private $envfieldcomp;
871 private $envfieldtype;
872
873 public function __construct($envfield, $formtext = '', $envfieldcomp, $envfieldtype)
874 {
875 parent::__construct($envfield, $formtext);
876 $this->envfieldcomp = $envfieldcomp;
877 $this->envfieldtype = $envfieldtype;
878 }
879
880 protected function check(UserFilterBuilder $ufb)
881 {
882 if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp) || $ufb->blank($this->envfieldtype)) {
883 $this->empty = true;
884 return true;
885 }
886
887 $this->val = $ufb->i($this->envfield);
888 $this->comp = $ufb->v($this->envfieldcomp);
889 $this->type = $ufb->v($this->envfieldtype);
890
891 if (!in_array($this->type, self::$validtypes)) {
892 return $this->raise("Le critère {$this->type} n'est pas valide pour le champ %s");
893 }
894
895 if (!in_array($this->comp, self::$validcomps)) {
896 return $this->raise("Le critère {$this->comp} n'est pas valide pour le champ %s");
897 }
898
899 if (preg_match('/^[0-9]{2}$/', $this->val)) {
900 $this->val += 1900;
901 }
902 if ($this->val < 1900 || $this->val > 9999) {
903 return $this->raise("Le champ %s doit être une année à 4 chiffres.");
904 }
905 return true;
906 }
907
908 protected function buildUFC(UserFilterBuilder $ufb) {
909 return new UFC_Promo($this->comp, $this->type, $this->val);
910 }
911
912 public function getEnvFieldNames()
913 {
914 return array($this->envfield, $this->envfieldcomp, $this->envfieldtype);
915 }
916 }
917 // }}}
918
919 // {{{ class UFBF_Sex
920 class UFBF_Sex extends UFBF_Enum
921 {
922 public function __construct($envfield, $formtext = '')
923 {
924 parent::__construct($envfield, $formtext, array(1, 2));
925 }
926
927 private static function getVal($id)
928 {
929 switch($id) {
930 case 1:
931 return User::GENDER_MALE;
932 break;
933 case 2:
934 return User::GENDER_FEMALE;
935 break;
936 }
937 }
938
939 protected function buildUFC(UserFilterBuilder $ufb)
940 {
941 return new UFC_Sex(self::getVal($this->val));
942 }
943 }
944 // }}}
945
946 // {{{ class UFBF_NotRegistered
947 // Simple field for selecting only alive, not registered users (for quick search)
948 class UFBF_NotRegistered extends UFBF_Bool
949 {
950 protected function buildUFC(UserFilterBuilder $ufb)
951 {
952 if ($this->val) {
953 return new PFC_And(
954 new PFC_Not(new UFC_Dead()),
955 new PFC_Not(new UFC_Registered())
956 );
957 }
958 }
959 }
960 // }}}
961
962 // {{{ class UFBF_Registered
963 class UFBF_Registered extends UFBF_Enum
964 {
965 public function __construct($envfield, $formtext = '')
966 {
967 parent::__construct($envfield, $formtext, array(1, 2));
968 }
969
970 protected function buildUFC(UserFilterBuilder $ufb)
971 {
972 if ($this->val == 1) {
973 return new UFC_Registered();
974 } else if ($this->val == 2) {
975 return new PFC_Not(new UFC_Registered());
976 }
977 }
978 }
979 // }}}
980
981 // {{{ class UFBF_HasEmailRedirect
982 class UFBF_HasEmailRedirect extends UFBF_Enum
983 {
984 public function __construct($envfield, $formtext = '')
985 {
986 parent::__construct($envfield, $formtext, array(1, 2));
987 }
988
989 protected function buildUFC(UserFilterBuilder $ufb)
990 {
991 if ($this->val == 1) {
992 return new UFC_HasEmailRedirect();
993 } else if ($this->val == 2) {
994 return new PFC_Not(new UFC_HasEmailRedirect());
995 }
996 }
997 }
998 // }}}
999
1000 // {{{ class UFBF_Dead
1001 class UFBF_Dead extends UFBF_Enum
1002 {
1003 public function __construct($envfield, $formtext = '')
1004 {
1005 parent::__construct($envfield, $formtext, array(1, 2));
1006 }
1007
1008 protected function buildUFC(UserFilterBuilder $ufb)
1009 {
1010 if ($this->val == 1) {
1011 return new PFC_Not(new UFC_Dead());
1012 } else if ($this->val == 2) {
1013 return new UFC_Dead();
1014 }
1015 }
1016 }
1017 // }}}
1018
1019 // {{{ class UFBF_Town
1020 /** Retrieves a town, either from a postal code or a town name
1021 */
1022 class UFBF_Town extends UFBF_Text
1023 {
1024 const TYPE_TEXT = 1;
1025 const TYPE_ZIP = 2;
1026 const TYPE_ANY = 3;
1027
1028 private $type;
1029 private $onlycurrentfield;
1030
1031 public function __construct($envfield, $formtext = '', $type = self::TYPE_ANY, $onlycurrentfield = 'only_current')
1032 {
1033 $this->type = $type;
1034 $this->onlycurrentfield = $onlycurrentfield;
1035 parent::__construct($envfield, $formtext, 2, 30);
1036 }
1037
1038 protected function buildUFC(UserFilterBuilder $ufb)
1039 {
1040 if ($ufb->isOn($this->onlycurrentfield)) {
1041 $flags = UFC_Address::FLAG_CURRENT;
1042 } else {
1043 $flags = UFC_Address::FLAG_ANY;
1044 }
1045
1046 if (preg_match('/[0-9]/', $this->val)) {
1047 if ($this->type & self::TYPE_ZIP) {
1048 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
1049 } else {
1050 return new PFC_False();
1051 }
1052 } else {
1053 $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val);
1054 $byzip = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
1055 if ($this->type & self::TYPE_ANY) {
1056 return new PFC_Or($byname, $byzip);
1057 } else if ($this->type & self::TYPE_TEXT) {
1058 return $byname;
1059 } else {
1060 return $byzip;
1061 }
1062 }
1063 }
1064
1065 public function getEnvFieldNames()
1066 {
1067 return array($this->envfield, $this->onlycurrentfield);
1068 }
1069 }
1070 // }}}
1071
1072 // {{{ class UFBF_Country
1073 class UFBF_Country extends UFBF_Mixed
1074 {
1075 protected $direnum = DirEnum::COUNTRIES;
1076 protected $onlycurrentfield;
1077
1078 public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current')
1079 {
1080 parent::__construct($envfieldtext, $envfieldindex, $formtext);
1081 $this->onlycurrentfield = $onlycurrentfield;
1082 }
1083
1084 protected function buildUFC(UserFilterBuilder $ufb)
1085 {
1086 if ($ufb->isOn($this->onlycurrentfield)) {
1087 $flags = UFC_Address::FLAG_CURRENT;
1088 } else {
1089 $flags = UFC_Address::FLAG_ANY;
1090 }
1091
1092 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_COUNTRY, UFC_Address::TYPE_ANY, $flags);
1093 }
1094
1095 public function getEnvFieldNames()
1096 {
1097 return array($this->envfield, $this->envfieldindex, $this->onlycurrentfield);
1098 }
1099 }
1100 // }}}
1101
1102 // {{{ class UFBF_AdminArea
1103 class UFBF_AdminArea extends UFBF_Index
1104 {
1105 protected $direnum = DirEnum::ADMINAREAS;
1106 protected $onlycurrentfield;
1107
1108 public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
1109 {
1110 parent::__construct($envfield, $formtext);
1111 $this->onlycurrentfield = $onlycurrentfield;
1112 }
1113
1114
1115 protected function buildUFC(UserFilterBuilder $ufb)
1116 {
1117 if ($ufb->isOn($this->onlycurrentfield)) {
1118 $flags = UFC_Address::FLAG_CURRENT;
1119 } else {
1120 $flags = UFC_Address::FLAG_ANY;
1121 }
1122
1123 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ADMAREA, UFC_Address::TYPE_ANY, $flags);
1124 }
1125
1126 public function getEnvFieldNames()
1127 {
1128 return array($this->envfield, $this->onlycurrentfield);
1129 }
1130 }
1131 // }}}
1132
1133 // {{{ class UFBF_SubAdminArea
1134 class UFBF_SubAdminArea extends UFBF_Index
1135 {
1136 protected $direnum = DirEnum::SUBADMINAREAS;
1137 protected $onlycurrentfield;
1138
1139 public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
1140 {
1141 parent::__construct($envfield, $formtext);
1142 $this->onlycurrentfield = $onlycurrentfield;
1143 }
1144
1145
1146 protected function buildUFC(UserFilterBuilder $ufb)
1147 {
1148 if ($ufb->isOn($this->onlycurrentfield)) {
1149 $flags = UFC_Address::FLAG_CURRENT;
1150 } else {
1151 $flags = UFC_Address::FLAG_ANY;
1152 }
1153
1154 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_SUBADMAREA, UFC_Address::TYPE_ANY, $flags);
1155 }
1156
1157 public function getEnvFieldNames()
1158 {
1159 return array($this->envfield, $this->onlycurrentfield);
1160 }
1161 }
1162 // }}}
1163
1164 // {{{ class UFBF_JobCompany
1165 class UFBF_JobCompany extends UFBF_Text
1166 {
1167 private $onlymentorfield;
1168
1169 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1170 {
1171 parent::__construct($envfield, $formtext);
1172 $this->onlymentorfield = $onlymentorfield;
1173 }
1174
1175 public function check(UserFilterBuilder $ufb) {
1176 if (parent::check($ufb)) {
1177 # No company check for mentors
1178 if ($ufb->isOn($this->onlymentorfield)) {
1179 $this->empty = true;
1180 }
1181 return true;
1182 } else {
1183 return false;
1184 }
1185 }
1186
1187 protected function buildUFC(UserFilterBuilder $ufb)
1188 {
1189 return new UFC_Job_Company(UFC_Job_Company::JOBNAME, $this->val);
1190 }
1191
1192 public function getEnvFieldNames()
1193 {
1194 return array($this->envfield, $this->onlymentorfield);
1195 }
1196 }
1197 // }}}
1198
1199 // {{{ class UFBF_JobTerms
1200 class UFBF_JobTerms extends UFBF_Index
1201 {
1202 protected function buildUFC(UserFilterBuilder $ufb)
1203 {
1204 return new UFC_Job_Terms($this->val);
1205 }
1206 }
1207 // }}}
1208
1209 // {{{ class UFBF_JobDescription
1210 class UFBF_JobDescription extends UFBF_Text
1211 {
1212 private $onlymentorfield;
1213
1214 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1215 {
1216 parent::__construct($envfield, $formtext);
1217 $this->onlymentorfield = $onlymentorfield;
1218 }
1219
1220 protected function buildUFC(UserFilterBuilder $ufb)
1221 {
1222 if ($ufb->isOn($this->onlymentorfield)) {
1223 return new UFC_Mentor_Expertise($this->val);
1224 } else {
1225 return new UFC_Job_Description($this->val, UserFilter::JOB_USERDEFINED);
1226 }
1227 }
1228
1229 public function getEnvFieldNames()
1230 {
1231 return array($this->envfield, $this->onlymentorfield);
1232 }
1233 }
1234 // }}}
1235
1236 // {{{ class UFBF_JobCv
1237 class UFBF_JobCv extends UFBF_Text
1238 {
1239 private $onlymentorfield;
1240
1241 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1242 {
1243 parent::__construct($envfield, $formtext);
1244 $this->onlymentorfield = $onlymentorfield;
1245 }
1246
1247 protected function buildUFC(UserFilterBuilder $ufb)
1248 {
1249 if ($ufb->isOn($this->onlymentorfield)) {
1250 return new UFC_Mentor_Expertise($this->val);
1251 } else {
1252 return new UFC_Job_Description($this->val, UserFilter::JOB_CV);
1253 }
1254 }
1255
1256 public function getEnvFieldNames()
1257 {
1258 return array($this->envfield, $this->onlymentorfield);
1259 }
1260 }
1261 // }}}
1262
1263 // {{{ class UFBF_Nationality
1264 class UFBF_Nationality extends UFBF_Mixed
1265 {
1266 protected $direnum = DirEnum::NATIONALITIES;
1267
1268 protected function buildUFC(UserFilterBuilder $ufb)
1269 {
1270 return new UFC_Nationality($this->val);
1271 }
1272 }
1273 // }}}
1274
1275 // {{{ class UFBF_Binet
1276 class UFBF_Binet extends UFBF_Mixed
1277 {
1278 protected $direnum = DirEnum::BINETS;
1279
1280 protected function buildUFC(UserFilterBuilder $ufb)
1281 {
1282 return new UFC_Binet($this->val);
1283 }
1284 }
1285 // }}}
1286
1287 // {{{ class UFBF_Group
1288 class UFBF_Group extends UFBF_Mixed
1289 {
1290 protected $direnum = DirEnum::GROUPESX;
1291
1292 protected function buildUFC(UserFilterBuilder $ufb)
1293 {
1294 if (count($this->val) == 1) {
1295 return new UFC_Group($this->val[0]);
1296 }
1297
1298 $or = new PFC_Or();
1299 foreach ($this->val as $grp) {
1300 $or->addChild(new UFC_Group($grp));
1301 }
1302 return $or;
1303 }
1304 }
1305 // }}}
1306
1307 // {{{ class UFBF_Section
1308 class UFBF_Section extends UFBF_Mixed
1309 {
1310 protected $direnum = DirEnum::SECTIONS;
1311
1312 protected function buildUFC(UserFilterBuilder $ufb)
1313 {
1314 return new UFC_Section($this->val);
1315 }
1316 }
1317 // }}}
1318
1319 // {{{ class UFBF_EducationSchool
1320 class UFBF_EducationSchool extends UFBF_Mixed
1321 {
1322 protected $direnum = DirEnum::EDUSCHOOLS;
1323
1324 protected function buildUFC(UserFilterBuilder $ufb)
1325 {
1326 return new UFC_EducationSchool($this->val);
1327 }
1328 }
1329 // }}}
1330
1331 // {{{ class UFBF_EducationDegree
1332 class UFBF_EducationDegree extends UFBF_Mixed
1333 {
1334 protected $direnum = DirEnum::EDUDEGREES;
1335
1336 protected function buildUFC(UserFilterBuilder $ufb)
1337 {
1338 return new UFC_EducationDegree($this->val);
1339 }
1340 }
1341 // }}}
1342
1343 // {{{ class UFBF_EducationField
1344 class UFBF_EducationField extends UFBF_Mixed
1345 {
1346 protected $direnum = DirEnum::EDUFIELDS;
1347
1348 protected function buildUFC(UserFilterBuilder $ufb)
1349 {
1350 return new UFC_EducationField($this->val);
1351 }
1352 }
1353 // }}}
1354
1355 // {{{ class UFBF_OriginCorps
1356 class UFBF_OriginCorps extends UFBF_Index
1357 {
1358 protected $direnum = DirEnum::ORIGINCORPS;
1359
1360 protected function buildUFC(UserFilterBuilder $ufb)
1361 {
1362 return new UFC_Corps(null, $this->val, UFC_Corps::ORIGIN);
1363 }
1364 }
1365 // }}}
1366
1367 // {{{ class UFBF_CurrentCorps
1368 class UFBF_CurrentCorps extends UFBF_Index
1369 {
1370 protected $direnum = DirEnum::CURRENTCORPS;
1371
1372 protected function buildUFC(UserFilterBuilder $ufb)
1373 {
1374 return new UFC_Corps(null, $this->val, UFC_Corps::CURRENT);
1375 }
1376 }
1377 // }}}
1378
1379 // {{{ class UFBF_CorpsRank
1380 class UFBF_CorpsRank extends UFBF_Index
1381 {
1382 protected $direnum = DirEnum::CORPSRANKS;
1383
1384 protected function buildUFC(UserFilterBuilder $ufb)
1385 {
1386 return new UFC_Corps_Rank(null, $this->val);
1387 }
1388 }
1389 // }}}
1390
1391 // {{{ class UFBF_Comment
1392 class UFBF_Comment extends UFBF_Text
1393 {
1394 protected function buildUFC(UserFilterBuilder $ufb)
1395 {
1396 return new UFC_Comment($this->val);
1397 }
1398 }
1399 // }}}
1400
1401 // {{{ class UFBF_Phone
1402 class UFBF_Phone extends UFBF_Text
1403 {
1404 protected function buildUFC(UserFilterBuilder $ufb)
1405 {
1406 return new UFC_Phone($this->val);
1407 }
1408 }
1409 // }}}
1410
1411 // {{{ class UFBF_Networking
1412 class UFBF_Networking extends UFBF_Text
1413 {
1414 private $networktypefield;
1415 private $nwtype;
1416
1417 public function __construct($envfield, $networktypefield, $formtext = '')
1418 {
1419 parent::__construct($envfield, $formtext);
1420 $this->networktypefield = $networktypefield;
1421 }
1422
1423 public function check(UserFilterBuilder $ufb)
1424 {
1425 if (parent::check($ufb)) {
1426 $this->nwtype = $ufb->i($this->networktypefield);
1427 return true;
1428 } else {
1429 return false;
1430 }
1431 }
1432
1433 public function isEmpty()
1434 {
1435 return parent::isEmpty() || $this->nwtype == 0;
1436 }
1437
1438 public function buildUFC(UserFilterBuilder $ufb)
1439 {
1440 return new UFC_Networking($this->nwtype, $this->val);
1441 }
1442
1443 public function getEnvFieldNames()
1444 {
1445 return array($this->envfield, $this->networktypefield);
1446 }
1447 }
1448 // }}}
1449
1450 // {{{ class UFBF_Mentor
1451 class UFBF_Mentor extends UFBF_Bool
1452 {
1453 protected function buildUFC(UserFilterBuilder $ufb)
1454 {
1455 return new UFC_Mentor();
1456 }
1457 }
1458 // }}}
1459
1460 // {{{ class UFBF_MentorCountry
1461 class UFBF_MentorCountry extends UFBF_Text
1462 {
1463 protected function buildUFC(UserFilterBuilder $ufb)
1464 {
1465 return new UFC_Mentor_Country($this->val);
1466 }
1467 }
1468 // }}}
1469
1470 // {{{ class UFBF_Mentorterm
1471 class UFBF_MentorTerm extends UFBF_Index
1472 {
1473 protected function buildUFC(UserFilterBuilder $ufb)
1474 {
1475 return new UFC_Mentor_Terms($this->val);
1476 }
1477 }
1478 // }}}
1479
1480 // {{{ class UFBF_MentorExpertise
1481 class UFBF_MentorExpertise extends UFBF_Text
1482 {
1483 protected function buildUFC(UserFilterBuilder $ufb)
1484 {
1485 return new UFC_Mentor_Expertise($this->val);
1486 }
1487 }
1488 // }}}
1489
1490 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1491 ?>