8293d21e0a6cf46882dfbcbd1f7cacf3ad082ccf
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 addPhone(Phone
&$phone)
218 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
219 $this->phones
[] = $phone;
223 public function setAddress(Address
$address)
225 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
226 $this->address
= $address;
234 const LINK_JOB
= 'job';
235 const LINK_COMPANY
= 'hq';
236 const LINK_PROFILE
= 'home';
248 public $subAdministrativeArea;
249 public $administrativeArea;
252 private $phones = array();
255 * pîd, id, link_id, link_type, flags, text, postcode, country
257 public function __construct($data)
259 foreach ($data as $key => $val) {
264 public function addPhone(Phone
&$phone)
266 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
267 $this->phones
[] = $phone;
271 public function phones()
273 return $this->phones
;
276 public function hasFlags($flags)
278 return $flags & $this->flags
;
282 // {{{ class Education
294 public $school_short;
299 public $degree_short;
300 public $degree_level;
302 public function __construct(array $data)
304 foreach ($data as $key => $val) {
307 $this->flags
= new PlFlagSet($this->flags
);
312 // {{{ class ProfileEducation [ Field ]
313 class ProfileEducation
extends ProfileField
315 private $educations = array();
317 public function __construct(PlInnerSubIterator
$it)
319 $this->pid
= $it->value();
320 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
321 while ($edu = $it->next()) {
322 $this->educations
[$edu['id']] = new Education($edu);
326 public function get($flags, $limit)
328 $educations = array();
330 $year = $year['year'];
332 foreach ($this->educations
as $id => $edu) {
334 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
336 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
338 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
340 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
342 $educations[$id] = $edu;
345 if ($limit != null
&& $nb >= $limit) {
349 return PlIteratorUtils
::fromArray($educations, 1, true
);
352 public static function fetchData(array $pids, $visibility)
354 $data = XDB
::iterator('SELECT pe.id, pe.pid,
355 pe.entry_year, pe.grad_year, pe.program, pe.flags,
356 pee.name AS school, pee.abbreviation AS school_short,
357 pee.url AS school_url, gc.countryFR AS country,
358 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
360 FROM profile_education AS pe
361 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
362 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
363 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
364 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
366 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
367 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
370 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
374 // {{{ class ProfileMedals [ Field ]
375 class ProfileMedals
extends ProfileField
377 public $medals = array();
379 public function __construct(PlIterator
$it)
381 while ($medal = $it->next()) {
382 $this->medals
[$medal['mid']] = $medal['gid'];
386 public static function fetchData(array $pids, $visibility)
388 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
389 FROM profile_medals AS pm
390 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
391 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
392 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
393 XDB
::formatArray($pids),
394 XDB
::formatArray($visibility)
397 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
401 // {{{ class ProfileNetworking [ Field ]
402 class ProfileNetworking
extends ProfileField
404 private $networks = array();
406 public function __construct(PlIterator
$it)
408 while ($network = $it->next()) {
409 $this->networks
[$network['nwid']] = $network['address'];
413 public static function fetchData(array $pids, $visibility)
415 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
416 FROM profile_networking
417 WHERE pid IN {?} AND pub IN {?}
418 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
422 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
425 public function get($flags, $limit = null
)
429 foreach ($this->networks
as $id => $nw) {
430 // XXX hardcoded reference to web site index
432 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
434 (! ($flags & self
::NETWORKING_WEB
))
443 return PlIteratorUtils
::fromArray($nws, 1, true
);
447 // {{{ class ProfilePhoto [ Field ]
448 class ProfilePhoto
extends ProfileField
452 public function __construct(array $data)
454 if ($data == null ||
count($data) == 0) {
457 $this->pid
= $data['pid'];
458 $this->pic
= PlImage
::fromDATA($data['attach'],
465 public static function fetchData(array $pids, $visibility)
467 $data = XDB
::iterator('SELECT *
469 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
470 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
477 // {{{ class ProfileCorps [ Field ]
478 class ProfileCorps
extends ProfileField
484 public function __construct(array $data)
486 $this->original
= $data['original_corpsid'];
487 $this->current
= $data['current_corpsid'];
488 $this->rank
= $data['rankid'];
489 $this->visibility
= $data['corps_pub'];
492 public static function fetchData(array $pids, $visibility)
494 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
497 WHERE pid IN {?} AND corps_pub IN {?}
498 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
499 XDB
::formatArray($pids),
500 XDB
::formatArray($visibility)
508 /** Loading of data for a Profile :
509 * 1) load jobs, addresses, phones
510 * 2) attach phones to addresses, jobs and profiles
511 * 3) attach addresses to jobs and profiles
514 // {{{ class ProfileAddresses [ Field ]
515 class ProfileAddresses
extends ProfileField
517 private $addresses = array();
519 public function __construct(PlIterator
$it)
521 if ($it instanceof PlInnerSubIterator
) {
522 $this->pid
= $it->value();
525 while ($addr = $it->next()) {
526 $this->addresses
[] = new Address($addr);
530 public function get($flags, $limit = null
)
534 foreach ($this->addresses
as $addr) {
535 if ($addr->hasFlags($flags)) {
539 if ($limit != null
&& $nb == $limit) {
543 return PlIteratorUtils
::fromArray($res, 1, true
);
546 public static function fetchData(array $pids, $visibility)
548 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
549 IF(pa.type = \'home\', pid, jobid) AS link_id,
550 pa.text, pa.postalCode, pa.latitude, pa.longitude,
551 gl.name AS locality, gas.name AS subAdministrativeArea,
552 ga.name AS administrativeArea, gc.countryFR AS country
553 FROM profile_addresses AS pa
554 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
555 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
556 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
557 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
558 WHERE pa.pid in {?} AND pa.pub IN {?}
559 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
562 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
565 public function addPhones(ProfilePhones
$phones)
567 $p = $phones->get(0);
568 while ($phone = $p->next()) {
569 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
570 $this->addresses
[$phone->link_id
]->addPhone($phone);
576 // {{{ class ProfilePhones [ Field ]
577 class ProfilePhones
extends ProfileField
579 private $phones = array();
581 public function __construct(PlIterator
$phones)
583 while ($phone = $it->next()) {
584 $this->phones
[] = Phone
::buildFromData($phone);
588 public function get($flags, $limit = null
)
592 foreach ($this->phones
as $id => $phone) {
593 $phones[$id] = $phone;
595 if ($limit != null
&& $nb == $limit) {
599 return PlIteratorUtils
::fromArray($phones, 1, true
);
602 public static function fetchData(array $pids, $visibility)
604 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
606 WHERE pid IN {?} AND pub IN {?}
607 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
608 XDB
::formatArray($pids),
609 XDB
::formatArray($visibility)
611 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
615 // {{{ class ProfileJobs [ Field ]
616 class ProfileJobs
extends ProfileField
618 private $jobs = array();
620 public function __construct(PlInnerSubIterator
$jobs)
622 $this->pid
= $jobs->value();
623 while ($job = $jobs->next()) {
624 $this->jobs
[$job['id']] = new Job($job);
628 public static function fetchData(array $pids, $visibility)
630 CompanyList
::preload($pids);
631 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
632 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
633 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
634 pjssse.name AS subsubsector
635 FROM profile_job AS pj
636 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
637 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
638 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
639 WHERE pj.pid IN {?} AND pj.pub IN {?}
640 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
642 $visibility, $pids, $visibility);
643 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
646 public function get($flags, $limit = null
)
650 foreach ($this->jobs
as $id => $job) {
653 if ($limit != null
&& $nb >= $limit) {
657 return PlIteratorUtils
::fromArray($jobs, 1, true
);
660 public function addPhones(ProfilePhones
$phones)
662 $p = $phones->get(0);
663 while ($phone = $p->next()) {
664 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
665 $this->jobs
[$phone->link_id
]->addPhones($phone);
670 public static function addAddresses(ProfileAddresses
$addresses)
672 $a = $addresses->get(Profile
::ADDRESS_PRO
);
673 while ($address = $a->next()) {
674 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
675 $this->jobs
[$address->link_id
]->setAddress($address);
680 public static function addCompanies(array $companies)
682 foreach ($this->jobs
as $job)
684 $job->company
= $companies[$job->jobid
];
690 // {{{ class CompanyList
693 static private $fullload = false
;
694 static private $companies = array();
696 static public function preload($pids = array())
698 if (self
::$fullload) {
703 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
704 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
710 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
711 pa.flags, pa.text, pa.postalCode, pa.countryId,
713 FROM profile_job_enum AS pje
714 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
717 while ($row = $it->next()) {
718 $cp = new Company($row);
719 $addr = new Address($row);
720 $cp->setAddress($addr);
721 self
::$companies[$row['id']] = $cp;
724 // TODO: add phones to addresses
725 if (count($pids) == 0) {
726 self
::$fullload = true
;
730 static public function get($id)
732 if (!array_key_exists($id, self
::$companies)) {
735 return self
::$companies[$id];
739 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: