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) {
155 public function hasFlags($flags) {
156 return $this->hasType($flags) && $this->hasLink($flags);
159 /** Returns true if this phone's type matches the flags
161 public function hasType($flags) {
162 $flags = $flags & Profile
::PHONE_TYPE_ANY
;
164 ($flags == Profile
::PHONE_TYPE_ANY
)
166 (($flags & Profile
::PHONE_TYPE_FAX
) && $this->type
== self
::TYPE_FAX
)
168 (($flags & Profile
::PHONE_TYPE_FIXED
) && $this->type
== self
::TYPE_FIXED
)
170 (($flags & Profile
::PHONE_TYPE_MOBILE
) && $this->type
== self
::TYPE_MOBILE
)
174 /** Returns true if this phone's link matches the flags
176 public function hasLink($flags) {
177 $flags = $flags & Profile
::PHONE_LINK_ANY
;
179 ($flags == Profile
::PHONE_LINK_ANY
)
181 (($flags & Profile
::PHONE_LINK_COMPANY
) && $this->link_type
== self
::LINK_COMPANY
)
183 (($flags & Profile
::PHONE_LINK_JOB
) && $this->link_type
== self
::LINK_JOB
)
185 (($flags & Profile
::PHONE_LINK_ADDRESS
) && $this->link_type
== self
::LINK_ADDRESS
)
187 (($flags & Profile
::PHONE_LINK_PROFILE
) && $this->link_type
== self
::LINK_PROFILE
)
199 public $phone = null
;
200 public $address = null
;
203 * $id, $name, $acronym, $url
205 public function __construct($data)
207 foreach ($data as $key => $val) {
212 public function setPhone(Phone
&$phone)
214 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
215 $this->phone
= $phone;
219 public function setAddress(Address
&$address)
221 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
222 $this->address
= $address;
234 public $company = null
;
235 private $phones = array();
236 private $address = null
;
246 public $subsubsector;
249 * pid, id, company_id, description, url, email
251 public function __construct($data)
253 foreach ($data as $key => $val) {
256 $this->company
= CompanyList
::get($this->jobid
);
259 public function phones()
261 return $this->phones
;
264 public function address()
266 return $this->address
;
269 public function addPhone(Phone
&$phone)
271 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
272 $this->phones
[] = $phone;
276 public function setAddress(Address
$address)
278 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
279 $this->address
= $address;
287 const LINK_JOB
= 'job';
288 const LINK_COMPANY
= 'hq';
289 const LINK_PROFILE
= 'home';
301 public $subAdministrativeArea;
302 public $administrativeArea;
307 private $phones = array();
310 * pîd, id, link_id, link_type, flags, text, postcode, country
312 public function __construct($data)
314 foreach ($data as $key => $val) {
317 $this->flags
= new PlFlagSet($this->flags
);
320 public function addPhone(Phone
&$phone)
322 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
323 $this->phones
[] = $phone;
327 public function phones()
329 return $this->phones
;
332 public function hasFlag($flag)
334 if (!$this->flags
instanceof PlFlagSet
) {
335 $this->flags
= new PlFlagSet($this->flags
);
337 return $this->flags
->hasFlag($flag);
341 // {{{ class Education
353 public $school_short;
358 public $degree_short;
359 public $degree_level;
363 public function __construct(array $data)
365 foreach ($data as $key => $val) {
368 $this->flags
= new PlFlagSet($this->flags
);
373 // {{{ class ProfileEducation [ Field ]
374 class ProfileEducation
extends ProfileField
376 private $educations = array();
378 public function __construct(PlInnerSubIterator
$it)
380 $this->pid
= $it->value();
381 while ($edu = $it->next()) {
382 $this->educations
[$edu['id']] = new Education($edu);
386 public function get($flags, $limit)
388 $educations = array();
390 $year = $year['year'];
392 foreach ($this->educations
as $id => $edu) {
394 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
396 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
398 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
400 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
402 ($flags & Profile
::EDUCATION_ALL
)
404 $educations[$id] = $edu;
407 if ($limit != null
&& $nb >= $limit) {
414 public static function fetchData(array $pids, ProfileVisibility
$visibility)
416 $data = XDB
::iterator('SELECT pe.id, pe.pid,
417 pe.entry_year, pe.grad_year, pe.program, pe.flags,
418 pee.name AS school, pee.abbreviation AS school_short,
419 pee.url AS school_url, gc.countryFR AS country,
420 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
422 FROM profile_education AS pe
423 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
424 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
425 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
426 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
428 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
429 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
432 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
436 // {{{ class ProfileMedals [ Field ]
437 class ProfileMedals
extends ProfileField
439 public $medals = array();
441 public function __construct(PlInnerSubIterator
$it)
443 $this->pid
= $it->value();
444 while ($medal = $it->next()) {
445 $this->medals
[$medal['mid']] = $medal;
449 public static function fetchData(array $pids, ProfileVisibility
$visibility)
451 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
452 FROM profile_medals AS pm
453 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
454 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
455 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
456 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
457 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
458 $pids, $visibility->levels());
460 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
464 // {{{ class ProfileNetworking [ Field ]
465 class ProfileNetworking
extends ProfileField
467 private $networks = array();
469 public function __construct(PlInnerSubIterator
$it)
471 $this->pid
= $it->value();
472 while ($network = $it->next()) {
473 $network['network_type'] = new PlFlagSet($network['network_type']);
474 $this->networks
[$network['id']] = $network;
478 public static function fetchData(array $pids, ProfileVisibility
$visibility)
480 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
481 FROM profile_networking AS pn
482 LEFT JOIN profile_networking_enum AS pne USING(nwid)
483 WHERE pid IN {?} AND pub IN {?}
484 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
486 $pids, $visibility->levels());
488 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
491 public function get($flags, $limit = null
)
494 $flags = Profile
::NETWORKING_ALL
;
498 foreach ($this->networks
as $id => $nw) {
499 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
500 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
501 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
502 ($flags & Profile
::NETWORKING_ALL
)) {
505 if (isset($limit) && $nb >= $limit) {
514 // {{{ class ProfileCorps [ Field ]
515 class ProfileCorps
extends ProfileField
520 public $original_name;
521 public $original_abbrev;
522 public $original_still_exists;
524 public $current_name;
525 public $current_abbrev;
526 public $current_still_exists;
527 public $current_rank;
528 public $current_rank_abbrev;
530 public function __construct(array $data)
532 foreach ($data as $key => $val) {
537 public static function fetchData(array $pids, ProfileVisibility
$visibility)
539 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
540 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
541 pceo.still_exists AS original_still_exists,
542 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
543 pcec.still_exists AS current_still_exists,
544 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
546 FROM profile_corps AS pc
547 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
548 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
549 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
550 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
551 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
552 $pids, $visibility->levels());
558 // {{{ class ProfileMentoringSectors [ Field ]
559 class ProfileMentoringSectors
extends ProfileField
561 public $sectors = array();
563 public function __construct(PlInnerSubIterator
$it)
565 $this->pid
= $it->value();
566 while ($sector = $it->next()) {
567 $this->sectors
[] = $sector;
571 public static function fetchData(array $pids, ProfileVisibility
$visibility)
573 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
574 FROM profile_mentor_sector AS pms
575 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
576 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
578 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
581 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
585 // {{{ class ProfileMentoringCountries [ Field ]
586 class ProfileMentoringCountries
extends ProfileField
588 public $countries = array();
590 public function __construct(PlInnerSubIterator
$it)
592 $this->pid
= $it->value();
593 while ($country = $it->next()) {
594 $this->countries
[$country['id']] = $country['name'];
598 public static function fetchData(array $pids, ProfileVisibility
$visibility)
600 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
601 FROM profile_mentor_country AS pmc
602 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
604 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
607 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
612 /** Loading of data for a Profile :
613 * 1) load jobs, addresses, phones
614 * 2) attach phones to addresses, jobs and profiles
615 * 3) attach addresses to jobs and profiles
618 // {{{ class ProfileAddresses [ Field ]
619 class ProfileAddresses
extends ProfileField
621 private $addresses = array();
623 public function __construct(PlIterator
$it)
625 if ($it instanceof PlInnerSubIterator
) {
626 $this->pid
= $it->value();
629 while ($addr = $it->next()) {
630 $this->addresses
[] = new Address($addr);
634 public function get($flags, $limit = null
)
638 foreach ($this->addresses
as $addr) {
640 ($flags & Profile
::ADDRESS_ALL
)
642 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
644 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
646 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== 'home')
648 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== '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(0);
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
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(0);
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
]->addPhones($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 while ($row = $it->next()) {
832 $cp = new Company($row);
833 $addr = new Address($row);
834 $cp->setAddress($addr);
835 self
::$companies[$row['id']] = $cp;
838 // TODO: add phones to addresses
839 if (count($pids) == 0) {
840 self
::$fullload = true
;
844 static public function get($id)
846 if (!array_key_exists($id, self
::$companies)) {
849 return self
::$companies[$id];
853 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: