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