ec64e92e8e038769fedaa879ae8242bf279e21e9
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()) {
64 // {{{ class ProfileFieldIterator
65 class ProfileFieldIterator
implements PlIterator
70 public function __construct($cls, array $pids, $visibility)
72 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
76 public function next()
78 $d = $this->data
->next();
87 public function total()
89 return $this->data
->total();
92 public function first()
94 return $this->data
->first();
97 public function last()
99 return $this->data
->last();
107 const TYPE_FAX
= 'fax';
108 const TYPE_FIXED
= 'fixed';
109 const TYPE_MOBILE
= 'mobile';
114 public $comment = '';
116 const LINK_JOB
= 'job';
117 const LINK_ADDRESS
= 'address';
118 const LINK_PROFILE
= 'user';
119 const LINK_COMPANY
= 'hq';
124 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
126 public function __construct($data)
128 foreach ($data as $key => $val) {
141 public $phone = null
;
142 public $address = null
;
145 * $id, $name, $acronym, $url
147 public function __construct($date)
149 foreach ($data as $key => $val) {
154 public function setPhone(Phone
&$phone)
156 if ($phone->link_type
== Phone
::LINK_COMPANY
&& $phone->link_id
== $this->id
) {
157 $this->phone
= $phone;
161 public function setAddress(Address
&$address)
163 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
164 $this->address
= $address;
176 private $company = null
;
177 private $phones = array();
178 private $address = null
;
187 * pid, id, company_id, description, url, email
189 public function __construct($data)
191 foreach ($data as $key => $val) {
196 public function phones()
198 return $this->phones
;
201 public function company()
203 return $this->company
;
206 public function addPhone(Phone
&$phone)
208 if ($phone->link_type
== Phone
::LINK_JOB
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
209 $this->phones
[] = $phone;
213 public function setAddress(Address
$address)
215 if ($address->link_id
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
216 $this->address
= $address;
220 public function setCompany(Company
$company)
222 $this->company
= $company;
229 const LINK_JOB
= 'job';
230 const LINK_COMPANY
= 'hq';
231 const LINK_PROFILE
= 'home';
241 private $phones = array();
244 * pîd, id, link_id, link_type, flags, text, postcode, country
246 public function __construct($data)
248 foreach ($data as $key => $val) {
253 public function addPhone(Phone
&$phone)
255 if ($phone->link_type
== Phone
::LINK_ADDRESS
&& $phone->link_id
== $this->id
&& $phone->pid
== $this->pid
) {
256 $this->phones
[] = $phone;
260 public function phones()
262 return $this->phones
;
265 public function hasFlags($flags)
267 return $flags & $this->flags
;
271 // {{{ class Education
283 public function __construct(array $data)
285 $this->eduid
= $data['eduid'];
286 $this->degreeid
= $data['degreeid'];
287 $this->fieldid
= $data['fieldid'];
289 $this->entry_year
= $data['entry_year'];
290 $this->grad_year
= $data['grad_year'];
291 $this->program
= $data['program'];
292 $this->flags
= new PlFlagSet($data['flags']);
297 // {{{ class ProfileEducation [ Field ]
298 class ProfileEducation
extends ProfileField
300 private $educations = array();
302 private function __construct(PlIterator
$it)
304 $this->pid
= $it->value();
305 $this->visibility
= Profile
::VISIBILITY_PUBLIC
;
306 while ($edu = $it->next()) {
307 $this->educations
[$edu['id']] = new Education($edu);
311 public function get($flags, $limit)
313 $educations = array();
315 $year = $year['year'];
317 foreach ($this->educations
as $id => $edu) {
319 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
321 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
323 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
325 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
327 $educations[$id] = $edu;
330 if ($limit != null
&& $nb >= $limit) {
334 return PlIteratorUtils
::fromArray($educations);
337 public static function fetchData(array $pids, $visibility)
339 $data = XDB
::iterator('SELECT id, pid, eduid, degreeid, fieldid,
340 entry_year, grad_year, program, flags
341 FROM profile_education
343 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
344 NOT FIND_IN_SET(\'primary\', flags), entry_year, id',
347 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
351 // {{{ class ProfileMedals [ Field ]
352 class ProfileMedals
extends ProfileField
354 public $medals = array();
356 private function __construct(PlIterator
$it)
358 while ($medal = $it->next()) {
359 $this->medals
[$medal['mid']] = $medal['gid'];
363 public static function fetchData(array $pids, $visibility)
365 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid
366 FROM profile_medals AS pm
367 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
368 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
369 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
370 XDB
::formatArray($pids),
371 XDB
::formatArray($visibility)
374 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
378 // {{{ class ProfileNetworking [ Field ]
379 class ProfileNetworking
extends ProfileField
381 private $networks = array();
382 private $visibilities = array();
384 private function __construct(PlIterator
$it)
386 while ($network = $it->next()) {
387 $this->networks
[$network['nwid']] = $network['address'];
388 $this->visibilities
[$network['nwid']] = $network['pub'];
392 public static function fetchData(array $pids, $visibility)
394 $data = XDB
::iterator('SELECT pid, nwid, address, pub
395 FROM profile_networking
396 WHERE pid IN {?} AND pub IN {?}
397 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
398 XDB
::formatArray($pids),
399 XDB
::formatArray($visibility)
402 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
405 public function networks()
408 foreach ($this->visibilities
as $id => $vis) {
409 if ($this->profile
->isVisible($vis)) {
410 $nws[$id] = $this->networks
[$id];
417 // {{{ class ProfilePhoto [ Field ]
418 class ProfilePhoto
extends ProfileField
422 public function __construct(array $data)
424 if ($data == null ||
count($data) == 0) {
427 $this->pid
= $data['pid'];
428 $this->pic
= PlImage
::fromDATA($data['attach'],
435 public static function fetchData(array $pids, $visibility)
437 $data = XDB
::iterator('SELECT *
439 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
440 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
447 // {{{ class ProfileCorps [ Field ]
448 class ProfileCorps
extends ProfileField
454 private function __construct(array $data)
456 $this->original
= $data['original_corpsid'];
457 $this->current
= $data['current_corpsid'];
458 $this->rank
= $data['rankid'];
459 $this->visibility
= $data['corps_pub'];
462 public static function fetchData(array $pids, $visibility)
464 $data = XDB
::iterator('SELECT pid, original_corpsid, current_corpsid,
467 WHERE pid IN {?} AND corps_pub IN {?}
468 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
469 XDB
::formatArray($pids),
470 XDB
::formatArray($visibility)
478 /** Loading of data for a Profile :
479 * 1) load jobs, addresses, phones
480 * 2) attach phones to addresses, jobs and profiles
481 * 3) attach addresses to jobs and profiles
484 // {{{ class ProfileAddresses [ Field ]
485 class ProfileAddresses
extends ProfileField
487 private $addresses = array();
489 public function __construct(PlIterator
$it)
491 if ($it instanceof PlInnerSubIterator
) {
492 $this->pid
= $it->value();
495 while ($addr = $it->next()) {
496 $this->addresses
[] = new Address($addr);
500 public function get($flags, $limit = null
)
504 foreach ($this->addresses
as $addr) {
505 if ($addr->hasFlags($flags)) {
509 if ($limit != null
&& $nb == $limit) {
513 return PlIteratorUtils
::fromArray($res);
516 public static function fetchData(array $pids, $visibility)
518 $data = XDB
::iterator('SELECT pid, text, postalCode, type, latitude, longitude,
520 FROM profile_addresses
521 WHERE pid in {?} AND pub IN {?}
522 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
525 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
528 public static function addPhones(array $addresses, $phones)
530 foreach ($phones as $phone) {
531 if ($phone->link_type
== Phone
::LINK_ADDRESS
) {
532 $addresses[$phone->link_id
]->addPhone($phone);
539 // {{{ class ProfilePhones [ Field ]
540 class ProfilePhones
extends ProfileField
542 private $phones = array();
544 private function __construct(PlIterator
$phones)
546 while ($phone = $it->next()) {
547 $this->phones
[] = Phone
::buildFromData($phone);
551 public static function fetchData(array $pids, $visibility)
553 $data = XDB
::iterator('SELECT type, search, display, link_type, comment
555 WHERE pid IN {?} AND pub IN {?}
556 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
557 XDB
::formatArray($pids),
558 XDB
::formatArray($visibility)
560 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
564 // {{{ class ProfileJobs [ Field ]
565 class ProfileJobs
extends ProfileField
567 private $jobs = array();
569 private function __construct(PlIterator
$jobs)
571 while ($job = $jobs->next()) {
572 $this->jobs
[] = Jobs
::buildFromData($job);
576 public static function fetchData(array $pids, $visibility)
578 $data = XDB
::iterator('SELECT description, url, jobid, IF(email_pub IN {?}, email, NULL) AS email
580 WHERE pid IN {?} AND pub IN {?}
581 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
582 XDB
::formatArray($visibility),
583 XDB
::formatArray($pids),
584 XDB
::formatArray($visibility)
586 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
589 public static function addPhones(array $jobs, array $phones)
591 foreach ($phones as $phone)
593 if ($phone->link_type
== Phone
::LINK_JOB
) {
594 $jobs[$phone->link_id
]->addPhones($phone);
600 public static function addAddresses(array $jobs, array $addresses)
602 foreach ($addresses as $address)
604 if ($address->link_type
== Address
::LINK_JOB
) {
605 $jobs[$address->link_id
]->setAddress($address);
611 public static function addCompanies(array $jobs, array $companies)
613 foreach ($jobs as $job)
615 $job->setCompany($companies[$job->company_id
]);
622 // {{{ class CompanyList
625 static private $fullload = false
;
626 static private $companies = array();
628 static public function preload($pids = array())
630 if (self
::$fullload) {
635 $join = 'LEFT JOIN profile_jobs ON (profile_job.jobid = profile_job_enum.id)';
636 $where = 'profile_jobs.pid IN ' . XDB
::formatArray($pids);
642 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronmy, pje.url,
643 pa.flags, pa.text, pa.postcode, pa.country,
645 FROM profile_job_enum AS pje
646 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
649 while ($row = $it->next()) {
650 $cp = Company
::buildFromData($row);
651 $addr = Address
::buildFromData($row);
652 $cp->setAddress($addr);
653 self
::$companies[$row['id']] = $cp;
656 // TODO: add phones to addresses
657 if (count($pids) == 0) {
658 self
::$fullload = true
;
662 static public function getCompany($id)
664 if (!array_key_exists($id, self
::$companies)) {
667 return self
::$companies[$id];
671 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: