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