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