26d73a1b4cf22515c7ad1adfaec4a3cfbefdcaed
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 /** The profile to which this field belongs
32 /** Fetches data from the database for the given pids, compatible with
33 * the visibility context.
34 * @param $pids An array of pids
35 * @param $visibility The level of visibility fetched fields must have
36 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
37 * XXX MUST be reimplemented for each kind of ProfileField
39 public static function fetchData(array $pids, ProfileVisibility
$visibility)
41 return PlIteratorUtils
::emptyIterator();
44 public static function buildForPID($cls, $pid, ProfileVisibility
$visibility)
46 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
47 return array_pop($res);
50 /** Build a list of ProfileFields from a set of pids
51 * @param $cls The name of the field to create ('ProfileMedals', ...)
52 * @param $pids An array of pids
53 * @param $visibility An array of allowed visibility contexts
54 * @return An array of $pid => ProfileField
56 public static function buildFromPIDs($cls, array $pids, ProfileVisibility
$visibility)
58 $it = new ProfileFieldIterator($cls, $pids, $visibility);
60 while ($pf = $it->next()) {
66 public static function getForPID($cls, $pid, ProfileVisibility
$visibility)
68 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
74 // {{{ class ProfileFieldIterator
75 class ProfileFieldIterator
implements PlIterator
80 public function __construct($cls, array $pids, ProfileVisibility
$visibility)
82 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
86 public function next()
88 $d = $this->data
->next();
97 public function total()
99 return $this->data
->total();
102 public function first()
104 return $this->data
->first();
107 public function last()
109 return $this->data
->last();
117 const TYPE_FAX
= 'fax';
118 const TYPE_FIXED
= 'fixed';
119 const TYPE_MOBILE
= 'mobile';
124 public $comment = '';
126 const LINK_JOB
= 'job';
127 const LINK_ADDRESS
= 'address';
128 const LINK_PROFILE
= 'user';
129 const LINK_COMPANY
= 'hq';
134 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
136 public function __construct($data)
138 foreach ($data as $key => $val) {
151 public $phone = null
;
152 public $address = null
;
155 * $id, $name, $acronym, $url
157 public function __construct($data)
159 foreach ($data as $key => $val) {
164 public function setPhone(Phone
&$phone)
166 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
167 $this->phone
= $phone;
171 public function setAddress(Address
&$address)
173 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
174 $this->address
= $address;
186 public $company = null
;
187 private $phones = array();
188 private $address = null
;
198 public $subsubsector;
201 * pid, id, company_id, description, url, email
203 public function __construct($data)
205 foreach ($data as $key => $val) {
208 $this->company
= CompanyList
::get($this->jobid
);
211 public function phones()
213 return $this->phones
;
216 public function address()
218 return $this->address
;
221 public function addPhone(Phone
&$phone)
223 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
224 $this->phones
[] = $phone;
228 public function setAddress(Address
$address)
230 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
231 $this->address
= $address;
239 const LINK_JOB
= 'job';
240 const LINK_COMPANY
= 'hq';
241 const LINK_PROFILE
= 'home';
253 public $subAdministrativeArea;
254 public $administrativeArea;
259 private $phones = array();
262 * pîd, id, link_id, link_type, flags, text, postcode, country
264 public function __construct($data)
266 foreach ($data as $key => $val) {
269 $this->flags
= new PlFlagSet($this->flags
);
272 public function addPhone(Phone
&$phone)
274 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
275 $this->phones
[] = $phone;
279 public function phones()
281 return $this->phones
;
284 public function hasFlag($flag)
286 return $this->flags
->hasFlag($flag);
290 // {{{ class Education
302 public $school_short;
307 public $degree_short;
308 public $degree_level;
312 public function __construct(array $data)
314 foreach ($data as $key => $val) {
317 $this->flags
= new PlFlagSet($this->flags
);
322 // {{{ class ProfileEducation [ Field ]
323 class ProfileEducation
extends ProfileField
325 private $educations = array();
327 public function __construct(PlInnerSubIterator
$it)
329 $this->pid
= $it->value();
330 while ($edu = $it->next()) {
331 $this->educations
[$edu['id']] = new Education($edu);
335 public function get($flags, $limit)
337 $educations = array();
339 $year = $year['year'];
341 foreach ($this->educations
as $id => $edu) {
343 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
345 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
347 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
349 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
351 ($flags & Profile
::EDUCATION_ALL
)
353 $educations[$id] = $edu;
356 if ($limit != null
&& $nb >= $limit) {
363 public static function fetchData(array $pids, ProfileVisibility
$visibility)
365 $data = XDB
::iterator('SELECT pe.id, pe.pid,
366 pe.entry_year, pe.grad_year, pe.program, pe.flags,
367 pee.name AS school, pee.abbreviation AS school_short,
368 pee.url AS school_url, gc.countryFR AS country,
369 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
371 FROM profile_education AS pe
372 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
373 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
374 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
375 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
377 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
378 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
381 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
385 // {{{ class ProfileMedals [ Field ]
386 class ProfileMedals
extends ProfileField
388 public $medals = array();
390 public function __construct(PlInnerSubIterator
$it)
392 $this->pid
= $it->value();
393 while ($medal = $it->next()) {
394 $this->medals
[$medal['mid']] = $medal;
398 public static function fetchData(array $pids, ProfileVisibility
$visibility)
400 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img
401 FROM profile_medals AS pm
402 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
403 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
404 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
405 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
406 $pids, $visibility->levels());
408 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
412 // {{{ class ProfileNetworking [ Field ]
413 class ProfileNetworking
extends ProfileField
415 private $networks = array();
417 public function __construct(PlIterator
$it)
419 while ($network = $it->next()) {
420 $this->networks
[$network['nwid']] = $network['address'];
424 public static function fetchData(array $pids, ProfileVisibility
$visibility)
426 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
427 FROM profile_networking
428 WHERE pid IN {?} AND pub IN {?}
429 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
431 $pids, $visibility->levels());
433 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
436 public function get($flags, $limit = null
)
440 foreach ($this->networks
as $id => $nw) {
441 // XXX hardcoded reference to web site index
443 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
445 (! ($flags & self
::NETWORKING_WEB
))
458 // {{{ class ProfileCorps [ Field ]
459 class ProfileCorps
extends ProfileField
464 public $original_name;
465 public $original_abbrev;
466 public $original_still_exists;
468 public $current_name;
469 public $current_abbrev;
470 public $current_still_exists;
471 public $current_rank;
472 public $current_rank_abbrev;
474 public function __construct(array $data)
476 foreach ($data as $key => $val) {
481 public static function fetchData(array $pids, ProfileVisibility
$visibility)
483 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
484 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
485 pceo.still_exists AS original_still_exists,
486 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
487 pcec.still_exists AS current_still_exists,
488 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
490 FROM profile_corps AS pc
491 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
492 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
493 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
494 WHERE pc.pid IN {?} AND pc.corps_pub IN {?}
495 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
496 $pids, $visibility->levels());
503 /** Loading of data for a Profile :
504 * 1) load jobs, addresses, phones
505 * 2) attach phones to addresses, jobs and profiles
506 * 3) attach addresses to jobs and profiles
509 // {{{ class ProfileAddresses [ Field ]
510 class ProfileAddresses
extends ProfileField
512 private $addresses = array();
514 public function __construct(PlIterator
$it)
516 if ($it instanceof PlInnerSubIterator
) {
517 $this->pid
= $it->value();
520 while ($addr = $it->next()) {
521 $this->addresses
[] = new Address($addr);
525 public function get($flags, $limit = null
)
529 foreach ($this->addresses
as $addr) {
531 ($flags & Profile
::ADDRESS_ALL
)
533 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
535 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
537 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== 'home')
539 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== 'job')
544 if ($limit != null
&& $nb == $limit) {
551 public static function fetchData(array $pids, ProfileVisibility
$visibility)
553 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
554 IF(pa.type = \'home\', pid, jobid) AS link_id,
555 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
556 gl.name AS locality, gas.name AS subAdministrativeArea,
557 ga.name AS administrativeArea, gc.countryFR AS country
558 FROM profile_addresses AS pa
559 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
560 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
561 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
562 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
563 WHERE pa.pid in {?} AND pa.pub IN {?}
564 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
565 $pids, $visibility->levels());
567 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
570 public function addPhones(ProfilePhones
$phones)
572 $p = $phones->get(0);
573 foreach ($p as $phone) {
574 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
575 $this->addresses
[$phone->link_id
]->addPhone($phone);
581 // {{{ class ProfilePhones [ Field ]
582 class ProfilePhones
extends ProfileField
584 private $phones = array();
586 public function __construct(PlInnerSubIterator
$it)
588 $this->pid
= $it->value();
589 while ($phone = $it->next()) {
590 $this->phones
[] = new Phone($phone);
594 public function get($flags, $limit = null
)
598 foreach ($this->phones
as $id => $phone) {
599 $phones[$id] = $phone;
601 if ($limit != null
&& $nb == $limit) {
608 public static function fetchData(array $pids, ProfileVisibility
$visibility)
610 $data = XDB
::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment
612 WHERE pid IN {?} AND pub IN {?}
613 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
614 $pids, $visibility->levels());
615 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
619 // {{{ class ProfileJobs [ Field ]
620 class ProfileJobs
extends ProfileField
622 private $jobs = array();
624 public function __construct(PlInnerSubIterator
$jobs)
626 $this->pid
= $jobs->value();
627 while ($job = $jobs->next()) {
628 $this->jobs
[$job['id']] = new Job($job);
632 public static function fetchData(array $pids, ProfileVisibility
$visibility)
634 CompanyList
::preload($pids);
635 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
636 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
637 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
638 pjssse.name AS subsubsector
639 FROM profile_job AS pj
640 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
641 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
642 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
643 WHERE pj.pid IN {?} AND pj.pub IN {?}
644 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
646 $visibility->levels(), $pids, $visibility->levels());
647 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
650 public function get($flags, $limit = null
)
654 foreach ($this->jobs
as $id => $job) {
657 if ($limit != null
&& $nb >= $limit) {
664 public function addPhones(ProfilePhones
$phones)
666 $p = $phones->get(0);
667 foreach ($p as $phone) {
668 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
669 $this->jobs
[$phone->link_id
]->addPhones($phone);
674 public static function addAddresses(ProfileAddresses
$addresses)
676 $a = $addresses->get(Profile
::ADDRESS_PRO
);
677 foreach ($a as $address) {
678 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
679 $this->jobs
[$address->link_id
]->setAddress($address);
684 public static function addCompanies(array $companies)
686 foreach ($this->jobs
as $job)
688 $job->company
= $companies[$job->jobid
];
694 // {{{ class CompanyList
697 static private $fullload = false
;
698 static private $companies = array();
700 static public function preload($pids = array())
702 if (self
::$fullload) {
707 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
708 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
714 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
715 pa.flags, pa.text, pa.postalCode, pa.countryId,
717 FROM profile_job_enum AS pje
718 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
721 while ($row = $it->next()) {
722 $cp = new Company($row);
723 $addr = new Address($row);
724 $cp->setAddress($addr);
725 self
::$companies[$row['id']] = $cp;
728 // TODO: add phones to addresses
729 if (count($pids) == 0) {
730 self
::$fullload = true
;
734 static public function get($id)
736 if (!array_key_exists($id, self
::$companies)) {
739 return self
::$companies[$id];
743 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: