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',
41 /** The profile to which this field belongs
45 /** Fetches data from the database for the given pids, compatible with
46 * the visibility context.
47 * @param $pids An array of pids
48 * @param $visibility The level of visibility fetched fields must have
49 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
51 * MUST be reimplemented for each kind of ProfileField.
53 public static function fetchData(array $pids, ProfileVisibility
$visibility)
55 return PlIteratorUtils
::emptyIterator();
58 public static function buildForPID($cls, $pid, ProfileVisibility
$visibility)
60 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
61 return array_pop($res);
64 /** Build a list of ProfileFields from a set of pids
65 * @param $cls The name of the field to create ('ProfileMedals', ...)
66 * @param $pids An array of pids
67 * @param $visibility An array of allowed visibility contexts
68 * @return An array of $pid => ProfileField
70 public static function buildFromPIDs($cls, array $pids, ProfileVisibility
$visibility)
72 $it = new ProfileFieldIterator($cls, $pids, $visibility);
74 while ($pf = $it->next()) {
80 public static function getForPID($cls, $pid, ProfileVisibility
$visibility)
82 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
88 // {{{ class ProfileFieldIterator
89 class ProfileFieldIterator
implements PlIterator
94 public function __construct($cls, array $pids, ProfileVisibility
$visibility)
96 if (is_numeric($cls) && isset(ProfileField
::$fields[$cls])) {
97 $cls = ProfileField
::$fields[$cls];
99 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
103 public function next()
105 $d = $this->data
->next();
114 public function total()
116 return $this->data
->total();
119 public function first()
121 return $this->data
->first();
124 public function last()
126 return $this->data
->last();
138 public $phone = null
;
139 public $address = null
;
142 * $id, $name, $acronym, $url
144 public function __construct($data)
146 foreach ($data as $key => $val) {
151 public function setPhone(Phone
$phone)
153 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
154 $this->phone
= $phone;
158 public function setAddress(Address
$address)
160 if ($address->type
== Address
::LINK_COMPANY
&& $address->jobid
== $this->id
) {
161 $this->address
= $address;
168 /** profile_job describes a Job, links to:
169 * - a Profile, through `pid`
170 * - a Company, through `jobid`
171 * The `id` field is the id of this job in the list of the jobs of its profile
173 * For the documentation of the phone table, please see classes/phone.php.
174 * For the documentation of the address table, please see classes/address.php.
176 * The possible relations are as follow:
177 * A Job is linked to a Company and a Profile
184 public $company = null
;
185 public $phones = array();
186 public $address = null
;
187 public $terms = array();
196 * pid, id, company_id, description, url, email
198 public function __construct($data)
200 foreach ($data as $key => $val) {
203 $this->company
= CompanyList
::get($this->jobid
);
204 if (is_null($this->company
)) {
205 $entreprises = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
206 foreach ($entreprises as $entreprise) {
207 if ($entreprise->id
== $this->id
) {
208 $this->company
= new Company(array('name' => $entreprise->name
));
214 public function phones()
216 return $this->phones
;
219 public function address()
221 return $this->address
;
224 public function addPhone(Phone
$phone)
226 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
227 $this->phones
[$phone->uniqueId()] = $phone;
231 public function setAddress(Address
$address)
233 if ($address->type
== Address
::LINK_JOB
&& $address->id
== $this->id
&& $address->pid
== $this->pid
) {
234 $this->address
= $address;
238 public function addTerm(JobTerm
$term)
240 $this->terms
[$term->jtid
] = $term;
253 * pid, jid, jtid, full_name
255 public function __construct($data)
257 foreach ($data as $key => $val) {
263 // {{{ class Education
275 public $school_short;
280 public $degree_short;
281 public $degree_level;
285 public function __construct(array $data)
287 foreach ($data as $key => $val) {
290 $this->flags
= new PlFlagSet($this->flags
);
295 // {{{ class ProfileEducation [ Field ]
296 class ProfileEducation
extends ProfileField
298 private $educations = array();
300 public function __construct(PlInnerSubIterator
$it)
302 $this->pid
= $it->value();
303 while ($edu = $it->next()) {
304 $this->educations
[$edu['id']] = new Education($edu);
308 public function get($flags, $limit)
310 $educations = array();
312 $year = $year['year'];
314 foreach ($this->educations
as $id => $edu) {
316 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
318 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
320 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
322 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
324 ($flags & Profile
::EDUCATION_ALL
)
326 $educations[$id] = $edu;
329 if ($limit != null
&& $nb >= $limit) {
336 public static function fetchData(array $pids, ProfileVisibility
$visibility)
338 $data = XDB
::iterator('SELECT pe.id, pe.pid,
339 pe.entry_year, pe.grad_year, pe.program, pe.flags,
340 pee.name AS school, pee.abbreviation AS school_short,
341 pee.url AS school_url, gc.country,
342 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
344 FROM profile_education AS pe
345 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
346 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
347 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
348 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
350 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
351 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
354 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
358 // {{{ class ProfileMedals [ Field ]
359 class ProfileMedals
extends ProfileField
361 public $medals = array();
363 public function __construct(PlInnerSubIterator
$it)
365 $this->pid
= $it->value();
366 while ($medal = $it->next()) {
367 $this->medals
[$medal['mid']] = $medal;
371 public static function fetchData(array $pids, ProfileVisibility
$visibility)
373 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
374 FROM profile_medals AS pm
375 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
376 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
377 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
378 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
379 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
380 $pids, $visibility->levels());
382 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
386 // {{{ class ProfileNetworking [ Field ]
387 class ProfileNetworking
extends ProfileField
389 private $networks = array();
391 public function __construct(PlInnerSubIterator
$it)
393 $this->pid
= $it->value();
394 while ($network = $it->next()) {
395 $network['network_type'] = new PlFlagSet($network['network_type']);
396 $this->networks
[$network['id']] = $network;
400 public static function fetchData(array $pids, ProfileVisibility
$visibility)
402 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
403 FROM profile_networking AS pn
404 LEFT JOIN profile_networking_enum AS pne USING(nwid)
405 WHERE pid IN {?} AND pub IN {?}
406 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
408 $pids, $visibility->levels());
410 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
413 public function get($flags, $limit = null
)
417 foreach ($this->networks
as $id => $nw) {
418 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
419 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
420 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
421 ($flags == Profile
::NETWORKING_ALL
)) {
424 if (isset($limit) && $nb >= $limit) {
433 // {{{ class ProfileCorps [ Field ]
434 class ProfileCorps
extends ProfileField
439 public $original_name;
440 public $original_abbrev;
441 public $original_still_exists;
443 public $current_name;
444 public $current_abbrev;
445 public $current_still_exists;
446 public $current_rank;
447 public $current_rank_abbrev;
449 public function __construct(array $data)
451 foreach ($data as $key => $val) {
456 public static function fetchData(array $pids, ProfileVisibility
$visibility)
458 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
459 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
460 pceo.still_exists AS original_still_exists,
461 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
462 pcec.still_exists AS current_still_exists,
463 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
465 FROM profile_corps AS pc
466 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
467 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
468 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
469 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
470 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
471 $pids, $visibility->levels());
477 // {{{ class ProfileMentoringCountries [ Field ]
478 class ProfileMentoringCountries
extends ProfileField
480 public $countries = array();
482 public function __construct(PlInnerSubIterator
$it)
484 $this->pid
= $it->value();
485 while ($country = $it->next()) {
486 $this->countries
[$country['id']] = $country['name'];
490 public static function fetchData(array $pids, ProfileVisibility
$visibility)
492 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name
493 FROM profile_mentor_country AS pmc
494 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
496 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
499 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
503 // {{{ class ProfileAddresses [ Field ]
504 class ProfileAddresses
extends ProfileField
506 private $addresses = array();
508 public function __construct(PlInnerSubIterator
$it)
510 $this->pid
= $it->value();
511 while ($address = $it->next()) {
512 $this->addresses
[] = new Address($address);
516 public function get($flags, $limit = null
)
518 $addresses = array();
520 foreach ($this->addresses
as $address) {
521 if ((($flags & Profile
::ADDRESS_MAIN
) && $address->hasFlag('current'))
522 ||
(($flags & Profile
::ADDRESS_POSTAL
) && $address->hasFlag('mail'))
523 ||
(($flags & Profile
::ADDRESS_PERSO
) && $address->type
== Address
::LINK_PROFILE
)
524 ||
(($flags & Profile
::ADDRESS_PRO
) && $address->type
== Address
::LINK_JOB
)
526 $addresses[] = $address;
529 if ($limit != null
&& $nb == $limit) {
536 public static function fetchData(array $pids, ProfileVisibility
$visibility)
538 $it = Address
::iterate($pids, array(), array(), $visibility->levels());
539 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
542 public function addPhones(ProfilePhones
$phones)
544 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
545 foreach ($p as $phone) {
546 /* We must iterate on the addresses because id is not uniq thus,
547 * $this->addresse[$phone->link_id] is invalid.
549 foreach ($this->addresses
as $address) {
550 if ($address->type
== Address
::LINK_PROFILE
&& $address->id
== $phone->link_id
) {
551 $address->addPhone($phone);
558 // {{{ class ProfilePhones [ Field ]
559 class ProfilePhones
extends ProfileField
561 private $phones = array();
563 public function __construct(PlInnerSubIterator
$it)
565 $this->pid
= $it->value();
566 while ($phone = $it->next()) {
567 $this->phones
[] = new Phone($phone);
571 public function get($flags, $limit = null
)
575 foreach ($this->phones
as $id => $phone) {
576 if ($phone->hasFlags($flags)) {
577 $phones[$id] = $phone;
579 if ($limit != null
&& $nb == $limit) {
587 public static function fetchData(array $pids, ProfileVisibility
$visibility)
589 $it = Phone
::iterate($pids, array(), array(), $visibility->levels());
590 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
594 // {{{ class ProfileJobs [ Field ]
595 class ProfileJobs
extends ProfileField
597 private $jobs = array();
599 public function __construct(PlInnerSubIterator
$jobs)
601 $this->pid
= $jobs->value();
602 while ($job = $jobs->next()) {
603 $this->jobs
[$job['id']] = new Job($job);
607 public static function fetchData(array $pids, ProfileVisibility
$visibility)
609 CompanyList
::preload($pids);
610 $data = XDB
::iterator('SELECT id, pid, description, url as user_site, jobid,
611 IF(email_pub IN {?}, email, NULL) AS user_email
613 WHERE pid IN {?} AND pub IN {?}
614 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ', id',
615 $visibility->levels(), $pids, $visibility->levels());
616 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
619 public function get($flags, $limit = null
)
623 foreach ($this->jobs
as $id => $job) {
626 if ($limit != null
&& $nb >= $limit) {
633 public function addPhones(ProfilePhones
$phones)
635 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
636 foreach ($p as $phone) {
637 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
638 $this->jobs
[$phone->link_id
]->addPhone($phone);
643 public function addAddresses(ProfileAddresses
$addresses)
645 $a = $addresses->get(Profile
::ADDRESS_PRO
);
646 foreach ($a as $address) {
647 if ($address->type
== Address
::LINK_JOB
&& array_key_exists($address->id
, $this->jobs
)) {
648 $this->jobs
[$address->id
]->setAddress($address);
653 public function addCompanies(array $companies)
655 foreach ($this->jobs
as $job) {
656 $this->company
= $companies[$job->jobid
];
660 public function addJobTerms(ProfileJobTerms
$jobterms)
662 $terms = $jobterms->get();
663 foreach ($terms as $term) {
664 if ($this->pid
== $term->pid
&& array_key_exists($term->jid
, $this->jobs
)) {
665 $this->jobs
[$term->jid
]->addTerm($term);
671 // {{{ class ProfileJobTerms [ Field ]
672 class ProfileJobTerms
extends ProfileField
674 private $jobterms = array();
676 public function __construct(PlInnerSubIterator
$it)
678 $this->pid
= $it->value();
679 while ($term = $it->next()) {
680 $this->jobterms
[] = new JobTerm($term);
684 public function get()
686 return $this->jobterms
;
689 public static function fetchData(array $pids, ProfileVisibility
$visibility)
691 $data = XDB
::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
692 FROM profile_job_term AS jt
693 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
694 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
695 WHERE jt.pid IN {?} AND j.pub IN {?}
696 ORDER BY ' . XDB
::formatCustomOrder('jt.pid', $pids),
697 $pids, $visibility->levels());
698 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
702 // {{{ class ProfileMentoringTerms [ Field ]
703 class ProfileMentoringTerms
extends ProfileJobTerms
705 public static function fetchData(array $pids, ProfileVisibility
$visibility)
707 $data = XDB
::iterator('SELECT mt.jtid, jte.full_name, mt.pid
708 FROM profile_mentor_term AS mt
709 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
711 ORDER BY ' . XDB
::formatCustomOrder('mt.pid', $pids),
713 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
717 // {{{ class CompanyList
720 static private $fullload = false
;
721 static private $companies = array();
723 static public function preload($pids = array())
725 if (self
::$fullload) {
730 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
731 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
737 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
738 pa.flags, pa.text, pa.postalCode, pa.countryId,
740 FROM profile_job_enum AS pje
741 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
744 $newcompanies = array();
745 while ($row = $it->next()) {
746 $cp = new Company($row);
747 $addr = new Address($row);
748 $cp->setAddress($addr);
749 if (!array_key_exists($row['id'], self
::$companies)) {
750 $newcompanies[] = $row['id'];
752 self
::$companies[$row['id']] = $cp;
756 if (count($newcompanies)) {
757 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies);
758 while ($phone = $it->next()) {
759 self
::$companies[$phone->link_id
]->setPhone($phone);
763 if (count($pids) == 0) {
764 self
::$fullload = true
;
768 static public function get($id)
770 if (!array_key_exists($id, self
::$companies)) {
773 if (isset(self
::$companies[$id])) {
774 return self
::$companies[$id];
780 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: