567f5e2277e90d59a6ec681dda8aedc51eb79e81
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)"
38 abstract public static function fetchData(array $pids, $visibility);
40 public static function buildForPID($cls, $pid, $visibility)
42 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
43 return array_pop($res);
46 /** Build a list of ProfileFields from a set of pids
47 * @param $cls The name of the field to create ('ProfileMedals', ...)
48 * @param $pids An array of pids
49 * @param $visibility An array of allowed visibility contexts
50 * @return An array of $pid => ProfileField
52 public static function buildFromPIDs($cls, array $pids, $visibility)
54 $it = new ProfileFieldIterator($cls, $pids, $visibility);
56 while ($pf = $it->next()) {
62 public static function getForPID($cls, $pid, $visibility)
64 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
70 // {{{ class ProfileFieldIterator
71 class ProfileFieldIterator
implements PlIterator
76 public function __construct($cls, array $pids, $visibility)
78 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
82 public function next()
84 $d = $this->data
->next();
93 public function total()
95 return $this->data
->total();
98 public function first()
100 return $this->data
->first();
103 public function last()
105 return $this->data
->last();
113 const TYPE_FAX
= 'fax';
114 const TYPE_FIXED
= 'fixed';
115 const TYPE_MOBILE
= 'mobile';
120 public $comment = '';
122 const LINK_JOB
= 'job';
123 const LINK_ADDRESS
= 'address';
124 const LINK_PROFILE
= 'user';
125 const LINK_COMPANY
= 'hq';
130 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
132 public function __construct($data)
134 foreach ($data as $key => $val) {
147 public $phone = null
;
148 public $address = null
;
151 * $id, $name, $acronym, $url
153 public function __construct($data)
155 foreach ($data as $key => $val) {
160 public function setPhone(Phone
&$phone)
162 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
163 $this->phone
= $phone;
167 public function setAddress(Address
&$address)
169 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
170 $this->address
= $address;
182 public $company = null
;
183 private $phones = array();
184 private $address = null
;
194 public $subsubsector;
197 * pid, id, company_id, description, url, email
199 public function __construct($data)
201 foreach ($data as $key => $val) {
204 $this->company
= CompanyList
::get($this->jobid
);
207 public function phones()
209 return $this->phones
;
212 public function addPhone(Phone
&$phone)
214 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
215 $this->phones
[] = $phone;
219 public function setAddress(Address
$address)
221 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
222 $this->address
= $address;
230 const LINK_JOB
= 'job';
231 const LINK_COMPANY
= 'hq';
232 const LINK_PROFILE
= 'home';
244 public $subAdministrativeArea;
245 public $administrativeArea;
248 private $phones = array();
251 * pîd, id, link_id, link_type, flags, text, postcode, country
253 public function __construct($data)
255 foreach ($data as $key => $val) {
260 public function addPhone(Phone
&$phone)
262 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
263 $this->phones
[] = $phone;
267 public function phones()
269 return $this->phones
;
272 public function hasFlags($flags)
274 return $flags & $this->flags
;
278 // {{{ class Education
290 public function __construct(array $data)
292 $this->eduid
= $data['eduid'];
293 $this->degreeid
= $data['degreeid'];
294 $this->fieldid
= $data['fieldid'];
296 $this->entry_year
= $data['entry_year'];
297 $this->grad_year
= $data['grad_year'];
298 $this->program
= $data['program'];
299 $this->flags
= new PlFlagSet($data['flags']);
304 // {{{ class ProfileEducation [ Field ]
305 class ProfileEducation
extends ProfileField
307 private $educations = array();
309 public function __construct(PlIterator
$it)
311 $this->pid
= $it->value();
312 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
313 while ($edu = $it->next()) {
314 $this->educations
[$edu['id']] = new Education($edu);
318 public function get($flags, $limit)
320 $educations = array();
322 $year = $year['year'];
324 foreach ($this->educations
as $id => $edu) {
326 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
328 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
330 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
332 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
334 $educations[$id] = $edu;
337 if ($limit != null
&& $nb >= $limit) {
341 return PlIteratorUtils
::fromArray($educations);
344 public static function fetchData(array $pids, $visibility)
346 $data = XDB
::iterator('SELECT id, pid, eduid, degreeid, fieldid,
347 entry_year, grad_year, program, flags
348 FROM profile_education
350 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
351 NOT FIND_IN_SET(\'primary\', flags), entry_year, 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(PlIterator
$it)
365 while ($medal = $it->next()) {
366 $this->medals
[$medal['mid']] = $medal['gid'];
370 public static function fetchData(array $pids, $visibility)
372 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
373 FROM profile_medals AS pm
374 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
375 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
376 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
377 XDB
::formatArray($pids),
378 XDB
::formatArray($visibility)
381 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
385 // {{{ class ProfileNetworking [ Field ]
386 class ProfileNetworking
extends ProfileField
388 private $networks = array();
390 public function __construct(PlIterator
$it)
392 while ($network = $it->next()) {
393 $this->networks
[$network['nwid']] = $network['address'];
397 public static function fetchData(array $pids, $visibility)
399 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
400 FROM profile_networking
401 WHERE pid IN {?} AND pub IN {?}
402 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
406 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
409 public function get($flags, $limit = null
)
413 foreach ($this->networks
as $id => $nw) {
414 // XXX hardcoded reference to web site index
416 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
418 (! ($flags & self
::NETWORKING_WEB
))
427 return PlIteratorUtils
::fromArray($nws);
431 // {{{ class ProfilePhoto [ Field ]
432 class ProfilePhoto
extends ProfileField
436 public function __construct(array $data)
438 if ($data == null ||
count($data) == 0) {
441 $this->pid
= $data['pid'];
442 $this->pic
= PlImage
::fromDATA($data['attach'],
449 public static function fetchData(array $pids, $visibility)
451 $data = XDB
::iterator('SELECT *
453 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
454 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
461 // {{{ class ProfileCorps [ Field ]
462 class ProfileCorps
extends ProfileField
468 public function __construct(array $data)
470 $this->original
= $data['original_corpsid'];
471 $this->current
= $data['current_corpsid'];
472 $this->rank
= $data['rankid'];
473 $this->visibility
= $data['corps_pub'];
476 public static function fetchData(array $pids, $visibility)
478 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
481 WHERE pid IN {?} AND corps_pub IN {?}
482 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
483 XDB
::formatArray($pids),
484 XDB
::formatArray($visibility)
492 /** Loading of data for a Profile :
493 * 1) load jobs, addresses, phones
494 * 2) attach phones to addresses, jobs and profiles
495 * 3) attach addresses to jobs and profiles
498 // {{{ class ProfileAddresses [ Field ]
499 class ProfileAddresses
extends ProfileField
501 private $addresses = array();
503 public function __construct(PlIterator
$it)
505 if ($it instanceof PlInnerSubIterator
) {
506 $this->pid
= $it->value();
509 while ($addr = $it->next()) {
510 $this->addresses
[] = new Address($addr);
514 public function get($flags, $limit = null
)
518 foreach ($this->addresses
as $addr) {
519 if ($addr->hasFlags($flags)) {
523 if ($limit != null
&& $nb == $limit) {
527 return PlIteratorUtils
::fromArray($res);
530 public static function fetchData(array $pids, $visibility)
532 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
533 IF(pa.type = \'home\', pid, jobid) AS link_id,
534 pa.text, pa.postalCode, pa.latitude, pa.longitude,
535 gl.name AS locality, gas.name AS subAdministrativeArea,
536 ga.name AS administrativeArea, gc.countryFR AS country
537 FROM profile_addresses AS pa
538 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
539 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
540 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
541 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
542 WHERE pa.pid in {?} AND pa.pub IN {?}
543 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
546 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
549 public function addPhones(ProfilePhones
$phones)
551 $p = $phones->get(0);
552 while ($phone = $p->next()) {
553 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
554 $this->addresses
[$phone->link_id
]->addPhone($phone);
560 // {{{ class ProfilePhones [ Field ]
561 class ProfilePhones
extends ProfileField
563 private $phones = array();
565 public function __construct(PlIterator
$phones)
567 while ($phone = $it->next()) {
568 $this->phones
[] = Phone
::buildFromData($phone);
572 public function get($flags, $limit = null
)
576 foreach ($this->phones
as $id => $phone) {
577 $phones[$id] = $phone;
579 if ($limit != null
&& $nb == $limit) {
583 return PlIteratorUtils
::fromArray($phones);
586 public static function fetchData(array $pids, $visibility)
588 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
590 WHERE pid IN {?} AND pub IN {?}
591 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
592 XDB
::formatArray($pids),
593 XDB
::formatArray($visibility)
595 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
599 // {{{ class ProfileJobs [ Field ]
600 class ProfileJobs
extends ProfileField
602 private $jobs = array();
604 public function __construct(PlIterator
$jobs)
606 while ($job = $jobs->next()) {
607 $this->jobs
[$job['id']] = new Job($job);
611 public static function fetchData(array $pids, $visibility)
613 CompanyList
::preload($pids);
614 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
615 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
616 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
617 pjssse.name AS subsubsector
618 FROM profile_job AS pj
619 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
620 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
621 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
622 WHERE pj.pid IN {?} AND pj.pub IN {?}
623 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
625 $visibility, $pids, $visibility);
626 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
629 public function get($flags, $limit = null
)
633 foreach ($this->jobs
as $id => $job) {
636 if ($limit != null
&& $nb >= $limit) {
640 return PlIteratorUtils
::fromArray($jobs);
643 public function addPhones(ProfilePhones
$phones)
645 $p = $phones->get(0);
646 while ($phone = $p->next()) {
647 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
648 $this->jobs
[$phone->link_id
]->addPhones($phone);
653 public static function addAddresses(ProfileAddresses
$addresses)
655 $a = $addresses->get(Profile
::ADDRESS_PRO
);
656 while ($address = $a->next()) {
657 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
658 $this->jobs
[$address->link_id
]->setAddress($address);
663 public static function addCompanies(array $companies)
665 foreach ($this->jobs
as $job)
667 $job->company
= $companies[$job->jobid
];
673 // {{{ class CompanyList
676 static private $fullload = false
;
677 static private $companies = array();
679 static public function preload($pids = array())
681 if (self
::$fullload) {
686 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
687 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
693 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
694 pa.flags, pa.text, pa.postalCode, pa.countryId,
696 FROM profile_job_enum AS pje
697 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
700 while ($row = $it->next()) {
701 $cp = new Company($row);
702 $addr = new Address($row);
703 $cp->setAddress($addr);
704 self
::$companies[$row['id']] = $cp;
707 // TODO: add phones to addresses
708 if (count($pids) == 0) {
709 self
::$fullload = true
;
713 static public function get($id)
715 if (!array_key_exists($id, self
::$companies)) {
718 return self
::$companies[$id];
722 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: