Merge remote branch 'origin/platal-1.0.0'
[platal.git] / include / profilefields.inc.php
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 Phone
128 class Phone
129 {
130 const TYPE_FAX = 'fax';
131 const TYPE_FIXED = 'fixed';
132 const TYPE_MOBILE = 'mobile';
133 public $type;
134
135 public $search;
136 public $display;
137 public $comment = '';
138
139 const LINK_JOB = 'pro';
140 const LINK_ADDRESS = 'address';
141 const LINK_PROFILE = 'user';
142 const LINK_COMPANY = 'hq';
143 public $link_type;
144 public $link_id;
145
146 public $id;
147
148 /** Fields are :
149 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
150 */
151 public function __construct($data)
152 {
153 foreach ($data as $key => $val) {
154 $this->$key = $val;
155 }
156 }
157
158 /** Returns the unique ID of a phone
159 * This ID will allow to link it to an address, a user or a job
160 * The format is address_addressId_phoneId (where phoneId is the id
161 * of the phone in the list of those associated with the address)
162 */
163 public function uid() {
164 return $this->link_type . '_' . $this->link_id . '_' . $this->id;
165 }
166
167 public function hasFlags($flags) {
168 return $this->hasType($flags) && $this->hasLink($flags);
169 }
170
171 /** Returns true if this phone's type matches the flags
172 */
173 public function hasType($flags) {
174 $flags = $flags & Profile::PHONE_TYPE_ANY;
175 return (
176 ($flags == Profile::PHONE_TYPE_ANY)
177 ||
178 (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
179 ||
180 (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
181 ||
182 (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
183 );
184 }
185
186 /** User accessible version of the type
187 */
188 public function displayType($short = false)
189 {
190 switch ($this->type) {
191 case Phone::TYPE_FIXED:
192 return $short ? 'Tél' : 'Fixe';
193 case Phone::TYPE_FAX:
194 return 'Fax';
195 case Phone::TYPE_MOBILE:
196 return $short ? 'Mob' : 'Mobile';
197 default:
198 return $this->type;
199 }
200 }
201
202 /** Returns true if this phone's link matches the flags
203 */
204 public function hasLink($flags) {
205 $flags = $flags & Profile::PHONE_LINK_ANY;
206 return (
207 ($flags == Profile::PHONE_LINK_ANY)
208 ||
209 (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
210 ||
211 (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
212 ||
213 (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
214 ||
215 (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
216 );
217 }
218 }
219 // }}}
220 // {{{ class Company
221 class Company
222 {
223 public $id;
224 public $name;
225 public $acronym;
226 public $url;
227 public $phone = null;
228 public $address = null;
229
230 /** Fields are:
231 * $id, $name, $acronym, $url
232 */
233 public function __construct($data)
234 {
235 foreach ($data as $key => $val) {
236 $this->$key = $val;
237 }
238 }
239
240 public function setPhone(Phone &$phone)
241 {
242 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
243 $this->phone = $phone;
244 }
245 }
246
247 public function setAddress(Address &$address)
248 {
249 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
250 $this->address = $address;
251 }
252 }
253
254 }
255 // }}}
256 // {{{ class Job
257 class Job
258 {
259 public $pid;
260 public $id;
261
262 public $company = null;
263 public $phones = array();
264 public $address = null;
265
266 public $jobid;
267
268 public $description;
269 public $user_site;
270 public $user_email;
271
272 public $sector;
273 public $subsector;
274 public $subsubsector;
275
276 /** Fields are:
277 * pid, id, company_id, description, url, email
278 */
279 public function __construct($data)
280 {
281 foreach ($data as $key => $val) {
282 $this->$key = $val;
283 }
284 $this->company = CompanyList::get($this->jobid);
285 if (is_null($this->company)) {
286 require_once 'validations.inc.php';
287 $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
288 $this->company = new Company(array('name' => $entreprise[$this->id]->name));
289 }
290 }
291
292 public function phones()
293 {
294 return $this->phones;
295 }
296
297 public function address()
298 {
299 return $this->address;
300 }
301
302 public function addPhone(Phone &$phone)
303 {
304 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
305 $this->phones[$phone->uid()] = $phone;
306 }
307 }
308
309 public function setAddress(Address $address)
310 {
311 if ($address->link_type == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
312 $this->address = $address;
313 }
314 }
315 }
316 // }}}
317 // {{{ class Address
318 class Address
319 {
320 const LINK_JOB = 'job';
321 const LINK_COMPANY = 'hq';
322 const LINK_PROFILE = 'home';
323
324 public $flags;
325 public $id; // The ID of the address among those associated with its link
326 public $link_id; // The ID of the object to which the address is linked (profile, job, company)
327 public $link_type;
328
329 public $text;
330 public $postalCode;
331 public $latitude;
332 public $longitude;
333
334 public $locality;
335 public $subAdministrativeArea;
336 public $administrativeArea;
337 public $country;
338
339 public $comment;
340
341 private $phones = array();
342
343 /** Fields are:
344 * pîd, id, link_id, link_type, flags, text, postcode, country
345 */
346 public function __construct($data)
347 {
348 foreach ($data as $key => $val) {
349 $this->$key = $val;
350 }
351 $this->flags = new PlFlagSet($this->flags);
352 }
353
354 public function uid() {
355 $uid = $this->link_type . '_';
356 if ($this->link_type != self::LINK_COMPANY) {
357 $uid .= $this->pid . '_';
358 }
359 $uid .= $this->link_id . '_' . $this->id;
360 }
361
362 public function addPhone(Phone &$phone)
363 {
364 if (
365 $phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id &&
366 ($this->link_type == self::LINK_COMPANY || $phone->pid == $this->pid) ) {
367 $this->phones[$phone->uid()] = $phone;
368 }
369 }
370
371 public function phones()
372 {
373 return $this->phones;
374 }
375
376 public function hasFlag($flag)
377 {
378 if (!$this->flags instanceof PlFlagSet) {
379 $this->flags = new PlFlagSet($this->flags);
380 }
381 return $this->flags->hasFlag($flag);
382 }
383 }
384 // }}}
385 // {{{ class Education
386 class Education
387 {
388 public $id;
389 public $pid;
390
391 public $entry_year;
392 public $grad_year;
393 public $program;
394 public $flags;
395
396 public $school;
397 public $school_short;
398 public $school_url;
399 public $country;
400
401 public $degree;
402 public $degree_short;
403 public $degree_level;
404
405 public $field;
406
407 public function __construct(array $data)
408 {
409 foreach ($data as $key => $val) {
410 $this->$key = $val;
411 }
412 $this->flags = new PlFlagSet($this->flags);
413 }
414 }
415 // }}}
416
417 // {{{ class ProfileEducation [ Field ]
418 class ProfileEducation extends ProfileField
419 {
420 private $educations = array();
421
422 public function __construct(PlInnerSubIterator $it)
423 {
424 $this->pid = $it->value();
425 while ($edu = $it->next()) {
426 $this->educations[$edu['id']] = new Education($edu);
427 }
428 }
429
430 public function get($flags, $limit)
431 {
432 $educations = array();
433 $year = getdate();
434 $year = $year['year'];
435 $nb = 0;
436 foreach ($this->educations as $id => $edu) {
437 if (
438 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
439 ||
440 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
441 ||
442 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
443 ||
444 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
445 ||
446 ($flags & Profile::EDUCATION_ALL)
447 ) {
448 $educations[$id] = $edu;
449 ++$nb;
450 }
451 if ($limit != null && $nb >= $limit) {
452 break;
453 }
454 }
455 return $educations;
456 }
457
458 public static function fetchData(array $pids, ProfileVisibility $visibility)
459 {
460 $data = XDB::iterator('SELECT pe.id, pe.pid,
461 pe.entry_year, pe.grad_year, pe.program, pe.flags,
462 pee.name AS school, pee.abbreviation AS school_short,
463 pee.url AS school_url, gc.countryFR AS country,
464 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
465 pefe.field
466 FROM profile_education AS pe
467 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
468 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
469 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
470 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
471 WHERE pe.pid IN {?}
472 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
473 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
474 $pids);
475
476 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
477 }
478 }
479 // }}}
480 // {{{ class ProfileMedals [ Field ]
481 class ProfileMedals extends ProfileField
482 {
483 public $medals = array();
484
485 public function __construct(PlInnerSubIterator $it)
486 {
487 $this->pid = $it->value();
488 while ($medal = $it->next()) {
489 $this->medals[$medal['mid']] = $medal;
490 }
491 }
492
493 public static function fetchData(array $pids, ProfileVisibility $visibility)
494 {
495 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
496 FROM profile_medals AS pm
497 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
498 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
499 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
500 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
501 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
502 $pids, $visibility->levels());
503
504 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
505 }
506 }
507 // }}}
508 // {{{ class ProfileNetworking [ Field ]
509 class ProfileNetworking extends ProfileField
510 {
511 private $networks = array();
512
513 public function __construct(PlInnerSubIterator $it)
514 {
515 $this->pid = $it->value();
516 while ($network = $it->next()) {
517 $network['network_type'] = new PlFlagSet($network['network_type']);
518 $this->networks[$network['id']] = $network;
519 }
520 }
521
522 public static function fetchData(array $pids, ProfileVisibility $visibility)
523 {
524 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
525 FROM profile_networking AS pn
526 LEFT JOIN profile_networking_enum AS pne USING(nwid)
527 WHERE pid IN {?} AND pub IN {?}
528 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
529 pn.nwid, id',
530 $pids, $visibility->levels());
531
532 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
533 }
534
535 public function get($flags, $limit = null)
536 {
537 $nws = array();
538 $nb = 0;
539 foreach ($this->networks as $id => $nw) {
540 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
541 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
542 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
543 ($flags == Profile::NETWORKING_ALL)) {
544 $nws[$id] = $nw;
545 ++$nb;
546 if (isset($limit) && $nb >= $limit) {
547 break;
548 }
549 }
550 }
551 return $nws;
552 }
553 }
554 // }}}
555 // {{{ class ProfileCorps [ Field ]
556 class ProfileCorps extends ProfileField
557 {
558 public $original;
559 public $current;
560
561 public $original_name;
562 public $original_abbrev;
563 public $original_still_exists;
564
565 public $current_name;
566 public $current_abbrev;
567 public $current_still_exists;
568 public $current_rank;
569 public $current_rank_abbrev;
570
571 public function __construct(array $data)
572 {
573 foreach ($data as $key => $val) {
574 $this->$key = $val;
575 }
576 }
577
578 public static function fetchData(array $pids, ProfileVisibility $visibility)
579 {
580 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
581 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
582 pceo.still_exists AS original_still_exists,
583 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
584 pcec.still_exists AS current_still_exists,
585 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
586 rankid
587 FROM profile_corps AS pc
588 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
589 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
590 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
591 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
592 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
593 $pids, $visibility->levels());
594
595 return $data;
596 }
597 }
598 // }}}
599 // {{{ class ProfileMentoringSectors [ Field ]
600 class ProfileMentoringSectors extends ProfileField
601 {
602 public $sectors = array();
603
604 public function __construct(PlInnerSubIterator $it)
605 {
606 $this->pid = $it->value();
607 while ($sector = $it->next()) {
608 $this->sectors[] = $sector;
609 }
610 }
611
612 public static function fetchData(array $pids, ProfileVisibility $visibility)
613 {
614 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
615 FROM profile_mentor_sector AS pms
616 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
617 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
618 WHERE pms.pid IN {?}
619 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
620 $pids);
621
622 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
623 }
624 }
625 // }}}
626 // {{{ class ProfileMentoringCountries [ Field ]
627 class ProfileMentoringCountries extends ProfileField
628 {
629 public $countries = array();
630
631 public function __construct(PlInnerSubIterator $it)
632 {
633 $this->pid = $it->value();
634 while ($country = $it->next()) {
635 $this->countries[$country['id']] = $country['name'];
636 }
637 }
638
639 public static function fetchData(array $pids, ProfileVisibility $visibility)
640 {
641 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
642 FROM profile_mentor_country AS pmc
643 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
644 WHERE pmc.pid IN {?}
645 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
646 $pids);
647
648 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
649 }
650 }
651 // }}}
652
653 /** Loading of data for a Profile :
654 * 1) load jobs, addresses, phones
655 * 2) attach phones to addresses, jobs and profiles
656 * 3) attach addresses to jobs and profiles
657 */
658
659 // {{{ Database schema (profile_address, profile_phones, profile_jobs)
660 /** The database for this is very unclear, so here is a little schema :
661 * profile_job describes a Job, links to:
662 * - a Profile, through `pid`
663 * - a Company, through `jobid`
664 * The `id` field is the id of this job in the list of the jobs of its profile
665 *
666 * profile_addresses describes an Address, which
667 * related to either a Profile, a Job or a Company:
668 * - for a Profile:
669 * - `type` is set to 'home'
670 * - `pid` is set to the related profile pid
671 * - `id` is the id of the address in the list of those related to that profile
672 * - `jobid` is empty
673 *
674 * - for a Company:
675 * - `type` is set to 'hq'
676 * - `pid` is set to 0
677 * - `jobid` is set to the id of the company
678 * - `id` is set to 0 (only one address per Company)
679 *
680 * - for a Job:
681 * - `type` is set to 'job'
682 * - `pid` is set to the pid of the Profile of the related Job
683 * - `jobid` is set to the Company of the job (this information is redundant
684 * with that of the row of profile_job for the related job)
685 * - `id` is the id of the job to which we refer (i.e `profile_job.id`)
686 *
687 * profile_phone describes a Phone, which can be related to an Address,
688 * a Job, a Profile or a Company:
689 * - for a Profile:
690 * - `link_type` is set to 'user'
691 * - `link_id` is set to 0
692 * - `pid` is set to the id of the related Profile
693 *
694 * - for a Company:
695 * - `link_type` is set to 'hq'
696 * - `link_id` is set to the id of the related Company
697 * - `pid` is set to 0
698 *
699 * - for an Address (this is only possible for a *personal* address)
700 * - `link_type` is set to 'address'
701 * - `link_id` is set to the related Address `id`
702 * - `pid` is set to the related Address `pid`
703 *
704 * - for a Job:
705 * - `link_type` is set to 'pro'
706 * - `link_id` is set to the related Job `id` (not `jobid`)
707 * - `pid` is set to the related Job `pid`
708 *
709 *
710 * The possible relations are as follow:
711 * An Address can be linked to a Company, a Profile, a Job
712 * A Job is linked to a Company and a Profile
713 * A Phone can be linked to a Company, a Profile, a Job, or a Profile-related Address
714 */
715 // }}}
716
717 // {{{ class ProfileAddresses [ Field ]
718 class ProfileAddresses extends ProfileField
719 {
720 private $addresses = array();
721
722 public function __construct(PlIterator $it)
723 {
724 if ($it instanceof PlInnerSubIterator) {
725 $this->pid = $it->value();
726 }
727
728 while ($addr = $it->next()) {
729 $this->addresses[] = new Address($addr);
730 }
731 }
732
733 public function get($flags, $limit = null)
734 {
735 $res = array();
736 $nb = 0;
737 foreach ($this->addresses as $addr) {
738 if (
739 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
740 ||
741 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
742 ||
743 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE)
744 ||
745 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
746 ) {
747 $res[] = $addr;
748 $nb++;
749 }
750 if ($limit != null && $nb == $limit) {
751 break;
752 }
753 }
754 return $res;
755 }
756
757 public static function fetchData(array $pids, ProfileVisibility $visibility)
758 {
759 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
760 IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id,
761 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
762 gl.name AS locality, gas.name AS subAdministrativeArea,
763 ga.name AS administrativeArea, gc.countryFR AS country
764 FROM profile_addresses AS pa
765 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
766 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
767 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
768 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
769 WHERE pa.pid in {?} AND pa.pub IN {?}
770 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
771 $pids, $visibility->levels());
772
773 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
774 }
775
776 public function addPhones(ProfilePhones $phones)
777 {
778 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
779 foreach ($p as $phone) {
780 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
781 $this->addresses[$phone->link_id]->addPhone($phone);
782 }
783 }
784 }
785 }
786 // }}}
787 // {{{ class ProfilePhones [ Field ]
788 class ProfilePhones extends ProfileField
789 {
790 private $phones = array();
791
792 public function __construct(PlInnerSubIterator $it)
793 {
794 $this->pid = $it->value();
795 while ($phone = $it->next()) {
796 $this->phones[] = new Phone($phone);
797 }
798 }
799
800 public function get($flags, $limit = null)
801 {
802 $phones = array();
803 $nb = 0;
804 foreach ($this->phones as $id => $phone) {
805 if ($phone->hasFlags($flags)) {
806 $phones[$id] = $phone;
807 ++$nb;
808 if ($limit != null && $nb == $limit) {
809 break;
810 }
811 }
812 }
813 return $phones;
814 }
815
816 public static function fetchData(array $pids, ProfileVisibility $visibility)
817 {
818 $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid, link_id, tel_id AS id
819 FROM profile_phones
820 WHERE pid IN {?} AND pub IN {?}
821 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
822 $pids, $visibility->levels());
823 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
824 }
825 }
826 // }}}
827 // {{{ class ProfileJobs [ Field ]
828 class ProfileJobs extends ProfileField
829 {
830 private $jobs = array();
831
832 public function __construct(PlInnerSubIterator $jobs)
833 {
834 $this->pid = $jobs->value();
835 while ($job = $jobs->next()) {
836 $this->jobs[$job['id']] = new Job($job);
837 }
838 }
839
840 public static function fetchData(array $pids, ProfileVisibility $visibility)
841 {
842 CompanyList::preload($pids);
843 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
844 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
845 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
846 pjssse.name AS subsubsector
847 FROM profile_job AS pj
848 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
849 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
850 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
851 WHERE pj.pid IN {?} AND pj.pub IN {?}
852 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
853 pj.id',
854 $visibility->levels(), $pids, $visibility->levels());
855 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
856 }
857
858 public function get($flags, $limit = null)
859 {
860 $jobs = array();
861 $nb = 0;
862 foreach ($this->jobs as $id => $job) {
863 $jobs[$id] = $job;
864 ++$nb;
865 if ($limit != null && $nb >= $limit) {
866 break;
867 }
868 }
869 return $jobs;
870 }
871
872 public function addPhones(ProfilePhones $phones)
873 {
874 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
875 foreach ($p as $phone) {
876 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
877 $this->jobs[$phone->link_id]->addPhone($phone);
878 }
879 }
880 }
881
882 public function addAddresses(ProfileAddresses $addresses)
883 {
884 $a = $addresses->get(Profile::ADDRESS_PRO);
885 foreach ($a as $address) {
886 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
887 $this->jobs[$address->link_id]->setAddress($address);
888 }
889 }
890 }
891
892 public function addCompanies(array $companies)
893 {
894 foreach ($this->jobs as $job) {
895 $this->company = $companies[$job->jobid];
896 }
897 }
898 }
899 // }}}
900
901 // {{{ class CompanyList
902 class CompanyList
903 {
904 static private $fullload = false;
905 static private $companies = array();
906
907 static public function preload($pids = array())
908 {
909 if (self::$fullload) {
910 return;
911 }
912 // Load raw data
913 if (count($pids)) {
914 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
915 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
916 } else {
917 $join = '';
918 $where = '';
919 }
920
921 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
922 pa.flags, pa.text, pa.postalCode, pa.countryId,
923 pa.type, pa.pub
924 FROM profile_job_enum AS pje
925 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
926 ' . $join . '
927 ' . $where);
928 $newcompanies = array();
929 while ($row = $it->next()) {
930 $cp = new Company($row);
931 $addr = new Address($row);
932 $cp->setAddress($addr);
933 if (!array_key_exists($row['id'], self::$companies)) {
934 $newcompanies[] = $row['id'];
935 }
936 self::$companies[$row['id']] = $cp;
937 }
938
939 // Add phones to hq
940 if (count($newcompanies)) {
941 $it = XDB::iterator('SELECT search_tel AS search, display_tel AS display, comment, link_id, tel_type AS type, link_type, tel_id AS id
942 FROM profile_phones
943 WHERE link_id IN {?} AND link_type = \'hq\'',
944 $newcompanies);
945 while ($row = $it->next()) {
946 $p = new Phone($row);
947 self::$companies[$row['link_id']]->setPhone($p);
948 }
949 }
950
951 if (count($pids) == 0) {
952 self::$fullload = true;
953 }
954 }
955
956 static public function get($id)
957 {
958 if (!array_key_exists($id, self::$companies)) {
959 self::preload();
960 }
961 return self::$companies[$id];
962 }
963 }
964
965 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
966 ?>