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