| 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 | // {{{ abstract class UserFilterCondition |
| 23 | /** This class describe objects which filter users based |
| 24 | * on various parameters. |
| 25 | * The parameters of the filter must be given to the constructor. |
| 26 | * The buildCondition function is called by UserFilter when |
| 27 | * actually building the query. That function must call |
| 28 | * $uf->addWheteverFilter so that the UserFilter makes |
| 29 | * adequate joins. It must return the 'WHERE' condition to use |
| 30 | * with the filter. |
| 31 | */ |
| 32 | abstract class UserFilterCondition implements PlFilterCondition |
| 33 | { |
| 34 | const OP_EQUALS = '=='; |
| 35 | const OP_GREATER = '>'; |
| 36 | const OP_NOTGREATER = '<='; |
| 37 | const OP_LESSER = '<'; |
| 38 | const OP_NOTLESSER = '>='; |
| 39 | const OP_NULL = 'null'; |
| 40 | const OP_NOTNULL = 'not null'; |
| 41 | const OP_CONTAINS = 'contains'; |
| 42 | const OP_PREFIX = 'prefix'; |
| 43 | const OP_SUFFIX = 'suffix'; |
| 44 | |
| 45 | protected function buildExport($type) |
| 46 | { |
| 47 | $export = array('type' => $type); |
| 48 | return $export; |
| 49 | } |
| 50 | |
| 51 | public function export() |
| 52 | { |
| 53 | throw new Exception("This class is not exportable"); |
| 54 | } |
| 55 | |
| 56 | public static function comparisonFromXDBWildcard($wildcard) |
| 57 | { |
| 58 | switch ($wildcard) { |
| 59 | case XDB::WILDCARD_EXACT: |
| 60 | return self::OP_EQUALS; |
| 61 | case XDB::WILDCARD_PREFIX: |
| 62 | return self::OP_PREFIX; |
| 63 | case XDB::WILDCARD_SUFFIX: |
| 64 | return self::OP_SUFFIX; |
| 65 | case XDB::WILDCARD_CONTAINS: |
| 66 | return self::OP_CONTAINS; |
| 67 | } |
| 68 | throw new Exception("Unknown wildcard mode: $wildcard"); |
| 69 | } |
| 70 | |
| 71 | public static function xdbWildcardFromComparison($comparison) |
| 72 | { |
| 73 | if (!self::isStringComparison($comparison)) { |
| 74 | throw new Exception("Unknown string coparison: $comparison"); |
| 75 | } |
| 76 | switch ($comparison) { |
| 77 | case self::OP_EQUALS: |
| 78 | return XDB::WILDCARD_EXACT; |
| 79 | case self::OP_PREFIX: |
| 80 | return XDB::WILDCARD_PREFIX; |
| 81 | case self::OP_SUFFIX: |
| 82 | return XDB::WILDCARD_SUFFIX; |
| 83 | case self::OP_CONTAINS: |
| 84 | return XDB::WILDCARD_CONTAINS; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private static function isNumericComparison($comparison) |
| 89 | { |
| 90 | return $comparison == self::OP_EQUALS |
| 91 | || $comparison == self::OP_GREATER |
| 92 | || $comparison == self::OP_NOTGREATER |
| 93 | || $comparison == self::OP_LESSER |
| 94 | || $comparison == self::OP_NOTLESSER; |
| 95 | } |
| 96 | |
| 97 | private static function isStringComparison($comparison) |
| 98 | { |
| 99 | return $comparison == self::OP_EQUALS |
| 100 | || $comparison == self::OP_CONTAINS |
| 101 | || $comparison == self::OP_PREFIX |
| 102 | || $comparison == self::OP_SUFFIX; |
| 103 | } |
| 104 | |
| 105 | public static function fromExport(array $export) |
| 106 | { |
| 107 | $export = new PlDict($export); |
| 108 | if (!$export->has('type')) { |
| 109 | throw new Exception("Missing type in export"); |
| 110 | } |
| 111 | $type = $export->s('type'); |
| 112 | $cond = null; |
| 113 | switch ($type) { |
| 114 | case 'and': |
| 115 | case 'or': |
| 116 | case 'not': |
| 117 | case 'true': |
| 118 | case 'false': |
| 119 | $class = 'pfc_' . $type; |
| 120 | $cond = new $class(); |
| 121 | break; |
| 122 | |
| 123 | case 'host': |
| 124 | if ($export->has('ip')) { |
| 125 | $cond = new UFC_Ip($export->s('ip')); |
| 126 | } |
| 127 | break; |
| 128 | |
| 129 | case 'comment': |
| 130 | if ($export->has('text') && $export->s('comparison') == self::OP_CONTAINS) { |
| 131 | $cond = new UFC_Comment($export->s('text')); |
| 132 | } |
| 133 | break; |
| 134 | |
| 135 | case 'promo': |
| 136 | if ($export->has('promo') && self::isNumericComparison($export->s('comparison'))) { |
| 137 | $cond = new UFC_Promo($export->s('comparison'), |
| 138 | $export->s('grade', UserFilter::DISPLAY), |
| 139 | $export->s('promo')); |
| 140 | } |
| 141 | break; |
| 142 | |
| 143 | case 'lastname': |
| 144 | case 'name': |
| 145 | case 'firstname': |
| 146 | case 'nickname': |
| 147 | case 'pseudonym': |
| 148 | if ($export->has('text')) { |
| 149 | $flag = self::xdbWildcardFromComparison($export->s('comparison')); |
| 150 | if ($export->b('search_in_variants')) { |
| 151 | $flag |= UFC_Name::VARIANTS; |
| 152 | } |
| 153 | if ($export->b('search_in_particle')) { |
| 154 | $flag |= UFC_Name::PARTICLE; |
| 155 | } |
| 156 | $cond = new UFC_Name($type, $export->s('text'), $flag); |
| 157 | } |
| 158 | break; |
| 159 | |
| 160 | case 'account_type': |
| 161 | case 'account_perm': |
| 162 | case 'hrpid': |
| 163 | case 'hruid': |
| 164 | $values = $export->v('values', array()); |
| 165 | $class = 'ufc_' . str_replace('_', '', $type); |
| 166 | $cond = new $class($values); |
| 167 | break; |
| 168 | |
| 169 | case 'has_profile': |
| 170 | $class = 'ufc_' . str_replace('_', '', $type); |
| 171 | $cond = new $class(); |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | throw new Exception("Unknown condition type: $type"); |
| 176 | } |
| 177 | if (is_null($cond)) { |
| 178 | throw new Exception("Unsupported $type definition"); |
| 179 | } |
| 180 | if ($cond instanceof PFC_NChildren) { |
| 181 | $children = $export->v('children', array()); |
| 182 | foreach ($children as $child) { |
| 183 | $cond->addChild(self::fromExport($child)); |
| 184 | } |
| 185 | } else if ($cond instanceof PFC_OneChild) { |
| 186 | if ($export->has('child')) { |
| 187 | $cond->setChild(self::fromExport($export->v('child'))); |
| 188 | } |
| 189 | } |
| 190 | return $cond; |
| 191 | } |
| 192 | } |
| 193 | // }}} |
| 194 | // {{{ class UFC_HasProfile |
| 195 | /** Filters users who have a profile |
| 196 | */ |
| 197 | class UFC_HasProfile extends UserFilterCondition |
| 198 | { |
| 199 | public function buildCondition(PlFilter $uf) |
| 200 | { |
| 201 | $uf->requireProfiles(); |
| 202 | return '$PID IS NOT NULL'; |
| 203 | } |
| 204 | |
| 205 | public function export() |
| 206 | { |
| 207 | return $this->buildExport('has_profile'); |
| 208 | } |
| 209 | } |
| 210 | // }}} |
| 211 | // {{{ class UFC_AccountType |
| 212 | /** Filters users who have one of the given account types |
| 213 | */ |
| 214 | class UFC_AccountType extends UserFilterCondition |
| 215 | { |
| 216 | private $types; |
| 217 | |
| 218 | public function __construct() |
| 219 | { |
| 220 | $this->types = pl_flatten(func_get_args()); |
| 221 | } |
| 222 | |
| 223 | public function buildCondition(PlFilter $uf) |
| 224 | { |
| 225 | $uf->requireAccounts(); |
| 226 | return XDB::format('a.type IN {?}', $this->types); |
| 227 | } |
| 228 | |
| 229 | public function export() |
| 230 | { |
| 231 | $export = $this->buildExport('account_type'); |
| 232 | $export['values'] = $this->types; |
| 233 | return $export; |
| 234 | } |
| 235 | } |
| 236 | // }}} |
| 237 | // {{{ class UFC_AccountPerm |
| 238 | /** Filters users who have one of the given permissions |
| 239 | */ |
| 240 | class UFC_AccountPerm extends UserFilterCondition |
| 241 | { |
| 242 | private $perms; |
| 243 | |
| 244 | public function __construct() |
| 245 | { |
| 246 | $this->perms = pl_flatten(func_get_args()); |
| 247 | } |
| 248 | |
| 249 | public function buildCondition(PlFilter $uf) |
| 250 | { |
| 251 | $uf->requirePerms(); |
| 252 | $conds = array(); |
| 253 | foreach ($this->perms as $perm) { |
| 254 | $conds[] = XDB::format('FIND_IN_SET({?}, IF(a.user_perms IS NULL, at.perms, |
| 255 | CONCAT(at.perms, \',\', a.user_perms)))', |
| 256 | $perm); |
| 257 | } |
| 258 | if (empty($conds)) { |
| 259 | return self::COND_TRUE; |
| 260 | } else { |
| 261 | return implode(' OR ', $conds); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | public function export() |
| 266 | { |
| 267 | $export = $this->buildExport('account_perm'); |
| 268 | $export['values'] = $this->perms; |
| 269 | return $export; |
| 270 | } |
| 271 | } |
| 272 | // }}} |
| 273 | // {{{ class UFC_Hruid |
| 274 | /** Filters users based on their hruid |
| 275 | * @param $val Either an hruid, or a list of those |
| 276 | */ |
| 277 | class UFC_Hruid extends UserFilterCondition |
| 278 | { |
| 279 | private $hruids; |
| 280 | |
| 281 | public function __construct() |
| 282 | { |
| 283 | $this->hruids = pl_flatten(func_get_args()); |
| 284 | } |
| 285 | |
| 286 | public function buildCondition(PlFilter $uf) |
| 287 | { |
| 288 | $uf->requireAccounts(); |
| 289 | return XDB::format('a.hruid IN {?}', $this->hruids); |
| 290 | } |
| 291 | |
| 292 | public function export() |
| 293 | { |
| 294 | $export = $this->buildExport('hruid'); |
| 295 | $export['values'] = $this->hruids; |
| 296 | return $export; |
| 297 | } |
| 298 | } |
| 299 | // }}} |
| 300 | // {{{ class UFC_Hrpid |
| 301 | /** Filters users based on the hrpid of their profiles |
| 302 | * @param $val Either an hrpid, or a list of those |
| 303 | */ |
| 304 | class UFC_Hrpid extends UserFilterCondition |
| 305 | { |
| 306 | private $hrpids; |
| 307 | |
| 308 | public function __construct() |
| 309 | { |
| 310 | $this->hrpids = pl_flatten(func_get_args()); |
| 311 | } |
| 312 | |
| 313 | public function buildCondition(PlFilter $uf) |
| 314 | { |
| 315 | $uf->requireProfiles(); |
| 316 | return XDB::format('p.hrpid IN {?}', $this->hrpids); |
| 317 | } |
| 318 | |
| 319 | public function export() |
| 320 | { |
| 321 | $export = $this->buildExport('hrpid'); |
| 322 | $export['values'] = $this->hrpids; |
| 323 | return $export; |
| 324 | } |
| 325 | } |
| 326 | // }}} |
| 327 | // {{{ class UFC_Ip |
| 328 | /** Filters users based on one of their last IPs |
| 329 | * @param $ip IP from which connection are checked |
| 330 | */ |
| 331 | class UFC_Ip extends UserFilterCondition |
| 332 | { |
| 333 | private $ip; |
| 334 | |
| 335 | public function __construct($ip) |
| 336 | { |
| 337 | $this->ip = $ip; |
| 338 | } |
| 339 | |
| 340 | public function buildCondition(PlFilter $uf) |
| 341 | { |
| 342 | $sub = $uf->addLoggerFilter(); |
| 343 | $ip = ip_to_uint($this->ip); |
| 344 | return XDB::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip); |
| 345 | } |
| 346 | |
| 347 | public function export() |
| 348 | { |
| 349 | $export = $this->buildExport('host'); |
| 350 | $export['ip'] = $this->ip; |
| 351 | return $export; |
| 352 | } |
| 353 | } |
| 354 | // }}} |
| 355 | // {{{ class UFC_Comment |
| 356 | class UFC_Comment extends UserFilterCondition |
| 357 | { |
| 358 | private $text; |
| 359 | |
| 360 | public function __construct($text) |
| 361 | { |
| 362 | $this->text = $text; |
| 363 | } |
| 364 | |
| 365 | public function buildCondition(PlFilter $uf) |
| 366 | { |
| 367 | $uf->requireProfiles(); |
| 368 | return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->text); |
| 369 | } |
| 370 | |
| 371 | public function export() |
| 372 | { |
| 373 | $export = $this->buildExport('comment'); |
| 374 | $export['comparison'] = self::OP_CONTAINS; |
| 375 | $export['text'] = $this->text; |
| 376 | return $export; |
| 377 | } |
| 378 | } |
| 379 | // }}} |
| 380 | // {{{ class UFC_Promo |
| 381 | /** Filters users based on promotion |
| 382 | * @param $comparison Comparison operator (>, =, ...) |
| 383 | * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation" |
| 384 | * @param $promo Promotion on which the filter is based |
| 385 | */ |
| 386 | class UFC_Promo extends UserFilterCondition |
| 387 | { |
| 388 | |
| 389 | private $grade; |
| 390 | private $promo; |
| 391 | private $comparison; |
| 392 | |
| 393 | public function __construct($comparison, $grade, $promo) |
| 394 | { |
| 395 | $this->grade = $grade; |
| 396 | $this->comparison = $comparison; |
| 397 | $this->promo = $promo; |
| 398 | if ($this->grade != UserFilter::DISPLAY) { |
| 399 | UserFilter::assertGrade($this->grade); |
| 400 | } |
| 401 | if ($this->grade == UserFilter::DISPLAY && $this->comparison != '=') { |
| 402 | // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004' |
| 403 | Platal::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison . "' aux promotions sans spécifier de formation (X/M/D)"); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | public function buildCondition(PlFilter $uf) |
| 408 | { |
| 409 | if ($this->grade == UserFilter::DISPLAY) { |
| 410 | $sub = $uf->addDisplayFilter(); |
| 411 | return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo); |
| 412 | } else { |
| 413 | $sub = $uf->addEducationFilter(true, $this->grade); |
| 414 | $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); |
| 415 | return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | public function export() |
| 420 | { |
| 421 | $export['comparison'] = $this->comparison; |
| 422 | if ($this->grade != UserFilter::DISPLAY) { |
| 423 | $export['grade'] = $this->grade; |
| 424 | } |
| 425 | $export['promo'] = $this->promo; |
| 426 | return $export; |
| 427 | } |
| 428 | } |
| 429 | // }}} |
| 430 | // {{{ class UFC_SchoolId |
| 431 | /** Filters users based on their shoold identifier |
| 432 | * @param type Parameter type (Xorg, AX, School) |
| 433 | * @param value Array of school ids |
| 434 | */ |
| 435 | class UFC_SchoolId extends UserFilterCondition |
| 436 | { |
| 437 | const AX = 'ax'; |
| 438 | const Xorg = 'xorg'; |
| 439 | const School = 'school'; |
| 440 | |
| 441 | private $type; |
| 442 | private $id; |
| 443 | |
| 444 | static public function assertType($type) |
| 445 | { |
| 446 | if ($type != self::AX && $type != self::Xorg && $type != self::School) { |
| 447 | Platal::page()->killError("Type de matricule invalide: $type"); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** Construct a UFC_SchoolId |
| 452 | * The first argument is the type, all following arguments can be either ids |
| 453 | * or arrays of ids to use: |
| 454 | * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4)); |
| 455 | */ |
| 456 | public function __construct($type) |
| 457 | { |
| 458 | $this->type = $type; |
| 459 | $ids = func_get_args(); |
| 460 | array_shift($ids); |
| 461 | $this->ids = pl_flatten($ids); |
| 462 | self::assertType($type); |
| 463 | } |
| 464 | |
| 465 | public function buildCondition(PlFilter $uf) |
| 466 | { |
| 467 | $uf->requireProfiles(); |
| 468 | $ids = $this->ids; |
| 469 | $type = $this->type; |
| 470 | if ($type == self::School) { |
| 471 | $type = self::Xorg; |
| 472 | $ids = array_map(array('Profile', 'getXorgId'), $ids); |
| 473 | } |
| 474 | return XDB::format('p.' . $type . '_id IN {?}', $ids); |
| 475 | } |
| 476 | } |
| 477 | // }}} |
| 478 | // {{{ class UFC_EducationSchool |
| 479 | /** Filters users by formation |
| 480 | * @param $val The formation to search (either ID or array of IDs) |
| 481 | */ |
| 482 | class UFC_EducationSchool extends UserFilterCondition |
| 483 | { |
| 484 | private $val; |
| 485 | |
| 486 | public function __construct() |
| 487 | { |
| 488 | $this->val = pl_flatten(func_get_args()); |
| 489 | } |
| 490 | |
| 491 | public function buildCondition(PlFilter $uf) |
| 492 | { |
| 493 | $sub = $uf->addEducationFilter(); |
| 494 | return XDB::format('pe' . $sub . '.eduid IN {?}', $this->val); |
| 495 | } |
| 496 | } |
| 497 | // }}} |
| 498 | // {{{ class UFC_EducationDegree |
| 499 | class UFC_EducationDegree extends UserFilterCondition |
| 500 | { |
| 501 | private $diploma; |
| 502 | |
| 503 | public function __construct() |
| 504 | { |
| 505 | $this->diploma = pl_flatten(func_get_args()); |
| 506 | } |
| 507 | |
| 508 | public function buildCondition(PlFilter $uf) |
| 509 | { |
| 510 | $sub = $uf->addEducationFilter(); |
| 511 | return XDB::format('pe' . $sub . '.degreeid IN {?}', $this->diploma); |
| 512 | } |
| 513 | } |
| 514 | // }}} |
| 515 | // {{{ class UFC_EducationField |
| 516 | class UFC_EducationField extends UserFilterCondition |
| 517 | { |
| 518 | private $val; |
| 519 | |
| 520 | public function __construct() |
| 521 | { |
| 522 | $this->val = pl_flatten(func_get_args()); |
| 523 | } |
| 524 | |
| 525 | public function buildCondition(PlFilter $uf) |
| 526 | { |
| 527 | $sub = $uf->addEducationFilter(); |
| 528 | return XDB::format('pe' . $sub . '.fieldid IN {?}', $this->val); |
| 529 | } |
| 530 | } |
| 531 | // }}} |
| 532 | // {{{ class UFC_Name |
| 533 | /** Filters users based on name |
| 534 | * @param $type Type of name field on which filtering is done (firstname, lastname...) |
| 535 | * @param $text Text on which to filter |
| 536 | * @param $mode Flag indicating search type (prefix, suffix, with particule...) |
| 537 | */ |
| 538 | class UFC_Name extends UserFilterCondition |
| 539 | { |
| 540 | const EXACT = XDB::WILDCARD_EXACT; // 0x000 |
| 541 | const PREFIX = XDB::WILDCARD_PREFIX; // 0x001 |
| 542 | const SUFFIX = XDB::WILDCARD_SUFFIX; // 0x002 |
| 543 | const CONTAINS = XDB::WILDCARD_CONTAINS; // 0x003 |
| 544 | const PARTICLE = 0x004; |
| 545 | const VARIANTS = 0x008; |
| 546 | |
| 547 | private $type; |
| 548 | private $text; |
| 549 | private $mode; |
| 550 | |
| 551 | public function __construct($type, $text, $mode) |
| 552 | { |
| 553 | $this->type = $type; |
| 554 | $this->text = $text; |
| 555 | $this->mode = $mode; |
| 556 | } |
| 557 | |
| 558 | private function buildNameQuery($type, $variant, $where, UserFilter $uf) |
| 559 | { |
| 560 | $sub = $uf->addNameFilter($type, $variant); |
| 561 | return str_replace('$ME', 'pn' . $sub, $where); |
| 562 | } |
| 563 | |
| 564 | public function buildCondition(PlFilter $uf) |
| 565 | { |
| 566 | $left = '$ME.name'; |
| 567 | if (($this->mode & self::PARTICLE) == self::PARTICLE) { |
| 568 | $left = 'CONCAT($ME.particle, \' \', $ME.name)'; |
| 569 | } |
| 570 | $right = XDB::formatWildcards($this->mode & self::CONTAINS, $this->text); |
| 571 | |
| 572 | $cond = $left . $right; |
| 573 | $conds = array($this->buildNameQuery($this->type, null, $cond, $uf)); |
| 574 | if (($this->mode & self::VARIANTS) != 0 && isset(Profile::$name_variants[$this->type])) { |
| 575 | foreach (Profile::$name_variants[$this->type] as $var) { |
| 576 | $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf); |
| 577 | } |
| 578 | } |
| 579 | return implode(' OR ', $conds); |
| 580 | } |
| 581 | |
| 582 | public function export() |
| 583 | { |
| 584 | $export = $this->buildExport($this->type); |
| 585 | if ($this->mode & self::VARIANTS) { |
| 586 | $export['search_in_variants'] = true; |
| 587 | } |
| 588 | if ($this->mode & self::PARTICLE) { |
| 589 | $export['search_in_particle'] = true; |
| 590 | } |
| 591 | $export['comparison'] = self::comparisonFromXDBWildcard($this->mode & 0x3); |
| 592 | $export['text'] = $this->text; |
| 593 | return $export; |
| 594 | } |
| 595 | } |
| 596 | // }}} |
| 597 | // {{{ class UFC_NameTokens |
| 598 | /** Selects users based on tokens in their name (for quicksearch) |
| 599 | * @param $tokens An array of tokens to search |
| 600 | * @param $flags Flags the tokens must have (e.g 'public' for public search) |
| 601 | * @param $soundex (bool) Whether those tokens are fulltext or soundex |
| 602 | */ |
| 603 | class UFC_NameTokens extends UserFilterCondition |
| 604 | { |
| 605 | /* Flags */ |
| 606 | const FLAG_PUBLIC = 'public'; |
| 607 | |
| 608 | private $tokens; |
| 609 | private $flags; |
| 610 | private $soundex; |
| 611 | private $exact; |
| 612 | |
| 613 | public function __construct($tokens, $flags = array(), $soundex = false, $exact = false) |
| 614 | { |
| 615 | if (is_array($tokens)) { |
| 616 | $this->tokens = $tokens; |
| 617 | } else { |
| 618 | $this->tokens = array($tokens); |
| 619 | } |
| 620 | if (is_array($flags)) { |
| 621 | $this->flags = $flags; |
| 622 | } else { |
| 623 | $this->flags = array($flags); |
| 624 | } |
| 625 | $this->soundex = $soundex; |
| 626 | $this->exact = $exact; |
| 627 | } |
| 628 | |
| 629 | public function buildCondition(PlFilter $uf) |
| 630 | { |
| 631 | $conds = array(); |
| 632 | foreach ($this->tokens as $i => $token) { |
| 633 | $sub = $uf->addNameTokensFilter($token); |
| 634 | if ($this->soundex) { |
| 635 | $c = XDB::format($sub . '.soundex = {?}', soundex_fr($token)); |
| 636 | } else if ($this->exact) { |
| 637 | $c = XDB::format($sub . '.token = {?}', $token); |
| 638 | } else { |
| 639 | $c = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $token); |
| 640 | } |
| 641 | if ($this->flags != null) { |
| 642 | $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags); |
| 643 | } |
| 644 | $conds[] = $c; |
| 645 | } |
| 646 | |
| 647 | return implode(' AND ', $conds); |
| 648 | } |
| 649 | } |
| 650 | // }}} |
| 651 | // {{{ class UFC_Nationality |
| 652 | class UFC_Nationality extends UserFilterCondition |
| 653 | { |
| 654 | private $val; |
| 655 | |
| 656 | public function __construct() |
| 657 | { |
| 658 | $this->val = pl_flatten(func_get_args()); |
| 659 | } |
| 660 | |
| 661 | public function buildCondition(PlFilter $uf) |
| 662 | { |
| 663 | $uf->requireProfiles(); |
| 664 | $nat = XDB::formatArray($this->val); |
| 665 | $conds = array( |
| 666 | 'p.nationality1 IN ' . $nat, |
| 667 | 'p.nationality2 IN ' . $nat, |
| 668 | 'p.nationality3 IN ' . $nat, |
| 669 | ); |
| 670 | return implode(' OR ', $conds); |
| 671 | } |
| 672 | } |
| 673 | // }}} |
| 674 | // {{{ class UFC_Dead |
| 675 | /** Filters users based on death date |
| 676 | * @param $comparison Comparison operator |
| 677 | * @param $date Date to which death date should be compared (DateTime object, string or timestamp) |
| 678 | */ |
| 679 | class UFC_Dead extends UserFilterCondition |
| 680 | { |
| 681 | private $comparison; |
| 682 | private $date; |
| 683 | |
| 684 | public function __construct($comparison = null, $date = null) |
| 685 | { |
| 686 | $this->comparison = $comparison; |
| 687 | $this->date = make_datetime($date); |
| 688 | } |
| 689 | |
| 690 | public function buildCondition(PlFilter $uf) |
| 691 | { |
| 692 | $uf->requireProfiles(); |
| 693 | $str = 'p.deathdate IS NOT NULL'; |
| 694 | if (!is_null($this->comparison)) { |
| 695 | $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d')); |
| 696 | } |
| 697 | return $str; |
| 698 | } |
| 699 | } |
| 700 | // }}} |
| 701 | // {{{ class UFC_Registered |
| 702 | /** Filters users based on registration state |
| 703 | * @param $active Whether we want to use only "active" users (i.e with a valid redirection) |
| 704 | * @param $comparison Comparison operator |
| 705 | * @param $date Date to which users registration date should be compared |
| 706 | */ |
| 707 | class UFC_Registered extends UserFilterCondition |
| 708 | { |
| 709 | private $active; |
| 710 | private $comparison; |
| 711 | private $date; |
| 712 | |
| 713 | public function __construct($active = false, $comparison = null, $date = null) |
| 714 | { |
| 715 | $this->active = $active; |
| 716 | $this->comparison = $comparison; |
| 717 | $this->date = make_datetime($date); |
| 718 | } |
| 719 | |
| 720 | public function buildCondition(PlFilter $uf) |
| 721 | { |
| 722 | $uf->requireAccounts(); |
| 723 | if ($this->active) { |
| 724 | $date = '$UID IS NOT NULL AND a.state = \'active\''; |
| 725 | } else { |
| 726 | $date = '$UID IS NOT NULL AND a.state != \'pending\''; |
| 727 | } |
| 728 | if (!is_null($this->comparison)) { |
| 729 | $date .= ' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d')); |
| 730 | } |
| 731 | return $date; |
| 732 | } |
| 733 | } |
| 734 | // }}} |
| 735 | // {{{ class UFC_ProfileUpdated |
| 736 | /** Filters users based on profile update date |
| 737 | * @param $comparison Comparison operator |
| 738 | * @param $date Date to which profile update date must be compared |
| 739 | */ |
| 740 | class UFC_ProfileUpdated extends UserFilterCondition |
| 741 | { |
| 742 | private $comparison; |
| 743 | private $date; |
| 744 | |
| 745 | public function __construct($comparison = null, $date = null) |
| 746 | { |
| 747 | $this->comparison = $comparison; |
| 748 | $this->date = $date; |
| 749 | } |
| 750 | |
| 751 | public function buildCondition(PlFilter $uf) |
| 752 | { |
| 753 | $uf->requireProfiles(); |
| 754 | return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date)); |
| 755 | } |
| 756 | } |
| 757 | // }}} |
| 758 | // {{{ class UFC_Birthday |
| 759 | /** Filters users based on next birthday date |
| 760 | * @param $comparison Comparison operator |
| 761 | * @param $date Date to which users next birthday date should be compared |
| 762 | */ |
| 763 | class UFC_Birthday extends UserFilterCondition |
| 764 | { |
| 765 | private $comparison; |
| 766 | private $date; |
| 767 | |
| 768 | public function __construct($comparison = null, $date = null) |
| 769 | { |
| 770 | $this->comparison = $comparison; |
| 771 | $this->date = $date; |
| 772 | } |
| 773 | |
| 774 | public function buildCondition(PlFilter $uf) |
| 775 | { |
| 776 | $uf->requireProfiles(); |
| 777 | return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date)); |
| 778 | } |
| 779 | } |
| 780 | // }}} |
| 781 | // {{{ class UFC_Sex |
| 782 | /** Filters users based on sex |
| 783 | * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users |
| 784 | */ |
| 785 | class UFC_Sex extends UserFilterCondition |
| 786 | { |
| 787 | private $sex; |
| 788 | public function __construct($sex) |
| 789 | { |
| 790 | $this->sex = $sex; |
| 791 | } |
| 792 | |
| 793 | public function buildCondition(PlFilter $uf) |
| 794 | { |
| 795 | if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) { |
| 796 | return self::COND_FALSE; |
| 797 | } else { |
| 798 | $uf->requireProfiles(); |
| 799 | return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male'); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | // }}} |
| 804 | // {{{ class UFC_Group |
| 805 | /** Filters users based on group membership |
| 806 | * @param $group Group whose members we are selecting |
| 807 | * @param $anim Whether to restrict selection to animators of that group |
| 808 | */ |
| 809 | class UFC_Group extends UserFilterCondition |
| 810 | { |
| 811 | private $group; |
| 812 | private $anim; |
| 813 | public function __construct($group, $anim = false) |
| 814 | { |
| 815 | $this->group = $group; |
| 816 | $this->anim = $anim; |
| 817 | } |
| 818 | |
| 819 | public function buildCondition(PlFilter $uf) |
| 820 | { |
| 821 | // Groups have AX visibility. |
| 822 | if ($uf->getVisibilityLevel() == ProfileVisibility::VIS_PUBLIC) { |
| 823 | return self::COND_TRUE; |
| 824 | } |
| 825 | $sub = $uf->addGroupFilter($this->group); |
| 826 | $where = 'gpm' . $sub . '.perms IS NOT NULL'; |
| 827 | if ($this->anim) { |
| 828 | $where .= ' AND gpm' . $sub . '.perms = \'admin\''; |
| 829 | } |
| 830 | return $where; |
| 831 | } |
| 832 | } |
| 833 | // }}} |
| 834 | // {{{ class UFC_Binet |
| 835 | /** Selects users based on their belonging to a given (list of) binet |
| 836 | * @param $binet either a binet_id or an array of binet_ids |
| 837 | */ |
| 838 | class UFC_Binet extends UserFilterCondition |
| 839 | { |
| 840 | private $val; |
| 841 | |
| 842 | public function __construct() |
| 843 | { |
| 844 | $this->val = pl_flatten(func_get_args()); |
| 845 | } |
| 846 | |
| 847 | public function buildCondition(PlFilter $uf) |
| 848 | { |
| 849 | // Binets are private. |
| 850 | if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) { |
| 851 | return self::CONF_TRUE; |
| 852 | } |
| 853 | $sub = $uf->addBinetsFilter(); |
| 854 | return XDB::format($sub . '.binet_id IN {?}', $this->val); |
| 855 | } |
| 856 | } |
| 857 | // }}} |
| 858 | // {{{ class UFC_Section |
| 859 | /** Selects users based on section |
| 860 | * @param $section ID of the section |
| 861 | */ |
| 862 | class UFC_Section extends UserFilterCondition |
| 863 | { |
| 864 | private $section; |
| 865 | |
| 866 | public function __construct() |
| 867 | { |
| 868 | $this->section = pl_flatten(func_get_args()); |
| 869 | } |
| 870 | |
| 871 | public function buildCondition(PlFilter $uf) |
| 872 | { |
| 873 | // Sections are private. |
| 874 | if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) { |
| 875 | return self::CONF_TRUE; |
| 876 | } |
| 877 | $uf->requireProfiles(); |
| 878 | return XDB::format('p.section IN {?}', $this->section); |
| 879 | } |
| 880 | } |
| 881 | // }}} |
| 882 | // {{{ class UFC_Email |
| 883 | /** Filters users based on an email or a list of emails |
| 884 | * @param $emails List of emails whose owner must be selected |
| 885 | */ |
| 886 | class UFC_Email extends UserFilterCondition |
| 887 | { |
| 888 | private $emails; |
| 889 | public function __construct() |
| 890 | { |
| 891 | $this->emails = pl_flatten(func_get_args()); |
| 892 | } |
| 893 | |
| 894 | public function buildCondition(PlFilter $uf) |
| 895 | { |
| 896 | $foreign = array(); |
| 897 | $virtual = array(); |
| 898 | $aliases = array(); |
| 899 | $cond = array(); |
| 900 | |
| 901 | if (count($this->emails) == 0) { |
| 902 | return PlFilterCondition::COND_TRUE; |
| 903 | } |
| 904 | |
| 905 | foreach ($this->emails as $entry) { |
| 906 | if (User::isForeignEmailAddress($entry)) { |
| 907 | $foreign[] = $entry; |
| 908 | } else if (User::isVirtualEmailAddress($entry)) { |
| 909 | $virtual[] = $entry; |
| 910 | } else { |
| 911 | @list($user, $domain) = explode('@', $entry); |
| 912 | $aliases[] = $user; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (count($foreign) > 0) { |
| 917 | $sub = $uf->addEmailRedirectFilter($foreign); |
| 918 | $cond[] = XDB::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign); |
| 919 | } |
| 920 | if (count($virtual) > 0) { |
| 921 | $sub = $uf->addVirtualEmailFilter($virtual); |
| 922 | $cond[] = 'vr' . $sub . '.redirect IS NOT NULL'; |
| 923 | } |
| 924 | if (count($aliases) > 0) { |
| 925 | $sub = $uf->addAliasFilter($aliases); |
| 926 | $cond[] = 'al' . $sub . '.alias IS NOT NULL'; |
| 927 | } |
| 928 | return '(' . implode(') OR (', $cond) . ')'; |
| 929 | } |
| 930 | } |
| 931 | // }}} |
| 932 | // {{{ class UFC_Address |
| 933 | abstract class UFC_Address extends UserFilterCondition |
| 934 | { |
| 935 | /** Valid address type ('hq' is reserved for company addresses) |
| 936 | */ |
| 937 | const TYPE_HOME = 1; |
| 938 | const TYPE_PRO = 2; |
| 939 | const TYPE_ANY = 3; |
| 940 | |
| 941 | /** Text for these types |
| 942 | */ |
| 943 | protected static $typetexts = array( |
| 944 | self::TYPE_HOME => 'home', |
| 945 | self::TYPE_PRO => 'pro', |
| 946 | ); |
| 947 | |
| 948 | protected $type; |
| 949 | |
| 950 | /** Flags for addresses |
| 951 | */ |
| 952 | const FLAG_CURRENT = 0x0001; |
| 953 | const FLAG_TEMP = 0x0002; |
| 954 | const FLAG_SECOND = 0x0004; |
| 955 | const FLAG_MAIL = 0x0008; |
| 956 | const FLAG_CEDEX = 0x0010; |
| 957 | |
| 958 | // Binary OR of those flags |
| 959 | const FLAG_ANY = 0x001F; |
| 960 | |
| 961 | /** Text of these flags |
| 962 | */ |
| 963 | protected static $flagtexts = array( |
| 964 | self::FLAG_CURRENT => 'current', |
| 965 | self::FLAG_TEMP => 'temporary', |
| 966 | self::FLAG_SECOND => 'secondary', |
| 967 | self::FLAG_MAIL => 'mail', |
| 968 | self::FLAG_CEDEX => 'cedex', |
| 969 | ); |
| 970 | |
| 971 | protected $flags; |
| 972 | |
| 973 | public function __construct($type = null, $flags = null) |
| 974 | { |
| 975 | $this->type = $type; |
| 976 | $this->flags = $flags; |
| 977 | } |
| 978 | |
| 979 | protected function initConds($sub, $vis_cond) |
| 980 | { |
| 981 | $conds = array($vis_cond); |
| 982 | |
| 983 | $types = array(); |
| 984 | foreach (self::$typetexts as $flag => $type) { |
| 985 | if ($flag & $this->type) { |
| 986 | $types[] = $type; |
| 987 | } |
| 988 | } |
| 989 | if (count($types)) { |
| 990 | $conds[] = XDB::format($sub . '.type IN {?}', $types); |
| 991 | } |
| 992 | |
| 993 | if ($this->flags != self::FLAG_ANY) { |
| 994 | foreach(self::$flagtexts as $flag => $text) { |
| 995 | if ($flag & $this->flags) { |
| 996 | $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)'; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | return $conds; |
| 1001 | } |
| 1002 | |
| 1003 | } |
| 1004 | // }}} |
| 1005 | // {{{ class UFC_AddressText |
| 1006 | /** Select users based on their address, using full text search |
| 1007 | * @param $text Text for filter in fulltext search |
| 1008 | * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*) |
| 1009 | * @param $type Filter on address type |
| 1010 | * @param $flags Filter on address flags |
| 1011 | * @param $country Filter on address country |
| 1012 | * @param $locality Filter on address locality |
| 1013 | */ |
| 1014 | class UFC_AddressText extends UFC_Address |
| 1015 | { |
| 1016 | |
| 1017 | private $text; |
| 1018 | private $textSearchMode; |
| 1019 | |
| 1020 | public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS, |
| 1021 | $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null) |
| 1022 | { |
| 1023 | parent::__construct($type, $flags); |
| 1024 | $this->text = $text; |
| 1025 | $this->textSearchMode = $textSearchMode; |
| 1026 | $this->country = $country; |
| 1027 | $this->locality = $locality; |
| 1028 | } |
| 1029 | |
| 1030 | private function mkMatch($txt) |
| 1031 | { |
| 1032 | return XDB::formatWildcards($this->textSearchMode, $txt); |
| 1033 | } |
| 1034 | |
| 1035 | public function buildCondition(PlFilter $uf) |
| 1036 | { |
| 1037 | $sub = $uf->addAddressFilter(); |
| 1038 | $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub')); |
| 1039 | if ($this->text != null) { |
| 1040 | $conds[] = $sub . '.text' . $this->mkMatch($this->text); |
| 1041 | } |
| 1042 | |
| 1043 | if ($this->country != null) { |
| 1044 | $subc = $uf->addAddressCountryFilter(); |
| 1045 | $subconds = array(); |
| 1046 | $subconds[] = $subc . '.country' . $this->mkMatch($this->country); |
| 1047 | $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country); |
| 1048 | $conds[] = implode(' OR ', $subconds); |
| 1049 | } |
| 1050 | |
| 1051 | if ($this->locality != null) { |
| 1052 | $subl = $uf->addAddressLocalityFilter(); |
| 1053 | $conds[] = $subl . '.name' . $this->mkMatch($this->locality); |
| 1054 | } |
| 1055 | |
| 1056 | return implode(' AND ', $conds); |
| 1057 | } |
| 1058 | } |
| 1059 | // }}} |
| 1060 | // {{{ class UFC_AddressField |
| 1061 | /** Filters users based on their address, |
| 1062 | * @param $val Either a code for one of the fields, or an array of such codes |
| 1063 | * @param $fieldtype The type of field to look for |
| 1064 | * @param $type Filter on address type |
| 1065 | * @param $flags Filter on address flags |
| 1066 | */ |
| 1067 | class UFC_AddressField extends UFC_Address |
| 1068 | { |
| 1069 | const FIELD_COUNTRY = 1; |
| 1070 | const FIELD_ADMAREA = 2; |
| 1071 | const FIELD_SUBADMAREA = 3; |
| 1072 | const FIELD_LOCALITY = 4; |
| 1073 | const FIELD_ZIPCODE = 5; |
| 1074 | |
| 1075 | /** Data of the filter |
| 1076 | */ |
| 1077 | private $val; |
| 1078 | private $fieldtype; |
| 1079 | |
| 1080 | public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY) |
| 1081 | { |
| 1082 | parent::__construct($type, $flags); |
| 1083 | |
| 1084 | if (!is_array($val)) { |
| 1085 | $val = array($val); |
| 1086 | } |
| 1087 | $this->val = $val; |
| 1088 | $this->fieldtype = $fieldtype; |
| 1089 | } |
| 1090 | |
| 1091 | public function buildCondition(PlFilter $uf) |
| 1092 | { |
| 1093 | $sub = $uf->addAddressFilter(); |
| 1094 | $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub')); |
| 1095 | |
| 1096 | switch ($this->fieldtype) { |
| 1097 | case self::FIELD_COUNTRY: |
| 1098 | $field = 'countryId'; |
| 1099 | break; |
| 1100 | case self::FIELD_ADMAREA: |
| 1101 | $field = 'administrativeAreaId'; |
| 1102 | break; |
| 1103 | case self::FIELD_SUBADMAREA: |
| 1104 | $field = 'subAdministrativeAreaId'; |
| 1105 | break; |
| 1106 | case self::FIELD_LOCALITY: |
| 1107 | $field = 'localityId'; |
| 1108 | break; |
| 1109 | case self::FIELD_ZIPCODE: |
| 1110 | $field = 'postalCode'; |
| 1111 | break; |
| 1112 | default: |
| 1113 | Platal::page()->killError('Invalid address field type: ' . $this->fieldtype); |
| 1114 | } |
| 1115 | $conds[] = XDB::format($sub . '.' . $field . ' IN {?}', $this->val); |
| 1116 | |
| 1117 | return implode(' AND ', $conds); |
| 1118 | } |
| 1119 | } |
| 1120 | // }}} |
| 1121 | // {{{ class UFC_Corps |
| 1122 | /** Filters users based on the corps they belong to |
| 1123 | * @param $corps Corps we are looking for (abbreviation) |
| 1124 | * @param $type Whether we search for original or current corps |
| 1125 | */ |
| 1126 | class UFC_Corps extends UserFilterCondition |
| 1127 | { |
| 1128 | const CURRENT = 1; |
| 1129 | const ORIGIN = 2; |
| 1130 | |
| 1131 | private $corps; |
| 1132 | private $type; |
| 1133 | |
| 1134 | public function __construct($corps, $type = self::CURRENT) |
| 1135 | { |
| 1136 | $this->corps = $corps; |
| 1137 | $this->type = $type; |
| 1138 | } |
| 1139 | |
| 1140 | public function buildCondition(PlFilter $uf) |
| 1141 | { |
| 1142 | /** Tables shortcuts: |
| 1143 | * pc for profile_corps, |
| 1144 | * pceo for profile_corps_enum - orginal |
| 1145 | * pcec for profile_corps_enum - current |
| 1146 | */ |
| 1147 | $sub = $uf->addCorpsFilter($this->type); |
| 1148 | $cond = $sub . '.abbreviation = ' . $corps; |
| 1149 | $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub'); |
| 1150 | return $cond; |
| 1151 | } |
| 1152 | } |
| 1153 | // }}} |
| 1154 | // {{{ class UFC_Corps_Rank |
| 1155 | /** Filters users based on their rank in the corps |
| 1156 | * @param $rank Rank we are looking for (abbreviation) |
| 1157 | */ |
| 1158 | class UFC_Corps_Rank extends UserFilterCondition |
| 1159 | { |
| 1160 | private $rank; |
| 1161 | public function __construct($rank) |
| 1162 | { |
| 1163 | $this->rank = $rank; |
| 1164 | } |
| 1165 | |
| 1166 | public function buildCondition(PlFilter $uf) |
| 1167 | { |
| 1168 | /** Tables shortcuts: |
| 1169 | * pc for profile_corps |
| 1170 | * pcr for profile_corps_rank |
| 1171 | */ |
| 1172 | $sub = $uf->addCorpsRankFilter(); |
| 1173 | $cond = $sub . '.abbreviation = ' . $rank; |
| 1174 | // XXX(x2006barrois): find a way to get rid of that hardcoded |
| 1175 | // reference to 'pc'. |
| 1176 | $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub'); |
| 1177 | return $cond; |
| 1178 | } |
| 1179 | } |
| 1180 | // }}} |
| 1181 | // {{{ class UFC_Job_Company |
| 1182 | /** Filters users based on the company they belong to |
| 1183 | * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM) |
| 1184 | * @param $value The searched value |
| 1185 | */ |
| 1186 | class UFC_Job_Company extends UserFilterCondition |
| 1187 | { |
| 1188 | const JOBID = 'id'; |
| 1189 | const JOBNAME = 'name'; |
| 1190 | const JOBACRONYM = 'acronym'; |
| 1191 | |
| 1192 | private $type; |
| 1193 | private $value; |
| 1194 | |
| 1195 | public function __construct($type, $value) |
| 1196 | { |
| 1197 | $this->assertType($type); |
| 1198 | $this->type = $type; |
| 1199 | $this->value = $value; |
| 1200 | } |
| 1201 | |
| 1202 | private function assertType($type) |
| 1203 | { |
| 1204 | if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) { |
| 1205 | Platal::page()->killError("Type de recherche non valide."); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | public function buildCondition(PlFilter $uf) |
| 1210 | { |
| 1211 | $sub = $uf->addJobCompanyFilter(); |
| 1212 | $cond = $sub . '.' . $this->type . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value); |
| 1213 | $jsub = $uf->addJobFilter(); |
| 1214 | $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub'); |
| 1215 | return $cond; |
| 1216 | } |
| 1217 | } |
| 1218 | // }}} |
| 1219 | // {{{ class UFC_Job_Terms |
| 1220 | /** Filters users based on the job terms they assigned to one of their |
| 1221 | * jobs. |
| 1222 | * @param $val The ID of the job term, or an array of such IDs |
| 1223 | */ |
| 1224 | class UFC_Job_Terms extends UserFilterCondition |
| 1225 | { |
| 1226 | private $val; |
| 1227 | |
| 1228 | public function __construct($val) |
| 1229 | { |
| 1230 | if (!is_array($val)) { |
| 1231 | $val = array($val); |
| 1232 | } |
| 1233 | $this->val = $val; |
| 1234 | } |
| 1235 | |
| 1236 | public function buildCondition(PlFilter $uf) |
| 1237 | { |
| 1238 | $sub = $uf->addJobTermsFilter(count($this->val)); |
| 1239 | $conditions = array(); |
| 1240 | foreach ($this->val as $i => $jtid) { |
| 1241 | $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB::escape($jtid); |
| 1242 | } |
| 1243 | $jsub = $uf->addJobFilter(); |
| 1244 | $conditions[] = $uf->getVisibilityCondition($jsub . '.pub'); |
| 1245 | return implode(' AND ', $conditions); |
| 1246 | } |
| 1247 | } |
| 1248 | // }}} |
| 1249 | // {{{ class UFC_Job_Description |
| 1250 | /** Filters users based on their job description |
| 1251 | * @param $description The text being searched for |
| 1252 | * @param $fields The fields to search for (CV, user-defined) |
| 1253 | */ |
| 1254 | class UFC_Job_Description extends UserFilterCondition |
| 1255 | { |
| 1256 | |
| 1257 | private $description; |
| 1258 | private $fields; |
| 1259 | |
| 1260 | public function __construct($description, $fields) |
| 1261 | { |
| 1262 | $this->fields = $fields; |
| 1263 | $this->description = $description; |
| 1264 | } |
| 1265 | |
| 1266 | public function buildCondition(PlFilter $uf) |
| 1267 | { |
| 1268 | $conds = array(); |
| 1269 | |
| 1270 | $jsub = $uf->addJobFilter(); |
| 1271 | // CV is private => if only CV requested, and not private, |
| 1272 | // don't do anything. Otherwise restrict to standard job visibility. |
| 1273 | if ($this->fields == UserFilter::JOB_CV) { |
| 1274 | if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) { |
| 1275 | return self::CONF_TRUE; |
| 1276 | } |
| 1277 | } else { |
| 1278 | $conds[] = $uf->getVisibilityCondition($jsub . '.pub'); |
| 1279 | } |
| 1280 | |
| 1281 | if ($this->fields & UserFilter::JOB_USERDEFINED) { |
| 1282 | $conds[] = $jsub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description); |
| 1283 | } |
| 1284 | if ($this->fields & UserFilter::JOB_CV && $uf->getVisibilityLevel() == ProfileVisibility::VIS_PRIVATE) { |
| 1285 | $uf->requireProfiles(); |
| 1286 | $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description); |
| 1287 | } |
| 1288 | return implode(' OR ', $conds); |
| 1289 | } |
| 1290 | } |
| 1291 | // }}} |
| 1292 | // {{{ class UFC_Networking |
| 1293 | /** Filters users based on network identity (IRC, ...) |
| 1294 | * @param $type Type of network (-1 for any) |
| 1295 | * @param $value Value to search |
| 1296 | */ |
| 1297 | class UFC_Networking extends UserFilterCondition |
| 1298 | { |
| 1299 | private $type; |
| 1300 | private $value; |
| 1301 | |
| 1302 | public function __construct($type, $value) |
| 1303 | { |
| 1304 | $this->type = $type; |
| 1305 | $this->value = $value; |
| 1306 | } |
| 1307 | |
| 1308 | public function buildCondition(PlFilter $uf) |
| 1309 | { |
| 1310 | $sub = $uf->addNetworkingFilter(); |
| 1311 | $conds = array(); |
| 1312 | $conds[] = $uf->getVisibilityCondition($sub . '.pub'); |
| 1313 | $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value); |
| 1314 | if ($this->type != -1) { |
| 1315 | $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type); |
| 1316 | } |
| 1317 | return implode(' AND ', $conds); |
| 1318 | } |
| 1319 | } |
| 1320 | // }}} |
| 1321 | // {{{ class UFC_Phone |
| 1322 | /** Filters users based on their phone number |
| 1323 | * @param $num_type Type of number (pro/user/home) |
| 1324 | * @param $phone_type Type of phone (fixed/mobile/fax) |
| 1325 | * @param $number Phone number |
| 1326 | */ |
| 1327 | class UFC_Phone extends UserFilterCondition |
| 1328 | { |
| 1329 | const NUM_PRO = 'pro'; |
| 1330 | const NUM_USER = 'user'; |
| 1331 | const NUM_HOME = 'address'; |
| 1332 | const NUM_ANY = 'any'; |
| 1333 | |
| 1334 | const PHONE_FIXED = 'fixed'; |
| 1335 | const PHONE_MOBILE = 'mobile'; |
| 1336 | const PHONE_FAX = 'fax'; |
| 1337 | const PHONE_ANY = 'any'; |
| 1338 | |
| 1339 | private $num_type; |
| 1340 | private $phone_type; |
| 1341 | private $number; |
| 1342 | |
| 1343 | public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY) |
| 1344 | { |
| 1345 | $phone = new Phone(array('display' => $number)); |
| 1346 | $phone->format(); |
| 1347 | $this->number = $phone->search; |
| 1348 | $this->num_type = $num_type; |
| 1349 | $this->phone_type = $phone_type; |
| 1350 | } |
| 1351 | |
| 1352 | public function buildCondition(PlFilter $uf) |
| 1353 | { |
| 1354 | $sub = $uf->addPhoneFilter(); |
| 1355 | $conds = array(); |
| 1356 | |
| 1357 | $conds[] = $uf->getVisibilityCondition($sub . '.pub'); |
| 1358 | |
| 1359 | $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number); |
| 1360 | if ($this->num_type != self::NUM_ANY) { |
| 1361 | $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type); |
| 1362 | } |
| 1363 | if ($this->phone_type != self::PHONE_ANY) { |
| 1364 | $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type); |
| 1365 | } |
| 1366 | return implode(' AND ', $conds); |
| 1367 | } |
| 1368 | } |
| 1369 | // }}} |
| 1370 | // {{{ class UFC_Medal |
| 1371 | /** Filters users based on their medals |
| 1372 | * @param $medal ID of the medal |
| 1373 | * @param $grade Grade of the medal (null for 'any') |
| 1374 | */ |
| 1375 | class UFC_Medal extends UserFilterCondition |
| 1376 | { |
| 1377 | private $medal; |
| 1378 | private $grade; |
| 1379 | |
| 1380 | public function __construct($medal, $grade = null) |
| 1381 | { |
| 1382 | $this->medal = $medal; |
| 1383 | $this->grade = $grade; |
| 1384 | } |
| 1385 | |
| 1386 | public function buildCondition(PlFilter $uf) |
| 1387 | { |
| 1388 | $conds = array(); |
| 1389 | |
| 1390 | // This will require profiles => table 'p' will be available. |
| 1391 | $sub = $uf->addMedalFilter(); |
| 1392 | |
| 1393 | $conds[] = $uf->getVisibilityCondition('p.medals_pub'); |
| 1394 | |
| 1395 | $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal); |
| 1396 | if ($this->grade != null) { |
| 1397 | $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade); |
| 1398 | } |
| 1399 | return implode(' AND ', $conds); |
| 1400 | } |
| 1401 | } |
| 1402 | // }}} |
| 1403 | // {{{ class UFC_Photo |
| 1404 | /** Filters profiles with photo |
| 1405 | */ |
| 1406 | class UFC_Photo extends UserFilterCondition |
| 1407 | { |
| 1408 | public function buildCondition(PlFilter $uf) |
| 1409 | { |
| 1410 | $sub = $uf->addPhotoFilter(); |
| 1411 | return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub'); |
| 1412 | } |
| 1413 | } |
| 1414 | // }}} |
| 1415 | // {{{ class UFC_Mentor |
| 1416 | class UFC_Mentor extends UserFilterCondition |
| 1417 | { |
| 1418 | public function buildCondition(PlFilter $uf) |
| 1419 | { |
| 1420 | $sub = $uf->addMentorFilter(UserFilter::MENTOR); |
| 1421 | return $sub . '.expertise IS NOT NULL'; |
| 1422 | } |
| 1423 | } |
| 1424 | // }}} |
| 1425 | // {{{ class UFC_Mentor_Expertise |
| 1426 | /** Filters users by mentoring expertise |
| 1427 | * @param $expertise Domain of expertise |
| 1428 | */ |
| 1429 | class UFC_Mentor_Expertise extends UserFilterCondition |
| 1430 | { |
| 1431 | private $expertise; |
| 1432 | |
| 1433 | public function __construct($expertise) |
| 1434 | { |
| 1435 | $this->expertise = $expertise; |
| 1436 | } |
| 1437 | |
| 1438 | public function buildCondition(PlFilter $uf) |
| 1439 | { |
| 1440 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE); |
| 1441 | return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise); |
| 1442 | } |
| 1443 | } |
| 1444 | // }}} |
| 1445 | // {{{ class UFC_Mentor_Country |
| 1446 | /** Filters users by mentoring country |
| 1447 | * @param $country Two-letters code of country being searched |
| 1448 | */ |
| 1449 | class UFC_Mentor_Country extends UserFilterCondition |
| 1450 | { |
| 1451 | private $country; |
| 1452 | |
| 1453 | public function __construct() |
| 1454 | { |
| 1455 | $this->country = pl_flatten(func_get_args()); |
| 1456 | } |
| 1457 | |
| 1458 | public function buildCondition(PlFilter $uf) |
| 1459 | { |
| 1460 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY); |
| 1461 | return $sub . '.country IN ' . XDB::format('{?}', $this->country); |
| 1462 | } |
| 1463 | } |
| 1464 | // }}} |
| 1465 | // {{{ class UFC_Mentor_Terms |
| 1466 | /** Filters users based on the job terms they used in mentoring. |
| 1467 | * @param $val The ID of the job term, or an array of such IDs |
| 1468 | */ |
| 1469 | class UFC_Mentor_Terms extends UserFilterCondition |
| 1470 | { |
| 1471 | private $val; |
| 1472 | |
| 1473 | public function __construct($val) |
| 1474 | { |
| 1475 | $this->val = $val; |
| 1476 | } |
| 1477 | |
| 1478 | public function buildCondition(PlFilter $uf) |
| 1479 | { |
| 1480 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_TERM); |
| 1481 | return $sub . '.jtid_1 = ' . XDB::escape($this->val); |
| 1482 | } |
| 1483 | } |
| 1484 | // }}} |
| 1485 | // {{{ class UFC_UserRelated |
| 1486 | /** Filters users based on a relation toward a user |
| 1487 | * @param $user User to which searched users are related |
| 1488 | */ |
| 1489 | abstract class UFC_UserRelated extends UserFilterCondition |
| 1490 | { |
| 1491 | protected $user; |
| 1492 | public function __construct(PlUser &$user) |
| 1493 | { |
| 1494 | $this->user =& $user; |
| 1495 | } |
| 1496 | } |
| 1497 | // }}} |
| 1498 | // {{{ class UFC_Contact |
| 1499 | /** Filters users who belong to selected user's contacts |
| 1500 | */ |
| 1501 | class UFC_Contact extends UFC_UserRelated |
| 1502 | { |
| 1503 | public function buildCondition(PlFilter $uf) |
| 1504 | { |
| 1505 | $sub = $uf->addContactFilter($this->user->id()); |
| 1506 | return 'c' . $sub . '.contact IS NOT NULL'; |
| 1507 | } |
| 1508 | } |
| 1509 | // }}} |
| 1510 | // {{{ class UFC_WatchRegistration |
| 1511 | /** Filters users being watched by selected user |
| 1512 | */ |
| 1513 | class UFC_WatchRegistration extends UFC_UserRelated |
| 1514 | { |
| 1515 | public function buildCondition(PlFilter $uf) |
| 1516 | { |
| 1517 | if (!$this->user->watchType('registration')) { |
| 1518 | return PlFilterCondition::COND_FALSE; |
| 1519 | } |
| 1520 | $uids = $this->user->watchUsers(); |
| 1521 | if (count($uids) == 0) { |
| 1522 | return PlFilterCondition::COND_FALSE; |
| 1523 | } else { |
| 1524 | return XDB::format('$UID IN {?}', $uids); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | // }}} |
| 1529 | // {{{ class UFC_WatchPromo |
| 1530 | /** Filters users belonging to a promo watched by selected user |
| 1531 | * @param $user Selected user (the one watching promo) |
| 1532 | * @param $grade Formation the user is watching |
| 1533 | */ |
| 1534 | class UFC_WatchPromo extends UFC_UserRelated |
| 1535 | { |
| 1536 | private $grade; |
| 1537 | public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING) |
| 1538 | { |
| 1539 | parent::__construct($user); |
| 1540 | $this->grade = $grade; |
| 1541 | } |
| 1542 | |
| 1543 | public function buildCondition(PlFilter $uf) |
| 1544 | { |
| 1545 | $promos = $this->user->watchPromos(); |
| 1546 | if (count($promos) == 0) { |
| 1547 | return PlFilterCondition::COND_FALSE; |
| 1548 | } else { |
| 1549 | $sube = $uf->addEducationFilter(true, $this->grade); |
| 1550 | $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade); |
| 1551 | return XDB::format($field . ' IN {?}', $promos); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | // }}} |
| 1556 | // {{{ class UFC_WatchContact |
| 1557 | /** Filters users watched by selected user |
| 1558 | */ |
| 1559 | class UFC_WatchContact extends UFC_Contact |
| 1560 | { |
| 1561 | public function buildCondition(PlFilter $uf) |
| 1562 | { |
| 1563 | if (!$this->user->watchContacts()) { |
| 1564 | return PlFilterCondition::COND_FALSE; |
| 1565 | } |
| 1566 | return parent::buildCondition($uf); |
| 1567 | } |
| 1568 | } |
| 1569 | // }}} |
| 1570 | // {{{ class UFC_MarketingHash |
| 1571 | /** Filters users using the hash generated |
| 1572 | * to send marketing emails to him. |
| 1573 | */ |
| 1574 | class UFC_MarketingHash extends UserFilterCondition |
| 1575 | { |
| 1576 | private $hash; |
| 1577 | |
| 1578 | public function __construct($hash) |
| 1579 | { |
| 1580 | $this->hash = $hash; |
| 1581 | } |
| 1582 | |
| 1583 | public function buildCondition(PlFilter $uf) |
| 1584 | { |
| 1585 | $table = $uf->addMarketingHash(); |
| 1586 | return XDB::format('rm.hash = {?}', $this->hash); |
| 1587 | } |
| 1588 | } |
| 1589 | // }}} |
| 1590 | |
| 1591 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 1592 | ?> |