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