| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 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 UserFilterBuilder |
| 23 | class UserFilterBuilder |
| 24 | { |
| 25 | private $envprefix; |
| 26 | private $fields; |
| 27 | private $valid = true; |
| 28 | private $ufc = null; |
| 29 | private $orders = array(); |
| 30 | |
| 31 | /** Constructor |
| 32 | * @param $fields An array of UFB_Field objects |
| 33 | * @param $envprefix Prefix to use for parts of the query |
| 34 | */ |
| 35 | public function __construct($fields, $envprefix = '') |
| 36 | { |
| 37 | $this->fields = $fields; |
| 38 | $this->envprefix = $envprefix; |
| 39 | } |
| 40 | |
| 41 | /** Builds the UFC; returns as soon as a field says it is invalid |
| 42 | */ |
| 43 | private function buildUFC() |
| 44 | { |
| 45 | if ($this->ufc != null) { |
| 46 | return; |
| 47 | } |
| 48 | $this->ufc = new PFC_And(); |
| 49 | |
| 50 | foreach ($this->fields as $field) { |
| 51 | $this->valid = $field->apply(&$this); |
| 52 | if (!$this->valid) { |
| 53 | return; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function addCond(PlFilterCondition &$cond) |
| 59 | { |
| 60 | $this->ufc->addChild($cond); |
| 61 | } |
| 62 | |
| 63 | public function addOrder(PlFilterOrder &$order) |
| 64 | { |
| 65 | $this->order[] = $order; |
| 66 | } |
| 67 | |
| 68 | public function isValid() |
| 69 | { |
| 70 | $this->buildUFC(); |
| 71 | return $this->valid; |
| 72 | } |
| 73 | |
| 74 | public function isEmpty() |
| 75 | { |
| 76 | $this->buildUFC(); |
| 77 | foreach ($this->fields as $field) { |
| 78 | if (! $field->isEmpty()) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** Returns the built UFC |
| 86 | * @return The UFC, or PFC_False() if an error happened |
| 87 | */ |
| 88 | public function getUFC() |
| 89 | { |
| 90 | $this->buildUFC(); |
| 91 | if ($this->valid) { |
| 92 | return $this->ufc; |
| 93 | } else { |
| 94 | return new PFC_False(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** Returns adequate orders |
| 99 | */ |
| 100 | public function getOrders() |
| 101 | { |
| 102 | $this->buildUFC(); |
| 103 | return $this->orders; |
| 104 | } |
| 105 | |
| 106 | /** Wrappers around Env::i/s/..., to add envprefix |
| 107 | */ |
| 108 | public function s($key, $def = '') |
| 109 | { |
| 110 | return Env::s($this->envprefix . $key, $def); |
| 111 | } |
| 112 | |
| 113 | public function t($key, $def = '') |
| 114 | { |
| 115 | return Env::t($this->envprefix . $key, $def); |
| 116 | } |
| 117 | |
| 118 | public function i($key, $def = 0) |
| 119 | { |
| 120 | return Env::i($this->envprefix . $key, $def); |
| 121 | } |
| 122 | |
| 123 | public function v($key, $def = null) |
| 124 | { |
| 125 | return Env::v($this->envprefix . $key, $def); |
| 126 | } |
| 127 | |
| 128 | public function b($key, $def = false) |
| 129 | { |
| 130 | return Env::b($this->envprefix . $key, $def); |
| 131 | } |
| 132 | |
| 133 | public function has($key) |
| 134 | { |
| 135 | return Env::has($this->envprefix . $key); |
| 136 | } |
| 137 | |
| 138 | public function blank($key, $strict = false) |
| 139 | { |
| 140 | return Env::blank($key, $strict); |
| 141 | } |
| 142 | |
| 143 | public function hasAlnum($key) |
| 144 | { |
| 145 | $str = $this->s($key); |
| 146 | return preg_match('/[a-z0-9]/i', $str); |
| 147 | } |
| 148 | |
| 149 | public function hasAlpha($key) |
| 150 | { |
| 151 | $str = $this->s($key); |
| 152 | return preg_match('/[a-z]/i', $str); |
| 153 | } |
| 154 | |
| 155 | public function isOn($key) |
| 156 | { |
| 157 | return $this->has($key) && $this->t($key) == 'on'; |
| 158 | } |
| 159 | } |
| 160 | // }}} |
| 161 | |
| 162 | // {{{ class UFB_QuickSearch |
| 163 | class UFB_QuickSearch extends UserFilterBuilder |
| 164 | { |
| 165 | public function __construct($envprefix = '') |
| 166 | { |
| 167 | $fields = array( |
| 168 | new UFBF_Quick('quick', 'Recherche rapide'), |
| 169 | ); |
| 170 | parent::__construct($fields, $envprefix); |
| 171 | } |
| 172 | } |
| 173 | // }}} |
| 174 | |
| 175 | // {{{ class UFB_AdvancedSearch |
| 176 | class UFB_AdvancedSearch extends UserFilterBuilder |
| 177 | { |
| 178 | public function __construct($envprefix = '') |
| 179 | { |
| 180 | $fields = array( |
| 181 | new UFBF_Name('name', 'Nom'), |
| 182 | new UFBF_Promo('promo1', 'Promotion', 'egal1'), |
| 183 | new UFBF_Promo('promo2', 'Promotion', 'egal2'), |
| 184 | new UFBF_Sex('woman', 'Sexe'), |
| 185 | new UFBF_Registered('subscriber', 'Inscrit'), |
| 186 | new UFBF_Dead('alive', 'En vie'), |
| 187 | |
| 188 | new UFBF_Town('city', 'Ville / Code Postal'), |
| 189 | new UFBF_Country('countryTxt', 'country', 'Pays'), |
| 190 | new UFBF_AdminArea('region', 'Région'), |
| 191 | |
| 192 | new UFBF_JobCompany('entreprise', 'Entreprise'), |
| 193 | new UFBF_JobSector('sector', 'Poste'), |
| 194 | new UFBF_JobDescription('jobdescription', 'Fonction'), |
| 195 | new UFBF_JobCv('cv', 'CV'), |
| 196 | new UFBF_JobTerms('jobterm', 'Mots-clefs'), |
| 197 | |
| 198 | new UFBF_Nationality('nationaliteTxt', 'nationalite', 'Nationalité'), |
| 199 | new UFBF_Binet('binetTxt', 'binet', 'Binet'), |
| 200 | new UFBF_Group('groupexTxt', 'groupex', 'Groupe X'), |
| 201 | new UFBF_Section('sectionTxt', 'section', 'Section'), |
| 202 | |
| 203 | new UFBF_EducationSchool('schoolTxt', 'school', "École d'application"), |
| 204 | new UFBF_EducationDegree('diplomaTxt', 'diploma', 'Diplôme'), |
| 205 | new UFBF_EducationField('fieldTxt', 'field', "Domaine d'études"), |
| 206 | |
| 207 | new UFBF_Comment('free', 'Commentaire'), |
| 208 | new UFBF_Phone('phone_number', 'Téléphone'), |
| 209 | new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'), |
| 210 | |
| 211 | new UFBF_Mentor('only_referent', 'Référent'), |
| 212 | ); |
| 213 | parent::__construct($fields, $envprefix); |
| 214 | } |
| 215 | } |
| 216 | // }}} |
| 217 | |
| 218 | // {{{ class UFB_MentorSearch |
| 219 | class UFB_MentorSearch extends UserFilterBuilder |
| 220 | { |
| 221 | public function __construct($envprefix = '') |
| 222 | { |
| 223 | $fields = array( |
| 224 | new UFBF_MentorCountry('country'), |
| 225 | new UFBF_MentorTerm('jobterm', 'jobtermText'), |
| 226 | new UFBF_MentorExpertise('expertise'), |
| 227 | ); |
| 228 | parent::__construct($fields, $envprefix); |
| 229 | } |
| 230 | } |
| 231 | // }}} |
| 232 | |
| 233 | // {{{ class UFB_Field |
| 234 | abstract class UFB_Field |
| 235 | { |
| 236 | protected $envfield; |
| 237 | protected $formtext; |
| 238 | |
| 239 | protected $empty = false; |
| 240 | protected $val = null; |
| 241 | |
| 242 | /** Constructor |
| 243 | * @param $envfield Name of the field in the environment |
| 244 | * @param $formtext User-friendly name of that field |
| 245 | */ |
| 246 | public function __construct($envfield, $formtext = '') |
| 247 | { |
| 248 | $this->envfield = $envfield; |
| 249 | if ($formtext != '') { |
| 250 | $this->formtext = $formtext; |
| 251 | } else { |
| 252 | $formtext = ucfirst($envfield); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** Prints the given error message to the user, and returns false |
| 257 | * in order to be used as return $this->raise('ERROR'); |
| 258 | * |
| 259 | * All %s in the $msg will be replaced with the formtext. |
| 260 | */ |
| 261 | protected function raise($msg) |
| 262 | { |
| 263 | Platal::page()->trigError(str_replace('%s', $this->formtext, $msg)); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | public function apply(UserFilterBuilder &$ufb) { |
| 268 | if (!$this->check($ufb)) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | if (!$this->empty) { |
| 273 | $ufc = $this->buildUFC($ufb); |
| 274 | if ($ufc != null) { |
| 275 | $ufb->addCond($ufc); |
| 276 | } |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | public function isEmpty() |
| 282 | { |
| 283 | return $this->empty; |
| 284 | } |
| 285 | |
| 286 | /** Create the UFC associated to the field; won't be called |
| 287 | * if the field is "empty" |
| 288 | * @param &$ufb UFB to which fields must be added |
| 289 | * @return UFC |
| 290 | */ |
| 291 | abstract protected function buildUFC(UserFilterBuilder &$ufb); |
| 292 | |
| 293 | /** This function is intended to run consistency checks on the value |
| 294 | * @return boolean Whether the input is valid |
| 295 | */ |
| 296 | abstract protected function check(UserFilterBuilder &$ufb); |
| 297 | } |
| 298 | // }}} |
| 299 | |
| 300 | // {{{ class UFBF_Text |
| 301 | abstract class UFBF_Text extends UFB_Field |
| 302 | { |
| 303 | private $minlength; |
| 304 | private $maxlength; |
| 305 | |
| 306 | public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255) |
| 307 | { |
| 308 | parent::__construct($envfield, $formtext); |
| 309 | $this->minlength = $minlength; |
| 310 | $this->maxlength = $maxlength; |
| 311 | } |
| 312 | |
| 313 | protected function check(UserFilterBuilder &$ufb) |
| 314 | { |
| 315 | if ($ufb->blank($this->envfield)) { |
| 316 | $this->empty = true; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | $this->val = $ufb->t($this->envfield); |
| 321 | if (strlen($this->val) < $this->minlength) { |
| 322 | return $this->raise("Le champ %s est trop court (minimum {$this->minlength})."); |
| 323 | } else if (strlen($this->val) > $this->maxlength) { |
| 324 | return $this->raise("Le champ %s est trop long (maximum {$this->maxlength})."); |
| 325 | } else if (preg_match(":[\]\[<>{}~/§_`|%$^=+]|\*\*:u", $this->val)) { |
| 326 | return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.'); |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | } |
| 332 | // }}} |
| 333 | |
| 334 | // {{{ class UFBF_Range |
| 335 | /** Subclass to use for fields which only allow integers within a range |
| 336 | */ |
| 337 | abstract class UFBF_Range extends UFB_Field |
| 338 | { |
| 339 | |
| 340 | private $min; |
| 341 | private $max; |
| 342 | |
| 343 | public function __construct($envfield, $formtext = '', $min = 0, $max = 65535) |
| 344 | { |
| 345 | parent::__construct($envfield, $formtext); |
| 346 | $this->min = $min; |
| 347 | $this->max = $max; |
| 348 | } |
| 349 | |
| 350 | protected function check(UserFilterBuilder &$ufb) |
| 351 | { |
| 352 | if ($ufb->blank($this->envfield)) { |
| 353 | $this->empty = true; |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | $this->val = $ufb->i($this->envfield); |
| 358 | if ($this->val < $this->min) { |
| 359 | return $this->raise("Le champs %s est inférieur au minimum ({$this->min})."); |
| 360 | } else if ($this->val > $this->max) { |
| 361 | return $this->raise("Le champ %s est supérieur au maximum ({$this->max})."); |
| 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | // }}} |
| 367 | |
| 368 | // {{{ class UFBF_Index |
| 369 | /** Subclass to use for indexed fields |
| 370 | */ |
| 371 | abstract class UFBF_Index extends UFB_Field |
| 372 | { |
| 373 | protected function check(UserFilterBuilder &$ufb) |
| 374 | { |
| 375 | if ($ufb->blank($this->envfield)) { |
| 376 | $this->empty = true; |
| 377 | } |
| 378 | $this->val = $ufb->i($this->envfield); |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | // }}} |
| 383 | |
| 384 | // {{{ class UFBF_Enum |
| 385 | /** Subclass to use for fields whose value must belong to a specific set of values |
| 386 | */ |
| 387 | abstract class UFBF_Enum extends UFB_Field |
| 388 | { |
| 389 | protected $allowedvalues; |
| 390 | |
| 391 | public function __construct($envfield, $formtext = '', $allowedvalues = array(), $strict = false) |
| 392 | { |
| 393 | parent::__construct($envfield, $formtext); |
| 394 | $this->allowedvalues = $allowedvalues; |
| 395 | $this->strict = $strict; |
| 396 | } |
| 397 | |
| 398 | protected function check(UserFilterBuilder &$ufb) |
| 399 | { |
| 400 | if ($ufb->blank($this->envfield)) { |
| 401 | $this->empty = true; |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | $this->val = $ufb->v($this->envfield); |
| 406 | if (! in_array($this->val, $this->allowedvalues)) { |
| 407 | if ($this->strict) { |
| 408 | return $this->raise("La valeur {$this->val} n'est pas valide pour le champ %s."); |
| 409 | } else { |
| 410 | $this->empty = true; |
| 411 | } |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | } |
| 416 | // }}} |
| 417 | |
| 418 | // {{{ class UFBF_Bool |
| 419 | abstract class UFBF_Bool extends UFB_Field |
| 420 | { |
| 421 | protected function check(UserFilterBuilder &$ufb) |
| 422 | { |
| 423 | if ($ufb->blank($this->envfield)) { |
| 424 | $this->empty = true; |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | $this->val = $ufb->b($this->envfield); |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | // }}} |
| 433 | |
| 434 | // {{{ class UFBF_Mixed |
| 435 | /** A class for building UFBFs when the user can input either a text or an ID |
| 436 | */ |
| 437 | abstract class UFBF_Mixed extends UFB_Field |
| 438 | { |
| 439 | /** Name of the DirEnum on which class is based |
| 440 | */ |
| 441 | protected $direnum; |
| 442 | |
| 443 | protected $envfieldindex; |
| 444 | |
| 445 | public function __construct($envfieldtext, $envfieldindex, $formtext = '') |
| 446 | { |
| 447 | parent::__construct($envfieldtext, $formtext); |
| 448 | $this->envfieldindex = $envfieldindex; |
| 449 | } |
| 450 | |
| 451 | protected function check(UserFilterBuilder &$ufb) |
| 452 | { |
| 453 | if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) { |
| 454 | $this->empty = true; |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | if (!$ufb->blank($this->envfieldindex)) { |
| 459 | $index = $ufb->v($this->envfieldindex); |
| 460 | if (is_int($index)) { |
| 461 | $index = intval($index); |
| 462 | } else { |
| 463 | $index = strtoupper($index); |
| 464 | } |
| 465 | $this->val = array($index); |
| 466 | } else { |
| 467 | $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield), |
| 468 | $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS); |
| 469 | if (count($indexes) == 0) { |
| 470 | return false; |
| 471 | } |
| 472 | $this->val = $indexes; |
| 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | } |
| 477 | // }}} |
| 478 | |
| 479 | // {{{ class UFBF_Quick |
| 480 | class UFBF_Quick extends UFB_Field |
| 481 | { |
| 482 | protected function check(UserFilterBuilder &$ufb) |
| 483 | { |
| 484 | if ($ufb->blank($this->envfield)) { |
| 485 | $this->empty = true; |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield))); |
| 490 | |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 495 | { |
| 496 | |
| 497 | $r = $s = $this->val; |
| 498 | |
| 499 | /** Admin: Email, IP |
| 500 | */ |
| 501 | if (S::admin() && strpos($s, '@') !== false) { |
| 502 | return new UFC_Email($s); |
| 503 | } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) { |
| 504 | return new UFC_Ip($s); |
| 505 | } |
| 506 | |
| 507 | $conds = new PFC_And(); |
| 508 | |
| 509 | /** Name |
| 510 | */ |
| 511 | $s = preg_replace('!\d+!', ' ', $s); |
| 512 | $strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY); |
| 513 | if (count($strings) > 5) { |
| 514 | Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte"); |
| 515 | $strings = array_slice($strings, 0, 5); |
| 516 | } |
| 517 | |
| 518 | if (count($strings)) { |
| 519 | if (S::logged()) { |
| 520 | $flags = array(); |
| 521 | } else { |
| 522 | $flags = array('public'); |
| 523 | } |
| 524 | if ($ufb->b('with_soundex')) { |
| 525 | $soundex = true; |
| 526 | $st = array(); |
| 527 | foreach ($strings as $string) { |
| 528 | $st[] = soundex_fr($string); |
| 529 | } |
| 530 | } else { |
| 531 | $soundex = false; |
| 532 | $st = $strings; |
| 533 | } |
| 534 | $exact =$ufb->b('exact'); |
| 535 | $conds->addChild(new UFC_NameTokens($st, $flags, $soundex, $exact)); |
| 536 | |
| 537 | $ufb->addOrder(new UFO_Score()); |
| 538 | } |
| 539 | |
| 540 | /** Promo ranges |
| 541 | */ |
| 542 | $s = preg_replace('! *- *!', '-', $r); |
| 543 | $s = preg_replace('!([<>]) *!', ' \1', $s); |
| 544 | $s = preg_replace('![^0-9\-><]!', ' ', $s); |
| 545 | $s = preg_replace('![<>\-] !', '', $s); |
| 546 | $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY); |
| 547 | foreach ($ranges as $r) { |
| 548 | if (preg_match('!^\d{4}$!', $r)) { |
| 549 | $conds->addChild(new UFC_Promo('=', UserFilter::DISPLAY, 'X' . $r)); |
| 550 | } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) { |
| 551 | $p1=min(intval($matches[1]), intval($matches[2])); |
| 552 | $p2=max(intval($matches[1]), intval($matches[2])); |
| 553 | $conds->addChild(new PFC_And( |
| 554 | new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $p1), |
| 555 | new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $p2) |
| 556 | )); |
| 557 | } elseif (preg_match('!^<(\d{4})!', $r, $matches)) { |
| 558 | $conds->addChild(new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $matches[1])); |
| 559 | } elseif (preg_match('!^>(\d{4})!', $r, $matches)) { |
| 560 | $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1])); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** Phone number |
| 565 | */ |
| 566 | $t = preg_replace('!(\d{4}-\d{4}|>\d{4}|<\d{4})!', '', $s); |
| 567 | $t = preg_replace('![<>\- ]!', '', $t); |
| 568 | if (strlen($t) > 4) { |
| 569 | $conds->addChild(new UFC_Phone($t)); |
| 570 | } |
| 571 | |
| 572 | return $conds; |
| 573 | } |
| 574 | } |
| 575 | // }}} |
| 576 | |
| 577 | // {{{ class UFBF_Name |
| 578 | class UFBF_Name extends UFBF_Text |
| 579 | { |
| 580 | protected function check(UserFilterBuilder &$ufb) |
| 581 | { |
| 582 | if (!parent::check($ufb)) { |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | $this->val = preg_split('/[[:space:]]/', $this->val); |
| 587 | if (count($this->val) == 0) { |
| 588 | $this->empty = true; |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 594 | { |
| 595 | return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact')); |
| 596 | } |
| 597 | } |
| 598 | // }}} |
| 599 | |
| 600 | // {{{ class UFBF_Promo |
| 601 | class UFBF_Promo extends UFB_Field |
| 602 | { |
| 603 | private static $validcomps = array('<', '<=', '=', '>=', '>'); |
| 604 | private $comp; |
| 605 | private $envfieldcomp; |
| 606 | |
| 607 | public function __construct($envfield, $fromtext = '', $envfieldcomp) |
| 608 | { |
| 609 | parent::__construct($envfield, $fromtext); |
| 610 | $this->envfieldcomp = $envfieldcomp; |
| 611 | } |
| 612 | |
| 613 | protected function check(UserFilterBuilder &$ufb) |
| 614 | { |
| 615 | if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp)) { |
| 616 | $this->empty = true; |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | $this->val = $ufb->i($this->envfield); |
| 621 | $this->comp = $ufb->v($this->envfieldcomp); |
| 622 | |
| 623 | if (!in_array($this->comp, self::$validcomps)) { |
| 624 | return $this->raise("Le critère {$this->comp} n'est pas valide pour le champ %s"); |
| 625 | } |
| 626 | |
| 627 | if (preg_match('/^[0-9]{2}$/', $this->val)) { |
| 628 | $this->val += 1900; |
| 629 | } |
| 630 | if ($this->val < 1900 || $this->val > 9999) { |
| 631 | return $this->raise("Le champ %s doit être une année à 4 chiffres."); |
| 632 | } |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | protected function buildUFC(UserFilterBuilder &$ufb) { |
| 637 | return new UFC_Promo($this->comp, UserFilter::GRADE_ING, $this->val); |
| 638 | } |
| 639 | } |
| 640 | // }}} |
| 641 | |
| 642 | // {{{ class UFBF_Sex |
| 643 | class UFBF_Sex extends UFBF_Enum |
| 644 | { |
| 645 | public function __construct($envfield, $formtext = '') |
| 646 | { |
| 647 | parent::__construct($envfield, $formtext, array(1, 2)); |
| 648 | } |
| 649 | |
| 650 | private static function getVal($id) |
| 651 | { |
| 652 | switch($id) { |
| 653 | case 1: |
| 654 | return User::GENDER_MALE; |
| 655 | break; |
| 656 | case 2: |
| 657 | return User::GENDER_FEMALE; |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 663 | { |
| 664 | return new UFC_Sex(self::getVal($this->val)); |
| 665 | } |
| 666 | } |
| 667 | // }}} |
| 668 | |
| 669 | // {{{ class UFBF_Registered |
| 670 | class UFBF_Registered extends UFBF_Enum |
| 671 | { |
| 672 | public function __construct($envfield, $formtext = '') |
| 673 | { |
| 674 | parent::__construct($envfield, $formtext, array(1, 2)); |
| 675 | } |
| 676 | |
| 677 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 678 | { |
| 679 | if ($this->val == 1) { |
| 680 | return new UFC_Registered(); |
| 681 | } else if ($this->val == 2) { |
| 682 | return new PFC_Not(new UFC_Registered()); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | // }}} |
| 687 | |
| 688 | // {{{ class UFBF_Dead |
| 689 | class UFBF_Dead extends UFBF_Enum |
| 690 | { |
| 691 | public function __construct($envfield, $formtext = '') |
| 692 | { |
| 693 | parent::__construct($envfield, $formtext, array(1, 2)); |
| 694 | } |
| 695 | |
| 696 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 697 | { |
| 698 | if ($this->val == 1) { |
| 699 | return new PFC_Not(new UFC_Dead()); |
| 700 | } else if ($this->val == 2) { |
| 701 | return new UFC_Dead(); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | // }}} |
| 706 | |
| 707 | // {{{ class UFBF_Town |
| 708 | /** Retrieves a town, either from a postal code or a town name |
| 709 | */ |
| 710 | class UFBF_Town extends UFBF_Text |
| 711 | { |
| 712 | const TYPE_TEXT = 1; |
| 713 | const TYPE_ZIP = 2; |
| 714 | const TYPE_ANY = 3; |
| 715 | |
| 716 | private $type; |
| 717 | private $onlycurrentfield; |
| 718 | |
| 719 | public function __construct($envfield, $formtext = '', $type = self::TYPE_ANY, $onlycurrentfield = 'only_current') |
| 720 | { |
| 721 | $this->type = $type; |
| 722 | $this->onlycurrentfield = $onlycurrentfield; |
| 723 | parent::__construct($envfield, $formtext, 2, 30); |
| 724 | } |
| 725 | |
| 726 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 727 | { |
| 728 | if ($ufb->isOn($this->onlycurrentfield)) { |
| 729 | $flags = UFC_Address::FLAG_CURRENT; |
| 730 | } else { |
| 731 | $flags = UFC_Address::FLAG_ANY; |
| 732 | } |
| 733 | |
| 734 | if (preg_match('/[0-9]/', $this->val)) { |
| 735 | if ($this->type & self::TYPE_ZIP) { |
| 736 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags); |
| 737 | } else { |
| 738 | return new PFC_False(); |
| 739 | } |
| 740 | } else { |
| 741 | $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val); |
| 742 | $byzip = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags); |
| 743 | if ($this->type & self::TYPE_ANY) { |
| 744 | return new PFC_Or($byname, $byzip); |
| 745 | } else if ($this->type & self::TYPE_TEXT) { |
| 746 | return $byname; |
| 747 | } else { |
| 748 | return $byzip; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | // }}} |
| 754 | |
| 755 | // {{{ class UFBF_Country |
| 756 | class UFBF_Country extends UFBF_Mixed |
| 757 | { |
| 758 | protected $direnum = DirEnum::COUNTRIES; |
| 759 | protected $onlycurrentfield; |
| 760 | |
| 761 | public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current') |
| 762 | { |
| 763 | parent::__construct($envfieldtext, $envfieldindex, $formtext); |
| 764 | $this->onlycurrentfield = $onlycurrentfield; |
| 765 | } |
| 766 | |
| 767 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 768 | { |
| 769 | if ($ufb->isOn($this->onlycurrentfield)) { |
| 770 | $flags = UFC_Address::FLAG_CURRENT; |
| 771 | } else { |
| 772 | $flags = UFC_Address::FLAG_ANY; |
| 773 | } |
| 774 | |
| 775 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_COUNTRY, UFC_Address::TYPE_ANY, $flags); |
| 776 | } |
| 777 | } |
| 778 | // }}} |
| 779 | |
| 780 | // {{{ class UFBF_AdminArea |
| 781 | class UFBF_AdminArea extends UFBF_Mixed |
| 782 | { |
| 783 | protected $direnum = DirEnum::ADMINAREAS; |
| 784 | protected $onlycurrentfield; |
| 785 | |
| 786 | public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current') |
| 787 | { |
| 788 | parent::__construct($envfieldtext, $envfieldindex, $formtext); |
| 789 | $this->onlycurrentfield = $onlycurrentfield; |
| 790 | } |
| 791 | |
| 792 | |
| 793 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 794 | { |
| 795 | if ($ufb->isOn($this->onlycurrentfield)) { |
| 796 | $flags = UFC_Address::FLAG_CURRENT; |
| 797 | } else { |
| 798 | $flags = UFC_Address::FLAG_ANY; |
| 799 | } |
| 800 | |
| 801 | return new UFC_AddressField($this->val, UFC_AddressField::FIELD_ADMAREA, UFC_Address::TYPE_ANY, $flags); |
| 802 | } |
| 803 | } |
| 804 | // }}} |
| 805 | |
| 806 | // {{{ class UFBF_JobCompany |
| 807 | class UFBF_JobCompany extends UFBF_Text |
| 808 | { |
| 809 | private $onlymentorfield; |
| 810 | |
| 811 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') |
| 812 | { |
| 813 | parent::__construct($envfield, $formtext); |
| 814 | $this->onlymentorfield = $onlymentorfield; |
| 815 | } |
| 816 | |
| 817 | public function check(UserFilterBuilder &$ufb) { |
| 818 | if (parent::check($ufb)) { |
| 819 | # No company check for mentors |
| 820 | if ($ufb->isOn($this->onlymentorfield)) { |
| 821 | $this->empty = true; |
| 822 | } |
| 823 | return true; |
| 824 | } else { |
| 825 | return false; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 830 | { |
| 831 | return new UFC_Job_Company(UFC_Job_Company::JOBNAME, $this->val); |
| 832 | } |
| 833 | } |
| 834 | // }}} |
| 835 | |
| 836 | // {{{ class UFBF_JobSector |
| 837 | class UFBF_JobSector extends UFBF_Mixed |
| 838 | { |
| 839 | protected $direnum = DirEnum::SECTORS; |
| 840 | private $onlymentorfield; |
| 841 | |
| 842 | public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlymentorfield = 'only_referent') |
| 843 | { |
| 844 | parent::__construct($envfieldtext, $envfieldindex, $formtext); |
| 845 | $this->onlymentorfield = $onlymentorfield; |
| 846 | } |
| 847 | |
| 848 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 849 | { |
| 850 | if ($ufb->isOn($this->onlymentorfield)) { |
| 851 | return new UFC_Mentor_Sectorization($this->val, UserFilter::JOB_SUBSECTOR); |
| 852 | } else { |
| 853 | return new UFC_Job_Sectorization($this->val, UserFilter::JOB_SUBSUBSECTOR); |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | // }}} |
| 858 | |
| 859 | // {{{ class UFBF_JobTerms |
| 860 | class UFBF_JobTerms extends UFBF_Index |
| 861 | { |
| 862 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 863 | { |
| 864 | return new UFC_Job_Terms($this->val); |
| 865 | } |
| 866 | } |
| 867 | // }}} |
| 868 | |
| 869 | // {{{ class UFBF_JobDescription |
| 870 | class UFBF_JobDescription extends UFBF_Text |
| 871 | { |
| 872 | private $onlymentorfield; |
| 873 | |
| 874 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') |
| 875 | { |
| 876 | parent::__construct($envfield, $formtext); |
| 877 | $this->onlymentorfield = $onlymentorfield; |
| 878 | } |
| 879 | |
| 880 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 881 | { |
| 882 | if ($ufb->isOn($this->onlymentorfield)) { |
| 883 | return new UFC_Mentor_Expertise($this->val); |
| 884 | } else { |
| 885 | return new UFC_Job_Description($this->val, UserFilter::JOB_USERDEFINED); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | // }}} |
| 890 | |
| 891 | // {{{ class UFBF_JobCv |
| 892 | class UFBF_JobCv extends UFBF_Text |
| 893 | { |
| 894 | private $onlymentorfield; |
| 895 | |
| 896 | public function __construct($envfield, $formtext = '', $onlymentorfield = 'only_referent') |
| 897 | { |
| 898 | parent::__construct($envfield, $formtext); |
| 899 | $this->onlymentorfield = $onlymentorfield; |
| 900 | } |
| 901 | |
| 902 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 903 | { |
| 904 | if ($ufb->isOn($this->onlymentorfield)) { |
| 905 | return new UFC_Mentor_Expertise($this->val); |
| 906 | } else { |
| 907 | return new UFC_Job_Description($this->val, UserFilter::JOB_CV); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | // }}} |
| 912 | |
| 913 | // {{{ class UFBF_Nationality |
| 914 | class UFBF_Nationality extends UFBF_Mixed |
| 915 | { |
| 916 | protected $direnum = DirEnum::NATIONALITIES; |
| 917 | |
| 918 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 919 | { |
| 920 | return new UFC_Nationality($this->val); |
| 921 | } |
| 922 | } |
| 923 | // }}} |
| 924 | |
| 925 | // {{{ class UFBF_Binet |
| 926 | class UFBF_Binet extends UFBF_Mixed |
| 927 | { |
| 928 | protected $direnum = DirEnum::BINETS; |
| 929 | |
| 930 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 931 | { |
| 932 | return new UFC_Binet($this->val); |
| 933 | } |
| 934 | } |
| 935 | // }}} |
| 936 | |
| 937 | // {{{ class UFBF_Group |
| 938 | class UFBF_Group extends UFBF_Mixed |
| 939 | { |
| 940 | protected $direnum = DirEnum::GROUPESX; |
| 941 | |
| 942 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 943 | { |
| 944 | if (count($this->val) == 1) { |
| 945 | return new UFC_Group($this->val[0]); |
| 946 | } |
| 947 | |
| 948 | $or = new PFC_Or(); |
| 949 | foreach ($this->val as $grp) { |
| 950 | $or->addChild(new UFC_Group($grp)); |
| 951 | } |
| 952 | return $or; |
| 953 | } |
| 954 | } |
| 955 | // }}} |
| 956 | |
| 957 | // {{{ class UFBF_Section |
| 958 | class UFBF_Section extends UFBF_Index |
| 959 | { |
| 960 | protected $direnum = DirEnum::SECTIONS; |
| 961 | |
| 962 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 963 | { |
| 964 | return new UFC_Section($this->val); |
| 965 | } |
| 966 | } |
| 967 | // }}} |
| 968 | |
| 969 | // {{{ class UFBF_EducationSchool |
| 970 | class UFBF_EducationSchool extends UFBF_Mixed |
| 971 | { |
| 972 | protected $direnum = DirEnum::EDUSCHOOLS; |
| 973 | |
| 974 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 975 | { |
| 976 | return new UFC_EducationSchool($this->val); |
| 977 | } |
| 978 | } |
| 979 | // }}} |
| 980 | |
| 981 | // {{{ class UFBF_EducationDegree |
| 982 | class UFBF_EducationDegree extends UFBF_Mixed |
| 983 | { |
| 984 | protected $direnum = DirEnum::EDUDEGREES; |
| 985 | |
| 986 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 987 | { |
| 988 | return new UFC_EducationDegree($this->val); |
| 989 | } |
| 990 | } |
| 991 | // }}} |
| 992 | |
| 993 | // {{{ class UFBF_EducationField |
| 994 | class UFBF_EducationField extends UFBF_Mixed |
| 995 | { |
| 996 | protected $direnum = DirEnum::EDUFIELDS; |
| 997 | |
| 998 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 999 | { |
| 1000 | return new UFC_EducationField($this->val); |
| 1001 | } |
| 1002 | } |
| 1003 | // }}} |
| 1004 | |
| 1005 | // {{{ class UFBF_Comment |
| 1006 | class UFBF_Comment extends UFBF_Text |
| 1007 | { |
| 1008 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1009 | { |
| 1010 | return new UFC_Comment($this->val); |
| 1011 | } |
| 1012 | } |
| 1013 | // }}} |
| 1014 | |
| 1015 | // {{{ class UFBF_Phone |
| 1016 | class UFBF_Phone extends UFBF_Text |
| 1017 | { |
| 1018 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1019 | { |
| 1020 | return new UFC_Phone($this->val); |
| 1021 | } |
| 1022 | } |
| 1023 | // }}} |
| 1024 | |
| 1025 | // {{{ class UFBF_Networking |
| 1026 | class UFBF_Networking extends UFBF_Text |
| 1027 | { |
| 1028 | private $networktypefield; |
| 1029 | private $nwtype; |
| 1030 | |
| 1031 | public function __construct($envfield, $networktypefield, $formtext = '') |
| 1032 | { |
| 1033 | parent::__construct($envfield, $formtext); |
| 1034 | $this->networktypefield = $networktypefield; |
| 1035 | } |
| 1036 | |
| 1037 | public function check(UserFilterBuilder &$ufb) |
| 1038 | { |
| 1039 | if (parent::check($ufb)) { |
| 1040 | $this->nwtype = $ufb->i($this->networktypefield); |
| 1041 | return true; |
| 1042 | } else { |
| 1043 | return false; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | public function buildUFC(UserFilterBuilder &$ufb) |
| 1048 | { |
| 1049 | return new UFC_Networking($this->nwtype, $this->val); |
| 1050 | } |
| 1051 | } |
| 1052 | // }}} |
| 1053 | |
| 1054 | // {{{ class UFBF_Mentor |
| 1055 | class UFBF_Mentor extends UFBF_Bool |
| 1056 | { |
| 1057 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1058 | { |
| 1059 | return new UFC_Mentor(); |
| 1060 | } |
| 1061 | } |
| 1062 | // }}} |
| 1063 | |
| 1064 | // {{{ class UFBF_MentorCountry |
| 1065 | class UFBF_MentorCountry extends UFBF_Text |
| 1066 | { |
| 1067 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1068 | { |
| 1069 | return new UFC_Mentor_Country($this->val); |
| 1070 | } |
| 1071 | } |
| 1072 | // }}} |
| 1073 | |
| 1074 | // {{{ class UFBF_Mentorterm |
| 1075 | class UFBF_MentorTerm extends UFBF_Index |
| 1076 | { |
| 1077 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1078 | { |
| 1079 | return new UFC_Mentor_Terms($this->val); |
| 1080 | } |
| 1081 | } |
| 1082 | // }}} |
| 1083 | |
| 1084 | // {{{ class UFBF_MentorSectorization |
| 1085 | class UFBF_MentorSectorization extends UFBF_Index |
| 1086 | { |
| 1087 | protected $type; |
| 1088 | |
| 1089 | public function __construct($envfield, $formtext = '', $type = UFC_Mentor_Sectorization::SECTOR) |
| 1090 | { |
| 1091 | parent::__construct($envfield, $formtext); |
| 1092 | $this->type = $type; |
| 1093 | } |
| 1094 | |
| 1095 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1096 | { |
| 1097 | return new UFC_Mentor_Sectorization($this->val, $this->type); |
| 1098 | } |
| 1099 | } |
| 1100 | // }}} |
| 1101 | |
| 1102 | // {{{ class UFBF_MentorExpertise |
| 1103 | class UFBF_MentorExpertise extends UFBF_Text |
| 1104 | { |
| 1105 | protected function buildUFC(UserFilterBuilder &$ufb) |
| 1106 | { |
| 1107 | return new UFC_Mentor_Expertise($this->val); |
| 1108 | } |
| 1109 | } |
| 1110 | // }}} |
| 1111 | |
| 1112 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 1113 | ?> |