9e91c901e7da85dd232cc52874b7640794f8bdc0
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($date)
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) {
202 public function phones()
204 return $this->phones
;
207 public function company()
209 return $this->company
;
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;
226 public function setCompany(Company
$company)
228 $this->company
= $company;
235 const LINK_JOB
= 'job';
236 const LINK_COMPANY
= 'hq';
237 const LINK_PROFILE
= 'home';
247 private $phones = array();
250 * pîd, id, link_id, link_type, flags, text, postcode, country
252 public function __construct($data)
254 foreach ($data as $key => $val) {
259 public function addPhone(Phone
&$phone)
261 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
262 $this->phones
[] = $phone;
266 public function phones()
268 return $this->phones
;
271 public function hasFlags($flags)
273 return $flags & $this->flags
;
277 // {{{ class Education
289 public function __construct(array $data)
291 $this->eduid
= $data['eduid'];
292 $this->degreeid
= $data['degreeid'];
293 $this->fieldid
= $data['fieldid'];
295 $this->entry_year
= $data['entry_year'];
296 $this->grad_year
= $data['grad_year'];
297 $this->program
= $data['program'];
298 $this->flags
= new PlFlagSet($data['flags']);
303 // {{{ class ProfileEducation [ Field ]
304 class ProfileEducation
extends ProfileField
306 private $educations = array();
308 private function __construct(PlIterator
$it)
310 $this->pid
= $it->value();
311 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
312 while ($edu = $it->next()) {
313 $this->educations
[$edu['id']] = new Education($edu);
317 public function get($flags, $limit)
319 $educations = array();
321 $year = $year['year'];
323 foreach ($this->educations
as $id => $edu) {
325 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
327 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
329 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
331 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
333 $educations[$id] = $edu;
336 if ($limit != null
&& $nb >= $limit) {
340 return PlIteratorUtils
::fromArray($educations);
343 public static function fetchData(array $pids, $visibility)
345 $data = XDB
::iterator('SELECT id, pid, eduid, degreeid, fieldid,
346 entry_year, grad_year, program, flags
347 FROM profile_education
349 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
350 NOT FIND_IN_SET(\'primary\', flags), entry_year, id',
353 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
357 // {{{ class ProfileMedals [ Field ]
358 class ProfileMedals
extends ProfileField
360 public $medals = array();
362 private function __construct(PlIterator
$it)
364 while ($medal = $it->next()) {
365 $this->medals
[$medal['mid']] = $medal['gid'];
369 public static function fetchData(array $pids, $visibility)
371 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
372 FROM profile_medals AS pm
373 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
374 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
375 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
376 XDB
::formatArray($pids),
377 XDB
::formatArray($visibility)
380 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
384 // {{{ class ProfileNetworking [ Field ]
385 class ProfileNetworking
extends ProfileField
387 private $networks = array();
389 private function __construct(PlIterator
$it)
391 while ($network = $it->next()) {
392 $this->networks
[$network['nwid']] = $network['address'];
396 public static function fetchData(array $pids, $visibility)
398 $data = XDB
::iterator('SELECT pid, nwid, address, network_type
399 FROM profile_networking
400 WHERE pid IN {?} AND pub IN {?}
401 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
405 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
408 public function get($flags, $limit = null
)
412 foreach ($this->networks
as $id => $nw) {
413 // XXX hardcoded reference to web site index
415 (($flags & self
::NETWORKING_WEB
) && $nw['network_type'] == 0)
417 (! ($flags & self
::NETWORKING_WEB
))
426 return PlIteratorUtils
::fromArray($nws);
430 // {{{ class ProfilePhoto [ Field ]
431 class ProfilePhoto
extends ProfileField
435 public function __construct(array $data)
437 if ($data == null ||
count($data) == 0) {
440 $this->pid
= $data['pid'];
441 $this->pic
= PlImage
::fromDATA($data['attach'],
448 public static function fetchData(array $pids, $visibility)
450 $data = XDB
::iterator('SELECT *
452 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
453 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
460 // {{{ class ProfileCorps [ Field ]
461 class ProfileCorps
extends ProfileField
467 private function __construct(array $data)
469 $this->original
= $data['original_corpsid'];
470 $this->current
= $data['current_corpsid'];
471 $this->rank
= $data['rankid'];
472 $this->visibility
= $data['corps_pub'];
475 public static function fetchData(array $pids, $visibility)
477 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
480 WHERE pid IN {?} AND corps_pub IN {?}
481 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
482 XDB
::formatArray($pids),
483 XDB
::formatArray($visibility)
491 /** Loading of data for a Profile :
492 * 1) load jobs, addresses, phones
493 * 2) attach phones to addresses, jobs and profiles
494 * 3) attach addresses to jobs and profiles
497 // {{{ class ProfileAddresses [ Field ]
498 class ProfileAddresses
extends ProfileField
500 private $addresses = array();
502 public function __construct(PlIterator
$it)
504 if ($it instanceof PlInnerSubIterator
) {
505 $this->pid
= $it->value();
508 while ($addr = $it->next()) {
509 $this->addresses
[] = new Address($addr);
513 public function get($flags, $limit = null
)
517 foreach ($this->addresses
as $addr) {
518 if ($addr->hasFlags($flags)) {
522 if ($limit != null
&& $nb == $limit) {
526 return PlIteratorUtils
::fromArray($res);
529 public static function fetchData(array $pids, $visibility)
531 $data = XDB
::iterator('SELECT pid, text, postalCode, type, latitude, longitude,
533 FROM profile_addresses
534 WHERE pid in {?} AND pub IN {?}
535 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
538 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
541 public static function addPhones(array $addresses, $phones)
543 foreach ($phones as $phone) {
544 if ($phone->link_type
== Phone
::LINK_ADDRESS
) {
545 $addresses[$phone->link_id
]->addPhone($phone);
552 // {{{ class ProfilePhones [ Field ]
553 class ProfilePhones
extends ProfileField
555 private $phones = array();
557 private function __construct(PlIterator
$phones)
559 while ($phone = $it->next()) {
560 $this->phones
[] = Phone
::buildFromData($phone);
564 public static function fetchData(array $pids, $visibility)
566 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
568 WHERE pid IN {?} AND pub IN {?}
569 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
570 XDB
::formatArray($pids),
571 XDB
::formatArray($visibility)
573 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
577 // {{{ class ProfileJobs [ Field ]
578 class ProfileJobs
extends ProfileField
580 private $jobs = array();
582 private function __construct(PlIterator
$jobs)
584 while ($job = $jobs->next()) {
585 $this->jobs
[] = Jobs
::buildFromData($job);
589 public static function fetchData(array $pids, $visibility)
591 $data = XDB
::iterator('SELECT description, url, jobid, IF(email_pub IN {?}, email, NULL) AS email
593 WHERE pid IN {?} AND pub IN {?}
594 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
595 XDB
::formatArray($visibility),
596 XDB
::formatArray($pids),
597 XDB
::formatArray($visibility)
599 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
602 public static function addPhones(array $jobs, array $phones)
604 foreach ($phones as $phone)
606 if ($phone->link_type
== Phone
::LINK_JOB
) {
607 $jobs[$phone->link_id
]->addPhones($phone);
613 public static function addAddresses(array $jobs, array $addresses)
615 foreach ($addresses as $address)
617 if ($address->link_type
== Address
::LINK_JOB
) {
618 $jobs[$address->link_id
]->setAddress($address);
624 public static function addCompanies(array $jobs, array $companies)
626 foreach ($jobs as $job)
628 $job->setCompany($companies[$job->company_id
]);
635 // {{{ class CompanyList
638 static private $fullload = false
;
639 static private $companies = array();
641 static public function preload($pids = array())
643 if (self
::$fullload) {
648 $join = 'LEFT JOIN profile_jobs ON (profile_job.jobid = profile_job_enum.id)';
649 $where = 'profile_jobs.pid IN ' . XDB
::formatArray($pids);
655 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronmy, pje.url,
656 pa.flags, pa.text, pa.postcode, pa.country,
658 FROM profile_job_enum AS pje
659 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
662 while ($row = $it->next()) {
663 $cp = Company
::buildFromData($row);
664 $addr = Address
::buildFromData($row);
665 $cp->setAddress($addr);
666 self
::$companies[$row['id']] = $cp;
669 // TODO: add phones to addresses
670 if (count($pids) == 0) {
671 self
::$fullload = true
;
675 static public function getCompany($id)
677 if (!array_key_exists($id, self
::$companies)) {
680 return self
::$companies[$id];
684 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: