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 /** Returns true if this phone's link matches the flags
178 public function hasLink($flags) {
179 $flags = $flags & Profile
::PHONE_LINK_ANY
;
181 ($flags == Profile
::PHONE_LINK_ANY
)
183 (($flags & Profile
::PHONE_LINK_COMPANY
) && $this->link_type
== self
::LINK_COMPANY
)
185 (($flags & Profile
::PHONE_LINK_JOB
) && $this->link_type
== self
::LINK_JOB
)
187 (($flags & Profile
::PHONE_LINK_ADDRESS
) && $this->link_type
== self
::LINK_ADDRESS
)
189 (($flags & Profile
::PHONE_LINK_PROFILE
) && $this->link_type
== self
::LINK_PROFILE
)
201 public $phone = null
;
202 public $address = null
;
205 * $id, $name, $acronym, $url
207 public function __construct($data)
209 foreach ($data as $key => $val) {
214 public function setPhone(Phone
&$phone)
216 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
217 $this->phone
= $phone;
221 public function setAddress(Address
&$address)
223 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
224 $this->address
= $address;
236 public $company = null
;
237 private $phones = array();
238 private $address = null
;
248 public $subsubsector;
251 * pid, id, company_id, description, url, email
253 public function __construct($data)
255 foreach ($data as $key => $val) {
258 $this->company
= CompanyList
::get($this->jobid
);
261 public function phones()
263 return $this->phones
;
266 public function address()
268 return $this->address
;
271 public function addPhone(Phone
&$phone)
273 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
274 $this->phones
[] = $phone;
278 public function setAddress(Address
$address)
280 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
281 $this->address
= $address;
289 const LINK_JOB
= 'job';
290 const LINK_COMPANY
= 'hq';
291 const LINK_PROFILE
= 'home';
303 public $subAdministrativeArea;
304 public $administrativeArea;
309 private $phones = array();
312 * pîd, id, link_id, link_type, flags, text, postcode, country
314 public function __construct($data)
316 foreach ($data as $key => $val) {
319 $this->flags
= new PlFlagSet($this->flags
);
322 public function addPhone(Phone
&$phone)
324 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
325 $this->phones
[$phone->id
] = $phone;
329 public function phones()
331 return $this->phones
;
334 public function hasFlag($flag)
336 if (!$this->flags
instanceof PlFlagSet
) {
337 $this->flags
= new PlFlagSet($this->flags
);
339 return $this->flags
->hasFlag($flag);
343 // {{{ class Education
355 public $school_short;
360 public $degree_short;
361 public $degree_level;
365 public function __construct(array $data)
367 foreach ($data as $key => $val) {
370 $this->flags
= new PlFlagSet($this->flags
);
375 // {{{ class ProfileEducation [ Field ]
376 class ProfileEducation
extends ProfileField
378 private $educations = array();
380 public function __construct(PlInnerSubIterator
$it)
382 $this->pid
= $it->value();
383 while ($edu = $it->next()) {
384 $this->educations
[$edu['id']] = new Education($edu);
388 public function get($flags, $limit)
390 $educations = array();
392 $year = $year['year'];
394 foreach ($this->educations
as $id => $edu) {
396 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
398 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
400 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
402 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
404 ($flags & Profile
::EDUCATION_ALL
)
406 $educations[$id] = $edu;
409 if ($limit != null
&& $nb >= $limit) {
416 public static function fetchData(array $pids, ProfileVisibility
$visibility)
418 $data = XDB
::iterator('SELECT pe.id, pe.pid,
419 pe.entry_year, pe.grad_year, pe.program, pe.flags,
420 pee.name AS school, pee.abbreviation AS school_short,
421 pee.url AS school_url, gc.countryFR AS country,
422 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
424 FROM profile_education AS pe
425 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
426 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
427 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
428 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
430 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
431 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
434 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
438 // {{{ class ProfileMedals [ Field ]
439 class ProfileMedals
extends ProfileField
441 public $medals = array();
443 public function __construct(PlInnerSubIterator
$it)
445 $this->pid
= $it->value();
446 while ($medal = $it->next()) {
447 $this->medals
[$medal['mid']] = $medal;
451 public static function fetchData(array $pids, ProfileVisibility
$visibility)
453 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
454 FROM profile_medals AS pm
455 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
456 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
457 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
458 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
459 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
460 $pids, $visibility->levels());
462 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
466 // {{{ class ProfileNetworking [ Field ]
467 class ProfileNetworking
extends ProfileField
469 private $networks = array();
471 public function __construct(PlInnerSubIterator
$it)
473 $this->pid
= $it->value();
474 while ($network = $it->next()) {
475 $network['network_type'] = new PlFlagSet($network['network_type']);
476 $this->networks
[$network['id']] = $network;
480 public static function fetchData(array $pids, ProfileVisibility
$visibility)
482 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
483 FROM profile_networking AS pn
484 LEFT JOIN profile_networking_enum AS pne USING(nwid)
485 WHERE pid IN {?} AND pub IN {?}
486 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
488 $pids, $visibility->levels());
490 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
493 public function get($flags, $limit = null
)
496 $flags = Profile
::NETWORKING_ALL
;
500 foreach ($this->networks
as $id => $nw) {
501 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
502 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
503 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
504 ($flags & Profile
::NETWORKING_ALL
)) {
507 if (isset($limit) && $nb >= $limit) {
516 // {{{ class ProfileCorps [ Field ]
517 class ProfileCorps
extends ProfileField
522 public $original_name;
523 public $original_abbrev;
524 public $original_still_exists;
526 public $current_name;
527 public $current_abbrev;
528 public $current_still_exists;
529 public $current_rank;
530 public $current_rank_abbrev;
532 public function __construct(array $data)
534 foreach ($data as $key => $val) {
539 public static function fetchData(array $pids, ProfileVisibility
$visibility)
541 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
542 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
543 pceo.still_exists AS original_still_exists,
544 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
545 pcec.still_exists AS current_still_exists,
546 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
548 FROM profile_corps AS pc
549 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
550 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
551 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
552 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
553 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
554 $pids, $visibility->levels());
560 // {{{ class ProfileMentoringSectors [ Field ]
561 class ProfileMentoringSectors
extends ProfileField
563 public $sectors = array();
565 public function __construct(PlInnerSubIterator
$it)
567 $this->pid
= $it->value();
568 while ($sector = $it->next()) {
569 $this->sectors
[] = $sector;
573 public static function fetchData(array $pids, ProfileVisibility
$visibility)
575 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
576 FROM profile_mentor_sector AS pms
577 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
578 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
580 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
583 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
587 // {{{ class ProfileMentoringCountries [ Field ]
588 class ProfileMentoringCountries
extends ProfileField
590 public $countries = array();
592 public function __construct(PlInnerSubIterator
$it)
594 $this->pid
= $it->value();
595 while ($country = $it->next()) {
596 $this->countries
[$country['id']] = $country['name'];
600 public static function fetchData(array $pids, ProfileVisibility
$visibility)
602 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
603 FROM profile_mentor_country AS pmc
604 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
606 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
609 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
614 /** Loading of data for a Profile :
615 * 1) load jobs, addresses, phones
616 * 2) attach phones to addresses, jobs and profiles
617 * 3) attach addresses to jobs and profiles
620 // {{{ class ProfileAddresses [ Field ]
621 class ProfileAddresses
extends ProfileField
623 private $addresses = array();
625 public function __construct(PlIterator
$it)
627 if ($it instanceof PlInnerSubIterator
) {
628 $this->pid
= $it->value();
631 while ($addr = $it->next()) {
632 $this->addresses
[$addr['id']] = new Address($addr);
636 public function get($flags, $limit = null
)
640 foreach ($this->addresses
as $addr) {
642 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
644 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
646 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== Address
::LINK_PROFILE
)
648 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== Address
::LINK_JOB
)
653 if ($limit != null
&& $nb == $limit) {
660 public static function fetchData(array $pids, ProfileVisibility
$visibility)
662 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
663 IF(pa.type = \'home\', pid, jobid) AS link_id,
664 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
665 gl.name AS locality, gas.name AS subAdministrativeArea,
666 ga.name AS administrativeArea, gc.countryFR AS country
667 FROM profile_addresses AS pa
668 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
669 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
670 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
671 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
672 WHERE pa.pid in {?} AND pa.pub IN {?}
673 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
674 $pids, $visibility->levels());
676 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
679 public function addPhones(ProfilePhones
$phones)
681 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
682 foreach ($p as $phone) {
683 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
684 $this->addresses
[$phone->link_id
]->addPhone($phone);
690 // {{{ class ProfilePhones [ Field ]
691 class ProfilePhones
extends ProfileField
693 private $phones = array();
695 public function __construct(PlInnerSubIterator
$it)
697 $this->pid
= $it->value();
698 while ($phone = $it->next()) {
699 $this->phones
[] = new Phone($phone);
703 public function get($flags, $limit = null
)
707 foreach ($this->phones
as $id => $phone) {
708 if ($phone->hasFlags($flags)) {
709 $phones[$id] = $phone;
711 if ($limit != null
&& $nb == $limit) {
719 public static function fetchData(array $pids, ProfileVisibility
$visibility)
721 $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
723 WHERE pid IN {?} AND pub IN {?}
724 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
725 $pids, $visibility->levels());
726 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
730 // {{{ class ProfileJobs [ Field ]
731 class ProfileJobs
extends ProfileField
733 private $jobs = array();
735 public function __construct(PlInnerSubIterator
$jobs)
737 $this->pid
= $jobs->value();
738 while ($job = $jobs->next()) {
739 $this->jobs
[$job['id']] = new Job($job);
743 public static function fetchData(array $pids, ProfileVisibility
$visibility)
745 CompanyList
::preload($pids);
746 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
747 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
748 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
749 pjssse.name AS subsubsector
750 FROM profile_job AS pj
751 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
752 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
753 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
754 WHERE pj.pid IN {?} AND pj.pub IN {?}
755 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
757 $visibility->levels(), $pids, $visibility->levels());
758 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
761 public function get($flags, $limit = null
)
765 foreach ($this->jobs
as $id => $job) {
768 if ($limit != null
&& $nb >= $limit) {
775 public function addPhones(ProfilePhones
$phones)
777 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
778 foreach ($p as $phone) {
779 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
780 $this->jobs
[$phone->link_id
]->addPhone($phone);
785 public function addAddresses(ProfileAddresses
$addresses)
787 $a = $addresses->get(Profile
::ADDRESS_PRO
);
788 foreach ($a as $address) {
789 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
790 $this->jobs
[$address->link_id
]->setAddress($address);
795 public function addCompanies(array $companies)
797 foreach ($this->jobs
as $job) {
798 $this->company
= $companies[$job->jobid
];
804 // {{{ class CompanyList
807 static private $fullload = false
;
808 static private $companies = array();
810 static public function preload($pids = array())
812 if (self
::$fullload) {
817 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
818 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
824 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
825 pa.flags, pa.text, pa.postalCode, pa.countryId,
827 FROM profile_job_enum AS pje
828 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
831 $newcompanies = array();
832 while ($row = $it->next()) {
833 $cp = new Company($row);
834 $addr = new Address($row);
835 $cp->setAddress($addr);
836 if (!array_key_exists($row['id'], self
::$companies)) {
837 $newcompanies[] = $row['id'];
839 self
::$companies[$row['id']] = $cp;
842 // TODO: determine whether there can be phones attached to a hq's address
844 $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
846 WHERE link_id IN {?} AND link_type = \'hq\'',
848 while ($row = $it->next()) {
849 $p = new Phone($row);
850 self
::$companies[$row['link_id']]->setPhone($p);
853 if (count($pids) == 0) {
854 self
::$fullload = true
;
858 static public function get($id)
860 if (!array_key_exists($id, self
::$companies)) {
863 return self
::$companies[$id];
867 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: