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();
200 * pid, id, company_id, description, url, email
202 public function __construct($data)
204 foreach ($data as $key => $val) {
207 $this->company
= CompanyList
::get($this->jobid
);
208 if (is_null($this->company
)) {
209 $entreprises = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
210 foreach ($entreprises as $entreprise) {
211 if ($entreprise->id
== $this->id
) {
212 $this->company
= new Company(array('name' => $entreprise->name
));
218 public function phones()
220 return $this->phones
;
223 public function address()
225 return $this->address
;
228 public function addPhone(Phone
$phone)
230 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
231 $this->phones
[$phone->uniqueId()] = $phone;
235 public function setAddress(Address
$address)
237 if ($address->type
== Address
::LINK_JOB
&& $address->id
== $this->id
&& $address->pid
== $this->pid
) {
238 $this->address
= $address;
242 public function addTerm(JobTerm
$term)
244 $this->terms
[$term->jtid
] = $term;
257 * pid, jid, jtid, full_name
259 public function __construct($data)
261 foreach ($data as $key => $val) {
267 // {{{ class Education
279 public $school_short;
284 public $degree_short;
285 public $degree_level;
289 public function __construct(array $data)
291 foreach ($data as $key => $val) {
294 $this->flags
= new PlFlagSet($this->flags
);
299 // {{{ class ProfileEducation [ Field ]
300 class ProfileEducation
extends ProfileField
302 private $educations = array();
304 public function __construct(PlInnerSubIterator
$it)
306 $this->pid
= $it->value();
307 while ($edu = $it->next()) {
308 $this->educations
[$edu['id']] = new Education($edu);
312 public function get($flags, $limit)
314 $educations = array();
316 $year = $year['year'];
318 foreach ($this->educations
as $id => $edu) {
320 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
322 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
324 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
326 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
328 ($flags & Profile
::EDUCATION_ALL
)
330 $educations[$id] = $edu;
333 if ($limit != null
&& $nb >= $limit) {
340 public static function fetchData(array $pids, Visibility
$visibility)
342 $data = XDB
::iterator('SELECT pe.id, pe.pid,
343 pe.entry_year, pe.grad_year, pe.program, pe.flags,
344 pee.name AS school, pee.abbreviation AS school_short,
345 pee.url AS school_url, gc.country,
346 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
348 FROM profile_education AS pe
349 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
350 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
351 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
352 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
354 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
355 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
358 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
362 // {{{ class ProfileMedals [ Field ]
363 class ProfileMedals
extends ProfileField
365 public $medals = array();
367 public function __construct(PlInnerSubIterator
$it)
369 $this->pid
= $it->value();
370 while ($medal = $it->next()) {
371 $this->medals
[$medal['mid']] = $medal;
375 public static function fetchData(array $pids, Visibility
$visibility)
377 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
378 FROM profile_medals AS pm
379 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
380 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
381 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
382 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
383 WHERE pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0
384 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
385 $visibility->level(), $pids);
387 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
391 // {{{ class ProfileNetworking [ Field ]
392 class ProfileNetworking
extends ProfileField
394 private $networks = array();
396 public function __construct(PlInnerSubIterator
$it)
398 $this->pid
= $it->value();
399 while ($network = $it->next()) {
400 $network['network_type'] = new PlFlagSet($network['network_type']);
401 $this->networks
[$network['id']] = $network;
405 public static function fetchData(array $pids, Visibility
$visibility)
407 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
408 FROM profile_networking AS pn
409 LEFT JOIN profile_networking_enum AS pne USING(nwid)
410 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
411 WHERE pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0
412 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
414 $visibility->level(), $pids);
416 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
419 public function get($flags, $limit = null
)
423 foreach ($this->networks
as $id => $nw) {
424 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
425 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
426 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
427 ($flags == Profile
::NETWORKING_ALL
)) {
430 if (isset($limit) && $nb >= $limit) {
439 // {{{ class ProfileCorps [ Field ]
440 class ProfileCorps
extends ProfileField
445 public $original_name;
446 public $original_abbrev;
447 public $original_still_exists;
449 public $current_name;
450 public $current_abbrev;
451 public $current_still_exists;
452 public $current_rank;
453 public $current_rank_abbrev;
455 public function __construct(array $data)
457 foreach ($data as $key => $val) {
462 public static function fetchData(array $pids, Visibility
$visibility)
464 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
465 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
466 pceo.still_exists AS original_still_exists,
467 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
468 pcec.still_exists AS current_still_exists,
469 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
471 FROM profile_corps AS pc
472 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
473 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
474 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
475 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
476 WHERE pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1
477 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
478 $visibility->level(), $pids);
484 // {{{ class ProfileMentoringCountries [ Field ]
485 class ProfileMentoringCountries
extends ProfileField
487 public $countries = array();
489 public function __construct(PlInnerSubIterator
$it)
491 $this->pid
= $it->value();
492 while ($country = $it->next()) {
493 $this->countries
[$country['id']] = $country['name'];
497 public static function fetchData(array $pids, Visibility
$visibility)
499 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name
500 FROM profile_mentor_country AS pmc
501 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
503 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
506 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
510 // {{{ class ProfileAddresses [ Field ]
511 class ProfileAddresses
extends ProfileField
513 private $addresses = array();
515 public function __construct(PlInnerSubIterator
$it)
517 $this->pid
= $it->value();
518 while ($address = $it->next()) {
519 $this->addresses
[] = new Address($address);
523 public function get($flags, $limit = null
)
525 $addresses = array();
527 foreach ($this->addresses
as $address) {
528 if ((($flags & Profile
::ADDRESS_MAIN
) && $address->hasFlag('current'))
529 ||
(($flags & Profile
::ADDRESS_POSTAL
) && $address->hasFlag('mail'))
530 ||
(($flags & Profile
::ADDRESS_PERSO
) && $address->type
== Address
::LINK_PROFILE
)
531 ||
(($flags & Profile
::ADDRESS_PRO
) && $address->type
== Address
::LINK_JOB
)
533 $addresses[] = $address;
536 if ($limit != null
&& $nb == $limit) {
543 public static function fetchData(array $pids, Visibility
$visibility)
545 $it = Address
::iterate($pids, array(), array(), $visibility);
546 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
549 public function addPhones(ProfilePhones
$phones)
551 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
552 foreach ($p as $phone) {
553 /* We must iterate on the addresses because id is not uniq thus,
554 * $this->addresse[$phone->link_id] is invalid.
556 foreach ($this->addresses
as $address) {
557 if ($address->type
== Address
::LINK_PROFILE
&& $address->id
== $phone->link_id
) {
558 $address->addPhone($phone);
565 // {{{ class ProfilePhones [ Field ]
566 class ProfilePhones
extends ProfileField
568 private $phones = array();
570 public function __construct(PlInnerSubIterator
$it)
572 $this->pid
= $it->value();
573 while ($phone = $it->next()) {
574 $this->phones
[] = new Phone($phone);
578 public function get($flags, $limit = null
)
582 foreach ($this->phones
as $id => $phone) {
583 if ($phone->hasFlags($flags)) {
584 $phones[$id] = $phone;
586 if ($limit != null
&& $nb == $limit) {
594 public static function fetchData(array $pids, Visibility
$visibility)
596 $it = Phone
::iterate($pids, array(), array(), $visibility);
597 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
601 // {{{ class ProfileJobs [ Field ]
602 class ProfileJobs
extends ProfileField
604 private $jobs = array();
606 public function __construct(PlInnerSubIterator
$jobs)
608 $this->pid
= $jobs->value();
609 while ($job = $jobs->next()) {
610 $this->jobs
[$job['id']] = new Job($job);
614 public static function fetchData(array $pids, Visibility
$visibility)
616 CompanyList
::preload($pids);
617 $data = XDB
::iterator('SELECT id, pid, description, url as user_site, jobid, pub, IF(pve.best_display_level + 0 <= email_pub + 0, email, NULL) AS user_email, email_pub
619 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
620 WHERE pid IN {?} AND pve.best_display_level + 0 <= pub + 0
621 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ', id',
622 $visibility->level(), $pids);
623 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
626 public function get($flags, $limit = null
)
630 foreach ($this->jobs
as $id => $job) {
633 if ($limit != null
&& $nb >= $limit) {
640 public function addPhones(ProfilePhones
$phones)
642 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
643 foreach ($p as $phone) {
644 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
645 $this->jobs
[$phone->link_id
]->addPhone($phone);
650 public function addAddresses(ProfileAddresses
$addresses)
652 $a = $addresses->get(Profile
::ADDRESS_PRO
);
653 foreach ($a as $address) {
654 if ($address->type
== Address
::LINK_JOB
&& array_key_exists($address->id
, $this->jobs
)) {
655 $this->jobs
[$address->id
]->setAddress($address);
660 public function addCompanies(array $companies)
662 foreach ($this->jobs
as $job) {
663 $this->company
= $companies[$job->jobid
];
667 public function addJobTerms(ProfileJobTerms
$jobterms)
669 $terms = $jobterms->get();
670 foreach ($terms as $term) {
671 if ($this->pid
== $term->pid
&& array_key_exists($term->jid
, $this->jobs
)) {
672 $this->jobs
[$term->jid
]->addTerm($term);
678 // {{{ class ProfileJobTerms [ Field ]
679 class ProfileJobTerms
extends ProfileField
681 private $jobterms = array();
683 public function __construct(PlInnerSubIterator
$it)
685 $this->pid
= $it->value();
686 while ($term = $it->next()) {
687 $this->jobterms
[] = new JobTerm($term);
691 public function get()
693 return $this->jobterms
;
696 public static function fetchData(array $pids, Visibility
$visibility)
698 $data = XDB
::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
699 FROM profile_job_term AS jt
700 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
701 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
702 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
703 WHERE jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0
704 ORDER BY ' . XDB
::formatCustomOrder('jt.pid', $pids),
705 $visibility->level(), $pids);
706 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
710 // {{{ class ProfileMentoringTerms [ Field ]
711 class ProfileMentoringTerms
extends ProfileJobTerms
713 public static function fetchData(array $pids, Visibility
$visibility)
715 $data = XDB
::iterator('SELECT mt.jtid, jte.full_name, mt.pid
716 FROM profile_mentor_term AS mt
717 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
719 ORDER BY ' . XDB
::formatCustomOrder('mt.pid', $pids),
721 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
725 // {{{ class ProfilePartnerSharing [ Field ]
726 class ProfilePartnerSharing
extends ProfileField
728 public function __construct(PlInnerSubIterator
$it)
730 require_once 'partnersharing.inc.php';
732 $this->pid
= $it->value();
733 while ($partner_settings = $it->next()) {
734 $settings = new PartnerSettings($partner_settings);
735 $this->partners_settings
[$settings->partner
->id
] = $settings;
739 public static function fetchData(array $pids, Visibility
$visibility)
741 $data = XDB
::iterator('SELECT ppss.pid, ppss.exposed_uid, ppss.sharing_level,
742 ppss.allow_email, ppss.partner_id,
743 ppse.shortname AS partner_shortname,
744 ppse.name AS partner_name,
745 ppse.url AS partner_url
746 FROM profile_partnersharing_settings AS ppss
747 LEFT JOIN profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id)
748 WHERE ppss.pid IN {?}
749 ORDER BY ' . XDB
::formatCustomOrder('ppss.pid', $pids),
751 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
754 public function get($partner_id)
756 if (isset($this->partners_settings
[$partner_id])) {
757 return $this->partners_settings
[$partner_id];
759 return PartnerSettings
::getEmpty($partner_id);
764 // {{{ class CompanyList
767 static private $fullload = false
;
768 static private $companies = array();
770 static public function preload($pids = array())
772 if (self
::$fullload) {
777 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
778 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
784 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
785 pa.flags, pa.text, pa.type, pa.pub
786 FROM profile_job_enum AS pje
787 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
790 $newcompanies = array();
791 while ($row = $it->next()) {
792 $cp = new Company($row);
793 $addr = new Address($row);
794 $cp->setAddress($addr);
795 if (!array_key_exists($row['id'], self
::$companies)) {
796 $newcompanies[] = $row['id'];
798 self
::$companies[$row['id']] = $cp;
802 if (count($newcompanies)) {
803 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies, Visibility
::defaultForRead());
804 while ($phone = $it->next()) {
805 self
::$companies[$phone->link_id
]->setPhone($phone);
809 if (count($pids) == 0) {
810 self
::$fullload = true
;
814 static public function get($id)
816 if (!array_key_exists($id, self
::$companies)) {
819 if (isset(self
::$companies[$id])) {
820 return self
::$companies[$id];
827 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: