2 /***************************************************************************
3 * Copyright (C) 2003-2011 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_COUNTRY
=> 'ProfileMentoringCountries',
37 Profile
::FETCH_JOB_TERMS
=> 'ProfileJobTerms',
38 Profile
::FETCH_MENTOR_TERMS
=> 'ProfileMentoringTerms',
39 Profile
::FETCH_PARTNER
=> 'ProfilePartnerSharing',
42 /** The profile to which this field belongs
46 /** Fetches data from the database for the given pids, compatible with
47 * the visibility context.
48 * @param $pids An array of pids
49 * @param $visibility The level of visibility fetched fields must have
50 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
52 * MUST be reimplemented for each kind of ProfileField.
54 public static function fetchData(array $pids, Visibility
$visibility)
56 return PlIteratorUtils
::emptyIterator();
59 public static function buildForPID($cls, $pid, Visibility
$visibility)
61 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
62 return array_pop($res);
65 /** Build a list of ProfileFields from a set of pids
66 * @param $cls The name of the field to create ('ProfileMedals', ...)
67 * @param $pids An array of pids
68 * @param $visibility An array of allowed visibility contexts
69 * @return An array of $pid => ProfileField
71 public static function buildFromPIDs($cls, array $pids, Visibility
$visibility)
73 $it = new ProfileFieldIterator($cls, $pids, $visibility);
75 while ($pf = $it->next()) {
81 public static function getForPID($cls, $pid, Visibility
$visibility)
83 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
89 // {{{ class ProfileFieldIterator
90 class ProfileFieldIterator
implements PlIterator
95 public function __construct($cls, array $pids, Visibility
$visibility)
97 if (is_numeric($cls) && isset(ProfileField
::$fields[$cls])) {
98 $cls = ProfileField
::$fields[$cls];
100 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
104 public function next()
106 $d = $this->data
->next();
115 public function total()
117 return $this->data
->total();
120 public function first()
122 return $this->data
->first();
125 public function last()
127 return $this->data
->last();
139 public $phone = null
;
140 public $address = null
;
143 * $id, $name, $acronym, $url
145 public function __construct($data)
147 foreach ($data as $key => $val) {
152 public function setPhone(Phone
$phone)
154 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
155 $this->phone
= $phone;
159 public function setAddress(Address
$address)
161 if ($address->type
== Address
::LINK_COMPANY
&& $address->jobid
== $this->id
) {
162 $this->address
= $address;
169 /** profile_job describes a Job, links to:
170 * - a Profile, through `pid`
171 * - a Company, through `jobid`
172 * The `id` field is the id of this job in the list of the jobs of its profile
174 * For the documentation of the phone table, please see classes/phone.php.
175 * For the documentation of the address table, please see classes/address.php.
177 * The possible relations are as follow:
178 * A Job is linked to a Company and a Profile
185 public $company = null
;
186 public $phones = array();
187 public $address = null
;
188 public $terms = array();
197 * pid, id, company_id, description, url, email
199 public function __construct($data)
201 foreach ($data as $key => $val) {
204 $this->company
= CompanyList
::get($this->jobid
);
205 if (is_null($this->company
)) {
206 $entreprises = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
207 foreach ($entreprises as $entreprise) {
208 if ($entreprise->id
== $this->id
) {
209 $this->company
= new Company(array('name' => $entreprise->name
));
215 public function phones()
217 return $this->phones
;
220 public function address()
222 return $this->address
;
225 public function addPhone(Phone
$phone)
227 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
228 $this->phones
[$phone->uniqueId()] = $phone;
232 public function setAddress(Address
$address)
234 if ($address->type
== Address
::LINK_JOB
&& $address->id
== $this->id
&& $address->pid
== $this->pid
) {
235 $this->address
= $address;
239 public function addTerm(JobTerm
$term)
241 $this->terms
[$term->jtid
] = $term;
254 * pid, jid, jtid, full_name
256 public function __construct($data)
258 foreach ($data as $key => $val) {
264 // {{{ class Education
276 public $school_short;
281 public $degree_short;
282 public $degree_level;
286 public function __construct(array $data)
288 foreach ($data as $key => $val) {
291 $this->flags
= new PlFlagSet($this->flags
);
296 // {{{ class ProfileEducation [ Field ]
297 class ProfileEducation
extends ProfileField
299 private $educations = array();
301 public function __construct(PlInnerSubIterator
$it)
303 $this->pid
= $it->value();
304 while ($edu = $it->next()) {
305 $this->educations
[$edu['id']] = new Education($edu);
309 public function get($flags, $limit)
311 $educations = array();
313 $year = $year['year'];
315 foreach ($this->educations
as $id => $edu) {
317 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
319 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
321 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
323 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
325 ($flags & Profile
::EDUCATION_ALL
)
327 $educations[$id] = $edu;
330 if ($limit != null
&& $nb >= $limit) {
337 public static function fetchData(array $pids, Visibility
$visibility)
339 $data = XDB
::iterator('SELECT pe.id, pe.pid,
340 pe.entry_year, pe.grad_year, pe.program, pe.flags,
341 pee.name AS school, pee.abbreviation AS school_short,
342 pee.url AS school_url, gc.country,
343 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
345 FROM profile_education AS pe
346 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
347 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
348 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
349 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
351 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
352 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
355 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
359 // {{{ class ProfileMedals [ Field ]
360 class ProfileMedals
extends ProfileField
362 public $medals = array();
364 public function __construct(PlInnerSubIterator
$it)
366 $this->pid
= $it->value();
367 while ($medal = $it->next()) {
368 $this->medals
[$medal['mid']] = $medal;
372 public static function fetchData(array $pids, Visibility
$visibility)
374 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
375 FROM profile_medals AS pm
376 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
377 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
378 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
379 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
380 WHERE pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0
381 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
382 $visibility->level(), $pids);
384 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
388 // {{{ class ProfileNetworking [ Field ]
389 class ProfileNetworking
extends ProfileField
391 private $networks = array();
393 public function __construct(PlInnerSubIterator
$it)
395 $this->pid
= $it->value();
396 while ($network = $it->next()) {
397 $network['network_type'] = new PlFlagSet($network['network_type']);
398 $this->networks
[$network['id']] = $network;
402 public static function fetchData(array $pids, Visibility
$visibility)
404 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
405 FROM profile_networking AS pn
406 LEFT JOIN profile_networking_enum AS pne USING(nwid)
407 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
408 WHERE pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0
409 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
411 $visibility->level(), $pids);
413 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
416 public function get($flags, $limit = null
)
420 foreach ($this->networks
as $id => $nw) {
421 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
422 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
423 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
424 ($flags == Profile
::NETWORKING_ALL
)) {
427 if (isset($limit) && $nb >= $limit) {
436 // {{{ class ProfileCorps [ Field ]
437 class ProfileCorps
extends ProfileField
442 public $original_name;
443 public $original_abbrev;
444 public $original_still_exists;
446 public $current_name;
447 public $current_abbrev;
448 public $current_still_exists;
449 public $current_rank;
450 public $current_rank_abbrev;
452 public function __construct(array $data)
454 foreach ($data as $key => $val) {
459 public static function fetchData(array $pids, Visibility
$visibility)
461 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
462 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
463 pceo.still_exists AS original_still_exists,
464 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
465 pcec.still_exists AS current_still_exists,
466 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
468 FROM profile_corps AS pc
469 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
470 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
471 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
472 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
473 WHERE pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1
474 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
475 $visibility->level(), $pids);
481 // {{{ class ProfileMentoringCountries [ Field ]
482 class ProfileMentoringCountries
extends ProfileField
484 public $countries = array();
486 public function __construct(PlInnerSubIterator
$it)
488 $this->pid
= $it->value();
489 while ($country = $it->next()) {
490 $this->countries
[$country['id']] = $country['name'];
494 public static function fetchData(array $pids, Visibility
$visibility)
496 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name
497 FROM profile_mentor_country AS pmc
498 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
500 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
503 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
507 // {{{ class ProfileAddresses [ Field ]
508 class ProfileAddresses
extends ProfileField
510 private $addresses = array();
512 public function __construct(PlInnerSubIterator
$it)
514 $this->pid
= $it->value();
515 while ($address = $it->next()) {
516 $this->addresses
[] = new Address($address);
520 public function get($flags, $limit = null
)
522 $addresses = array();
524 foreach ($this->addresses
as $address) {
525 if ((($flags & Profile
::ADDRESS_MAIN
) && $address->hasFlag('current'))
526 ||
(($flags & Profile
::ADDRESS_POSTAL
) && $address->hasFlag('mail'))
527 ||
(($flags & Profile
::ADDRESS_PERSO
) && $address->type
== Address
::LINK_PROFILE
)
528 ||
(($flags & Profile
::ADDRESS_PRO
) && $address->type
== Address
::LINK_JOB
)
530 $addresses[] = $address;
533 if ($limit != null
&& $nb == $limit) {
540 public static function fetchData(array $pids, Visibility
$visibility)
542 $it = Address
::iterate($pids, array(), array(), $visibility);
543 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
546 public function addPhones(ProfilePhones
$phones)
548 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
549 foreach ($p as $phone) {
550 /* We must iterate on the addresses because id is not uniq thus,
551 * $this->addresse[$phone->link_id] is invalid.
553 foreach ($this->addresses
as $address) {
554 if ($address->type
== Address
::LINK_PROFILE
&& $address->id
== $phone->link_id
) {
555 $address->addPhone($phone);
562 // {{{ class ProfilePhones [ Field ]
563 class ProfilePhones
extends ProfileField
565 private $phones = array();
567 public function __construct(PlInnerSubIterator
$it)
569 $this->pid
= $it->value();
570 while ($phone = $it->next()) {
571 $this->phones
[] = new Phone($phone);
575 public function get($flags, $limit = null
)
579 foreach ($this->phones
as $id => $phone) {
580 if ($phone->hasFlags($flags)) {
581 $phones[$id] = $phone;
583 if ($limit != null
&& $nb == $limit) {
591 public static function fetchData(array $pids, Visibility
$visibility)
593 $it = Phone
::iterate($pids, array(), array(), $visibility);
594 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
598 // {{{ class ProfileJobs [ Field ]
599 class ProfileJobs
extends ProfileField
601 private $jobs = array();
603 public function __construct(PlInnerSubIterator
$jobs)
605 $this->pid
= $jobs->value();
606 while ($job = $jobs->next()) {
607 $this->jobs
[$job['id']] = new Job($job);
611 public static function fetchData(array $pids, Visibility
$visibility)
613 CompanyList
::preload($pids);
614 $data = XDB
::iterator('SELECT id, pid, description, url as user_site, jobid,
615 IF(pve.best_display_level + 0 <= email_pub + 0, email, NULL) AS user_email
617 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
618 WHERE pid IN {?} AND pve.best_display_level + 0 <= pub + 0
619 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ', id',
620 $visibility->level(), $pids);
621 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
624 public function get($flags, $limit = null
)
628 foreach ($this->jobs
as $id => $job) {
631 if ($limit != null
&& $nb >= $limit) {
638 public function addPhones(ProfilePhones
$phones)
640 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
641 foreach ($p as $phone) {
642 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
643 $this->jobs
[$phone->link_id
]->addPhone($phone);
648 public function addAddresses(ProfileAddresses
$addresses)
650 $a = $addresses->get(Profile
::ADDRESS_PRO
);
651 foreach ($a as $address) {
652 if ($address->type
== Address
::LINK_JOB
&& array_key_exists($address->id
, $this->jobs
)) {
653 $this->jobs
[$address->id
]->setAddress($address);
658 public function addCompanies(array $companies)
660 foreach ($this->jobs
as $job) {
661 $this->company
= $companies[$job->jobid
];
665 public function addJobTerms(ProfileJobTerms
$jobterms)
667 $terms = $jobterms->get();
668 foreach ($terms as $term) {
669 if ($this->pid
== $term->pid
&& array_key_exists($term->jid
, $this->jobs
)) {
670 $this->jobs
[$term->jid
]->addTerm($term);
676 // {{{ class ProfileJobTerms [ Field ]
677 class ProfileJobTerms
extends ProfileField
679 private $jobterms = array();
681 public function __construct(PlInnerSubIterator
$it)
683 $this->pid
= $it->value();
684 while ($term = $it->next()) {
685 $this->jobterms
[] = new JobTerm($term);
689 public function get()
691 return $this->jobterms
;
694 public static function fetchData(array $pids, Visibility
$visibility)
696 $data = XDB
::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
697 FROM profile_job_term AS jt
698 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
699 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
700 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
701 WHERE jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0
702 ORDER BY ' . XDB
::formatCustomOrder('jt.pid', $pids),
703 $visibility->level(), $pids);
704 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
708 // {{{ class ProfileMentoringTerms [ Field ]
709 class ProfileMentoringTerms
extends ProfileJobTerms
711 public static function fetchData(array $pids, Visibility
$visibility)
713 $data = XDB
::iterator('SELECT mt.jtid, jte.full_name, mt.pid
714 FROM profile_mentor_term AS mt
715 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
717 ORDER BY ' . XDB
::formatCustomOrder('mt.pid', $pids),
719 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
723 // {{{ class ProfilePartnerSharing [ Field ]
724 class ProfilePartnerSharing
extends ProfileField
726 public function __construct(PlInnerSubIterator
$it)
728 require_once 'partnersharing.inc.php';
730 $this->pid
= $it->value();
731 while ($partner_settings = $it->next()) {
732 $settings = new PartnerSettings($partner_settings);
733 $this->partners_settings
[$settings->partner
->id
] = $settings;
737 public static function fetchData(array $pids, Visibility
$visibility)
739 $data = XDB
::iterator('SELECT ppss.pid, ppss.exposed_uid, ppss.sharing_level,
740 ppss.allow_email, ppss.partner_id,
741 ppse.shortname AS partner_shortname,
742 ppse.name AS partner_name,
743 ppse.url AS partner_url
744 FROM profile_partnersharing_settings AS ppss
745 LEFT JOIN profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id)
746 WHERE ppss.pid IN {?}
747 ORDER BY ' . XDB
::formatCustomOrder('ppss.pid', $pids),
749 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
752 public function get($partner_id)
754 if (isset($this->partners_settings
[$partner_id])) {
755 return $this->partners_settings
[$partner_id];
757 return PartnerSettings
::getEmpty($partner_id);
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.type, pa.pub
784 FROM profile_job_enum AS pje
785 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
788 $newcompanies = array();
789 while ($row = $it->next()) {
790 $cp = new Company($row);
791 $addr = new Address($row);
792 $cp->setAddress($addr);
793 if (!array_key_exists($row['id'], self
::$companies)) {
794 $newcompanies[] = $row['id'];
796 self
::$companies[$row['id']] = $cp;
800 if (count($newcompanies)) {
801 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies, Visibility
::defaultForRead());
802 while ($phone = $it->next()) {
803 self
::$companies[$phone->link_id
]->setPhone($phone);
807 if (count($pids) == 0) {
808 self
::$fullload = true
;
812 static public function get($id)
814 if (!array_key_exists($id, self
::$companies)) {
817 if (isset(self
::$companies[$id])) {
818 return self
::$companies[$id];
825 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: