| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class Profile |
| 23 | { |
| 24 | static private $v_values = array('public' => array('public'), |
| 25 | 'ax' => array('ax', 'public'), |
| 26 | 'private' => array('private', 'ax', 'public')); |
| 27 | |
| 28 | const VISIBILITY_PUBLIC = 'public'; |
| 29 | const VISIBILITY_AX = 'ax'; |
| 30 | const VISIBILITY_PRIVATE = 'private'; |
| 31 | |
| 32 | /* name tokens */ |
| 33 | const LASTNAME = 'lastname'; |
| 34 | const FIRSTNAME = 'firstname'; |
| 35 | const NICKNAME = 'nickname'; |
| 36 | const PSEUDONYM = 'pseudonym'; |
| 37 | const NAME = 'name'; |
| 38 | /* name variants */ |
| 39 | const VN_MARITAL = 'marital'; |
| 40 | const VN_ORDINARY = 'ordinary'; |
| 41 | const VN_OTHER = 'other'; |
| 42 | const VN_INI = 'ini'; |
| 43 | /* display names */ |
| 44 | const DN_FULL = 'directory_name'; |
| 45 | const DN_DISPLAY = 'yourself'; |
| 46 | const DN_YOURSELF = 'yourself'; |
| 47 | const DN_DIRECTORY = 'directory_name'; |
| 48 | const DN_PRIVATE = 'private_name'; |
| 49 | const DN_PUBLIC = 'public_name'; |
| 50 | const DN_SHORT = 'short_name'; |
| 51 | const DN_SORT = 'sort_name'; |
| 52 | /* education related names */ |
| 53 | const EDU_X = 'École polytechnique'; |
| 54 | const DEGREE_X = 'Ingénieur'; |
| 55 | const DEGREE_M = 'Master'; |
| 56 | const DEGREE_D = 'Doctorat'; |
| 57 | |
| 58 | static public $name_variants = array( |
| 59 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), |
| 60 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
| 61 | ); |
| 62 | |
| 63 | const ADDRESS_MAIN = 0x000001; |
| 64 | const ADDRESS_PERSO = 0x000002; |
| 65 | const ADDRESS_PRO = 0x000004; |
| 66 | const ADDRESS_ALL = 0x000006; |
| 67 | const ADDRESS_POSTAL = 0x000008; |
| 68 | |
| 69 | const EDUCATION_MAIN = 0x000010; |
| 70 | const EDUCATION_EXTRA = 0x000020; |
| 71 | const EDUCATION_ALL = 0x000040; |
| 72 | const EDUCATION_FINISHED = 0x000080; |
| 73 | const EDUCATION_CURRENT = 0x000100; |
| 74 | |
| 75 | const JOBS_MAIN = 0x001000; |
| 76 | const JOBS_ALL = 0x002000; |
| 77 | const JOBS_FINISHED = 0x004000; |
| 78 | const JOBS_CURRENT = 0x008000; |
| 79 | |
| 80 | const NETWORKING_ALL = 0x000000; |
| 81 | const NETWORKING_WEB = 0x010000; |
| 82 | const NETWORKING_IM = 0x020000; |
| 83 | const NETWORKING_SOCIAL = 0x040000; |
| 84 | |
| 85 | const FETCH_ADDRESSES = 0x00001; |
| 86 | const FETCH_CORPS = 0x00002; |
| 87 | const FETCH_EDU = 0x00004; |
| 88 | const FETCH_JOBS = 0x00008; |
| 89 | const FETCH_MEDALS = 0x00010; |
| 90 | const FETCH_NETWORKING = 0x00020; |
| 91 | const FETCH_PHONES = 0x00040; |
| 92 | const FETCH_PHOTO = 0x00080; |
| 93 | |
| 94 | const FETCH_ALL = 0x000FF; |
| 95 | |
| 96 | private $pid; |
| 97 | private $hrpid; |
| 98 | private $data = array(); |
| 99 | |
| 100 | private $visibility = null; |
| 101 | |
| 102 | private function __construct(array $data) |
| 103 | { |
| 104 | $this->data = $data; |
| 105 | $this->pid = $this->data['pid']; |
| 106 | $this->hrpid = $this->data['hrpid']; |
| 107 | if (!S::logged()) { |
| 108 | $this->setVisibilityLevel(self::VISIBILITY_PUBLIC); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static private $contexts = array(); |
| 113 | |
| 114 | /** Returns the best visibility context toward $visibility |
| 115 | * @param $visibility A wished visibility level |
| 116 | * @return The closest allowed visibility level |
| 117 | * |
| 118 | * if $visibility is null, the best visibility is returned |
| 119 | */ |
| 120 | static public function getVisibilityContext($visibility = null) |
| 121 | { |
| 122 | if (array_key_exists($visibility, self::$contexts)) { |
| 123 | return self::$contexts[$visibility]; |
| 124 | } |
| 125 | |
| 126 | $asked_vis = $visibility; |
| 127 | |
| 128 | if (S::logged()) { |
| 129 | $minvis = self::VISIBILITY_PRIVATE; |
| 130 | } else { |
| 131 | $minvis = self::VISIBILITY_PUBLIC; |
| 132 | } |
| 133 | if ($visibility == null) { |
| 134 | $visibility = array($minvis); |
| 135 | } |
| 136 | |
| 137 | if ($minvis == self::VISIBILITY_PUBLIC) { |
| 138 | $visibility = array(self::VISIBILITY_PUBLIC); |
| 139 | } |
| 140 | |
| 141 | self::$contexts[$asked_vis] = $visibility; |
| 142 | |
| 143 | return $visibility; |
| 144 | } |
| 145 | |
| 146 | public function id() |
| 147 | { |
| 148 | return $this->pid; |
| 149 | } |
| 150 | |
| 151 | public function hrid() |
| 152 | { |
| 153 | return $this->hrpid; |
| 154 | } |
| 155 | |
| 156 | public function promo() |
| 157 | { |
| 158 | return $this->promo; |
| 159 | } |
| 160 | |
| 161 | public function yearpromo() |
| 162 | { |
| 163 | return intval(substr($this->promo, 1, 4)); |
| 164 | } |
| 165 | |
| 166 | /** Print a name with the given formatting: |
| 167 | * %s = • for women |
| 168 | * %f = firstname |
| 169 | * %l = lastname |
| 170 | * %F = fullname |
| 171 | * %S = shortname |
| 172 | * %p = promo |
| 173 | */ |
| 174 | public function name($format) |
| 175 | { |
| 176 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), |
| 177 | array($this->isFemale() ? '•' : '', |
| 178 | $this->firstName(), $this->lastName(), |
| 179 | $this->fullName(), $this->shortName(), |
| 180 | $this->promo()), $format); |
| 181 | } |
| 182 | |
| 183 | public function fullName($with_promo = false) |
| 184 | { |
| 185 | if ($with_promo) { |
| 186 | return $this->full_name . ' (' . $this->promo . ')'; |
| 187 | } |
| 188 | return $this->full_name; |
| 189 | } |
| 190 | |
| 191 | public function shortName($with_promo = false) |
| 192 | { |
| 193 | if ($with_promo) { |
| 194 | return $this->short_name . ' (' . $this->promo . ')'; |
| 195 | } |
| 196 | return $this->short_name; |
| 197 | } |
| 198 | |
| 199 | public function firstName() |
| 200 | { |
| 201 | return $this->firstname; |
| 202 | } |
| 203 | |
| 204 | public function firstNames() |
| 205 | { |
| 206 | return $this->nameVariants(self::FIRSTNAME); |
| 207 | } |
| 208 | |
| 209 | public function lastName() |
| 210 | { |
| 211 | return $this->lastname; |
| 212 | } |
| 213 | |
| 214 | public function lastNames() |
| 215 | { |
| 216 | return $this->nameVariants(self::LASTNAME); |
| 217 | } |
| 218 | |
| 219 | public function isFemale() |
| 220 | { |
| 221 | return $this->sex == PlUser::GENDER_FEMALE; |
| 222 | } |
| 223 | |
| 224 | public function data() |
| 225 | { |
| 226 | $this->first_name; |
| 227 | return $this->data; |
| 228 | } |
| 229 | |
| 230 | private function nameVariants($type) |
| 231 | { |
| 232 | $vals = array($this->$type); |
| 233 | foreach (self::$name_variants[$type] as $var) { |
| 234 | $vartype = $type . '_' . $var; |
| 235 | $varname = $this->$vartype; |
| 236 | if ($varname != null && $varname != "") { |
| 237 | $vals[] = $varname; |
| 238 | } |
| 239 | } |
| 240 | return array_unique($vals); |
| 241 | } |
| 242 | |
| 243 | public function nationalities() |
| 244 | { |
| 245 | $nats = array(); |
| 246 | if ($this->nationality1) { |
| 247 | $nats[] = $this->nationality1; |
| 248 | } |
| 249 | if ($this->nationality2) { |
| 250 | $nats[] = $this->nationality2; |
| 251 | } |
| 252 | if ($this->nationality3) { |
| 253 | $nats[] = $this->nationality3; |
| 254 | } |
| 255 | return $nats; |
| 256 | } |
| 257 | |
| 258 | public function __get($name) |
| 259 | { |
| 260 | if (property_exists($this, $name)) { |
| 261 | return $this->$name; |
| 262 | } |
| 263 | |
| 264 | if (isset($this->data[$name])) { |
| 265 | return $this->data[$name]; |
| 266 | } |
| 267 | |
| 268 | return null; |
| 269 | } |
| 270 | |
| 271 | public function __isset($name) |
| 272 | { |
| 273 | return property_exists($this, $name) || isset($this->data[$name]); |
| 274 | } |
| 275 | |
| 276 | /** Sets the level of visibility of the profile |
| 277 | * Sets $this->visibility to a list of valid visibilities. |
| 278 | * @param one of the self::VISIBILITY_* values |
| 279 | */ |
| 280 | public function setVisibilityLevel($visibility) |
| 281 | { |
| 282 | if ($visibility != self::VISIBILITY_PRIVATE |
| 283 | && $visibility != self::VISIBILITY_AX |
| 284 | && $visibility != self::VISIBILITY_PUBLIC) { |
| 285 | Platal::page()->kill("Visibility invalide: " . $visibility); |
| 286 | } |
| 287 | $this->visibility = self::$v_values[$visibility]; |
| 288 | if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) { |
| 289 | unset($this->data['mobile']); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** Determine whether an item with visibility $visibility can be displayed |
| 294 | * with the current level of visibility of the profile |
| 295 | * @param $visibility The level of visibility to be checked |
| 296 | */ |
| 297 | public function isVisible($visibility) |
| 298 | { |
| 299 | return in_array($visibility, $this->visibility); |
| 300 | } |
| 301 | |
| 302 | public static function getCompatibleVisibilities($visibility) |
| 303 | { |
| 304 | return self::$v_values[$visibility]; |
| 305 | } |
| 306 | |
| 307 | /* Photo |
| 308 | */ |
| 309 | private $photo = null; |
| 310 | public function setPhoto(ProfilePhoto $photo) |
| 311 | { |
| 312 | $this->photo = $photo; |
| 313 | } |
| 314 | |
| 315 | public function getPhoto($fallback = true) |
| 316 | { |
| 317 | if ($this->photo != null) { |
| 318 | return $this->photo->pic; |
| 319 | } else if ($fallback) { |
| 320 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png', |
| 321 | 'image/png'); |
| 322 | } |
| 323 | return null; |
| 324 | } |
| 325 | |
| 326 | /* Addresses |
| 327 | */ |
| 328 | private $addresses = null; |
| 329 | public function setAddresses(ProfileAddresses $addr) |
| 330 | { |
| 331 | $this->addresses = $addr; |
| 332 | } |
| 333 | |
| 334 | public function getAddresses($flags, $limit = null) |
| 335 | { |
| 336 | if ($this->addresses == null) { |
| 337 | return PlIteratorUtils::fromArray(array()); |
| 338 | } else { |
| 339 | return $this->addresses->get($flags, $limit); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | public function getMainAddress() |
| 344 | { |
| 345 | $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); |
| 346 | if ($it->total() == 0) { |
| 347 | return null; |
| 348 | } else { |
| 349 | return $it->next(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /* Educations |
| 355 | */ |
| 356 | public function getEducations($flags, $limit = null) |
| 357 | { |
| 358 | $where = XDB::format('pe.pid = {?}', $this->id()); |
| 359 | if ($flags & self::EDUCATION_MAIN) { |
| 360 | $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)'; |
| 361 | } else if ($flags & self::EDUCATION_EXTRA) { |
| 362 | $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)'; |
| 363 | } else if ($flags & self::EDUCATION_FINISHED) { |
| 364 | $where .= ' AND pe.grad_year <= YEAR(CURDATE())'; |
| 365 | } else if ($flags & self::EDUCATION_CURRENT) { |
| 366 | $where .= ' AND pe.grad_year > YEAR(CURDATE())'; |
| 367 | } |
| 368 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
| 369 | return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program, |
| 370 | pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country, |
| 371 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field, |
| 372 | FIND_IN_SET(\'primary\', pe.flags) AS prim |
| 373 | FROM profile_education AS pe |
| 374 | INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) |
| 375 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) |
| 376 | INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) |
| 377 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) |
| 378 | WHERE ' . $where . ' |
| 379 | ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id |
| 380 | ' . $limit); |
| 381 | } |
| 382 | |
| 383 | public function getExtraEducations($limit = null) |
| 384 | { |
| 385 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /** Networking |
| 390 | */ |
| 391 | |
| 392 | public function getNetworking($flags, $limit = null) |
| 393 | { |
| 394 | $where = XDB::format('pn.pid = {?}', $this->id()); |
| 395 | if ($flags & self::NETWORKING_WEB) { |
| 396 | $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index |
| 397 | } |
| 398 | if ($this->visibility) { |
| 399 | $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility); |
| 400 | } |
| 401 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
| 402 | return XDB::iterator('SELECT pne.name, pne.icon, |
| 403 | IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address), |
| 404 | pn.address) AS address |
| 405 | FROM profile_networking AS pn |
| 406 | INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type) |
| 407 | WHERE ' . $where . ' |
| 408 | ORDER BY pn.network_type, pn.nwid |
| 409 | ' . $limit); |
| 410 | } |
| 411 | |
| 412 | public function getWebSite() |
| 413 | { |
| 414 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); |
| 415 | if ($site->total() != 1) { |
| 416 | return null; |
| 417 | } |
| 418 | $site = $site->next(); |
| 419 | return $site['address']; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /** Jobs |
| 424 | */ |
| 425 | |
| 426 | public function getJobs($flags, $limit = null) |
| 427 | { |
| 428 | $where = XDB::format('pj.pid = {?}', $this->id()); |
| 429 | $cond = 'TRUE'; |
| 430 | if ($this->visibility) { |
| 431 | $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility); |
| 432 | $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility); |
| 433 | } |
| 434 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
| 435 | return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code, |
| 436 | pj.description, pj.url AS user_site, |
| 437 | IF (' . $cond . ', pj.email, NULL) AS user_email, |
| 438 | pjse.name AS sector, pjsse.name AS subsector, |
| 439 | pjssse.name AS subsubsector |
| 440 | FROM profile_job AS pj |
| 441 | INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid) |
| 442 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) |
| 443 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) |
| 444 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) |
| 445 | WHERE ' . $where . ' |
| 446 | ORDER BY pj.id |
| 447 | ' . $limit); |
| 448 | } |
| 449 | |
| 450 | public function getMailJob() |
| 451 | { |
| 452 | $job = $this->getJobs(self::JOBS_MAIN, 1); |
| 453 | if ($job->total() != 1) { |
| 454 | return null; |
| 455 | } |
| 456 | return $job->next(); |
| 457 | } |
| 458 | |
| 459 | /* Binets |
| 460 | */ |
| 461 | public function getBinets() |
| 462 | { |
| 463 | return XDB::fetchColumn('SELECT binet_id |
| 464 | FROM profile_binets |
| 465 | WHERE pid = {?}', $this->id()); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | public function owner() |
| 470 | { |
| 471 | return User::getSilent($this); |
| 472 | } |
| 473 | |
| 474 | public function compareNames($firstname, $lastname) |
| 475 | { |
| 476 | $_lastname = mb_strtoupper($this->lastName()); |
| 477 | $_firstname = mb_strtoupper($this->firstName()); |
| 478 | $lastname = mb_strtoupper($lastname); |
| 479 | $firstname = mb_strtoupper($firstname); |
| 480 | |
| 481 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); |
| 482 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); |
| 483 | $maxlen = 0; |
| 484 | |
| 485 | foreach ($tokens as $str) { |
| 486 | $isOk &= (strpos($_lastname, $str) !== false); |
| 487 | $maxlen = max($maxlen, strlen($str)); |
| 488 | } |
| 489 | |
| 490 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); |
| 491 | } |
| 492 | |
| 493 | private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
| 494 | { |
| 495 | if (count($pids) == 0) { |
| 496 | return array(); |
| 497 | } |
| 498 | |
| 499 | if ($respect_order) { |
| 500 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); |
| 501 | } else { |
| 502 | $order = ''; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, |
| 507 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, |
| 508 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, |
| 509 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, |
| 510 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, |
| 511 | pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub, |
| 512 | ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub, |
| 513 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, |
| 514 | ap.uid AS owner_id |
| 515 | FROM profiles AS p |
| 516 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) |
| 517 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) |
| 518 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
| 519 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') |
| 520 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid |
| 521 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') |
| 522 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid |
| 523 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') |
| 524 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid |
| 525 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') |
| 526 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid |
| 527 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') |
| 528 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') |
| 529 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) |
| 530 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 531 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' |
| 532 | GROUP BY p.pid |
| 533 | ' . $order); |
| 534 | return new ProfileDataIterator($it, $pids, $fields, $visibility); |
| 535 | } |
| 536 | |
| 537 | public static function getPID($login) |
| 538 | { |
| 539 | if ($login instanceof PlUser) { |
| 540 | return XDB::fetchOneCell('SELECT pid |
| 541 | FROM account_profiles |
| 542 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', |
| 543 | $login->id()); |
| 544 | } else if (ctype_digit($login)) { |
| 545 | return XDB::fetchOneCell('SELECT pid |
| 546 | FROM profiles |
| 547 | WHERE pid = {?}', $login); |
| 548 | } else { |
| 549 | return XDB::fetchOneCell('SELECT pid |
| 550 | FROM profiles |
| 551 | WHERE hrpid = {?}', $login); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
| 556 | { |
| 557 | if ($respect_order) { |
| 558 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); |
| 559 | } else { |
| 560 | $order = ''; |
| 561 | } |
| 562 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid |
| 563 | FROM account_profiles AS ap |
| 564 | WHERE FIND_IN_SET(\'owner\', ap.perms) |
| 565 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
| 566 | ' . $order); |
| 567 | } |
| 568 | |
| 569 | /** Return the profile associated with the given login. |
| 570 | */ |
| 571 | public static function get($login, $fields = 0x0000, $visibility = null) |
| 572 | { |
| 573 | if (is_array($login)) { |
| 574 | return new Profile($login); |
| 575 | } |
| 576 | $pid = self::getPID($login); |
| 577 | if (!is_null($pid)) { |
| 578 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
| 579 | return $it->next(); |
| 580 | } else { |
| 581 | /* Let say we can identify a profile using the identifiers of its owner. |
| 582 | */ |
| 583 | if (!($login instanceof PlUser)) { |
| 584 | $user = User::getSilent($login); |
| 585 | if ($user && $user->hasProfile()) { |
| 586 | return $user->profile(); |
| 587 | } |
| 588 | } |
| 589 | return null; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
| 594 | { |
| 595 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
| 596 | } |
| 597 | |
| 598 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
| 599 | { |
| 600 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
| 601 | } |
| 602 | |
| 603 | /** Return profiles for the list of pids. |
| 604 | */ |
| 605 | public static function getBulkProfilesWithPIDs(array $pids, $fields = self::FETCH_ADDRESSES, $visibility = null) |
| 606 | { |
| 607 | if (count($pids) == 0) { |
| 608 | return array(); |
| 609 | } |
| 610 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
| 611 | $profiles = array(); |
| 612 | while ($p = $it->next()) { |
| 613 | $profiles[$p->id()] = $p; |
| 614 | } |
| 615 | return $profiles; |
| 616 | } |
| 617 | |
| 618 | /** Return profiles for uids. |
| 619 | */ |
| 620 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
| 621 | { |
| 622 | if (count($uids) == 0) { |
| 623 | return array(); |
| 624 | } |
| 625 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
| 626 | } |
| 627 | |
| 628 | public static function isDisplayName($name) |
| 629 | { |
| 630 | return $name == self::DN_FULL || $name == self::DN_DISPLAY |
| 631 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY |
| 632 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC |
| 633 | || $name == self::DN_SHORT || $name == self::DN_SORT; |
| 634 | } |
| 635 | |
| 636 | public static function getNameTypeId($type, $for_sql = false) |
| 637 | { |
| 638 | if (!S::has('name_types')) { |
| 639 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
| 640 | FROM profile_name_enum'); |
| 641 | S::set('name_types', $table); |
| 642 | } else { |
| 643 | $table = S::v('name_types'); |
| 644 | } |
| 645 | if ($for_sql) { |
| 646 | return XDB::escape($table[$type]); |
| 647 | } else { |
| 648 | return $table[$type]; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | public static function rebuildSearchTokens($pid) |
| 653 | { |
| 654 | XDB::execute('DELETE FROM search_name |
| 655 | WHERE pid = {?}', |
| 656 | $pid); |
| 657 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, |
| 658 | FIND_IN_SET('public', e.flags) AS public |
| 659 | FROM profile_name AS n |
| 660 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) |
| 661 | WHERE n.pid = {?}", |
| 662 | $pid); |
| 663 | |
| 664 | foreach ($keys as $i => $key) { |
| 665 | if ($key['name'] == '') { |
| 666 | continue; |
| 667 | } |
| 668 | $toks = preg_split('/[ \'\-]+/', $key['name']); |
| 669 | $token = ''; |
| 670 | $first = 5; |
| 671 | while ($toks) { |
| 672 | $token = strtolower(replace_accent(array_pop($toks) . $token)); |
| 673 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); |
| 674 | XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags) |
| 675 | VALUES ({?}, {?}, {?}, {?}, {?})', |
| 676 | $token, $pid, soundex_fr($token), $score, $key['public']); |
| 677 | $first = 0; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /** The school identifier consists of 6 digits. The first 3 represent the |
| 683 | * promotion entry year. The last 3 indicate the student's rank. |
| 684 | * |
| 685 | * Our identifier consists of 8 digits and both half have the same role. |
| 686 | * This enables us to deal with bigger promotions and with a wider range |
| 687 | * of promotions. |
| 688 | * |
| 689 | * getSchoolId returns a school identifier given one of ours. |
| 690 | * getXorgId returns a X.org identifier given a school identifier. |
| 691 | */ |
| 692 | public static function getSchoolId($xorgId) |
| 693 | { |
| 694 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { |
| 695 | return null; |
| 696 | } |
| 697 | |
| 698 | $year = intval(substr($xorgId, 0, 4)); |
| 699 | $rank = intval(substr($xorgId, 5, 3)); |
| 700 | if ($year < 1996) { |
| 701 | return null; |
| 702 | } elseif ($year < 2000) { |
| 703 | $year = intval(substr(1900 - $year, 1, 3)); |
| 704 | return sprintf('%02u0%03u', $year, $rank); |
| 705 | } else { |
| 706 | $year = intval(substr(1900 - $year, 1, 3)); |
| 707 | return sprintf('%03u%03u', $year, $rank); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | public static function getXorgId($schoolId) |
| 712 | { |
| 713 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
| 714 | return null; |
| 715 | } |
| 716 | |
| 717 | $year = intval(substr($schoolId, 0, 3)); |
| 718 | $rank = intval(substr($schoolId, 3, 3)); |
| 719 | |
| 720 | if ($year > 200) { |
| 721 | $year /= 10; |
| 722 | } |
| 723 | if ($year < 96) { |
| 724 | return null; |
| 725 | } else { |
| 726 | return sprintf('%04u%04u', 1900 + $year, $rank); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | class ProfileDataIterator |
| 732 | { |
| 733 | private $iterator = null; |
| 734 | private $fields; |
| 735 | |
| 736 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null) |
| 737 | { |
| 738 | require_once 'profilefields.inc.php'; |
| 739 | $visibility = Profile::getVisibilityContext($visibility); |
| 740 | $this->fields = $fields; |
| 741 | |
| 742 | $subits = array(); |
| 743 | $callbacks = array(); |
| 744 | |
| 745 | $subits[0] = $it; |
| 746 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); |
| 747 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); |
| 748 | |
| 749 | if ($fields & Profile::FETCH_ADDRESSES) { |
| 750 | $callbacks[Profile::FETCH_ADDRESSES] = $cb; |
| 751 | $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility); |
| 752 | } |
| 753 | |
| 754 | if ($fields & Profile::FETCH_CORPS) { |
| 755 | $callbacks[Profile::FETCH_CORPS] = $cb; |
| 756 | $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility); |
| 757 | } |
| 758 | |
| 759 | if ($fields & Profile::FETCH_EDU) { |
| 760 | $callbacks[Profile::FETCH_EDU] = $cb; |
| 761 | $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility); |
| 762 | } |
| 763 | |
| 764 | if ($fields & Profile::FETCH_JOBS) { |
| 765 | $callbacks[Profile::FETCH_JOBS] = $cb; |
| 766 | $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility); |
| 767 | } |
| 768 | |
| 769 | if ($fields & Profile::FETCH_MEDALS) { |
| 770 | $callbacks[Profile::FETCH_MEDALS] = $cb; |
| 771 | $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility); |
| 772 | } |
| 773 | |
| 774 | if ($fields & Profile::FETCH_NETWORKING) { |
| 775 | $callbacks[Profile::FETCH_NETWORKING] = $cb; |
| 776 | $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility); |
| 777 | } |
| 778 | |
| 779 | if ($fields & Profile::FETCH_PHONES) { |
| 780 | $callbacks[Profile::FETCH_PHONES] = $cb; |
| 781 | $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility); |
| 782 | } |
| 783 | |
| 784 | if ($fields & Profile::FETCH_PHOTO) { |
| 785 | $callbacks[Profile::FETCH_PHOTO] = $cb; |
| 786 | $subits[Profile::FETCH_PHOTO] = new ProfileFieldIterator('ProfilePhoto', $pids, $visibility); |
| 787 | } |
| 788 | |
| 789 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); |
| 790 | } |
| 791 | |
| 792 | private function consolidateFields(array $pf) |
| 793 | { |
| 794 | if ($this->fields & Profile::FETCH_PHONES) { |
| 795 | $phones = $pf[Profile::FETCH_PHONES]; |
| 796 | |
| 797 | if ($this->fields & Profile::FETCH_ADDRESSES && $pf[Profile::FETCH_ADDRESSES] != null) { |
| 798 | $pf[Profile::FETCH_ADDRESSES]->addPhones($phones); |
| 799 | } |
| 800 | if ($this->fields & Profile::FETCH_JOBS && $pf[Profile::FETCH_JOBS] != null) { |
| 801 | $pf[Profile::FETCH_JOBS]->addPhones($phones); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | if ($this->fields & Profile::FETCH_ADDRESSES) { |
| 806 | $addrs = $pf[Profile::FETCH_ADDRESSES]; |
| 807 | if ($this->fields & Profile::FETCH_JOBS && $pf[Profile::FETCH_JOBS] != null) { |
| 808 | $pf[Profile::FETCH_JOBS]->addAddresses($addrs); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | return $pf; |
| 813 | } |
| 814 | |
| 815 | private function fillProfile(array $vals) |
| 816 | { |
| 817 | $vals = $this->consolidateFields($vals); |
| 818 | |
| 819 | $pf = Profile::get($vals[0]); |
| 820 | if ($this->fields & Profile::FETCH_ADDRESSES) { |
| 821 | if ($vals[Profile::FETCH_ADDRESSES] != null) { |
| 822 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); |
| 823 | } |
| 824 | } |
| 825 | if ($this->fields & Profile::FETCH_CORPS) { |
| 826 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
| 827 | } |
| 828 | if ($this->fields & Profile::FETCH_EDU) { |
| 829 | $pf->setEdu($vals[Profile::FETCH_EDU]); |
| 830 | } |
| 831 | if ($this->fields & Profile::FETCH_JOBS) { |
| 832 | $pf->setJobs($vals[Profile::FETCH_JOBS]); |
| 833 | } |
| 834 | if ($this->fields & Profile::FETCH_MEDALS) { |
| 835 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
| 836 | } |
| 837 | if ($this->fields & Profile::FETCH_NETWORKING) { |
| 838 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
| 839 | } |
| 840 | if ($this->fields & Profile::FETCH_PHONES) { |
| 841 | $pf->setPhones($vals[Profile::FETCH_PHONES]); |
| 842 | } |
| 843 | if ($this->fields & Profile::FETCH_PHOTO) { |
| 844 | if ($vals[Profile::FETCH_PHOTO] != null) { |
| 845 | $pf->setPhoto($vals[Profile::FETCH_PHOTO]); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return $pf; |
| 850 | } |
| 851 | |
| 852 | public function next() |
| 853 | { |
| 854 | $vals = $this->iterator->next(); |
| 855 | if ($vals == null) { |
| 856 | return null; |
| 857 | } |
| 858 | return $this->fillProfile($vals); |
| 859 | } |
| 860 | |
| 861 | public function first() |
| 862 | { |
| 863 | return $this->iterator->first(); |
| 864 | } |
| 865 | |
| 866 | public function last() |
| 867 | { |
| 868 | return $this->iterator->last(); |
| 869 | } |
| 870 | |
| 871 | public function total() |
| 872 | { |
| 873 | return $this->iterator->total(); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /** Iterator over a set of Profiles |
| 878 | * @param an XDB::Iterator obtained from a Profile::fetchProfileData |
| 879 | */ |
| 880 | class ProfileIterator implements PlIterator |
| 881 | { |
| 882 | private $pdi; |
| 883 | private $dbiter; |
| 884 | |
| 885 | public function __construct(ProfileDataIterator &$pdi) |
| 886 | { |
| 887 | $this->pdi = $pdi; |
| 888 | $this->dbiter = $pdi->iterator(); |
| 889 | } |
| 890 | |
| 891 | public function next() |
| 892 | { |
| 893 | $data = $this->dbiter->next(); |
| 894 | if ($data == null) { |
| 895 | return null; |
| 896 | } else { |
| 897 | return $this->pdi->fillProfile(Profile::get($data)); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | public function total() |
| 902 | { |
| 903 | return $this->dbiter->total(); |
| 904 | } |
| 905 | |
| 906 | public function first() |
| 907 | { |
| 908 | return $this->dbiter->first(); |
| 909 | } |
| 910 | |
| 911 | public function last() |
| 912 | { |
| 913 | return $this->dbiter->last(); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 918 | ?> |