| 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 | require_once dirname(__FILE__) . '/userfilter/conditions.inc.php'; |
| 23 | require_once dirname(__FILE__) . '/userfilter/orders.inc.php'; |
| 24 | |
| 25 | /*********************************** |
| 26 | ********************************* |
| 27 | USER FILTER CLASS |
| 28 | ********************************* |
| 29 | ***********************************/ |
| 30 | |
| 31 | // {{{ class UserFilter |
| 32 | /** This class provides a convenient and centralized way of filtering users. |
| 33 | * |
| 34 | * Usage: |
| 35 | * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t)); |
| 36 | * |
| 37 | * Resulting UserFilter can be used to: |
| 38 | * - get a list of User objects matching the filter |
| 39 | * - get a list of UIDs matching the filter |
| 40 | * - get the number of users matching the filter |
| 41 | * - check whether a given User matches the filter |
| 42 | * - filter a list of User objects depending on whether they match the filter |
| 43 | * |
| 44 | * Usage for UFC and UFO objects: |
| 45 | * A UserFilter will call all private functions named XXXJoins. |
| 46 | * These functions must return an array containing the list of join |
| 47 | * required by the various UFC and UFO associated to the UserFilter. |
| 48 | * Entries in those returned array are of the following form: |
| 49 | * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter') |
| 50 | * which will be translated into : |
| 51 | * join_type JOIN joined_table AS join_tablealias ON (join_criter) |
| 52 | * in the final query. |
| 53 | * |
| 54 | * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with |
| 55 | * profile.pid, and $UID with accounts.uid. |
| 56 | * |
| 57 | * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined; |
| 58 | * its parameter will be used to set various private vars of the UserFilter describing |
| 59 | * the required joins ; such a function shall return the "join_tablealias" to use |
| 60 | * when referring to the joined table. |
| 61 | * |
| 62 | * For example, if data from profile_job must be available to filter results, |
| 63 | * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and |
| 64 | * return 'pj', the short name to use when referring to profile_job; when building |
| 65 | * the query, calling the jobJoins function will return an array containing a single |
| 66 | * row: |
| 67 | * 'pj' => array('left', 'profile_job', '$ME.pid = $UID'); |
| 68 | * |
| 69 | * The 'register_optional' function can be used to generate unique table aliases when |
| 70 | * the same table has to be joined several times with different aliases. |
| 71 | */ |
| 72 | class UserFilter extends PlFilter |
| 73 | { |
| 74 | protected $joinMethods = array(); |
| 75 | |
| 76 | protected $joinMetas = array( |
| 77 | '$PID' => 'p.pid', |
| 78 | '$UID' => 'a.uid', |
| 79 | ); |
| 80 | |
| 81 | private $root; |
| 82 | private $sort = array(); |
| 83 | private $grouper = null; |
| 84 | private $query = null; |
| 85 | private $orderby = null; |
| 86 | |
| 87 | // Store the current 'search' visibility. |
| 88 | private $profile_visibility = null; |
| 89 | |
| 90 | private $lastusercount = null; |
| 91 | private $lastprofilecount = null; |
| 92 | |
| 93 | public function __construct($cond = null, $sort = null) |
| 94 | { |
| 95 | if (empty($this->joinMethods)) { |
| 96 | $class = new ReflectionClass('UserFilter'); |
| 97 | foreach ($class->getMethods() as $method) { |
| 98 | $name = $method->getName(); |
| 99 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { |
| 100 | $this->joinMethods[] = $name; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if (!is_null($cond)) { |
| 105 | if ($cond instanceof PlFilterCondition) { |
| 106 | $this->setCondition($cond); |
| 107 | } |
| 108 | } |
| 109 | if (!is_null($sort)) { |
| 110 | if ($sort instanceof PlFilterOrder) { |
| 111 | $this->addSort($sort); |
| 112 | } else if (is_array($sort)) { |
| 113 | foreach ($sort as $s) { |
| 114 | $this->addSort($s); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // This will set the visibility to the default correct level. |
| 120 | $this->profile_visibility = new ProfileVisibility(); |
| 121 | } |
| 122 | |
| 123 | public function getVisibilityLevels() |
| 124 | { |
| 125 | return $this->profile_visibility->levels(); |
| 126 | } |
| 127 | |
| 128 | public function getVisibilityLevel() |
| 129 | { |
| 130 | return $this->profile_visibility->level(); |
| 131 | } |
| 132 | |
| 133 | public function restrictVisibilityTo($level) |
| 134 | { |
| 135 | $this->profile_visibility->setLevel($level); |
| 136 | } |
| 137 | |
| 138 | public function getVisibilityCondition($field) |
| 139 | { |
| 140 | return $field . ' IN ' . XDB::formatArray($this->getVisibilityLevels()); |
| 141 | } |
| 142 | |
| 143 | private function buildQuery() |
| 144 | { |
| 145 | // The root condition is built first because some orders need info |
| 146 | // available only once all UFC have set their conditions (UFO_Score) |
| 147 | if (is_null($this->query)) { |
| 148 | $where = $this->root->buildCondition($this); |
| 149 | $where = str_replace(array_keys($this->joinMetas), |
| 150 | $this->joinMetas, |
| 151 | $where); |
| 152 | } |
| 153 | if (is_null($this->orderby)) { |
| 154 | $orders = array(); |
| 155 | foreach ($this->sort as $sort) { |
| 156 | $orders = array_merge($orders, $sort->buildSort($this)); |
| 157 | } |
| 158 | if (count($orders) == 0) { |
| 159 | $this->orderby = ''; |
| 160 | } else { |
| 161 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); |
| 162 | } |
| 163 | $this->orderby = str_replace(array_keys($this->joinMetas), |
| 164 | $this->joinMetas, |
| 165 | $this->orderby); |
| 166 | } |
| 167 | if (is_null($this->query)) { |
| 168 | if ($this->with_accounts) { |
| 169 | $from = 'accounts AS a'; |
| 170 | } else { |
| 171 | $this->requireProfiles(); |
| 172 | $from = 'profiles AS p'; |
| 173 | } |
| 174 | $joins = $this->buildJoins(); |
| 175 | $this->query = 'FROM ' . $from . ' |
| 176 | ' . $joins . ' |
| 177 | WHERE (' . $where . ')'; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public function hasGroups() |
| 182 | { |
| 183 | return $this->grouper != null; |
| 184 | } |
| 185 | |
| 186 | public function getGroups() |
| 187 | { |
| 188 | return $this->getUIDGroups(); |
| 189 | } |
| 190 | |
| 191 | public function getUIDGroups() |
| 192 | { |
| 193 | $this->requireAccounts(); |
| 194 | $this->buildQuery(); |
| 195 | $token = $this->grouper->getGroupToken($this); |
| 196 | |
| 197 | $groups = XDB::rawFetchAllRow('SELECT ' . $token . ', COUNT(a.uid) |
| 198 | ' . $this->query . ' |
| 199 | GROUP BY ' . $token, |
| 200 | 0); |
| 201 | return $groups; |
| 202 | } |
| 203 | |
| 204 | public function getPIDGroups() |
| 205 | { |
| 206 | $this->requireProfiles(); |
| 207 | $this->buildQuery(); |
| 208 | $token = $this->grouper->getGroupToken($this); |
| 209 | |
| 210 | $groups = XDB::rawFetchAllRow('SELECT ' . $token . ', COUNT(p.pid) |
| 211 | ' . $this->query . ' |
| 212 | GROUP BY ' . $token, |
| 213 | 0); |
| 214 | return $groups; |
| 215 | } |
| 216 | |
| 217 | private function getUIDList($uids = null, PlLimit $limit) |
| 218 | { |
| 219 | $this->requireAccounts(); |
| 220 | $this->buildQuery(); |
| 221 | $lim = $limit->getSql(); |
| 222 | $cond = ''; |
| 223 | if (!empty($uids)) { |
| 224 | $cond = XDB::format(' AND a.uid IN {?}', $uids); |
| 225 | } |
| 226 | $fetched = XDB::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid |
| 227 | ' . $this->query . $cond . ' |
| 228 | GROUP BY a.uid |
| 229 | ' . $this->orderby . ' |
| 230 | ' . $lim); |
| 231 | $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
| 232 | return $fetched; |
| 233 | } |
| 234 | |
| 235 | private function getPIDList($pids = null, PlLimit $limit) |
| 236 | { |
| 237 | $this->requireProfiles(); |
| 238 | $this->buildQuery(); |
| 239 | $lim = $limit->getSql(); |
| 240 | $cond = ''; |
| 241 | if (!is_null($pids)) { |
| 242 | $cond = XDB::format(' AND p.pid IN {?}', $pids); |
| 243 | } |
| 244 | $fetched = XDB::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid |
| 245 | ' . $this->query . $cond . ' |
| 246 | GROUP BY p.pid |
| 247 | ' . $this->orderby . ' |
| 248 | ' . $lim); |
| 249 | $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
| 250 | return $fetched; |
| 251 | } |
| 252 | |
| 253 | private static function defaultLimit($limit) { |
| 254 | if ($limit == null) { |
| 255 | return new PlLimit(); |
| 256 | } else { |
| 257 | return $limit; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** Check that the user match the given rule. |
| 262 | */ |
| 263 | public function checkUser(PlUser $user) |
| 264 | { |
| 265 | $this->requireAccounts(); |
| 266 | $this->buildQuery(); |
| 267 | $count = (int)XDB::rawFetchOneCell('SELECT COUNT(*) |
| 268 | ' . $this->query |
| 269 | . XDB::format(' AND a.uid = {?}', $user->id())); |
| 270 | return $count == 1; |
| 271 | } |
| 272 | |
| 273 | /** Check that the profile match the given rule. |
| 274 | */ |
| 275 | public function checkProfile(Profile $profile) |
| 276 | { |
| 277 | $this->requireProfiles(); |
| 278 | $this->buildQuery(); |
| 279 | $count = (int)XDB::rawFetchOneCell('SELECT COUNT(*) |
| 280 | ' . $this->query |
| 281 | . XDB::format(' AND p.pid = {?}', $profile->id())); |
| 282 | return $count == 1; |
| 283 | } |
| 284 | |
| 285 | /** Default filter is on users |
| 286 | */ |
| 287 | public function filter(array $users, $limit = null) |
| 288 | { |
| 289 | return $this->filterUsers($users, self::defaultLimit($limit)); |
| 290 | } |
| 291 | |
| 292 | /** Filter a list of users to extract the users matching the rule. |
| 293 | */ |
| 294 | public function filterUsers(array $users, $limit = null) |
| 295 | { |
| 296 | $limit = self::defaultLimit($limit); |
| 297 | $this->requireAccounts(); |
| 298 | $this->buildQuery(); |
| 299 | $table = array(); |
| 300 | $uids = array(); |
| 301 | foreach ($users as $user) { |
| 302 | if ($user instanceof PlUser) { |
| 303 | $uid = $user->id(); |
| 304 | } else { |
| 305 | $uid = $user; |
| 306 | } |
| 307 | $uids[] = $uid; |
| 308 | $table[$uid] = $user; |
| 309 | } |
| 310 | $fetched = $this->getUIDList($uids, $limit); |
| 311 | $output = array(); |
| 312 | foreach ($fetched as $uid) { |
| 313 | $output[] = $table[$uid]; |
| 314 | } |
| 315 | return $output; |
| 316 | } |
| 317 | |
| 318 | /** Filter a list of profiles to extract the users matching the rule. |
| 319 | */ |
| 320 | public function filterProfiles(array $profiles, $limit = null) |
| 321 | { |
| 322 | $limit = self::defaultLimit($limit); |
| 323 | $this->requireProfiles(); |
| 324 | $this->buildQuery(); |
| 325 | $table = array(); |
| 326 | $pids = array(); |
| 327 | foreach ($profiles as $profile) { |
| 328 | if ($profile instanceof Profile) { |
| 329 | $pid = $profile->id(); |
| 330 | } else { |
| 331 | $pid = $profile; |
| 332 | } |
| 333 | $pids[] = $pid; |
| 334 | $table[$pid] = $profile; |
| 335 | } |
| 336 | $fetched = $this->getPIDList($pids, $limit); |
| 337 | $output = array(); |
| 338 | foreach ($fetched as $pid) { |
| 339 | $output[] = $table[$pid]; |
| 340 | } |
| 341 | return $output; |
| 342 | } |
| 343 | |
| 344 | public function getUIDs($limit = null) |
| 345 | { |
| 346 | $limit = self::defaultLimit($limit); |
| 347 | return $this->getUIDList(null, $limit); |
| 348 | } |
| 349 | |
| 350 | public function getUID($pos = 0) |
| 351 | { |
| 352 | $uids =$this->getUIDList(null, new PlLimit(1, $pos)); |
| 353 | if (count($uids) == 0) { |
| 354 | return null; |
| 355 | } else { |
| 356 | return $uids[0]; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | public function getPIDs($limit = null) |
| 361 | { |
| 362 | $limit = self::defaultLimit($limit); |
| 363 | return $this->getPIDList(null, $limit); |
| 364 | } |
| 365 | |
| 366 | public function getPID($pos = 0) |
| 367 | { |
| 368 | $pids =$this->getPIDList(null, new PlLimit(1, $pos)); |
| 369 | if (count($pids) == 0) { |
| 370 | return null; |
| 371 | } else { |
| 372 | return $pids[0]; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | public function getUsers($limit = null) |
| 377 | { |
| 378 | return User::getBulkUsersWithUIDs($this->getUIDs($limit)); |
| 379 | } |
| 380 | |
| 381 | public function getUser($pos = 0) |
| 382 | { |
| 383 | $uid = $this->getUID($pos); |
| 384 | if ($uid == null) { |
| 385 | return null; |
| 386 | } else { |
| 387 | return User::getWithUID($uid); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | public function iterUsers($limit = null) |
| 392 | { |
| 393 | return User::iterOverUIDs($this->getUIDs($limit)); |
| 394 | } |
| 395 | |
| 396 | public function getProfiles($limit = null, $fields = 0x0000, $visibility = null) |
| 397 | { |
| 398 | return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility); |
| 399 | } |
| 400 | |
| 401 | public function getProfile($pos = 0, $fields = 0x0000, $visibility = null) |
| 402 | { |
| 403 | $pid = $this->getPID($pos); |
| 404 | if ($pid == null) { |
| 405 | return null; |
| 406 | } else { |
| 407 | return Profile::get($pid, $fields, $visibility); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null) |
| 412 | { |
| 413 | return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility); |
| 414 | } |
| 415 | |
| 416 | public function get($limit = null) |
| 417 | { |
| 418 | return $this->getUsers($limit); |
| 419 | } |
| 420 | |
| 421 | public function getIds($limit = null) |
| 422 | { |
| 423 | return $this->getUIDs(); |
| 424 | } |
| 425 | |
| 426 | public function getTotalCount() |
| 427 | { |
| 428 | return $this->getTotalUserCount(); |
| 429 | } |
| 430 | |
| 431 | public function getTotalUserCount() |
| 432 | { |
| 433 | if (is_null($this->lastusercount)) { |
| 434 | $this->requireAccounts(); |
| 435 | $this->buildQuery(); |
| 436 | return (int)XDB::rawFetchOneCell('SELECT COUNT(DISTINCT a.uid) |
| 437 | ' . $this->query); |
| 438 | } else { |
| 439 | return $this->lastusercount; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | public function getTotalProfileCount() |
| 444 | { |
| 445 | if (is_null($this->lastprofilecount)) { |
| 446 | $this->requireProfiles(); |
| 447 | $this->buildQuery(); |
| 448 | return (int)XDB::rawFetchOneCell('SELECT COUNT(DISTINCT p.pid) |
| 449 | ' . $this->query); |
| 450 | } else { |
| 451 | return $this->lastprofilecount; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | public function setCondition(PlFilterCondition $cond) |
| 456 | { |
| 457 | $this->root =& $cond; |
| 458 | $this->query = null; |
| 459 | } |
| 460 | |
| 461 | public function addSort(PlFilterOrder $sort) |
| 462 | { |
| 463 | if (count($this->sort) == 0 && $sort instanceof PlFilterGroupableOrder) |
| 464 | { |
| 465 | $this->grouper = $sort; |
| 466 | } |
| 467 | $this->sort[] = $sort; |
| 468 | $this->orderby = null; |
| 469 | } |
| 470 | |
| 471 | public function export() |
| 472 | { |
| 473 | $export = array('conditions' => $this->root->export()); |
| 474 | if (!empty($this->sort)) { |
| 475 | $export['sorts'] = array(); |
| 476 | foreach ($this->sort as $sort) { |
| 477 | $export['sorts'][] = $sort->export(); |
| 478 | } |
| 479 | } |
| 480 | return $export; |
| 481 | } |
| 482 | |
| 483 | public function exportConditions() |
| 484 | { |
| 485 | return $this->root->export(); |
| 486 | } |
| 487 | |
| 488 | public static function fromExport(array $export) |
| 489 | { |
| 490 | $export = new PlDict($export); |
| 491 | if (!$export->has('conditions')) { |
| 492 | throw new Exception("Cannot build a user filter without conditions"); |
| 493 | } |
| 494 | $cond = UserFilterCondition::fromExport($export->v('conditions')); |
| 495 | $sorts = null; |
| 496 | if ($export->has('sorts')) { |
| 497 | $sorts = array(); |
| 498 | foreach ($export->v('sorts') as $sort) { |
| 499 | $sorts[] = UserFilterOrder::fromExport($sort); |
| 500 | } |
| 501 | } |
| 502 | return new UserFilter($cond, $sorts); |
| 503 | } |
| 504 | |
| 505 | public static function fromJSon($json) |
| 506 | { |
| 507 | $export = json_decode($json, true); |
| 508 | if (is_null($export)) { |
| 509 | throw new Exception("Invalid json: $json"); |
| 510 | } |
| 511 | return self::fromExport($json); |
| 512 | } |
| 513 | |
| 514 | public static function fromExportedConditions(array $export) |
| 515 | { |
| 516 | $cond = UserFilterCondition::fromExport($export); |
| 517 | return new UserFilter($cond); |
| 518 | } |
| 519 | |
| 520 | public static function fromJSonConditions($json) |
| 521 | { |
| 522 | $export = json_decode($json, true); |
| 523 | if (is_null($export)) { |
| 524 | throw new Exception("Invalid json: $json"); |
| 525 | } |
| 526 | return self::fromExportedConditions($json); |
| 527 | } |
| 528 | |
| 529 | static public function getLegacy($promo_min, $promo_max) |
| 530 | { |
| 531 | if ($promo_min != 0) { |
| 532 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
| 533 | } else { |
| 534 | $min = new PFC_True(); |
| 535 | } |
| 536 | if ($promo_max != 0) { |
| 537 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
| 538 | } else { |
| 539 | $max = new PFC_True(); |
| 540 | } |
| 541 | return new UserFilter(new PFC_And($min, $max)); |
| 542 | } |
| 543 | |
| 544 | static public function sortByName() |
| 545 | { |
| 546 | return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
| 547 | } |
| 548 | |
| 549 | static public function sortByPromo() |
| 550 | { |
| 551 | return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
| 552 | } |
| 553 | |
| 554 | static private function getDBSuffix($string) |
| 555 | { |
| 556 | if (is_array($string)) { |
| 557 | if (count($string) == 1) { |
| 558 | return self::getDBSuffix(array_pop($string)); |
| 559 | } |
| 560 | return md5(implode('|', $string)); |
| 561 | } else { |
| 562 | return preg_replace('/[^a-z0-9]/i', '', $string); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /** Stores a new (and unique) table alias in the &$table table |
| 568 | * @param &$table Array in which the table alias must be stored |
| 569 | * @param $val Value which will then be used to build the join |
| 570 | * @return Name of the newly created alias |
| 571 | */ |
| 572 | private $option = 0; |
| 573 | private function register_optional(array &$table, $val) |
| 574 | { |
| 575 | if (is_null($val)) { |
| 576 | $sub = $this->option++; |
| 577 | $index = null; |
| 578 | } else { |
| 579 | $sub = self::getDBSuffix($val); |
| 580 | $index = $val; |
| 581 | } |
| 582 | $sub = '_' . $sub; |
| 583 | $table[$sub] = $index; |
| 584 | return $sub; |
| 585 | } |
| 586 | |
| 587 | /** PROFILE VS ACCOUNT |
| 588 | */ |
| 589 | private $with_profiles = false; |
| 590 | private $with_accounts = false; |
| 591 | public function requireAccounts() |
| 592 | { |
| 593 | $this->with_accounts = true; |
| 594 | } |
| 595 | |
| 596 | public function accountsRequired() |
| 597 | { |
| 598 | return $this->with_accounts; |
| 599 | } |
| 600 | |
| 601 | public function requireProfiles() |
| 602 | { |
| 603 | $this->with_profiles = true; |
| 604 | } |
| 605 | |
| 606 | public function profilesRequired() |
| 607 | { |
| 608 | return $this->with_profiles; |
| 609 | } |
| 610 | |
| 611 | protected function accountJoins() |
| 612 | { |
| 613 | $joins = array(); |
| 614 | if ($this->with_profiles && $this->with_accounts) { |
| 615 | $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)'); |
| 616 | $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid'); |
| 617 | } |
| 618 | return $joins; |
| 619 | } |
| 620 | |
| 621 | /** PERMISSIONS |
| 622 | */ |
| 623 | private $at = false; |
| 624 | public function requirePerms() |
| 625 | { |
| 626 | $this->requireAccounts(); |
| 627 | $this->at = true; |
| 628 | return 'at'; |
| 629 | } |
| 630 | |
| 631 | protected function permJoins() |
| 632 | { |
| 633 | if ($this->at) { |
| 634 | return array('at' => PlSqlJoin::left('account_types', '$ME.type = a.type')); |
| 635 | } else { |
| 636 | return array(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /** DISPLAY |
| 641 | */ |
| 642 | const DISPLAY = 'display'; |
| 643 | private $pd = false; |
| 644 | public function addDisplayFilter() |
| 645 | { |
| 646 | $this->requireProfiles(); |
| 647 | $this->pd = true; |
| 648 | return ''; |
| 649 | } |
| 650 | |
| 651 | protected function displayJoins() |
| 652 | { |
| 653 | if ($this->pd) { |
| 654 | return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID')); |
| 655 | } else { |
| 656 | return array(); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /** LOGGER |
| 661 | */ |
| 662 | |
| 663 | private $with_logger = false; |
| 664 | public function addLoggerFilter() |
| 665 | { |
| 666 | $this->with_logger = true; |
| 667 | $this->requireAccounts(); |
| 668 | return 'ls'; |
| 669 | } |
| 670 | protected function loggerJoins() |
| 671 | { |
| 672 | $joins = array(); |
| 673 | if ($this->with_logger) { |
| 674 | $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID'); |
| 675 | } |
| 676 | return $joins; |
| 677 | } |
| 678 | |
| 679 | /** NAMES |
| 680 | */ |
| 681 | |
| 682 | static public function assertName($name) |
| 683 | { |
| 684 | if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) { |
| 685 | Platal::page()->kill('Invalid name type: ' . $name); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | private $pn = array(); |
| 690 | public function addNameFilter($type, $variant = null) |
| 691 | { |
| 692 | $this->requireProfiles(); |
| 693 | if (!is_null($variant)) { |
| 694 | $ft = $type . '_' . $variant; |
| 695 | } else { |
| 696 | $ft = $type; |
| 697 | } |
| 698 | $sub = '_' . $ft; |
| 699 | self::assertName($ft); |
| 700 | |
| 701 | if (!is_null($variant) && $variant == 'other') { |
| 702 | $sub .= $this->option++; |
| 703 | } |
| 704 | $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft); |
| 705 | return $sub; |
| 706 | } |
| 707 | |
| 708 | protected function nameJoins() |
| 709 | { |
| 710 | $joins = array(); |
| 711 | foreach ($this->pn as $sub => $type) { |
| 712 | $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type); |
| 713 | } |
| 714 | return $joins; |
| 715 | } |
| 716 | |
| 717 | /** NAMETOKENS |
| 718 | */ |
| 719 | private $name_tokens = array(); |
| 720 | private $nb_tokens = 0; |
| 721 | |
| 722 | public function addNameTokensFilter($token) |
| 723 | { |
| 724 | $this->requireProfiles(); |
| 725 | $sub = 'sn' . (1 + $this->nb_tokens); |
| 726 | $this->nb_tokens++; |
| 727 | $this->name_tokens[$sub] = $token; |
| 728 | return $sub; |
| 729 | } |
| 730 | |
| 731 | protected function nameTokensJoins() |
| 732 | { |
| 733 | /* We don't return joins, since with_sn forces the SELECT to run on search_name first */ |
| 734 | $joins = array(); |
| 735 | foreach ($this->name_tokens as $sub => $token) { |
| 736 | $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID'); |
| 737 | } |
| 738 | return $joins; |
| 739 | } |
| 740 | |
| 741 | public function getNameTokens() |
| 742 | { |
| 743 | return $this->name_tokens; |
| 744 | } |
| 745 | |
| 746 | /** NATIONALITY |
| 747 | */ |
| 748 | |
| 749 | private $with_nat = false; |
| 750 | public function addNationalityFilter() |
| 751 | { |
| 752 | $this->with_nat = true; |
| 753 | return 'ngc'; |
| 754 | } |
| 755 | |
| 756 | protected function nationalityJoins() |
| 757 | { |
| 758 | $joins = array(); |
| 759 | if ($this->with_nat) { |
| 760 | $joins['ngc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = p.nationality1 OR $ME.iso_3166_1_a2 = p.nationality2 OR $ME.iso_3166_1_a2 = p.nationality3'); |
| 761 | } |
| 762 | return $joins; |
| 763 | } |
| 764 | |
| 765 | /** EDUCATION |
| 766 | */ |
| 767 | const GRADE_ING = 'Ing.'; |
| 768 | const GRADE_PHD = 'PhD'; |
| 769 | const GRADE_MST = 'M%'; |
| 770 | static public function isGrade($grade) |
| 771 | { |
| 772 | return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST); |
| 773 | } |
| 774 | |
| 775 | static public function assertGrade($grade) |
| 776 | { |
| 777 | if (!self::isGrade($grade)) { |
| 778 | Platal::page()->killError("Diplôme non valide: $grade"); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | static public function promoYear($grade) |
| 783 | { |
| 784 | // XXX: Definition of promotion for phds and masters might change in near future. |
| 785 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; |
| 786 | } |
| 787 | |
| 788 | private $pepe = array(); |
| 789 | private $with_pee = false; |
| 790 | public function addEducationFilter($x = false, $grade = null) |
| 791 | { |
| 792 | $this->requireProfiles(); |
| 793 | if (!$x) { |
| 794 | $index = $this->option; |
| 795 | $sub = $this->option++; |
| 796 | } else { |
| 797 | self::assertGrade($grade); |
| 798 | $index = $grade; |
| 799 | $sub = $grade[0]; |
| 800 | $this->with_pee = true; |
| 801 | } |
| 802 | $sub = '_' . $sub; |
| 803 | $this->pepe[$index] = $sub; |
| 804 | return $sub; |
| 805 | } |
| 806 | |
| 807 | protected function educationJoins() |
| 808 | { |
| 809 | $joins = array(); |
| 810 | if ($this->with_pee) { |
| 811 | $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\''); |
| 812 | } |
| 813 | foreach ($this->pepe as $grade => $sub) { |
| 814 | if ($this->isGrade($grade)) { |
| 815 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID'); |
| 816 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade); |
| 817 | } else { |
| 818 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID'); |
| 819 | $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); |
| 820 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); |
| 821 | } |
| 822 | } |
| 823 | return $joins; |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /** GROUPS |
| 828 | */ |
| 829 | private $gpm = array(); |
| 830 | public function addGroupFilter($group = null) |
| 831 | { |
| 832 | $this->requireAccounts(); |
| 833 | if (!is_null($group)) { |
| 834 | if (is_int($group) || ctype_digit($group)) { |
| 835 | $index = $sub = $group; |
| 836 | } else { |
| 837 | $index = $group; |
| 838 | $sub = self::getDBSuffix($group); |
| 839 | } |
| 840 | } else { |
| 841 | $sub = 'group_' . $this->option++; |
| 842 | $index = null; |
| 843 | } |
| 844 | $sub = '_' . $sub; |
| 845 | $this->gpm[$sub] = $index; |
| 846 | return $sub; |
| 847 | } |
| 848 | |
| 849 | protected function groupJoins() |
| 850 | { |
| 851 | $joins = array(); |
| 852 | foreach ($this->gpm as $sub => $key) { |
| 853 | if (is_null($key)) { |
| 854 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups'); |
| 855 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); |
| 856 | } else if (is_int($key) || ctype_digit($key)) { |
| 857 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key); |
| 858 | } else { |
| 859 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key); |
| 860 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); |
| 861 | } |
| 862 | } |
| 863 | return $joins; |
| 864 | } |
| 865 | |
| 866 | /** NLS |
| 867 | */ |
| 868 | private $nls = array(); |
| 869 | public function addNewsLetterFilter($nlid) |
| 870 | { |
| 871 | $this->requireAccounts(); |
| 872 | $sub = 'nl_' . $nlid; |
| 873 | $this->nls[$nlid] = $sub; |
| 874 | return $sub; |
| 875 | } |
| 876 | |
| 877 | protected function newsLetterJoins() |
| 878 | { |
| 879 | $joins = array(); |
| 880 | foreach ($this->nls as $key => $sub) { |
| 881 | $joins[$sub] = PlSqlJoin::left('newsletter_ins', '$ME.nlid = {?} AND $ME.uid = $UID', $key); |
| 882 | } |
| 883 | return $joins; |
| 884 | } |
| 885 | |
| 886 | /** BINETS |
| 887 | */ |
| 888 | |
| 889 | private $with_bi = false; |
| 890 | private $with_bd = false; |
| 891 | public function addBinetsFilter($with_enum = false) |
| 892 | { |
| 893 | $this->requireProfiles(); |
| 894 | $this->with_bi = true; |
| 895 | if ($with_enum) { |
| 896 | $this->with_bd = true; |
| 897 | return 'bd'; |
| 898 | } else { |
| 899 | return 'bi'; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | protected function binetsJoins() |
| 904 | { |
| 905 | $joins = array(); |
| 906 | if ($this->with_bi) { |
| 907 | $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID'); |
| 908 | } |
| 909 | if ($this->with_bd) { |
| 910 | $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id'); |
| 911 | } |
| 912 | return $joins; |
| 913 | } |
| 914 | |
| 915 | /** EMAILS |
| 916 | */ |
| 917 | private $ra = array(); |
| 918 | /** Allows filtering by redirection. |
| 919 | * @param $email If null, enable a left join on the email redirection table |
| 920 | * (email_redirect_account); otherwise, perform a left join on users having |
| 921 | * that email as a redirection. |
| 922 | * @return Suffix to use to access the adequate table. |
| 923 | */ |
| 924 | public function addEmailRedirectFilter($email = null) |
| 925 | { |
| 926 | $this->requireAccounts(); |
| 927 | return $this->register_optional($this->ra, $email); |
| 928 | } |
| 929 | |
| 930 | const ALIAS_BEST = 'bestalias'; |
| 931 | const ALIAS_FORLIFE = 'forlife'; |
| 932 | const ALIAS_AUXILIARY = 'alias_aux'; |
| 933 | private $sa = array(); |
| 934 | /** Allows filtering by source email. |
| 935 | * @param $email If null, enable a left join on the email source table |
| 936 | * (email_source_account); otherwise, perform a left join on users having |
| 937 | * that email as a source email. |
| 938 | * @return Suffix to use to access the adequate table. |
| 939 | */ |
| 940 | public function addAliasFilter($email = null) |
| 941 | { |
| 942 | $this->requireAccounts(); |
| 943 | return $this->register_optional($this->sa, $email); |
| 944 | } |
| 945 | |
| 946 | private $with_rf = false; |
| 947 | /** Allows filtering by active redirection. |
| 948 | * @return Suffix to use to access the adequate table. |
| 949 | */ |
| 950 | public function addActiveEmailRedirectFilter($email = null) |
| 951 | { |
| 952 | $this->requireAccounts(); |
| 953 | $this->with_rf = true; |
| 954 | } |
| 955 | |
| 956 | protected function emailJoins() |
| 957 | { |
| 958 | global $globals; |
| 959 | $joins = array(); |
| 960 | foreach ($this->ra as $sub => $redirections) { |
| 961 | if (is_null($redirections)) { |
| 962 | $joins['ra' . $sub] = PlSqlJoin::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\''); |
| 963 | } else { |
| 964 | if (!is_array($redirections)) { |
| 965 | $key = array($redirections); |
| 966 | } |
| 967 | $joins['ra' . $sub] = PlSqlJoin::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\' |
| 968 | AND $ME.redirect IN {?}', $redirections); |
| 969 | } |
| 970 | } |
| 971 | foreach ($this->sa as $sub => $emails) { |
| 972 | if (is_null($emails)) { |
| 973 | $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID'); |
| 974 | } else if ($key == self::ALIAS_BEST) { |
| 975 | $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND FIND_IN_SET(\'bestalias\', $ME.flags)'); |
| 976 | } else if ($key == self::ALIAS_FORLIFE) { |
| 977 | $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'forlife\''); |
| 978 | } else if ($key == self::ALIAS_AUXILIARY) { |
| 979 | $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'alias_aux\''); |
| 980 | } else { |
| 981 | if (!is_array($emails)) { |
| 982 | $key = array($emails); |
| 983 | } |
| 984 | $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.email IN {?}', $emails); |
| 985 | } |
| 986 | } |
| 987 | if ($this->with_rf) { |
| 988 | $joins['rf'] = PlSqlJoin::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\' AND $ME.flags = \'active\'');; |
| 989 | } |
| 990 | return $joins; |
| 991 | } |
| 992 | |
| 993 | |
| 994 | /** ADDRESSES |
| 995 | */ |
| 996 | private $with_pa = false; |
| 997 | public function addAddressFilter() |
| 998 | { |
| 999 | $this->requireProfiles(); |
| 1000 | $this->with_pa = true; |
| 1001 | return 'pa'; |
| 1002 | } |
| 1003 | |
| 1004 | private $with_pac = false; |
| 1005 | public function addAddressCountryFilter() |
| 1006 | { |
| 1007 | $this->requireProfiles(); |
| 1008 | $this->addAddressFilter(); |
| 1009 | $this->with_pac = true; |
| 1010 | return 'gc'; |
| 1011 | } |
| 1012 | |
| 1013 | private $with_pal = false; |
| 1014 | public function addAddressLocalityFilter() |
| 1015 | { |
| 1016 | $this->requireProfiles(); |
| 1017 | $this->addAddressFilter(); |
| 1018 | $this->with_pal = true; |
| 1019 | return 'gl'; |
| 1020 | } |
| 1021 | |
| 1022 | protected function addressJoins() |
| 1023 | { |
| 1024 | $joins = array(); |
| 1025 | if ($this->with_pa) { |
| 1026 | $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID'); |
| 1027 | } |
| 1028 | if ($this->with_pac) { |
| 1029 | $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID'); |
| 1030 | } |
| 1031 | if ($this->with_pal) { |
| 1032 | $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID'); |
| 1033 | } |
| 1034 | return $joins; |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | /** CORPS |
| 1039 | */ |
| 1040 | |
| 1041 | private $pc = false; |
| 1042 | private $pce = array(); |
| 1043 | private $pcr = false; |
| 1044 | public function addCorpsFilter($type) |
| 1045 | { |
| 1046 | $this->requireProfiles(); |
| 1047 | $this->pc = true; |
| 1048 | if ($type == UFC_Corps::CURRENT) { |
| 1049 | $this->pce['pcec'] = 'current_corpsid'; |
| 1050 | return 'pcec'; |
| 1051 | } else if ($type == UFC_Corps::ORIGIN) { |
| 1052 | $this->pce['pceo'] = 'original_corpsid'; |
| 1053 | return 'pceo'; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | public function addCorpsRankFilter() |
| 1058 | { |
| 1059 | $this->requireProfiles(); |
| 1060 | $this->pc = true; |
| 1061 | $this->pcr = true; |
| 1062 | return 'pcr'; |
| 1063 | } |
| 1064 | |
| 1065 | protected function corpsJoins() |
| 1066 | { |
| 1067 | $joins = array(); |
| 1068 | if ($this->pc) { |
| 1069 | $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID'); |
| 1070 | } |
| 1071 | if ($this->pcr) { |
| 1072 | $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid'); |
| 1073 | } |
| 1074 | foreach($this->pce as $sub => $field) { |
| 1075 | $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field); |
| 1076 | } |
| 1077 | return $joins; |
| 1078 | } |
| 1079 | |
| 1080 | /** JOBS |
| 1081 | */ |
| 1082 | |
| 1083 | const JOB_USERDEFINED = 0x0001; |
| 1084 | const JOB_CV = 0x0002; |
| 1085 | const JOB_ANY = 0x0003; |
| 1086 | |
| 1087 | /** Joins : |
| 1088 | * pj => profile_job |
| 1089 | * pje => profile_job_enum |
| 1090 | * pjt => profile_job_terms |
| 1091 | */ |
| 1092 | private $with_pj = false; |
| 1093 | private $with_pje = false; |
| 1094 | private $with_pjt = 0; |
| 1095 | |
| 1096 | public function addJobFilter() |
| 1097 | { |
| 1098 | $this->requireProfiles(); |
| 1099 | $this->with_pj = true; |
| 1100 | return 'pj'; |
| 1101 | } |
| 1102 | |
| 1103 | public function addJobCompanyFilter() |
| 1104 | { |
| 1105 | $this->addJobFilter(); |
| 1106 | $this->with_pje = true; |
| 1107 | return 'pje'; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * Adds a filter on job terms of profile. |
| 1112 | * @param $nb the number of job terms to use |
| 1113 | * @return an array of the fields to filter (one for each term). |
| 1114 | */ |
| 1115 | public function addJobTermsFilter($nb = 1) |
| 1116 | { |
| 1117 | $this->with_pjt = $nb; |
| 1118 | $jobtermstable = array(); |
| 1119 | for ($i = 1; $i <= $nb; ++$i) { |
| 1120 | $jobtermstable[] = 'pjtr_'.$i; |
| 1121 | } |
| 1122 | return $jobtermstable; |
| 1123 | } |
| 1124 | |
| 1125 | protected function jobJoins() |
| 1126 | { |
| 1127 | $joins = array(); |
| 1128 | if ($this->with_pj) { |
| 1129 | $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID'); |
| 1130 | } |
| 1131 | if ($this->with_pje) { |
| 1132 | $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid'); |
| 1133 | } |
| 1134 | if ($this->with_pjt > 0) { |
| 1135 | for ($i = 1; $i <= $this->with_pjt; ++$i) { |
| 1136 | $joins['pjt_'.$i] = PlSqlJoin::left('profile_job_term', '$ME.pid = $PID'); |
| 1137 | $joins['pjtr_'.$i] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid'); |
| 1138 | } |
| 1139 | } |
| 1140 | return $joins; |
| 1141 | } |
| 1142 | |
| 1143 | /** NETWORKING |
| 1144 | */ |
| 1145 | |
| 1146 | private $with_pnw = false; |
| 1147 | public function addNetworkingFilter() |
| 1148 | { |
| 1149 | $this->requireAccounts(); |
| 1150 | $this->with_pnw = true; |
| 1151 | return 'pnw'; |
| 1152 | } |
| 1153 | |
| 1154 | protected function networkingJoins() |
| 1155 | { |
| 1156 | $joins = array(); |
| 1157 | if ($this->with_pnw) { |
| 1158 | $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID'); |
| 1159 | } |
| 1160 | return $joins; |
| 1161 | } |
| 1162 | |
| 1163 | /** PHONE |
| 1164 | */ |
| 1165 | |
| 1166 | private $with_ptel = false; |
| 1167 | |
| 1168 | public function addPhoneFilter() |
| 1169 | { |
| 1170 | $this->requireAccounts(); |
| 1171 | $this->with_ptel = true; |
| 1172 | return 'ptel'; |
| 1173 | } |
| 1174 | |
| 1175 | protected function phoneJoins() |
| 1176 | { |
| 1177 | $joins = array(); |
| 1178 | if ($this->with_ptel) { |
| 1179 | $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID'); |
| 1180 | } |
| 1181 | return $joins; |
| 1182 | } |
| 1183 | |
| 1184 | /** MEDALS |
| 1185 | */ |
| 1186 | |
| 1187 | private $with_pmed = false; |
| 1188 | public function addMedalFilter() |
| 1189 | { |
| 1190 | $this->requireProfiles(); |
| 1191 | $this->with_pmed = true; |
| 1192 | return 'pmed'; |
| 1193 | } |
| 1194 | |
| 1195 | protected function medalJoins() |
| 1196 | { |
| 1197 | $joins = array(); |
| 1198 | if ($this->with_pmed) { |
| 1199 | $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID'); |
| 1200 | } |
| 1201 | return $joins; |
| 1202 | } |
| 1203 | |
| 1204 | /** MENTORING |
| 1205 | */ |
| 1206 | |
| 1207 | private $pms = array(); |
| 1208 | private $mjtr = false; |
| 1209 | const MENTOR = 1; |
| 1210 | const MENTOR_EXPERTISE = 2; |
| 1211 | const MENTOR_COUNTRY = 3; |
| 1212 | const MENTOR_TERM = 4; |
| 1213 | |
| 1214 | public function addMentorFilter($type) |
| 1215 | { |
| 1216 | $this->requireAccounts(); |
| 1217 | switch($type) { |
| 1218 | case self::MENTOR: |
| 1219 | $this->pms['pm'] = 'profile_mentor'; |
| 1220 | return 'pm'; |
| 1221 | case self::MENTOR_EXPERTISE: |
| 1222 | $this->pms['pme'] = 'profile_mentor'; |
| 1223 | return 'pme'; |
| 1224 | case self::MENTOR_COUNTRY: |
| 1225 | $this->pms['pmc'] = 'profile_mentor_country'; |
| 1226 | return 'pmc'; |
| 1227 | case self::MENTOR_TERM: |
| 1228 | $this->pms['pmt'] = 'profile_mentor_term'; |
| 1229 | $this->mjtr = true; |
| 1230 | return 'mjtr'; |
| 1231 | default: |
| 1232 | Platal::page()->killError("Undefined mentor filter."); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | protected function mentorJoins() |
| 1237 | { |
| 1238 | $joins = array(); |
| 1239 | foreach ($this->pms as $sub => $tab) { |
| 1240 | $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID'); |
| 1241 | } |
| 1242 | if ($this->mjtr) { |
| 1243 | $joins['mjtr'] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid'); |
| 1244 | } |
| 1245 | return $joins; |
| 1246 | } |
| 1247 | |
| 1248 | /** CONTACTS |
| 1249 | */ |
| 1250 | private $cts = array(); |
| 1251 | public function addContactFilter($uid = null) |
| 1252 | { |
| 1253 | $this->requireProfiles(); |
| 1254 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); |
| 1255 | } |
| 1256 | |
| 1257 | protected function contactJoins() |
| 1258 | { |
| 1259 | $joins = array(); |
| 1260 | foreach ($this->cts as $sub=>$key) { |
| 1261 | if (is_null($key)) { |
| 1262 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID'); |
| 1263 | } else { |
| 1264 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5)); |
| 1265 | } |
| 1266 | } |
| 1267 | return $joins; |
| 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | /** CARNET |
| 1272 | */ |
| 1273 | private $wn = array(); |
| 1274 | public function addWatchRegistrationFilter($uid = null) |
| 1275 | { |
| 1276 | $this->requireAccounts(); |
| 1277 | return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid); |
| 1278 | } |
| 1279 | |
| 1280 | private $wp = array(); |
| 1281 | public function addWatchPromoFilter($uid = null) |
| 1282 | { |
| 1283 | $this->requireAccounts(); |
| 1284 | return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid); |
| 1285 | } |
| 1286 | |
| 1287 | private $w = array(); |
| 1288 | public function addWatchFilter($uid = null) |
| 1289 | { |
| 1290 | $this->requireAccounts(); |
| 1291 | return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid); |
| 1292 | } |
| 1293 | |
| 1294 | protected function watchJoins() |
| 1295 | { |
| 1296 | $joins = array(); |
| 1297 | foreach ($this->w as $sub=>$key) { |
| 1298 | if (is_null($key)) { |
| 1299 | $joins['w' . $sub] = PlSqlJoin::left('watch'); |
| 1300 | } else { |
| 1301 | $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5)); |
| 1302 | } |
| 1303 | } |
| 1304 | foreach ($this->wn as $sub=>$key) { |
| 1305 | if (is_null($key)) { |
| 1306 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
| 1307 | } else { |
| 1308 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
| 1309 | } |
| 1310 | } |
| 1311 | foreach ($this->wn as $sub=>$key) { |
| 1312 | if (is_null($key)) { |
| 1313 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
| 1314 | } else { |
| 1315 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
| 1316 | } |
| 1317 | } |
| 1318 | foreach ($this->wp as $sub=>$key) { |
| 1319 | if (is_null($key)) { |
| 1320 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo'); |
| 1321 | } else { |
| 1322 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5)); |
| 1323 | } |
| 1324 | } |
| 1325 | return $joins; |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | /** PHOTOS |
| 1330 | */ |
| 1331 | private $with_photo; |
| 1332 | public function addPhotoFilter() |
| 1333 | { |
| 1334 | $this->requireProfiles(); |
| 1335 | $this->with_photo = true; |
| 1336 | return 'photo'; |
| 1337 | } |
| 1338 | |
| 1339 | protected function photoJoins() |
| 1340 | { |
| 1341 | if ($this->with_photo) { |
| 1342 | return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID')); |
| 1343 | } else { |
| 1344 | return array(); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | /** MARKETING |
| 1350 | */ |
| 1351 | private $with_rm; |
| 1352 | public function addMarketingHash() |
| 1353 | { |
| 1354 | $this->requireAccounts(); |
| 1355 | $this->with_rm = true; |
| 1356 | } |
| 1357 | |
| 1358 | protected function marketingJoins() |
| 1359 | { |
| 1360 | if ($this->with_rm) { |
| 1361 | return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID')); |
| 1362 | } else { |
| 1363 | return array(); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | // }}} |
| 1368 | // {{{ class ProfileFilter |
| 1369 | class ProfileFilter extends UserFilter |
| 1370 | { |
| 1371 | public function get($limit = null) |
| 1372 | { |
| 1373 | return $this->getProfiles($limit); |
| 1374 | } |
| 1375 | |
| 1376 | public function getIds($limit = null) |
| 1377 | { |
| 1378 | return $this->getPIDs(); |
| 1379 | } |
| 1380 | |
| 1381 | public function filter(array $profiles, $limit = null) |
| 1382 | { |
| 1383 | return $this->filterProfiles($profiles, self::defaultLimit($limit)); |
| 1384 | } |
| 1385 | |
| 1386 | public function getTotalCount() |
| 1387 | { |
| 1388 | return $this->getTotalProfileCount(); |
| 1389 | } |
| 1390 | |
| 1391 | public function getGroups() |
| 1392 | { |
| 1393 | return $this->getPIDGroups(); |
| 1394 | } |
| 1395 | } |
| 1396 | // }}} |
| 1397 | |
| 1398 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 1399 | ?> |