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
= 'job';
139 const LINK_ADDRESS
= 'address';
140 const LINK_PROFILE
= 'user';
141 const LINK_COMPANY
= 'hq';
146 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
148 public function __construct($data)
150 foreach ($data as $key => $val) {
163 public $phone = null
;
164 public $address = null
;
167 * $id, $name, $acronym, $url
169 public function __construct($data)
171 foreach ($data as $key => $val) {
176 public function setPhone(Phone
&$phone)
178 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
179 $this->phone
= $phone;
183 public function setAddress(Address
&$address)
185 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
186 $this->address
= $address;
198 public $company = null
;
199 private $phones = array();
200 private $address = null
;
210 public $subsubsector;
213 * pid, id, company_id, description, url, email
215 public function __construct($data)
217 foreach ($data as $key => $val) {
220 $this->company
= CompanyList
::get($this->jobid
);
223 public function phones()
225 return $this->phones
;
228 public function address()
230 return $this->address
;
233 public function addPhone(Phone
&$phone)
235 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
236 $this->phones
[] = $phone;
240 public function setAddress(Address
$address)
242 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
243 $this->address
= $address;
251 const LINK_JOB
= 'job';
252 const LINK_COMPANY
= 'hq';
253 const LINK_PROFILE
= 'home';
265 public $subAdministrativeArea;
266 public $administrativeArea;
271 private $phones = array();
274 * pîd, id, link_id, link_type, flags, text, postcode, country
276 public function __construct($data)
278 foreach ($data as $key => $val) {
281 $this->flags
= new PlFlagSet($this->flags
);
284 public function addPhone(Phone
&$phone)
286 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
287 $this->phones
[] = $phone;
291 public function phones()
293 return $this->phones
;
296 public function hasFlag($flag)
298 if (!$this->flags
instanceof PlFlagSet
) {
299 $this->flags
= new PlFlagSet($this->flags
);
301 return $this->flags
->hasFlag($flag);
305 // {{{ class Education
317 public $school_short;
322 public $degree_short;
323 public $degree_level;
327 public function __construct(array $data)
329 foreach ($data as $key => $val) {
332 $this->flags
= new PlFlagSet($this->flags
);
337 // {{{ class ProfileEducation [ Field ]
338 class ProfileEducation
extends ProfileField
340 private $educations = array();
342 public function __construct(PlInnerSubIterator
$it)
344 $this->pid
= $it->value();
345 while ($edu = $it->next()) {
346 $this->educations
[$edu['id']] = new Education($edu);
350 public function get($flags, $limit)
352 $educations = array();
354 $year = $year['year'];
356 foreach ($this->educations
as $id => $edu) {
358 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
360 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
362 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
364 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
366 ($flags & Profile
::EDUCATION_ALL
)
368 $educations[$id] = $edu;
371 if ($limit != null
&& $nb >= $limit) {
378 public static function fetchData(array $pids, ProfileVisibility
$visibility)
380 $data = XDB
::iterator('SELECT pe.id, pe.pid,
381 pe.entry_year, pe.grad_year, pe.program, pe.flags,
382 pee.name AS school, pee.abbreviation AS school_short,
383 pee.url AS school_url, gc.countryFR AS country,
384 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
386 FROM profile_education AS pe
387 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
388 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
389 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
390 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
392 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
393 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
396 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
400 // {{{ class ProfileMedals [ Field ]
401 class ProfileMedals
extends ProfileField
403 public $medals = array();
405 public function __construct(PlInnerSubIterator
$it)
407 $this->pid
= $it->value();
408 while ($medal = $it->next()) {
409 $this->medals
[$medal['mid']] = $medal;
413 public static function fetchData(array $pids, ProfileVisibility
$visibility)
415 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
416 FROM profile_medals AS pm
417 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
418 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
419 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
420 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
421 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
422 $pids, $visibility->levels());
424 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
428 // {{{ class ProfileNetworking [ Field ]
429 class ProfileNetworking
extends ProfileField
431 private $networks = array();
433 public function __construct(PlIterator
$it)
435 while ($network = $it->next()) {
436 $this->networks
[$network['nwid']] = $network['address'];
440 public static function fetchData(array $pids, ProfileVisibility
$visibility)
442 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
443 FROM profile_networking
444 WHERE pid IN {?} AND pub IN {?}
445 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
447 $pids, $visibility->levels());
449 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
452 public function get($flags, $limit = null
)
456 foreach ($this->networks
as $id => $nw) {
457 // XXX hardcoded reference to web site index
459 (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type'] == 0)
461 (! ($flags & Profile
::NETWORKING_WEB
))
474 // {{{ class ProfileCorps [ Field ]
475 class ProfileCorps
extends ProfileField
480 public $original_name;
481 public $original_abbrev;
482 public $original_still_exists;
484 public $current_name;
485 public $current_abbrev;
486 public $current_still_exists;
487 public $current_rank;
488 public $current_rank_abbrev;
490 public function __construct(array $data)
492 foreach ($data as $key => $val) {
497 public static function fetchData(array $pids, ProfileVisibility
$visibility)
499 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
500 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
501 pceo.still_exists AS original_still_exists,
502 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
503 pcec.still_exists AS current_still_exists,
504 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
506 FROM profile_corps AS pc
507 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
508 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
509 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
510 WHERE pc.pid IN {?} AND pc.corps_pub IN {?}
511 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
512 $pids, $visibility->levels());
518 // {{{ class ProfileMentoringSectors [ Field ]
519 class ProfileMentoringSectors
extends ProfileField
521 public $sectors = array();
523 public function __construct(PlInnerSubIterator
$it)
525 $this->pid
= $it->value();
526 while ($sector = $it->next()) {
527 $this->sectors
[] = $sector;
531 public static function fetchData(array $pids, ProfileVisibility
$visibility)
533 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
534 FROM profile_mentor_sector AS pms
535 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
536 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
538 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
541 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
545 // {{{ class ProfileMentoringCountries [ Field ]
546 class ProfileMentoringCountries
extends ProfileField
548 public $countries = array();
550 public function __construct(PlInnerSubIterator
$it)
552 $this->pid
= $it->value();
553 while ($country = $it->next()) {
554 $this->countries
[$country['id']] = $country['name'];
558 public static function fetchData(array $pids, ProfileVisibility
$visibility)
560 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
561 FROM profile_mentor_country AS pmc
562 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
564 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
567 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
572 /** Loading of data for a Profile :
573 * 1) load jobs, addresses, phones
574 * 2) attach phones to addresses, jobs and profiles
575 * 3) attach addresses to jobs and profiles
578 // {{{ class ProfileAddresses [ Field ]
579 class ProfileAddresses
extends ProfileField
581 private $addresses = array();
583 public function __construct(PlIterator
$it)
585 if ($it instanceof PlInnerSubIterator
) {
586 $this->pid
= $it->value();
589 while ($addr = $it->next()) {
590 $this->addresses
[] = new Address($addr);
594 public function get($flags, $limit = null
)
598 foreach ($this->addresses
as $addr) {
600 ($flags & Profile
::ADDRESS_ALL
)
602 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
604 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
606 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== 'home')
608 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== 'job')
613 if ($limit != null
&& $nb == $limit) {
620 public static function fetchData(array $pids, ProfileVisibility
$visibility)
622 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
623 IF(pa.type = \'home\', pid, jobid) AS link_id,
624 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
625 gl.name AS locality, gas.name AS subAdministrativeArea,
626 ga.name AS administrativeArea, gc.countryFR AS country
627 FROM profile_addresses AS pa
628 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
629 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
630 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
631 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
632 WHERE pa.pid in {?} AND pa.pub IN {?}
633 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
634 $pids, $visibility->levels());
636 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
639 public function addPhones(ProfilePhones
$phones)
641 $p = $phones->get(0);
642 foreach ($p as $phone) {
643 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
644 $this->addresses
[$phone->link_id
]->addPhone($phone);
650 // {{{ class ProfilePhones [ Field ]
651 class ProfilePhones
extends ProfileField
653 private $phones = array();
655 public function __construct(PlInnerSubIterator
$it)
657 $this->pid
= $it->value();
658 while ($phone = $it->next()) {
659 $this->phones
[] = new Phone($phone);
663 public function get($flags, $limit = null
)
667 foreach ($this->phones
as $id => $phone) {
668 $phones[$id] = $phone;
670 if ($limit != null
&& $nb == $limit) {
677 public static function fetchData(array $pids, ProfileVisibility
$visibility)
679 $data = XDB
::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid
681 WHERE pid IN {?} AND pub IN {?}
682 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
683 $pids, $visibility->levels());
684 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
688 // {{{ class ProfileJobs [ Field ]
689 class ProfileJobs
extends ProfileField
691 private $jobs = array();
693 public function __construct(PlInnerSubIterator
$jobs)
695 $this->pid
= $jobs->value();
696 while ($job = $jobs->next()) {
697 $this->jobs
[$job['id']] = new Job($job);
701 public static function fetchData(array $pids, ProfileVisibility
$visibility)
703 CompanyList
::preload($pids);
704 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
705 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
706 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
707 pjssse.name AS subsubsector
708 FROM profile_job AS pj
709 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
710 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
711 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
712 WHERE pj.pid IN {?} AND pj.pub IN {?}
713 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
715 $visibility->levels(), $pids, $visibility->levels());
716 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
719 public function get($flags, $limit = null
)
723 foreach ($this->jobs
as $id => $job) {
726 if ($limit != null
&& $nb >= $limit) {
733 public function addPhones(ProfilePhones
$phones)
735 $p = $phones->get(0);
736 foreach ($p as $phone) {
737 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
738 $this->jobs
[$phone->link_id
]->addPhones($phone);
743 public function addAddresses(ProfileAddresses
$addresses)
745 $a = $addresses->get(Profile
::ADDRESS_PRO
);
746 foreach ($a as $address) {
747 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
748 $this->jobs
[$address->link_id
]->setAddress($address);
753 public function addCompanies(array $companies)
755 foreach ($this->jobs
as $job) {
756 $this->company
= $companies[$job->jobid
];
762 // {{{ class CompanyList
765 static private $fullload = false
;
766 static private $companies = array();
768 static public function preload($pids = array())
770 if (self
::$fullload) {
775 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
776 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
782 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
783 pa.flags, pa.text, pa.postalCode, pa.countryId,
785 FROM profile_job_enum AS pje
786 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
789 while ($row = $it->next()) {
790 $cp = new Company($row);
791 $addr = new Address($row);
792 $cp->setAddress($addr);
793 self
::$companies[$row['id']] = $cp;
796 // TODO: add phones to addresses
797 if (count($pids) == 0) {
798 self
::$fullload = true
;
802 static public function get($id)
804 if (!array_key_exists($id, self
::$companies)) {
807 return self
::$companies[$id];
811 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: