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 function __construct(array $data)
296 $this->eduid
= $data['eduid'];
297 $this->degreeid
= $data['degreeid'];
298 $this->fieldid
= $data['fieldid'];
300 $this->entry_year
= $data['entry_year'];
301 $this->grad_year
= $data['grad_year'];
302 $this->program
= $data['program'];
303 $this->flags
= new PlFlagSet($data['flags']);
308 // {{{ class ProfileEducation [ Field ]
309 class ProfileEducation
extends ProfileField
311 private $educations = array();
313 public function __construct(PlIterator
$it)
315 $this->pid
= $it->value();
316 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
317 while ($edu = $it->next()) {
318 $this->educations
[$edu['id']] = new Education($edu);
322 public function get($flags, $limit)
324 $educations = array();
326 $year = $year['year'];
328 foreach ($this->educations
as $id => $edu) {
330 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
332 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
334 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
336 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
338 $educations[$id] = $edu;
341 if ($limit != null
&& $nb >= $limit) {
345 return PlIteratorUtils
::fromArray($educations);
348 public static function fetchData(array $pids, $visibility)
350 $data = XDB
::iterator('SELECT id, pid, eduid, degreeid, fieldid,
351 entry_year, grad_year, program, flags
352 FROM profile_education
354 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
355 NOT FIND_IN_SET(\'primary\', flags), entry_year, 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(PlIterator
$it)
369 while ($medal = $it->next()) {
370 $this->medals
[$medal['mid']] = $medal['gid'];
374 public static function fetchData(array $pids, $visibility)
376 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
377 FROM profile_medals AS pm
378 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
379 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
380 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
381 XDB
::formatArray($pids),
382 XDB
::formatArray($visibility)
385 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
389 // {{{ class ProfileNetworking [ Field ]
390 class ProfileNetworking
extends ProfileField
392 private $networks = array();
394 public function __construct(PlIterator
$it)
396 while ($network = $it->next()) {
397 $this->networks
[$network['nwid']] = $network['address'];
401 public static function fetchData(array $pids, $visibility)
403 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
404 FROM profile_networking
405 WHERE pid IN {?} AND pub IN {?}
406 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
410 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
413 public function get($flags, $limit = null
)
417 foreach ($this->networks
as $id => $nw) {
418 // XXX hardcoded reference to web site index
420 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
422 (! ($flags & self
::NETWORKING_WEB
))
431 return PlIteratorUtils
::fromArray($nws, 1, true
);
435 // {{{ class ProfilePhoto [ Field ]
436 class ProfilePhoto
extends ProfileField
440 public function __construct(array $data)
442 if ($data == null ||
count($data) == 0) {
445 $this->pid
= $data['pid'];
446 $this->pic
= PlImage
::fromDATA($data['attach'],
453 public static function fetchData(array $pids, $visibility)
455 $data = XDB
::iterator('SELECT *
457 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
458 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
465 // {{{ class ProfileCorps [ Field ]
466 class ProfileCorps
extends ProfileField
472 public function __construct(array $data)
474 $this->original
= $data['original_corpsid'];
475 $this->current
= $data['current_corpsid'];
476 $this->rank
= $data['rankid'];
477 $this->visibility
= $data['corps_pub'];
480 public static function fetchData(array $pids, $visibility)
482 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
485 WHERE pid IN {?} AND corps_pub IN {?}
486 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
487 XDB
::formatArray($pids),
488 XDB
::formatArray($visibility)
496 /** Loading of data for a Profile :
497 * 1) load jobs, addresses, phones
498 * 2) attach phones to addresses, jobs and profiles
499 * 3) attach addresses to jobs and profiles
502 // {{{ class ProfileAddresses [ Field ]
503 class ProfileAddresses
extends ProfileField
505 private $addresses = array();
507 public function __construct(PlIterator
$it)
509 if ($it instanceof PlInnerSubIterator
) {
510 $this->pid
= $it->value();
513 while ($addr = $it->next()) {
514 $this->addresses
[] = new Address($addr);
518 public function get($flags, $limit = null
)
522 foreach ($this->addresses
as $addr) {
523 if ($addr->hasFlags($flags)) {
527 if ($limit != null
&& $nb == $limit) {
531 return PlIteratorUtils
::fromArray($res, 1, true
);
534 public static function fetchData(array $pids, $visibility)
536 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
537 IF(pa.type = \'home\', pid, jobid) AS link_id,
538 pa.text, pa.postalCode, pa.latitude, pa.longitude,
539 gl.name AS locality, gas.name AS subAdministrativeArea,
540 ga.name AS administrativeArea, gc.countryFR AS country
541 FROM profile_addresses AS pa
542 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
543 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
544 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
545 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
546 WHERE pa.pid in {?} AND pa.pub IN {?}
547 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
550 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
553 public function addPhones(ProfilePhones
$phones)
555 $p = $phones->get(0);
556 while ($phone = $p->next()) {
557 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
558 $this->addresses
[$phone->link_id
]->addPhone($phone);
564 // {{{ class ProfilePhones [ Field ]
565 class ProfilePhones
extends ProfileField
567 private $phones = array();
569 public function __construct(PlIterator
$phones)
571 while ($phone = $it->next()) {
572 $this->phones
[] = Phone
::buildFromData($phone);
576 public function get($flags, $limit = null
)
580 foreach ($this->phones
as $id => $phone) {
581 $phones[$id] = $phone;
583 if ($limit != null
&& $nb == $limit) {
587 return PlIteratorUtils
::fromArray($phones, 1, true
);
590 public static function fetchData(array $pids, $visibility)
592 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
594 WHERE pid IN {?} AND pub IN {?}
595 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
596 XDB
::formatArray($pids),
597 XDB
::formatArray($visibility)
599 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
603 // {{{ class ProfileJobs [ Field ]
604 class ProfileJobs
extends ProfileField
606 private $jobs = array();
608 public function __construct(PlInnerSubIterator
$jobs)
610 $this->pid
= $jobs->value();
611 while ($job = $jobs->next()) {
612 $this->jobs
[$job['id']] = new Job($job);
616 public static function fetchData(array $pids, $visibility)
618 CompanyList
::preload($pids);
619 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
620 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
621 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
622 pjssse.name AS subsubsector
623 FROM profile_job AS pj
624 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
625 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
626 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
627 WHERE pj.pid IN {?} AND pj.pub IN {?}
628 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
630 $visibility, $pids, $visibility);
631 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
634 public function get($flags, $limit = null
)
638 foreach ($this->jobs
as $id => $job) {
641 if ($limit != null
&& $nb >= $limit) {
645 return PlIteratorUtils
::fromArray($jobs, 1, true
);
648 public function addPhones(ProfilePhones
$phones)
650 $p = $phones->get(0);
651 while ($phone = $p->next()) {
652 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
653 $this->jobs
[$phone->link_id
]->addPhones($phone);
658 public static function addAddresses(ProfileAddresses
$addresses)
660 $a = $addresses->get(Profile
::ADDRESS_PRO
);
661 while ($address = $a->next()) {
662 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
663 $this->jobs
[$address->link_id
]->setAddress($address);
668 public static function addCompanies(array $companies)
670 foreach ($this->jobs
as $job)
672 $job->company
= $companies[$job->jobid
];
678 // {{{ class CompanyList
681 static private $fullload = false
;
682 static private $companies = array();
684 static public function preload($pids = array())
686 if (self
::$fullload) {
691 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
692 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
698 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
699 pa.flags, pa.text, pa.postalCode, pa.countryId,
701 FROM profile_job_enum AS pje
702 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
705 while ($row = $it->next()) {
706 $cp = new Company($row);
707 $addr = new Address($row);
708 $cp->setAddress($addr);
709 self
::$companies[$row['id']] = $cp;
712 // TODO: add phones to addresses
713 if (count($pids) == 0) {
714 self
::$fullload = true
;
718 static public function get($id)
720 if (!array_key_exists($id, self
::$companies)) {
723 return self
::$companies[$id];
727 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: