Popup only one profile.
[platal.git] / include / ufbuilder.inc.php
CommitLineData
d9b3d712
RB
1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
d9b3d712
RB
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
e21504a3
RB
22// {{{ class StoredUserFilterBuilder
23class 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
21b67462 105// {{{ class UserFilterBuilder
d9b3d712
RB
106class UserFilterBuilder
107{
108 private $envprefix;
109 private $fields;
110 private $valid = true;
111 private $ufc = null;
59c6cb70 112 private $orders = array();
e21504a3 113 private $fake_env = null;
d9b3d712
RB
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
e21504a3
RB
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
d9b3d712
RB
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
59c6cb70
RB
157 public function addOrder(PlFilterOrder &$order)
158 {
159 $this->order[] = $order;
160 }
161
d9b3d712
RB
162 public function isValid()
163 {
164 $this->buildUFC();
165 return $this->valid;
166 }
167
6faa0186
RB
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
d9b3d712
RB
179 /** Returns the built UFC
180 * @return The UFC, or PFC_False() if an error happened
181 */
3314838e 182 public function getUFC()
d9b3d712
RB
183 {
184 $this->buildUFC();
185 if ($this->valid) {
186 return $this->ufc;
187 } else {
188 return new PFC_False();
189 }
190 }
191
59c6cb70
RB
192 /** Returns adequate orders
193 */
194 public function getOrders()
195 {
196 $this->buildUFC();
197 return $this->orders;
198 }
199
e21504a3
RB
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
d9b3d712
RB
229 /** Wrappers around Env::i/s/..., to add envprefix
230 */
0f17825b
FB
231 public function s($key, $def = '')
232 {
e21504a3
RB
233 if ($this->fake_env) {
234 return $this->fake_env->s($key, $def);
235 } else {
236 return Env::s($this->envprefix . $key, $def);
237 }
0f17825b
FB
238 }
239
240 public function t($key, $def = '')
241 {
e21504a3
RB
242 if ($this->fake_env) {
243 return $this->fake_env->t($key, $def);
244 } else {
245 return Env::t($this->envprefix . $key, $def);
246 }
d9b3d712
RB
247 }
248
0f17825b
FB
249 public function i($key, $def = 0)
250 {
e21504a3
RB
251 if ($this->fake_env) {
252 return $this->fake_env->i($key, $def);
253 } else {
254 return Env::i($this->envprefix . $key, $def);
255 }
d9b3d712
RB
256 }
257
0f17825b
FB
258 public function v($key, $def = null)
259 {
e21504a3
RB
260 if ($this->fake_env) {
261 return $this->fake_env->v($key, $def);
262 } else {
263 return Env::v($this->envprefix . $key, $def);
264 }
d9b3d712
RB
265 }
266
0f17825b
FB
267 public function b($key, $def = false)
268 {
e21504a3
RB
269 if ($this->fake_env) {
270 return $this->fake_env->b($key, $def);
271 } else {
272 return Env::b($this->envprefix . $key, $def);
273 }
0f17825b
FB
274 }
275
276 public function has($key)
277 {
e21504a3
RB
278 if ($this->fake_env) {
279 return $this->fake_env->has($key);
280 } else {
281 return Env::has($this->envprefix . $key);
282 }
0f17825b
FB
283 }
284
285 public function blank($key, $strict = false)
286 {
e21504a3
RB
287 if ($this->fake_env) {
288 return $this->fake_env->blank($key, $strict);
289 } else {
290 return Env::blank($key, $strict);
291 }
d9b3d712 292 }
d9696b0a 293
78be0329
FB
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
0f17825b
FB
306 public function isOn($key)
307 {
308 return $this->has($key) && $this->t($key) == 'on';
d9696b0a 309 }
d9b3d712 310}
21b67462 311// }}}
d9b3d712 312
59c6cb70
RB
313// {{{ class UFB_QuickSearch
314class UFB_QuickSearch extends UserFilterBuilder
315{
316 public function __construct($envprefix = '')
317 {
318 $fields = array(
319 new UFBF_Quick('quick', 'Recherche rapide'),
78a47eb4 320 new UFBF_NotRegistered('nonins', 'Non inscrits'),
59c6cb70
RB
321 );
322 parent::__construct($fields, $envprefix);
323 }
324}
325// }}}
326
21b67462
RB
327// {{{ class UFB_AdvancedSearch
328class UFB_AdvancedSearch extends UserFilterBuilder
329{
0f567f55
RB
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 = '')
21b67462
RB
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'),
86ab1c8f
SJ
347 new UFBF_AdminArea('administrativearea', 'Région'),
348 new UFBF_SubAdminArea('subadministrativearea', 'Département'),
21b67462
RB
349
350 new UFBF_JobCompany('entreprise', 'Entreprise'),
21b67462
RB
351 new UFBF_JobDescription('jobdescription', 'Fonction'),
352 new UFBF_JobCv('cv', 'CV'),
3ac45f10 353 new UFBF_JobTerms('jobterm', 'Mots-clefs'),
21b67462
RB
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
fb3b6547
RB
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"),
21b67462
RB
363
364 new UFBF_Comment('free', 'Commentaire'),
3ed7556a
RB
365 new UFBF_Phone('phone_number', 'Téléphone'),
366 new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'),
96f01fba
RB
367
368 new UFBF_Mentor('only_referent', 'Référent'),
21b67462 369 );
0f567f55
RB
370
371 if ($include_admin || $include_ax) {
372 $fields[] = new UFBF_SchoolIds('schoolid_ax', 'Matricule AX', UFC_SchoolId::AX);
373 }
374
21b67462
RB
375 parent::__construct($fields, $envprefix);
376 }
377}
378// }}}
379
6faa0186
RB
380// {{{ class UFB_MentorSearch
381class UFB_MentorSearch extends UserFilterBuilder
382{
383 public function __construct($envprefix = '')
384 {
385 $fields = array(
459e6f81
PC
386 new UFBF_MentorCountry('country'),
387 new UFBF_MentorTerm('jobterm', 'jobtermText'),
6faa0186
RB
388 new UFBF_MentorExpertise('expertise'),
389 );
390 parent::__construct($fields, $envprefix);
391 }
392}
393// }}}
394
e21504a3
RB
395// {{{ class UFB_NewsLetter
396class 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
21b67462 417// {{{ class UFB_Field
d9b3d712
RB
418abstract 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
1d364832 456 if (!$this->isEmpty()) {
d9b3d712
RB
457 $ufc = $this->buildUFC($ufb);
458 if ($ufc != null) {
459 $ufb->addCond($ufc);
460 }
461 }
462 return true;
463 }
464
6faa0186
RB
465 public function isEmpty()
466 {
467 return $this->empty;
468 }
469
d9b3d712
RB
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);
e21504a3
RB
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 }
d9b3d712 492}
21b67462 493// }}}
d9b3d712 494
21b67462 495// {{{ class UFBF_Text
d9b3d712
RB
496abstract class UFBF_Text extends UFB_Field
497{
d9b3d712
RB
498 private $minlength;
499 private $maxlength;
500
f3f800d8 501 public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255)
d9b3d712
RB
502 {
503 parent::__construct($envfield, $formtext);
d9b3d712
RB
504 $this->minlength = $minlength;
505 $this->maxlength = $maxlength;
506 }
507
508 protected function check(UserFilterBuilder &$ufb)
509 {
0f17825b 510 if ($ufb->blank($this->envfield)) {
d9b3d712
RB
511 $this->empty = true;
512 return true;
513 }
514
0f17825b 515 $this->val = $ufb->t($this->envfield);
d9b3d712
RB
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}).");
5a0b9531 520 } else if (preg_match(":[\]\[<>{}~§_`|%$^=]|\*\*:u", $this->val)) {
f3f800d8 521 return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.');
d9b3d712 522 }
f3f800d8 523
d9b3d712
RB
524 return true;
525 }
526}
21b67462 527// }}}
d9b3d712 528
21b67462 529// {{{ class UFBF_Range
d9b3d712
RB
530/** Subclass to use for fields which only allow integers within a range
531 */
532abstract 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 {
0f17825b 547 if ($ufb->blank($this->envfield)) {
d9b3d712
RB
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}
21b67462 561// }}}
d9b3d712 562
21b67462 563// {{{ class UFBF_Index
d9b3d712
RB
564/** Subclass to use for indexed fields
565 */
566abstract class UFBF_Index extends UFB_Field
567{
568 protected function check(UserFilterBuilder &$ufb)
569 {
0f17825b 570 if ($ufb->blank($this->envfield)) {
d9b3d712
RB
571 $this->empty = true;
572 }
6faa0186 573 $this->val = $ufb->i($this->envfield);
d9b3d712
RB
574 return true;
575 }
576}
21b67462 577// }}}
d9b3d712 578
21b67462 579// {{{ class UFBF_Enum
d9b3d712
RB
580/** Subclass to use for fields whose value must belong to a specific set of values
581 */
582abstract class UFBF_Enum extends UFB_Field
583{
21b67462
RB
584 protected $allowedvalues;
585
586 public function __construct($envfield, $formtext = '', $allowedvalues = array(), $strict = false)
d9b3d712
RB
587 {
588 parent::__construct($envfield, $formtext);
589 $this->allowedvalues = $allowedvalues;
21b67462 590 $this->strict = $strict;
d9b3d712
RB
591 }
592
593 protected function check(UserFilterBuilder &$ufb)
594 {
0f17825b 595 if ($ufb->blank($this->envfield)) {
d9b3d712
RB
596 $this->empty = true;
597 return true;
598 }
599
600 $this->val = $ufb->v($this->envfield);
601 if (! in_array($this->val, $this->allowedvalues)) {
21b67462
RB
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 }
d9b3d712
RB
607 }
608 return true;
609 }
610}
21b67462 611// }}}
d9b3d712 612
21b67462
RB
613// {{{ class UFBF_Bool
614abstract class UFBF_Bool extends UFB_Field
d9b3d712 615{
21b67462
RB
616 protected function check(UserFilterBuilder &$ufb)
617 {
0f17825b 618 if ($ufb->blank($this->envfield)) {
21b67462
RB
619 $this->empty = true;
620 return true;
621 }
622
0f17825b 623 $this->val = $ufb->b($this->envfield);
21b67462
RB
624 return true;
625 }
626}
627// }}}
d9b3d712 628
21b67462
RB
629// {{{ class UFBF_Mixed
630/** A class for building UFBFs when the user can input either a text or an ID
631 */
632abstract 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 = '')
d9b3d712 641 {
21b67462
RB
642 parent::__construct($envfieldtext, $formtext);
643 $this->envfieldindex = $envfieldindex;
d9b3d712
RB
644 }
645
21b67462 646 protected function check(UserFilterBuilder &$ufb)
d9b3d712 647 {
78be0329 648 if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) {
21b67462
RB
649 $this->empty = true;
650 return true;
651 }
652
0f17825b 653 if (!$ufb->blank($this->envfieldindex)) {
21b67462
RB
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);
d9b3d712 661 } else {
aab2ffdd 662 $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield),
0f17825b 663 $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS);
21b67462
RB
664 if (count($indexes) == 0) {
665 return false;
666 }
667 $this->val = $indexes;
d9b3d712 668 }
21b67462 669 return true;
d9b3d712 670 }
e21504a3
RB
671
672 public function getEnvFieldNames()
673 {
674 return array($this->envfieldindex, $this->envfield);
675 }
d9b3d712 676}
21b67462 677// }}}
d9b3d712 678
f3f800d8 679// {{{ class UFBF_Quick
59c6cb70
RB
680class UFBF_Quick extends UFB_Field
681{
682 protected function check(UserFilterBuilder &$ufb)
683 {
0f17825b 684 if ($ufb->blank($this->envfield)) {
59c6cb70
RB
685 $this->empty = true;
686 return true;
687 }
688
0f17825b 689 $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield)));
4b2e2074
RB
690
691 return true;
59c6cb70
RB
692 }
693
694 protected function buildUFC(UserFilterBuilder &$ufb)
695 {
59c6cb70 696
4b2e2074 697 $r = $s = $this->val;
59c6cb70
RB
698
699 /** Admin: Email, IP
700 */
701 if (S::admin() && strpos($s, '@') !== false) {
4b2e2074 702 return new UFC_Email($s);
59c6cb70 703 } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
0c457792 704 return new UFC_Ip($s);
59c6cb70
RB
705 }
706
4b2e2074
RB
707 $conds = new PFC_And();
708
59c6cb70
RB
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 }
0f17825b 724 $exact =$ufb->b('exact');
9d590571 725 $conds->addChild(new UFC_NameTokens($strings, $flags, $ufb->b('with_soundex'), $exact));
59c6cb70 726
4b2e2074 727 $ufb->addOrder(new UFO_Score());
59c6cb70
RB
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)) {
4b2e2074 750 $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1]));
59c6cb70
RB
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
0f567f55
RB
767// {{{ class UFBF_SchoolIds
768class UFBF_SchoolIds extends UFB_Field
769{
770 // One of UFC_SchoolId types
771 protected $type;
e21504a3
RB
772 protected $reversed_envfield;
773 protected $reversed = false;
0f567f55 774
e21504a3 775 public function __construct($envfield, $formtext, $type = UFC_SchoolId::AX, $reversed_envfield = '')
0f567f55
RB
776 {
777 parent::__construct($envfield, $formtext);
778 $this->type = $type;
e21504a3
RB
779 if ($reversed_envfield == '') {
780 $reversed_envfield = $envfield . '_reversed';
781 }
782 $this->reversed_envfield = $reversed_envfield;
0f567f55
RB
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
e21504a3 804 $this->reversed = $ufb->b($this->reversed_envfield);
0f567f55
RB
805 $this->val = $ids;
806 return true;
807 }
808
809 protected function buildUFC(UserFilterBuilder &$ufb)
810 {
e21504a3
RB
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 }
0f567f55
RB
817 }
818}
819// }}}
820
21b67462
RB
821// {{{ class UFBF_Name
822class 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 {
0f17825b 839 return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact'));
21b67462
RB
840 }
841}
842// }}}
843
844// {{{ class UFBF_Promo
d9b3d712
RB
845class UFBF_Promo extends UFB_Field
846{
847 private static $validcomps = array('<', '<=', '=', '>=', '>');
848 private $comp;
849 private $envfieldcomp;
850
e21504a3 851 public function __construct($envfield, $formtext = '', $envfieldcomp)
d9b3d712 852 {
e21504a3 853 parent::__construct($envfield, $formtext);
d9b3d712
RB
854 $this->envfieldcomp = $envfieldcomp;
855 }
856
857 protected function check(UserFilterBuilder &$ufb)
858 {
0f17825b 859 if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp)) {
d9b3d712
RB
860 $this->empty = true;
861 return true;
862 }
863
9e6b7376
RB
864 $this->val = $ufb->i($this->envfield);
865 $this->comp = $ufb->v($this->envfieldcomp);
d9b3d712
RB
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) {
999db5aa 881 return new UFC_Promo($this->comp, UserFilter::GRADE_ING, $this->val);
d9b3d712 882 }
e21504a3
RB
883
884 public function getEnvFieldNames()
885 {
886 return array($this->envfield, $this->envfieldcomp);
887 }
d9b3d712 888}
21b67462
RB
889// }}}
890
891// {{{ class UFBF_Sex
892class 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
78a47eb4
RB
918// {{{ class UFBF_NotRegistered
919// Simple field for selecting only alive, not registered users (for quick search)
920class 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
21b67462
RB
934// {{{ class UFBF_Registered
935class 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) {
e637cb69 947 return new PFC_Not(new UFC_Registered());
21b67462
RB
948 }
949 }
950}
951// }}}
d9b3d712 952
21b67462
RB
953// {{{ class UFBF_Dead
954class 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) {
2d9951d8 964 return new PFC_Not(new UFC_Dead());
21b67462
RB
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 */
975class 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;
d9696b0a
RB
982 private $onlycurrentfield;
983
984 public function __construct($envfield, $formtext = '', $type = self::TYPE_ANY, $onlycurrentfield = 'only_current')
21b67462
RB
985 {
986 $this->type = $type;
d9696b0a 987 $this->onlycurrentfield = $onlycurrentfield;
f3f800d8 988 parent::__construct($envfield, $formtext, 2, 30);
21b67462
RB
989 }
990
991 protected function buildUFC(UserFilterBuilder &$ufb)
992 {
d9696b0a
RB
993 if ($ufb->isOn($this->onlycurrentfield)) {
994 $flags = UFC_Address::FLAG_CURRENT;
995 } else {
996 $flags = UFC_Address::FLAG_ANY;
997 }
998
21b67462
RB
999 if (preg_match('/[0-9]/', $this->val)) {
1000 if ($this->type & self::TYPE_ZIP) {
d9696b0a 1001 return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
21b67462
RB
1002 } else {
1003 return new PFC_False();
1004 }
1005 } else {
1f8dfc60 1006 $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val);
d9696b0a 1007 $byzip = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
21b67462
RB
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 }
e21504a3
RB
1017
1018 public function getEnvFieldNames()
1019 {
1020 return array($this->envfield, $this->onlycurrentfield);
1021 }
21b67462
RB
1022}
1023// }}}
1024
1025// {{{ class UFBF_Country
1026class UFBF_Country extends UFBF_Mixed
1027{
1028 protected $direnum = DirEnum::COUNTRIES;
d9696b0a
RB
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 }
21b67462
RB
1036
1037 protected function buildUFC(UserFilterBuilder &$ufb)
1038 {
d9696b0a
RB
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);
21b67462 1046 }
e21504a3
RB
1047
1048 public function getEnvFieldNames()
1049 {
1050 return array($this->envfield, $this->envfieldindex, $this->onlycurrentfield);
1051 }
21b67462
RB
1052}
1053// }}}
1054
1055// {{{ class UFBF_AdminArea
32283a1a 1056class UFBF_AdminArea extends UFBF_Index
21b67462
RB
1057{
1058 protected $direnum = DirEnum::ADMINAREAS;
d9696b0a
RB
1059 protected $onlycurrentfield;
1060
32283a1a 1061 public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
d9696b0a 1062 {
32283a1a 1063 parent::__construct($envfield, $formtext);
d9696b0a
RB
1064 $this->onlycurrentfield = $onlycurrentfield;
1065 }
1066
21b67462
RB
1067
1068 protected function buildUFC(UserFilterBuilder &$ufb)
1069 {
d9696b0a
RB
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);
86ab1c8f 1077 }
e21504a3
RB
1078
1079 public function getEnvFieldNames()
1080 {
1081 return array($this->envfield, $this->onlycurrentfield);
1082 }
86ab1c8f
SJ
1083}
1084// }}}
1085
1086// {{{ class UFBF_SubAdminArea
1087class 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);
21b67462 1108 }
e21504a3
RB
1109
1110 public function getEnvFieldNames()
1111 {
1112 return array($this->envfield, $this->onlycurrentfield);
1113 }
21b67462
RB
1114}
1115// }}}
1116
1117// {{{ class UFBF_JobCompany
1118class UFBF_JobCompany extends UFBF_Text
1119{
d9696b0a
RB
1120 private $onlymentorfield;
1121
1122 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1123 {
1124 parent::__construct($envfield, $formtext);
1125 $this->onlymentorfield = $onlymentorfield;
1126 }
1127
1128 public function check(UserFilterBuilder &$ufb) {
1129 if (parent::check($ufb)) {
1130 # No company check for mentors
1131 if ($ufb->isOn($this->onlymentorfield)) {
1132 $this->empty = true;
1133 }
1134 return true;
1135 } else {
1136 return false;
1137 }
1138 }
1139
21b67462
RB
1140 protected function buildUFC(UserFilterBuilder &$ufb)
1141 {
1142 return new UFC_Job_Company(UFC_Job_Company::JOBNAME, $this->val);
1143 }
e21504a3
RB
1144
1145 public function getEnvFieldNames()
1146 {
1147 return array($this->envfield, $this->onlymentorfield);
1148 }
21b67462
RB
1149}
1150// }}}
1151
3ac45f10
PC
1152// {{{ class UFBF_JobTerms
1153class UFBF_JobTerms extends UFBF_Index
1154{
1155 protected function buildUFC(UserFilterBuilder &$ufb)
1156 {
1157 return new UFC_Job_Terms($this->val);
1158 }
1159}
1160// }}}
1161
21b67462
RB
1162// {{{ class UFBF_JobDescription
1163class UFBF_JobDescription extends UFBF_Text
1164{
d9696b0a
RB
1165 private $onlymentorfield;
1166
1167 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1168 {
1169 parent::__construct($envfield, $formtext);
1170 $this->onlymentorfield = $onlymentorfield;
1171 }
1172
21b67462
RB
1173 protected function buildUFC(UserFilterBuilder &$ufb)
1174 {
d9696b0a
RB
1175 if ($ufb->isOn($this->onlymentorfield)) {
1176 return new UFC_Mentor_Expertise($this->val);
1177 } else {
1178 return new UFC_Job_Description($this->val, UserFilter::JOB_USERDEFINED);
1179 }
21b67462 1180 }
e21504a3
RB
1181
1182 public function getEnvFieldNames()
1183 {
1184 return array($this->envfield, $this->onlymentorfield);
1185 }
21b67462
RB
1186}
1187// }}}
1188
1189// {{{ class UFBF_JobCv
1190class UFBF_JobCv extends UFBF_Text
1191{
d9696b0a
RB
1192 private $onlymentorfield;
1193
1194 public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent')
1195 {
1196 parent::__construct($envfield, $formtext);
1197 $this->onlymentorfield = $onlymentorfield;
1198 }
1199
21b67462
RB
1200 protected function buildUFC(UserFilterBuilder &$ufb)
1201 {
d9696b0a
RB
1202 if ($ufb->isOn($this->onlymentorfield)) {
1203 return new UFC_Mentor_Expertise($this->val);
1204 } else {
1205 return new UFC_Job_Description($this->val, UserFilter::JOB_CV);
1206 }
21b67462 1207 }
e21504a3
RB
1208
1209 public function getEnvFieldNames()
1210 {
1211 return array($this->envfield, $this->onlymentorfield);
1212 }
21b67462
RB
1213}
1214// }}}
1215
1216// {{{ class UFBF_Nationality
1217class UFBF_Nationality extends UFBF_Mixed
1218{
1219 protected $direnum = DirEnum::NATIONALITIES;
1220
1221 protected function buildUFC(UserFilterBuilder &$ufb)
1222 {
1223 return new UFC_Nationality($this->val);
1224 }
1225}
1226// }}}
1227
1228// {{{ class UFBF_Binet
1229class UFBF_Binet extends UFBF_Mixed
1230{
1231 protected $direnum = DirEnum::BINETS;
1232
1233 protected function buildUFC(UserFilterBuilder &$ufb)
1234 {
1235 return new UFC_Binet($this->val);
1236 }
1237}
1238// }}}
1239
1240// {{{ class UFBF_Group
1241class UFBF_Group extends UFBF_Mixed
1242{
1243 protected $direnum = DirEnum::GROUPESX;
1244
1245 protected function buildUFC(UserFilterBuilder &$ufb)
1246 {
1247 if (count($this->val) == 1) {
1248 return new UFC_Group($this->val[0]);
1249 }
1250
1251 $or = new PFC_Or();
1252 foreach ($this->val as $grp) {
1253 $or->addChild(new UFC_Group($grp));
1254 }
1255 return $or;
1256 }
1257}
1258// }}}
1259
1260// {{{ class UFBF_Section
442f967f 1261class UFBF_Section extends UFBF_Mixed
21b67462
RB
1262{
1263 protected $direnum = DirEnum::SECTIONS;
1264
1265 protected function buildUFC(UserFilterBuilder &$ufb)
1266 {
1267 return new UFC_Section($this->val);
1268 }
1269}
1270// }}}
1271
fb3b6547
RB
1272// {{{ class UFBF_EducationSchool
1273class UFBF_EducationSchool extends UFBF_Mixed
21b67462 1274{
fb3b6547 1275 protected $direnum = DirEnum::EDUSCHOOLS;
21b67462
RB
1276
1277 protected function buildUFC(UserFilterBuilder &$ufb)
1278 {
fb3b6547 1279 return new UFC_EducationSchool($this->val);
21b67462
RB
1280 }
1281}
1282// }}}
1283
fb3b6547
RB
1284// {{{ class UFBF_EducationDegree
1285class UFBF_EducationDegree extends UFBF_Mixed
21b67462 1286{
fb3b6547 1287 protected $direnum = DirEnum::EDUDEGREES;
21b67462
RB
1288
1289 protected function buildUFC(UserFilterBuilder &$ufb)
1290 {
fb3b6547 1291 return new UFC_EducationDegree($this->val);
21b67462
RB
1292 }
1293}
1294// }}}
1295
fb3b6547
RB
1296// {{{ class UFBF_EducationField
1297class UFBF_EducationField extends UFBF_Mixed
21b67462 1298{
fb3b6547 1299 protected $direnum = DirEnum::EDUFIELDS;
21b67462
RB
1300
1301 protected function buildUFC(UserFilterBuilder &$ufb)
1302 {
fb3b6547 1303 return new UFC_EducationField($this->val);
21b67462
RB
1304 }
1305}
1306// }}}
1307
1308// {{{ class UFBF_Comment
1309class UFBF_Comment extends UFBF_Text
1310{
1311 protected function buildUFC(UserFilterBuilder &$ufb)
1312 {
1313 return new UFC_Comment($this->val);
1314 }
1315}
1316// }}}
3ed7556a
RB
1317
1318// {{{ class UFBF_Phone
1319class UFBF_Phone extends UFBF_Text
1320{
1321 protected function buildUFC(UserFilterBuilder &$ufb)
1322 {
1323 return new UFC_Phone($this->val);
1324 }
1325}
1326// }}}
1327
1328// {{{ class UFBF_Networking
1329class UFBF_Networking extends UFBF_Text
1330{
1331 private $networktypefield;
1332 private $nwtype;
1333
1334 public function __construct($envfield, $networktypefield, $formtext = '')
1335 {
1336 parent::__construct($envfield, $formtext);
1337 $this->networktypefield = $networktypefield;
1338 }
1339
1340 public function check(UserFilterBuilder &$ufb)
1341 {
1342 if (parent::check($ufb)) {
1343 $this->nwtype = $ufb->i($this->networktypefield);
1344 return true;
1345 } else {
1346 return false;
1347 }
1348 }
1349
1d364832
RB
1350 public function isEmpty()
1351 {
1352 return parent::isEmpty() || $this->nwtype == 0;
1353 }
1354
3ed7556a
RB
1355 public function buildUFC(UserFilterBuilder &$ufb)
1356 {
1357 return new UFC_Networking($this->nwtype, $this->val);
1358 }
e21504a3
RB
1359
1360 public function getEnvFieldNames()
1361 {
1362 return array($this->envfield, $this->networktypefield);
1363 }
3ed7556a
RB
1364}
1365// }}}
6faa0186 1366
96f01fba
RB
1367// {{{ class UFBF_Mentor
1368class UFBF_Mentor extends UFBF_Bool
1369{
1370 protected function buildUFC(UserFilterBuilder &$ufb)
1371 {
1372 return new UFC_Mentor();
1373 }
1374}
1375// }}}
1376
6faa0186 1377// {{{ class UFBF_MentorCountry
459e6f81 1378class UFBF_MentorCountry extends UFBF_Text
6faa0186
RB
1379{
1380 protected function buildUFC(UserFilterBuilder &$ufb)
1381 {
1382 return new UFC_Mentor_Country($this->val);
1383 }
1384}
1385// }}}
1386
459e6f81
PC
1387// {{{ class UFBF_Mentorterm
1388class UFBF_MentorTerm extends UFBF_Index
1389{
1390 protected function buildUFC(UserFilterBuilder &$ufb)
1391 {
1392 return new UFC_Mentor_Terms($this->val);
1393 }
1394}
1395// }}}
1396
6faa0186
RB
1397// {{{ class UFBF_MentorExpertise
1398class UFBF_MentorExpertise extends UFBF_Text
1399{
1400 protected function buildUFC(UserFilterBuilder &$ufb)
1401 {
1402 return new UFC_Mentor_Expertise($this->val);
1403 }
1404}
1405// }}}
05fa89a5
FB
1406
1407// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
d9b3d712 1408?>