b1f0f59e7e5af55435c169fd73f5a54a2a32dc70
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';
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 pid, text, postalCode, type, latitude, longitude,
534 FROM profile_addresses
535 WHERE pid in {?} AND pub IN {?}
536 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
539 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
542 public function addPhones(ProfilePhones
$phones)
544 $p = $phones->get(0);
545 while ($phone = $p->next()) {
546 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& array_key_exists($phone->link_id
, $this->addresses
)) {
547 $this->addresses
[$phone->link_id
]->addPhone($phone);
553 // {{{ class ProfilePhones [ Field ]
554 class ProfilePhones
extends ProfileField
556 private $phones = array();
558 public function __construct(PlIterator
$phones)
560 while ($phone = $it->next()) {
561 $this->phones
[] = Phone
::buildFromData($phone);
565 public function get($flags, $limit = null
)
569 foreach ($this->phones
as $id => $phone) {
570 $phones[$id] = $phone;
572 if ($limit != null
&& $nb == $limit) {
576 return PlIteratorUtils
::fromArray($phones);
579 public static function fetchData(array $pids, $visibility)
581 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
583 WHERE pid IN {?} AND pub IN {?}
584 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
585 XDB
::formatArray($pids),
586 XDB
::formatArray($visibility)
588 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
592 // {{{ class ProfileJobs [ Field ]
593 class ProfileJobs
extends ProfileField
595 private $jobs = array();
597 public function __construct(PlIterator
$jobs)
599 while ($job = $jobs->next()) {
600 $this->jobs
[$job['id']] = new Job($job);
604 public static function fetchData(array $pids, $visibility)
606 $data = XDB
::iterator('SELECT id, pid, description, url,
607 jobid, sectorid, subsectorid, subsubsectorid,
608 IF(email_pub IN {?}, email, NULL) AS email
610 WHERE pid IN {?} AND pub IN {?}
611 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
613 $visibility, $pids, $visibility);
614 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
617 public function get($flags, $limit = null
)
621 foreach ($this->jobs
as $id => $job) {
624 if ($limit != null
&& $nb >= $limit) {
628 return PlIteratorUtils
::fromArray($jobs);
631 public function addPhones(ProfilePhones
$phones)
633 $p = $phones->get(0);
634 while ($phone = $p->next()) {
635 if ($phone->link_type
== Phone
::LINK_JOB
&& array_key_exists($phone->link_id
, $this->jobs
)) {
636 $this->jobs
[$phone->link_id
]->addPhones($phone);
641 public static function addAddresses(ProfileAddresses
$addresses)
643 $a = $addresses->get(Profile
::ADDRESS_PRO
);
644 while ($address = $a->next()) {
645 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
646 $this->jobs
[$address->link_id
]->setAddress($address);
651 public static function addCompanies(array $companies)
653 foreach ($this->jobs
as $job)
655 $job->setCompany($companies[$job->company_id
]);
661 // {{{ class CompanyList
664 static private $fullload = false
;
665 static private $companies = array();
667 static public function preload($pids = array())
669 if (self
::$fullload) {
674 $join = 'LEFT JOIN profile_jobs ON (profile_job.jobid = profile_job_enum.id)';
675 $where = 'profile_jobs.pid IN ' . XDB
::formatArray($pids);
681 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
682 pa.flags, pa.text, pa.postalCode, pa.countryId,
684 FROM profile_job_enum AS pje
685 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
688 while ($row = $it->next()) {
689 $cp = new Company($row);
690 $addr = new Address($row);
691 $cp->setAddress($addr);
692 self
::$companies[$row['id']] = $cp;
695 // TODO: add phones to addresses
696 if (count($pids) == 0) {
697 self
::$fullload = true
;
701 static public function get($id)
703 if (!array_key_exists($id, self
::$companies)) {
706 return self
::$companies[$id];
710 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: