d8212b50da565813eba669f614162c118a08e635
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 private $company = null
;
183 private $phones = array();
184 private $address = null
;
193 * pid, id, company_id, description, url, email
195 public function __construct($data)
197 foreach ($data as $key => $val) {
200 $this->setCompany(CompanyList
::get($this->jobid
));
203 public function phones()
205 return $this->phones
;
208 public function company()
210 return $this->company
;
213 public function addPhone(Phone
&$phone)
215 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
216 $this->phones
[] = $phone;
220 public function setAddress(Address
$address)
222 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
223 $this->address
= $address;
227 public function setCompany(Company
$company)
229 $this->company
= $company;
236 const LINK_JOB
= 'job';
237 const LINK_COMPANY
= 'hq';
238 const LINK_PROFILE
= 'home';
250 public $subAdministrativeArea;
251 public $administrativeArea;
254 private $phones = array();
257 * pîd, id, link_id, link_type, flags, text, postcode, country
259 public function __construct($data)
261 foreach ($data as $key => $val) {
266 public function addPhone(Phone
&$phone)
268 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
269 $this->phones
[] = $phone;
273 public function phones()
275 return $this->phones
;
278 public function hasFlags($flags)
280 return $flags & $this->flags
;
284 // {{{ class Education
296 public function __construct(array $data)
298 $this->eduid
= $data['eduid'];
299 $this->degreeid
= $data['degreeid'];
300 $this->fieldid
= $data['fieldid'];
302 $this->entry_year
= $data['entry_year'];
303 $this->grad_year
= $data['grad_year'];
304 $this->program
= $data['program'];
305 $this->flags
= new PlFlagSet($data['flags']);
310 // {{{ class ProfileEducation [ Field ]
311 class ProfileEducation
extends ProfileField
313 private $educations = array();
315 public function __construct(PlIterator
$it)
317 $this->pid
= $it->value();
318 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
319 while ($edu = $it->next()) {
320 $this->educations
[$edu['id']] = new Education($edu);
324 public function get($flags, $limit)
326 $educations = array();
328 $year = $year['year'];
330 foreach ($this->educations
as $id => $edu) {
332 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
334 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
336 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
338 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
340 $educations[$id] = $edu;
343 if ($limit != null
&& $nb >= $limit) {
347 return PlIteratorUtils
::fromArray($educations);
350 public static function fetchData(array $pids, $visibility)
352 $data = XDB
::iterator('SELECT id, pid, eduid, degreeid, fieldid,
353 entry_year, grad_year, program, flags
354 FROM profile_education
356 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
357 NOT FIND_IN_SET(\'primary\', flags), entry_year, id',
360 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
364 // {{{ class ProfileMedals [ Field ]
365 class ProfileMedals
extends ProfileField
367 public $medals = array();
369 public function __construct(PlIterator
$it)
371 while ($medal = $it->next()) {
372 $this->medals
[$medal['mid']] = $medal['gid'];
376 public static function fetchData(array $pids, $visibility)
378 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
379 FROM profile_medals AS pm
380 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
381 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
382 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
383 XDB
::formatArray($pids),
384 XDB
::formatArray($visibility)
387 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
391 // {{{ class ProfileNetworking [ Field ]
392 class ProfileNetworking
extends ProfileField
394 private $networks = array();
396 public function __construct(PlIterator
$it)
398 while ($network = $it->next()) {
399 $this->networks
[$network['nwid']] = $network['address'];
403 public static function fetchData(array $pids, $visibility)
405 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
406 FROM profile_networking
407 WHERE pid IN {?} AND pub IN {?}
408 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
412 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
415 public function get($flags, $limit = null
)
419 foreach ($this->networks
as $id => $nw) {
420 // XXX hardcoded reference to web site index
422 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
424 (! ($flags & self
::NETWORKING_WEB
))
433 return PlIteratorUtils
::fromArray($nws);
437 // {{{ class ProfilePhoto [ Field ]
438 class ProfilePhoto
extends ProfileField
442 public function __construct(array $data)
444 if ($data == null ||
count($data) == 0) {
447 $this->pid
= $data['pid'];
448 $this->pic
= PlImage
::fromDATA($data['attach'],
455 public static function fetchData(array $pids, $visibility)
457 $data = XDB
::iterator('SELECT *
459 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
460 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
467 // {{{ class ProfileCorps [ Field ]
468 class ProfileCorps
extends ProfileField
474 public function __construct(array $data)
476 $this->original
= $data['original_corpsid'];
477 $this->current
= $data['current_corpsid'];
478 $this->rank
= $data['rankid'];
479 $this->visibility
= $data['corps_pub'];
482 public static function fetchData(array $pids, $visibility)
484 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
487 WHERE pid IN {?} AND corps_pub IN {?}
488 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
489 XDB
::formatArray($pids),
490 XDB
::formatArray($visibility)
498 /** Loading of data for a Profile :
499 * 1) load jobs, addresses, phones
500 * 2) attach phones to addresses, jobs and profiles
501 * 3) attach addresses to jobs and profiles
504 // {{{ class ProfileAddresses [ Field ]
505 class ProfileAddresses
extends ProfileField
507 private $addresses = array();
509 public function __construct(PlIterator
$it)
511 if ($it instanceof PlInnerSubIterator
) {
512 $this->pid
= $it->value();
515 while ($addr = $it->next()) {
516 $this->addresses
[] = new Address($addr);
520 public function get($flags, $limit = null
)
524 foreach ($this->addresses
as $addr) {
525 if ($addr->hasFlags($flags)) {
529 if ($limit != null
&& $nb == $limit) {
533 return PlIteratorUtils
::fromArray($res);
536 public static function fetchData(array $pids, $visibility)
538 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
539 IF(pa.type = \'home\', pid, jobid) AS link_id,
540 pa.text, pa.postalCode, pa.latitude, pa.longitude,
541 gl.name AS locality, gas.name AS subAdministrativeArea,
542 ga.name AS administrativeArea, gc.countryFR AS country
543 FROM profile_addresses AS pa
544 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
545 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
546 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
547 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
548 WHERE pa.pid in {?} AND pa.pub IN {?}
549 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
552 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
555 public function addPhones(ProfilePhones
$phones)
557 $p = $phones->get(0);
558 while ($phone = $p->next()) {
559 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
560 $this->addresses
[$phone->link_id
]->addPhone($phone);
566 // {{{ class ProfilePhones [ Field ]
567 class ProfilePhones
extends ProfileField
569 private $phones = array();
571 public function __construct(PlIterator
$phones)
573 while ($phone = $it->next()) {
574 $this->phones
[] = Phone
::buildFromData($phone);
578 public function get($flags, $limit = null
)
582 foreach ($this->phones
as $id => $phone) {
583 $phones[$id] = $phone;
585 if ($limit != null
&& $nb == $limit) {
589 return PlIteratorUtils
::fromArray($phones);
592 public static function fetchData(array $pids, $visibility)
594 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
596 WHERE pid IN {?} AND pub IN {?}
597 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
598 XDB
::formatArray($pids),
599 XDB
::formatArray($visibility)
601 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
605 // {{{ class ProfileJobs [ Field ]
606 class ProfileJobs
extends ProfileField
608 private $jobs = array();
610 public function __construct(PlIterator
$jobs)
612 while ($job = $jobs->next()) {
613 $this->jobs
[$job['id']] = new Job($job);
617 public static function fetchData(array $pids, $visibility)
619 CompanyList
::preload($pids);
620 $data = XDB
::iterator('SELECT id, pid, description, url,
621 jobid, sectorid, subsectorid, subsubsectorid,
622 IF(email_pub IN {?}, email, NULL) AS email
624 WHERE pid IN {?} AND pub IN {?}
625 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
627 $visibility, $pids, $visibility);
628 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
631 public function get($flags, $limit = null
)
635 foreach ($this->jobs
as $id => $job) {
638 if ($limit != null
&& $nb >= $limit) {
642 return PlIteratorUtils
::fromArray($jobs);
645 public function addPhones(ProfilePhones
$phones)
647 $p = $phones->get(0);
648 while ($phone = $p->next()) {
649 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
650 $this->jobs
[$phone->link_id
]->addPhones($phone);
655 public static function addAddresses(ProfileAddresses
$addresses)
657 $a = $addresses->get(Profile
::ADDRESS_PRO
);
658 while ($address = $a->next()) {
659 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
660 $this->jobs
[$address->link_id
]->setAddress($address);
665 public static function addCompanies(array $companies)
667 foreach ($this->jobs
as $job)
669 $job->setCompany($companies[$job->company_id
]);
675 // {{{ class CompanyList
678 static private $fullload = false
;
679 static private $companies = array();
681 static public function preload($pids = array())
683 if (self
::$fullload) {
688 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
689 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
695 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
696 pa.flags, pa.text, pa.postalCode, pa.countryId,
698 FROM profile_job_enum AS pje
699 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
702 while ($row = $it->next()) {
703 $cp = new Company($row);
704 $addr = new Address($row);
705 $cp->setAddress($addr);
706 self
::$companies[$row['id']] = $cp;
709 // TODO: add phones to addresses
710 if (count($pids) == 0) {
711 self
::$fullload = true
;
715 static public function get($id)
717 if (!array_key_exists($id, self
::$companies)) {
720 return self
::$companies[$id];
724 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: