| 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 ProfileField |
| 23 | /** To store a "field" from the profile |
| 24 | * Provides functions for loading a batch of such data |
| 25 | */ |
| 26 | abstract class ProfileField |
| 27 | { |
| 28 | public static $fields = array( |
| 29 | Profile::FETCH_ADDRESSES => 'ProfileAddresses', |
| 30 | Profile::FETCH_CORPS => 'ProfileCorps', |
| 31 | Profile::FETCH_EDU => 'ProfileEducation', |
| 32 | Profile::FETCH_JOBS => 'ProfileJobs', |
| 33 | Profile::FETCH_MEDALS => 'ProfileMedals', |
| 34 | Profile::FETCH_NETWORKING => 'ProfileNetworking', |
| 35 | Profile::FETCH_PHONES => 'ProfilePhones', |
| 36 | Profile::FETCH_MENTOR_SECTOR => 'ProfileMentoringSectors', |
| 37 | Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries', |
| 38 | ); |
| 39 | |
| 40 | /** The profile to which this field belongs |
| 41 | */ |
| 42 | public $pid; |
| 43 | |
| 44 | /** Fetches data from the database for the given pids, compatible with |
| 45 | * the visibility context. |
| 46 | * @param $pids An array of pids |
| 47 | * @param $visibility The level of visibility fetched fields must have |
| 48 | * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)" |
| 49 | * XXX MUST be reimplemented for each kind of ProfileField |
| 50 | */ |
| 51 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 52 | { |
| 53 | return PlIteratorUtils::emptyIterator(); |
| 54 | } |
| 55 | |
| 56 | public static function buildForPID($cls, $pid, ProfileVisibility $visibility) |
| 57 | { |
| 58 | $res = self::buildFromPIDs($cls, array($pid), $visibility); |
| 59 | return array_pop($res); |
| 60 | } |
| 61 | |
| 62 | /** Build a list of ProfileFields from a set of pids |
| 63 | * @param $cls The name of the field to create ('ProfileMedals', ...) |
| 64 | * @param $pids An array of pids |
| 65 | * @param $visibility An array of allowed visibility contexts |
| 66 | * @return An array of $pid => ProfileField |
| 67 | */ |
| 68 | public static function buildFromPIDs($cls, array $pids, ProfileVisibility $visibility) |
| 69 | { |
| 70 | $it = new ProfileFieldIterator($cls, $pids, $visibility); |
| 71 | $res = array(); |
| 72 | while ($pf = $it->next()) { |
| 73 | $res[$pf->pid] = $pf; |
| 74 | } |
| 75 | return $res; |
| 76 | } |
| 77 | |
| 78 | public static function getForPID($cls, $pid, ProfileVisibility $visibility) |
| 79 | { |
| 80 | $it = new ProfileFieldIterator($cls, array($pid), $visibility); |
| 81 | return $it->next(); |
| 82 | } |
| 83 | } |
| 84 | // }}} |
| 85 | |
| 86 | // {{{ class ProfileFieldIterator |
| 87 | class ProfileFieldIterator implements PlIterator |
| 88 | { |
| 89 | private $data; |
| 90 | private $cls; |
| 91 | |
| 92 | public function __construct($cls, array $pids, ProfileVisibility $visibility) |
| 93 | { |
| 94 | $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility); |
| 95 | $this->cls = $cls; |
| 96 | } |
| 97 | |
| 98 | public function next() |
| 99 | { |
| 100 | $d = $this->data->next(); |
| 101 | if ($d == null) { |
| 102 | return null; |
| 103 | } else { |
| 104 | $cls = $this->cls; |
| 105 | return new $cls($d); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public function total() |
| 110 | { |
| 111 | return $this->data->total(); |
| 112 | } |
| 113 | |
| 114 | public function first() |
| 115 | { |
| 116 | return $this->data->first(); |
| 117 | } |
| 118 | |
| 119 | public function last() |
| 120 | { |
| 121 | return $this->data->last(); |
| 122 | } |
| 123 | } |
| 124 | // }}} |
| 125 | |
| 126 | // {{{ class Phone |
| 127 | class Phone |
| 128 | { |
| 129 | const TYPE_FAX = 'fax'; |
| 130 | const TYPE_FIXED = 'fixed'; |
| 131 | const TYPE_MOBILE = 'mobile'; |
| 132 | public $type; |
| 133 | |
| 134 | public $search; |
| 135 | public $display; |
| 136 | public $comment = ''; |
| 137 | |
| 138 | const LINK_JOB = 'job'; |
| 139 | const LINK_ADDRESS = 'address'; |
| 140 | const LINK_PROFILE = 'user'; |
| 141 | const LINK_COMPANY = 'hq'; |
| 142 | public $link_type; |
| 143 | public $link_id; |
| 144 | |
| 145 | /** Fields are : |
| 146 | * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id |
| 147 | */ |
| 148 | public function __construct($data) |
| 149 | { |
| 150 | foreach ($data as $key => $val) { |
| 151 | $this->$key = $val; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | // }}} |
| 156 | // {{{ class Company |
| 157 | class Company |
| 158 | { |
| 159 | public $id; |
| 160 | public $name; |
| 161 | public $acronym; |
| 162 | public $url; |
| 163 | public $phone = null; |
| 164 | public $address = null; |
| 165 | |
| 166 | /** Fields are: |
| 167 | * $id, $name, $acronym, $url |
| 168 | */ |
| 169 | public function __construct($data) |
| 170 | { |
| 171 | foreach ($data as $key => $val) { |
| 172 | $this->$key = $val; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function setPhone(Phone &$phone) |
| 177 | { |
| 178 | if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) { |
| 179 | $this->phone = $phone; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public function setAddress(Address &$address) |
| 184 | { |
| 185 | if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) { |
| 186 | $this->address = $address; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | // }}} |
| 192 | // {{{ class Job |
| 193 | class Job |
| 194 | { |
| 195 | public $pid; |
| 196 | public $id; |
| 197 | |
| 198 | public $company = null; |
| 199 | private $phones = array(); |
| 200 | private $address = null; |
| 201 | |
| 202 | public $jobid; |
| 203 | |
| 204 | public $description; |
| 205 | public $user_site; |
| 206 | public $user_email; |
| 207 | |
| 208 | public $sector; |
| 209 | public $subsector; |
| 210 | public $subsubsector; |
| 211 | |
| 212 | /** Fields are: |
| 213 | * pid, id, company_id, description, url, email |
| 214 | */ |
| 215 | public function __construct($data) |
| 216 | { |
| 217 | foreach ($data as $key => $val) { |
| 218 | $this->$key = $val; |
| 219 | } |
| 220 | $this->company = CompanyList::get($this->jobid); |
| 221 | } |
| 222 | |
| 223 | public function phones() |
| 224 | { |
| 225 | return $this->phones; |
| 226 | } |
| 227 | |
| 228 | public function address() |
| 229 | { |
| 230 | return $this->address; |
| 231 | } |
| 232 | |
| 233 | public function addPhone(Phone &$phone) |
| 234 | { |
| 235 | if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) { |
| 236 | $this->phones[] = $phone; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | public function setAddress(Address $address) |
| 241 | { |
| 242 | if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) { |
| 243 | $this->address = $address; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | // }}} |
| 248 | // {{{ class Address |
| 249 | class Address |
| 250 | { |
| 251 | const LINK_JOB = 'job'; |
| 252 | const LINK_COMPANY = 'hq'; |
| 253 | const LINK_PROFILE = 'home'; |
| 254 | |
| 255 | public $flags; |
| 256 | public $link_id; |
| 257 | public $link_type; |
| 258 | |
| 259 | public $text; |
| 260 | public $postalCode; |
| 261 | public $latitude; |
| 262 | public $longitude; |
| 263 | |
| 264 | public $locality; |
| 265 | public $subAdministrativeArea; |
| 266 | public $administrativeArea; |
| 267 | public $country; |
| 268 | |
| 269 | public $comment; |
| 270 | |
| 271 | private $phones = array(); |
| 272 | |
| 273 | /** Fields are: |
| 274 | * pîd, id, link_id, link_type, flags, text, postcode, country |
| 275 | */ |
| 276 | public function __construct($data) |
| 277 | { |
| 278 | foreach ($data as $key => $val) { |
| 279 | $this->$key = $val; |
| 280 | } |
| 281 | $this->flags = new PlFlagSet($this->flags); |
| 282 | } |
| 283 | |
| 284 | public function addPhone(Phone &$phone) |
| 285 | { |
| 286 | if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) { |
| 287 | $this->phones[] = $phone; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | public function phones() |
| 292 | { |
| 293 | return $this->phones; |
| 294 | } |
| 295 | |
| 296 | public function hasFlag($flag) |
| 297 | { |
| 298 | return $this->flags->hasFlag($flag); |
| 299 | } |
| 300 | } |
| 301 | // }}} |
| 302 | // {{{ class Education |
| 303 | class Education |
| 304 | { |
| 305 | public $id; |
| 306 | public $pid; |
| 307 | |
| 308 | public $entry_year; |
| 309 | public $grad_year; |
| 310 | public $program; |
| 311 | public $flags; |
| 312 | |
| 313 | public $school; |
| 314 | public $school_short; |
| 315 | public $school_url; |
| 316 | public $country; |
| 317 | |
| 318 | public $degree; |
| 319 | public $degree_short; |
| 320 | public $degree_level; |
| 321 | |
| 322 | public $field; |
| 323 | |
| 324 | public function __construct(array $data) |
| 325 | { |
| 326 | foreach ($data as $key => $val) { |
| 327 | $this->$key = $val; |
| 328 | } |
| 329 | $this->flags = new PlFlagSet($this->flags); |
| 330 | } |
| 331 | } |
| 332 | // }}} |
| 333 | |
| 334 | // {{{ class ProfileEducation [ Field ] |
| 335 | class ProfileEducation extends ProfileField |
| 336 | { |
| 337 | private $educations = array(); |
| 338 | |
| 339 | public function __construct(PlInnerSubIterator $it) |
| 340 | { |
| 341 | $this->pid = $it->value(); |
| 342 | while ($edu = $it->next()) { |
| 343 | $this->educations[$edu['id']] = new Education($edu); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public function get($flags, $limit) |
| 348 | { |
| 349 | $educations = array(); |
| 350 | $year = getdate(); |
| 351 | $year = $year['year']; |
| 352 | $nb = 0; |
| 353 | foreach ($this->educations as $id => $edu) { |
| 354 | if ( |
| 355 | (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary')) |
| 356 | || |
| 357 | (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary')) |
| 358 | || |
| 359 | (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year) |
| 360 | || |
| 361 | (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year) |
| 362 | || |
| 363 | ($flags & Profile::EDUCATION_ALL) |
| 364 | ) { |
| 365 | $educations[$id] = $edu; |
| 366 | ++$nb; |
| 367 | } |
| 368 | if ($limit != null && $nb >= $limit) { |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | return $educations; |
| 373 | } |
| 374 | |
| 375 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 376 | { |
| 377 | $data = XDB::iterator('SELECT pe.id, pe.pid, |
| 378 | pe.entry_year, pe.grad_year, pe.program, pe.flags, |
| 379 | pee.name AS school, pee.abbreviation AS school_short, |
| 380 | pee.url AS school_url, gc.countryFR AS country, |
| 381 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, |
| 382 | pefe.field |
| 383 | FROM profile_education AS pe |
| 384 | LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid) |
| 385 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) |
| 386 | LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid) |
| 387 | LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid) |
| 388 | WHERE pe.pid IN {?} |
| 389 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
| 390 | NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id', |
| 391 | $pids); |
| 392 | |
| 393 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 394 | } |
| 395 | } |
| 396 | // }}} |
| 397 | // {{{ class ProfileMedals [ Field ] |
| 398 | class ProfileMedals extends ProfileField |
| 399 | { |
| 400 | public $medals = array(); |
| 401 | |
| 402 | public function __construct(PlInnerSubIterator $it) |
| 403 | { |
| 404 | $this->pid = $it->value(); |
| 405 | while ($medal = $it->next()) { |
| 406 | $this->medals[$medal['mid']] = $medal; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 411 | { |
| 412 | $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img |
| 413 | FROM profile_medals AS pm |
| 414 | LEFT JOIN profiles AS p ON (pm.pid = p.pid) |
| 415 | LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid) |
| 416 | WHERE pm.pid IN {?} AND p.medals_pub IN {?} |
| 417 | ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids), |
| 418 | $pids, $visibility->levels()); |
| 419 | |
| 420 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 421 | } |
| 422 | } |
| 423 | // }}} |
| 424 | // {{{ class ProfileNetworking [ Field ] |
| 425 | class ProfileNetworking extends ProfileField |
| 426 | { |
| 427 | private $networks = array(); |
| 428 | |
| 429 | public function __construct(PlIterator $it) |
| 430 | { |
| 431 | while ($network = $it->next()) { |
| 432 | $this->networks[$network['nwid']] = $network['address']; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 437 | { |
| 438 | $data = XDB::iterator('SELECT pid, nwid, address, network_type |
| 439 | FROM profile_networking |
| 440 | WHERE pid IN {?} AND pub IN {?} |
| 441 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
| 442 | network_type, nwid', |
| 443 | $pids, $visibility->levels()); |
| 444 | |
| 445 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 446 | } |
| 447 | |
| 448 | public function get($flags, $limit = null) |
| 449 | { |
| 450 | $nws = array(); |
| 451 | $nb = 0; |
| 452 | foreach ($this->networks as $id => $nw) { |
| 453 | // XXX hardcoded reference to web site index |
| 454 | if ( |
| 455 | (($flags & Profile::NETWORKING_WEB) && $nw['network_type'] == 0) |
| 456 | || |
| 457 | (! ($flags & Profile::NETWORKING_WEB)) |
| 458 | ) { |
| 459 | $nws[$id] = $nw; |
| 460 | ++$nb; |
| 461 | } |
| 462 | if ($nb >= $limit) { |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | return $nws; |
| 467 | } |
| 468 | } |
| 469 | // }}} |
| 470 | // {{{ class ProfileCorps [ Field ] |
| 471 | class ProfileCorps extends ProfileField |
| 472 | { |
| 473 | public $original; |
| 474 | public $current; |
| 475 | |
| 476 | public $original_name; |
| 477 | public $original_abbrev; |
| 478 | public $original_still_exists; |
| 479 | |
| 480 | public $current_name; |
| 481 | public $current_abbrev; |
| 482 | public $current_still_exists; |
| 483 | public $current_rank; |
| 484 | public $current_rank_abbrev; |
| 485 | |
| 486 | public function __construct(array $data) |
| 487 | { |
| 488 | foreach ($data as $key => $val) { |
| 489 | $this->$key = $val; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 494 | { |
| 495 | $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current, |
| 496 | pceo.name AS original_name, pceo.abbreviation AS original_abbrev, |
| 497 | pceo.still_exists AS original_still_exists, |
| 498 | pcec.name AS current_name, pcec.abbreviation AS current_abbrev, |
| 499 | pcec.still_exists AS current_still_exists, |
| 500 | pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev, |
| 501 | rankid |
| 502 | FROM profile_corps AS pc |
| 503 | LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid) |
| 504 | LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid) |
| 505 | LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid) |
| 506 | WHERE pc.pid IN {?} AND pc.corps_pub IN {?} |
| 507 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
| 508 | $pids, $visibility->levels()); |
| 509 | |
| 510 | return $data; |
| 511 | } |
| 512 | } |
| 513 | // }}} |
| 514 | // {{{ class ProfileMentoringSectors [ Field ] |
| 515 | class ProfileMentoringSectors extends ProfileField |
| 516 | { |
| 517 | public $sectors = array(); |
| 518 | |
| 519 | public function __construct(PlInnerSubIterator $it) |
| 520 | { |
| 521 | $this->pid = $it->value(); |
| 522 | while ($sector = $it->next()) { |
| 523 | $this->sectors[] = $sector; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 528 | { |
| 529 | $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector |
| 530 | FROM profile_mentor_sector AS pms |
| 531 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid) |
| 532 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid) |
| 533 | WHERE pms.pid IN {?} |
| 534 | ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids), |
| 535 | $pids); |
| 536 | |
| 537 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 538 | } |
| 539 | } |
| 540 | // }}} |
| 541 | // {{{ class ProfileMentoringCountries [ Field ] |
| 542 | class ProfileMentoringCountries extends ProfileField |
| 543 | { |
| 544 | public $countries = array(); |
| 545 | |
| 546 | public function __construct(PlInnerSubIterator $it) |
| 547 | { |
| 548 | $this->pid = $it->value(); |
| 549 | while ($country = $it->next()) { |
| 550 | $this->countries[$country['id']] = $country['name']; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 555 | { |
| 556 | $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name |
| 557 | FROM profile_mentor_country AS pmc |
| 558 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country) |
| 559 | WHERE pmc.pid IN {?} |
| 560 | ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids), |
| 561 | $pids); |
| 562 | |
| 563 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 564 | } |
| 565 | } |
| 566 | // }}} |
| 567 | |
| 568 | /** Loading of data for a Profile : |
| 569 | * 1) load jobs, addresses, phones |
| 570 | * 2) attach phones to addresses, jobs and profiles |
| 571 | * 3) attach addresses to jobs and profiles |
| 572 | */ |
| 573 | |
| 574 | // {{{ class ProfileAddresses [ Field ] |
| 575 | class ProfileAddresses extends ProfileField |
| 576 | { |
| 577 | private $addresses = array(); |
| 578 | |
| 579 | public function __construct(PlIterator $it) |
| 580 | { |
| 581 | if ($it instanceof PlInnerSubIterator) { |
| 582 | $this->pid = $it->value(); |
| 583 | } |
| 584 | |
| 585 | while ($addr = $it->next()) { |
| 586 | $this->addresses[] = new Address($addr); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | public function get($flags, $limit = null) |
| 591 | { |
| 592 | $res = array(); |
| 593 | $nb = 0; |
| 594 | foreach ($this->addresses as $addr) { |
| 595 | if ( |
| 596 | ($flags & Profile::ADDRESS_ALL) |
| 597 | || |
| 598 | (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current')) |
| 599 | || |
| 600 | (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail')) |
| 601 | || |
| 602 | (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == 'home') |
| 603 | || |
| 604 | (($flags & Profile::ADDRESS_PRO) && $addr->link_type == 'job') |
| 605 | ) { |
| 606 | $res[] = $addr; |
| 607 | $nb++; |
| 608 | } |
| 609 | if ($limit != null && $nb == $limit) { |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | return $res; |
| 614 | } |
| 615 | |
| 616 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 617 | { |
| 618 | $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type, |
| 619 | IF(pa.type = \'home\', pid, jobid) AS link_id, |
| 620 | pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment, |
| 621 | gl.name AS locality, gas.name AS subAdministrativeArea, |
| 622 | ga.name AS administrativeArea, gc.countryFR AS country |
| 623 | FROM profile_addresses AS pa |
| 624 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) |
| 625 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) |
| 626 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) |
| 627 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) |
| 628 | WHERE pa.pid in {?} AND pa.pub IN {?} |
| 629 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
| 630 | $pids, $visibility->levels()); |
| 631 | |
| 632 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 633 | } |
| 634 | |
| 635 | public function addPhones(ProfilePhones $phones) |
| 636 | { |
| 637 | $p = $phones->get(0); |
| 638 | foreach ($p as $phone) { |
| 639 | if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) { |
| 640 | $this->addresses[$phone->link_id]->addPhone($phone); |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | // }}} |
| 646 | // {{{ class ProfilePhones [ Field ] |
| 647 | class ProfilePhones extends ProfileField |
| 648 | { |
| 649 | private $phones = array(); |
| 650 | |
| 651 | public function __construct(PlInnerSubIterator $it) |
| 652 | { |
| 653 | $this->pid = $it->value(); |
| 654 | while ($phone = $it->next()) { |
| 655 | $this->phones[] = new Phone($phone); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | public function get($flags, $limit = null) |
| 660 | { |
| 661 | $phones = array(); |
| 662 | $nb = 0; |
| 663 | foreach ($this->phones as $id => $phone) { |
| 664 | $phones[$id] = $phone; |
| 665 | ++$nb; |
| 666 | if ($limit != null && $nb == $limit) { |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | return $phones; |
| 671 | } |
| 672 | |
| 673 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 674 | { |
| 675 | $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid |
| 676 | FROM profile_phones |
| 677 | WHERE pid IN {?} AND pub IN {?} |
| 678 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
| 679 | $pids, $visibility->levels()); |
| 680 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 681 | } |
| 682 | } |
| 683 | // }}} |
| 684 | // {{{ class ProfileJobs [ Field ] |
| 685 | class ProfileJobs extends ProfileField |
| 686 | { |
| 687 | private $jobs = array(); |
| 688 | |
| 689 | public function __construct(PlInnerSubIterator $jobs) |
| 690 | { |
| 691 | $this->pid = $jobs->value(); |
| 692 | while ($job = $jobs->next()) { |
| 693 | $this->jobs[$job['id']] = new Job($job); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
| 698 | { |
| 699 | CompanyList::preload($pids); |
| 700 | $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site, |
| 701 | IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email, |
| 702 | pj.jobid, pjse.name AS sector, pjsse.name AS subsector, |
| 703 | pjssse.name AS subsubsector |
| 704 | FROM profile_job AS pj |
| 705 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) |
| 706 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) |
| 707 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) |
| 708 | WHERE pj.pid IN {?} AND pj.pub IN {?} |
| 709 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
| 710 | pj.id', |
| 711 | $visibility->levels(), $pids, $visibility->levels()); |
| 712 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
| 713 | } |
| 714 | |
| 715 | public function get($flags, $limit = null) |
| 716 | { |
| 717 | $jobs = array(); |
| 718 | $nb = 0; |
| 719 | foreach ($this->jobs as $id => $job) { |
| 720 | $jobs[$id] = $job; |
| 721 | ++$nb; |
| 722 | if ($limit != null && $nb >= $limit) { |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | return $jobs; |
| 727 | } |
| 728 | |
| 729 | public function addPhones(ProfilePhones $phones) |
| 730 | { |
| 731 | $p = $phones->get(0); |
| 732 | foreach ($p as $phone) { |
| 733 | if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) { |
| 734 | $this->jobs[$phone->link_id]->addPhones($phone); |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | public static function addAddresses(ProfileAddresses $addresses) |
| 740 | { |
| 741 | $a = $addresses->get(Profile::ADDRESS_PRO); |
| 742 | foreach ($a as $address) { |
| 743 | if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) { |
| 744 | $this->jobs[$address->link_id]->setAddress($address); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | public static function addCompanies(array $companies) |
| 750 | { |
| 751 | foreach ($this->jobs as $job) |
| 752 | { |
| 753 | $job->company = $companies[$job->jobid]; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | // }}} |
| 758 | |
| 759 | // {{{ class CompanyList |
| 760 | class CompanyList |
| 761 | { |
| 762 | static private $fullload = false; |
| 763 | static private $companies = array(); |
| 764 | |
| 765 | static public function preload($pids = array()) |
| 766 | { |
| 767 | if (self::$fullload) { |
| 768 | return; |
| 769 | } |
| 770 | // Load raw data |
| 771 | if (count($pids)) { |
| 772 | $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)'; |
| 773 | $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids); |
| 774 | } else { |
| 775 | $join = ''; |
| 776 | $where = ''; |
| 777 | } |
| 778 | |
| 779 | $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url, |
| 780 | pa.flags, pa.text, pa.postalCode, pa.countryId, |
| 781 | pa.type, pa.pub |
| 782 | FROM profile_job_enum AS pje |
| 783 | LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\') |
| 784 | ' . $join . ' |
| 785 | ' . $where); |
| 786 | while ($row = $it->next()) { |
| 787 | $cp = new Company($row); |
| 788 | $addr = new Address($row); |
| 789 | $cp->setAddress($addr); |
| 790 | self::$companies[$row['id']] = $cp; |
| 791 | } |
| 792 | |
| 793 | // TODO: add phones to addresses |
| 794 | if (count($pids) == 0) { |
| 795 | self::$fullload = true; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | static public function get($id) |
| 800 | { |
| 801 | if (!array_key_exists($id, self::$companies)) { |
| 802 | self::preload(); |
| 803 | } |
| 804 | return self::$companies[$id]; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 809 | ?> |