5641072419baaa5453c45be06275997fb24c966c
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, $visibility)
41 return PlIteratorUtils
::emptyIterator();
44 public static function buildForPID($cls, $pid, $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, $visibility)
58 $it = new ProfileFieldIterator($cls, $pids, $visibility);
60 while ($pf = $it->next()) {
66 public static function getForPID($cls, $pid, $visibility)
68 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
74 // {{{ class ProfileFieldIterator
75 class ProfileFieldIterator
implements PlIterator
80 public function __construct($cls, array $pids, $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 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
331 while ($edu = $it->next()) {
332 $this->educations
[$edu['id']] = new Education($edu);
336 public function get($flags, $limit)
338 $educations = array();
340 $year = $year['year'];
342 foreach ($this->educations
as $id => $edu) {
344 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
346 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
348 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
350 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
352 ($flags & Profile
::EDUCATION_ALL
)
354 $educations[$id] = $edu;
357 if ($limit != null
&& $nb >= $limit) {
364 public static function fetchData(array $pids, $visibility)
366 $data = XDB
::iterator('SELECT pe.id, pe.pid,
367 pe.entry_year, pe.grad_year, pe.program, pe.flags,
368 pee.name AS school, pee.abbreviation AS school_short,
369 pee.url AS school_url, gc.countryFR AS country,
370 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
372 FROM profile_education AS pe
373 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
374 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
375 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
376 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
378 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
379 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
382 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
386 // {{{ class ProfileMedals [ Field ]
387 class ProfileMedals
extends ProfileField
389 public $medals = array();
391 public function __construct(PlInnerSubIterator
$it)
393 $this->pid
= $it->value();
394 while ($medal = $it->next()) {
395 $this->medals
[$medal['mid']] = $medal;
399 public static function fetchData(array $pids, $visibility)
401 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img
402 FROM profile_medals AS pm
403 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
404 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
405 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
406 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
409 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
413 // {{{ class ProfileNetworking [ Field ]
414 class ProfileNetworking
extends ProfileField
416 private $networks = array();
418 public function __construct(PlIterator
$it)
420 while ($network = $it->next()) {
421 $this->networks
[$network['nwid']] = $network['address'];
425 public static function fetchData(array $pids, $visibility)
427 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
428 FROM profile_networking
429 WHERE pid IN {?} AND pub IN {?}
430 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
434 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
437 public function get($flags, $limit = null
)
441 foreach ($this->networks
as $id => $nw) {
442 // XXX hardcoded reference to web site index
444 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
446 (! ($flags & self
::NETWORKING_WEB
))
459 // {{{ class ProfileCorps [ Field ]
460 class ProfileCorps
extends ProfileField
465 public $original_name;
466 public $original_abbrev;
467 public $original_still_exists;
469 public $current_name;
470 public $current_abbrev;
471 public $current_still_exists;
472 public $current_rank;
473 public $current_rank_abbrev;
475 public function __construct(array $data)
477 foreach ($data as $key => $val) {
482 public static function fetchData(array $pids, $visibility)
484 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
485 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
486 pceo.still_exists AS original_still_exists,
487 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
488 pcec.still_exists AS current_still_exists,
489 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
491 FROM profile_corps AS pc
492 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
493 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
494 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
495 WHERE pc.pid IN {?} AND pc.corps_pub IN {?}
496 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
504 /** Loading of data for a Profile :
505 * 1) load jobs, addresses, phones
506 * 2) attach phones to addresses, jobs and profiles
507 * 3) attach addresses to jobs and profiles
510 // {{{ class ProfileAddresses [ Field ]
511 class ProfileAddresses
extends ProfileField
513 private $addresses = array();
515 public function __construct(PlIterator
$it)
517 if ($it instanceof PlInnerSubIterator
) {
518 $this->pid
= $it->value();
521 while ($addr = $it->next()) {
522 $this->addresses
[] = new Address($addr);
526 public function get($flags, $limit = null
)
530 foreach ($this->addresses
as $addr) {
532 ($flags & Profile
::ADDRESS_ALL
)
534 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
536 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
538 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== 'home')
540 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== 'job')
545 if ($limit != null
&& $nb == $limit) {
552 public static function fetchData(array $pids, $visibility)
554 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
555 IF(pa.type = \'home\', pid, jobid) AS link_id,
556 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
557 gl.name AS locality, gas.name AS subAdministrativeArea,
558 ga.name AS administrativeArea, gc.countryFR AS country
559 FROM profile_addresses AS pa
560 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
561 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
562 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
563 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
564 WHERE pa.pid in {?} AND pa.pub IN {?}
565 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
568 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
571 public function addPhones(ProfilePhones
$phones)
573 $p = $phones->get(0);
574 foreach ($p as $phone) {
575 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
576 $this->addresses
[$phone->link_id
]->addPhone($phone);
582 // {{{ class ProfilePhones [ Field ]
583 class ProfilePhones
extends ProfileField
585 private $phones = array();
587 public function __construct(PlInnerSubIterator
$it)
589 $this->pid
= $it->value();
590 while ($phone = $it->next()) {
591 $this->phones
[] = new Phone($phone);
595 public function get($flags, $limit = null
)
599 foreach ($this->phones
as $id => $phone) {
600 $phones[$id] = $phone;
602 if ($limit != null
&& $nb == $limit) {
609 public static function fetchData(array $pids, $visibility)
611 $data = XDB
::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment
613 WHERE pid IN {?} AND pub IN {?}
614 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
616 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
620 // {{{ class ProfileJobs [ Field ]
621 class ProfileJobs
extends ProfileField
623 private $jobs = array();
625 public function __construct(PlInnerSubIterator
$jobs)
627 $this->pid
= $jobs->value();
628 while ($job = $jobs->next()) {
629 $this->jobs
[$job['id']] = new Job($job);
633 public static function fetchData(array $pids, $visibility)
635 CompanyList
::preload($pids);
636 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
637 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
638 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
639 pjssse.name AS subsubsector
640 FROM profile_job AS pj
641 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
642 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
643 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
644 WHERE pj.pid IN {?} AND pj.pub IN {?}
645 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
647 $visibility, $pids, $visibility);
648 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
651 public function get($flags, $limit = null
)
655 foreach ($this->jobs
as $id => $job) {
658 if ($limit != null
&& $nb >= $limit) {
665 public function addPhones(ProfilePhones
$phones)
667 $p = $phones->get(0);
668 foreach ($p as $phone) {
669 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
670 $this->jobs
[$phone->link_id
]->addPhones($phone);
675 public static function addAddresses(ProfileAddresses
$addresses)
677 $a = $addresses->get(Profile
::ADDRESS_PRO
);
678 foreach ($a as $address) {
679 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
680 $this->jobs
[$address->link_id
]->setAddress($address);
685 public static function addCompanies(array $companies)
687 foreach ($this->jobs
as $job)
689 $job->company
= $companies[$job->jobid
];
695 // {{{ class CompanyList
698 static private $fullload = false
;
699 static private $companies = array();
701 static public function preload($pids = array())
703 if (self
::$fullload) {
708 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
709 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
715 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
716 pa.flags, pa.text, pa.postalCode, pa.countryId,
718 FROM profile_job_enum AS pje
719 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
722 while ($row = $it->next()) {
723 $cp = new Company($row);
724 $addr = new Address($row);
725 $cp->setAddress($addr);
726 self
::$companies[$row['id']] = $cp;
729 // TODO: add phones to addresses
730 if (count($pids) == 0) {
731 self
::$fullload = true
;
735 static public function get($id)
737 if (!array_key_exists($id, self
::$companies)) {
740 return self
::$companies[$id];
744 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: