2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ class ProfileField
23 /** To store a "field" from the profile
24 * Provides functions for loading a batch of such data
26 abstract class ProfileField
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',
40 /** The profile to which this field belongs
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
51 public static function fetchData(array $pids, ProfileVisibility
$visibility)
53 return PlIteratorUtils
::emptyIterator();
56 public static function buildForPID($cls, $pid, ProfileVisibility
$visibility)
58 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
59 return array_pop($res);
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
68 public static function buildFromPIDs($cls, array $pids, ProfileVisibility
$visibility)
70 $it = new ProfileFieldIterator($cls, $pids, $visibility);
72 while ($pf = $it->next()) {
78 public static function getForPID($cls, $pid, ProfileVisibility
$visibility)
80 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
86 // {{{ class ProfileFieldIterator
87 class ProfileFieldIterator
implements PlIterator
92 public function __construct($cls, array $pids, ProfileVisibility
$visibility)
94 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
98 public function next()
100 $d = $this->data
->next();
109 public function total()
111 return $this->data
->total();
114 public function first()
116 return $this->data
->first();
119 public function last()
121 return $this->data
->last();
129 const TYPE_FAX
= 'fax';
130 const TYPE_FIXED
= 'fixed';
131 const TYPE_MOBILE
= 'mobile';
136 public $comment = '';
138 const LINK_JOB
= 'pro';
139 const LINK_ADDRESS
= 'address';
140 const LINK_PROFILE
= 'user';
141 const LINK_COMPANY
= 'hq';
148 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
150 public function __construct($data)
152 foreach ($data as $key => $val) {
157 public function hasFlags($flags) {
158 return $this->hasType($flags) && $this->hasLink($flags);
161 /** Returns true if this phone's type matches the flags
163 public function hasType($flags) {
164 $flags = $flags & Profile
::PHONE_TYPE_ANY
;
166 ($flags == Profile
::PHONE_TYPE_ANY
)
168 (($flags & Profile
::PHONE_TYPE_FAX
) && $this->type
== self
::TYPE_FAX
)
170 (($flags & Profile
::PHONE_TYPE_FIXED
) && $this->type
== self
::TYPE_FIXED
)
172 (($flags & Profile
::PHONE_TYPE_MOBILE
) && $this->type
== self
::TYPE_MOBILE
)
176 /** User accessible version of the type
178 public function displayType($short = false
)
180 switch ($this->type
) {
181 case Phone
::TYPE_FIXED
:
182 return $short ?
'Tél' : 'Fixe';
183 case Phone
::TYPE_FAX
:
185 case Phone
::TYPE_MOBILE
:
186 return $short ?
'Mob' : 'Mobile';
192 /** Returns true if this phone's link matches the flags
194 public function hasLink($flags) {
195 $flags = $flags & Profile
::PHONE_LINK_ANY
;
197 ($flags == Profile
::PHONE_LINK_ANY
)
199 (($flags & Profile
::PHONE_LINK_COMPANY
) && $this->link_type
== self
::LINK_COMPANY
)
201 (($flags & Profile
::PHONE_LINK_JOB
) && $this->link_type
== self
::LINK_JOB
)
203 (($flags & Profile
::PHONE_LINK_ADDRESS
) && $this->link_type
== self
::LINK_ADDRESS
)
205 (($flags & Profile
::PHONE_LINK_PROFILE
) && $this->link_type
== self
::LINK_PROFILE
)
217 public $phone = null
;
218 public $address = null
;
221 * $id, $name, $acronym, $url
223 public function __construct($data)
225 foreach ($data as $key => $val) {
230 public function setPhone(Phone
&$phone)
232 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
233 $this->phone
= $phone;
237 public function setAddress(Address
&$address)
239 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
240 $this->address
= $address;
252 public $company = null
;
253 private $phones = array();
254 private $address = null
;
264 public $subsubsector;
267 * pid, id, company_id, description, url, email
269 public function __construct($data)
271 foreach ($data as $key => $val) {
274 $this->company
= CompanyList
::get($this->jobid
);
277 public function phones()
279 return $this->phones
;
282 public function address()
284 return $this->address
;
287 public function addPhone(Phone
&$phone)
289 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
290 $this->phones
[] = $phone;
294 public function setAddress(Address
$address)
296 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
297 $this->address
= $address;
305 const LINK_JOB
= 'job';
306 const LINK_COMPANY
= 'hq';
307 const LINK_PROFILE
= 'home';
319 public $subAdministrativeArea;
320 public $administrativeArea;
325 private $phones = array();
328 * pîd, id, link_id, link_type, flags, text, postcode, country
330 public function __construct($data)
332 foreach ($data as $key => $val) {
335 $this->flags
= new PlFlagSet($this->flags
);
338 public function addPhone(Phone
&$phone)
340 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
341 $this->phones
[$phone->id
] = $phone;
345 public function phones()
347 return $this->phones
;
350 public function hasFlag($flag)
352 if (!$this->flags
instanceof PlFlagSet
) {
353 $this->flags
= new PlFlagSet($this->flags
);
355 return $this->flags
->hasFlag($flag);
359 // {{{ class Education
371 public $school_short;
376 public $degree_short;
377 public $degree_level;
381 public function __construct(array $data)
383 foreach ($data as $key => $val) {
386 $this->flags
= new PlFlagSet($this->flags
);
391 // {{{ class ProfileEducation [ Field ]
392 class ProfileEducation
extends ProfileField
394 private $educations = array();
396 public function __construct(PlInnerSubIterator
$it)
398 $this->pid
= $it->value();
399 while ($edu = $it->next()) {
400 $this->educations
[$edu['id']] = new Education($edu);
404 public function get($flags, $limit)
406 $educations = array();
408 $year = $year['year'];
410 foreach ($this->educations
as $id => $edu) {
412 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
414 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
416 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
418 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
420 ($flags & Profile
::EDUCATION_ALL
)
422 $educations[$id] = $edu;
425 if ($limit != null
&& $nb >= $limit) {
432 public static function fetchData(array $pids, ProfileVisibility
$visibility)
434 $data = XDB
::iterator('SELECT pe.id, pe.pid,
435 pe.entry_year, pe.grad_year, pe.program, pe.flags,
436 pee.name AS school, pee.abbreviation AS school_short,
437 pee.url AS school_url, gc.countryFR AS country,
438 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
440 FROM profile_education AS pe
441 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
442 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
443 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
444 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
446 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
447 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
450 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
454 // {{{ class ProfileMedals [ Field ]
455 class ProfileMedals
extends ProfileField
457 public $medals = array();
459 public function __construct(PlInnerSubIterator
$it)
461 $this->pid
= $it->value();
462 while ($medal = $it->next()) {
463 $this->medals
[$medal['mid']] = $medal;
467 public static function fetchData(array $pids, ProfileVisibility
$visibility)
469 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
470 FROM profile_medals AS pm
471 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
472 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
473 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
474 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
475 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
476 $pids, $visibility->levels());
478 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
482 // {{{ class ProfileNetworking [ Field ]
483 class ProfileNetworking
extends ProfileField
485 private $networks = array();
487 public function __construct(PlInnerSubIterator
$it)
489 $this->pid
= $it->value();
490 while ($network = $it->next()) {
491 $network['network_type'] = new PlFlagSet($network['network_type']);
492 $this->networks
[$network['id']] = $network;
496 public static function fetchData(array $pids, ProfileVisibility
$visibility)
498 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
499 FROM profile_networking AS pn
500 LEFT JOIN profile_networking_enum AS pne USING(nwid)
501 WHERE pid IN {?} AND pub IN {?}
502 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
504 $pids, $visibility->levels());
506 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
509 public function get($flags, $limit = null
)
512 $flags = Profile
::NETWORKING_ALL
;
516 foreach ($this->networks
as $id => $nw) {
517 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
518 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
519 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
520 ($flags & Profile
::NETWORKING_ALL
)) {
523 if (isset($limit) && $nb >= $limit) {
532 // {{{ class ProfileCorps [ Field ]
533 class ProfileCorps
extends ProfileField
538 public $original_name;
539 public $original_abbrev;
540 public $original_still_exists;
542 public $current_name;
543 public $current_abbrev;
544 public $current_still_exists;
545 public $current_rank;
546 public $current_rank_abbrev;
548 public function __construct(array $data)
550 foreach ($data as $key => $val) {
555 public static function fetchData(array $pids, ProfileVisibility
$visibility)
557 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
558 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
559 pceo.still_exists AS original_still_exists,
560 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
561 pcec.still_exists AS current_still_exists,
562 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
564 FROM profile_corps AS pc
565 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
566 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
567 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
568 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
569 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
570 $pids, $visibility->levels());
576 // {{{ class ProfileMentoringSectors [ Field ]
577 class ProfileMentoringSectors
extends ProfileField
579 public $sectors = array();
581 public function __construct(PlInnerSubIterator
$it)
583 $this->pid
= $it->value();
584 while ($sector = $it->next()) {
585 $this->sectors
[] = $sector;
589 public static function fetchData(array $pids, ProfileVisibility
$visibility)
591 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
592 FROM profile_mentor_sector AS pms
593 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
594 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
596 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
599 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
603 // {{{ class ProfileMentoringCountries [ Field ]
604 class ProfileMentoringCountries
extends ProfileField
606 public $countries = array();
608 public function __construct(PlInnerSubIterator
$it)
610 $this->pid
= $it->value();
611 while ($country = $it->next()) {
612 $this->countries
[$country['id']] = $country['name'];
616 public static function fetchData(array $pids, ProfileVisibility
$visibility)
618 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
619 FROM profile_mentor_country AS pmc
620 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
622 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
625 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
630 /** Loading of data for a Profile :
631 * 1) load jobs, addresses, phones
632 * 2) attach phones to addresses, jobs and profiles
633 * 3) attach addresses to jobs and profiles
636 // {{{ class ProfileAddresses [ Field ]
637 class ProfileAddresses
extends ProfileField
639 private $addresses = array();
641 public function __construct(PlIterator
$it)
643 if ($it instanceof PlInnerSubIterator
) {
644 $this->pid
= $it->value();
647 while ($addr = $it->next()) {
648 $this->addresses
[$addr['id']] = new Address($addr);
652 public function get($flags, $limit = null
)
656 foreach ($this->addresses
as $addr) {
658 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
660 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
662 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== Address
::LINK_PROFILE
)
664 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== Address
::LINK_JOB
)
669 if ($limit != null
&& $nb == $limit) {
676 public static function fetchData(array $pids, ProfileVisibility
$visibility)
678 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
679 IF(pa.type = \'home\', pid, jobid) AS link_id,
680 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
681 gl.name AS locality, gas.name AS subAdministrativeArea,
682 ga.name AS administrativeArea, gc.countryFR AS country
683 FROM profile_addresses AS pa
684 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
685 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
686 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
687 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
688 WHERE pa.pid in {?} AND pa.pub IN {?}
689 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
690 $pids, $visibility->levels());
692 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
695 public function addPhones(ProfilePhones
$phones)
697 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
698 foreach ($p as $phone) {
699 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
700 $this->addresses
[$phone->link_id
]->addPhone($phone);
706 // {{{ class ProfilePhones [ Field ]
707 class ProfilePhones
extends ProfileField
709 private $phones = array();
711 public function __construct(PlInnerSubIterator
$it)
713 $this->pid
= $it->value();
714 while ($phone = $it->next()) {
715 $this->phones
[] = new Phone($phone);
719 public function get($flags, $limit = null
)
723 foreach ($this->phones
as $id => $phone) {
724 if ($phone->hasFlags($flags)) {
725 $phones[$id] = $phone;
727 if ($limit != null
&& $nb == $limit) {
735 public static function fetchData(array $pids, ProfileVisibility
$visibility)
737 $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
739 WHERE pid IN {?} AND pub IN {?}
740 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
741 $pids, $visibility->levels());
742 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
746 // {{{ class ProfileJobs [ Field ]
747 class ProfileJobs
extends ProfileField
749 private $jobs = array();
751 public function __construct(PlInnerSubIterator
$jobs)
753 $this->pid
= $jobs->value();
754 while ($job = $jobs->next()) {
755 $this->jobs
[$job['id']] = new Job($job);
759 public static function fetchData(array $pids, ProfileVisibility
$visibility)
761 CompanyList
::preload($pids);
762 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
763 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
764 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
765 pjssse.name AS subsubsector
766 FROM profile_job AS pj
767 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
768 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
769 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
770 WHERE pj.pid IN {?} AND pj.pub IN {?}
771 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
773 $visibility->levels(), $pids, $visibility->levels());
774 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
777 public function get($flags, $limit = null
)
781 foreach ($this->jobs
as $id => $job) {
784 if ($limit != null
&& $nb >= $limit) {
791 public function addPhones(ProfilePhones
$phones)
793 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
794 foreach ($p as $phone) {
795 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
796 $this->jobs
[$phone->link_id
]->addPhone($phone);
801 public function addAddresses(ProfileAddresses
$addresses)
803 $a = $addresses->get(Profile
::ADDRESS_PRO
);
804 foreach ($a as $address) {
805 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
806 $this->jobs
[$address->link_id
]->setAddress($address);
811 public function addCompanies(array $companies)
813 foreach ($this->jobs
as $job) {
814 $this->company
= $companies[$job->jobid
];
820 // {{{ class CompanyList
823 static private $fullload = false
;
824 static private $companies = array();
826 static public function preload($pids = array())
828 if (self
::$fullload) {
833 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
834 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
840 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
841 pa.flags, pa.text, pa.postalCode, pa.countryId,
843 FROM profile_job_enum AS pje
844 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
847 $newcompanies = array();
848 while ($row = $it->next()) {
849 $cp = new Company($row);
850 $addr = new Address($row);
851 $cp->setAddress($addr);
852 if (!array_key_exists($row['id'], self
::$companies)) {
853 $newcompanies[] = $row['id'];
855 self
::$companies[$row['id']] = $cp;
858 // TODO: determine whether there can be phones attached to a hq's address
860 if (count($newcompanies)) {
861 $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
863 WHERE link_id IN {?} AND link_type = \'hq\'',
865 while ($row = $it->next()) {
866 $p = new Phone($row);
867 self
::$companies[$row['link_id']]->setPhone($p);
871 if (count($pids) == 0) {
872 self
::$fullload = true
;
876 static public function get($id)
878 if (!array_key_exists($id, self
::$companies)) {
881 return self
::$companies[$id];
885 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: