Fix warning about abstract static functions
[platal.git] / include / profilefields.inc.php
CommitLineData
9f21bd15
RB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22// {{{ class ProfileField
23/** To store a "field" from the profile
24 * Provides functions for loading a batch of such data
25 */
26abstract class ProfileField
27{
28 /** The profile to which this field belongs
29 */
30 public $pid;
31
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)"
b3c0f165 37 * XXX MUST be reimplemented for each kind of ProfileField
9f21bd15 38 */
b3c0f165
RB
39 public static function fetchData(array $pids, $visibility)
40 {
41 return PlIteratorUtils::emptyIterator();
42 }
9f21bd15
RB
43
44 public static function buildForPID($cls, $pid, $visibility)
45 {
46 $res = self::buildFromPIDs($cls, array($pid), $visibility);
47 return array_pop($res);
48 }
49
50 /** Build a list of ProfileFields from a set of pids
51 * @param $cls The name of the field to create ('ProfileMedals', ...)
52 * @param $pids An array of pids
53 * @param $visibility An array of allowed visibility contexts
54 * @return An array of $pid => ProfileField
55 */
56 public static function buildFromPIDs($cls, array $pids, $visibility)
57 {
58 $it = new ProfileFieldIterator($cls, $pids, $visibility);
59 $res = array();
60 while ($pf = $it->next()) {
61 $res[$pf->pid] = $pf;
62 }
63 return $res;
64 }
990cb17b
RB
65
66 public static function getForPID($cls, $pid, $visibility)
67 {
68 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
69 return $it->next();
70 }
9f21bd15
RB
71}
72// }}}
73
74// {{{ class ProfileFieldIterator
75class ProfileFieldIterator implements PlIterator
76{
77 private $data;
78 private $cls;
79
80 public function __construct($cls, array $pids, $visibility)
81 {
82 $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
83 $this->cls = $cls;
84 }
85
86 public function next()
87 {
88 $d = $this->data->next();
89 if ($d == null) {
90 return null;
91 } else {
92 $cls = $this->cls;
93 return new $cls($d);
94 }
95 }
96
97 public function total()
98 {
99 return $this->data->total();
100 }
101
102 public function first()
103 {
104 return $this->data->first();
105 }
106
107 public function last()
108 {
109 return $this->data->last();
110 }
111}
112// }}}
113
114// {{{ class Phone
115class Phone
116{
117 const TYPE_FAX = 'fax';
118 const TYPE_FIXED = 'fixed';
119 const TYPE_MOBILE = 'mobile';
120 public $type;
121
122 public $search;
123 public $display;
124 public $comment = '';
125
126 const LINK_JOB = 'job';
127 const LINK_ADDRESS = 'address';
128 const LINK_PROFILE = 'user';
129 const LINK_COMPANY = 'hq';
130 public $link_type;
131 public $link_id;
132
133 /** Fields are :
134 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
135 */
136 public function __construct($data)
137 {
138 foreach ($data as $key => $val) {
139 $this->$key = $val;
140 }
141 }
142}
143// }}}
144// {{{ class Company
145class Company
146{
147 public $id;
148 public $name;
149 public $acronym;
150 public $url;
151 public $phone = null;
152 public $address = null;
153
154 /** Fields are:
155 * $id, $name, $acronym, $url
156 */
df85e426 157 public function __construct($data)
9f21bd15
RB
158 {
159 foreach ($data as $key => $val) {
160 $this->$key = $val;
161 }
162 }
163
164 public function setPhone(Phone &$phone)
165 {
166 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
167 $this->phone = $phone;
168 }
169 }
170
171 public function setAddress(Address &$address)
172 {
173 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
174 $this->address = $address;
175 }
176 }
177
178}
179// }}}
180// {{{ class Job
181class Job
182{
183 public $pid;
184 public $id;
185
17982ebf 186 public $company = null;
9f21bd15
RB
187 private $phones = array();
188 private $address = null;
189
17982ebf 190 public $jobid;
9f21bd15
RB
191
192 public $description;
17982ebf
RB
193 public $user_site;
194 public $user_email;
195
196 public $sector;
197 public $subsector;
198 public $subsubsector;
9f21bd15
RB
199
200 /** Fields are:
201 * pid, id, company_id, description, url, email
202 */
203 public function __construct($data)
204 {
205 foreach ($data as $key => $val) {
206 $this->$key = $val;
207 }
17982ebf 208 $this->company = CompanyList::get($this->jobid);
9f21bd15
RB
209 }
210
211 public function phones()
212 {
213 return $this->phones;
214 }
215
9f21bd15
RB
216 public function addPhone(Phone &$phone)
217 {
218 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
219 $this->phones[] = $phone;
220 }
221 }
222
223 public function setAddress(Address $address)
224 {
225 if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
226 $this->address = $address;
227 }
228 }
9f21bd15
RB
229}
230// }}}
231// {{{ class Address
232class Address
233{
234 const LINK_JOB = 'job';
235 const LINK_COMPANY = 'hq';
236 const LINK_PROFILE = 'home';
237
fc10e72b 238 public $flags;
9f21bd15
RB
239 public $link_id;
240 public $link_type;
241
9f21bd15 242 public $text;
df85e426 243 public $postalCode;
fc10e72b
RB
244 public $latitude;
245 public $longitude;
246
247 public $locality;
248 public $subAdministrativeArea;
249 public $administrativeArea;
9f21bd15
RB
250 public $country;
251
252 private $phones = array();
253
254 /** Fields are:
255 * pîd, id, link_id, link_type, flags, text, postcode, country
256 */
257 public function __construct($data)
258 {
259 foreach ($data as $key => $val) {
260 $this->$key = $val;
261 }
262 }
263
264 public function addPhone(Phone &$phone)
265 {
266 if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
267 $this->phones[] = $phone;
268 }
269 }
270
271 public function phones()
272 {
273 return $this->phones;
274 }
3bad2574
RB
275
276 public function hasFlags($flags)
277 {
278 return $flags & $this->flags;
279 }
9f21bd15
RB
280}
281// }}}
a060e1c3
RB
282// {{{ class Education
283class Education
284{
285 public $eduid;
286 public $degreeid;
287 public $fieldid;
288
289 public $entry_year;
290 public $grad_year;
291 public $program;
292 public $flags;
293
294 public function __construct(array $data)
295 {
296 $this->eduid = $data['eduid'];
297 $this->degreeid = $data['degreeid'];
298 $this->fieldid = $data['fieldid'];
299
300 $this->entry_year = $data['entry_year'];
301 $this->grad_year = $data['grad_year'];
302 $this->program = $data['program'];
303 $this->flags = new PlFlagSet($data['flags']);
304 }
305}
306// }}}
9f21bd15
RB
307
308// {{{ class ProfileEducation [ Field ]
309class ProfileEducation extends ProfileField
310{
a060e1c3 311 private $educations = array();
9f21bd15 312
56afc44b 313 public function __construct(PlIterator $it)
9f21bd15 314 {
a060e1c3 315 $this->pid = $it->value();
9f21bd15
RB
316 $this->visibility = Profile::VISIBILITY_PUBLIC;
317 while ($edu = $it->next()) {
a060e1c3
RB
318 $this->educations[$edu['id']] = new Education($edu);
319 }
320 }
321
322 public function get($flags, $limit)
323 {
324 $educations = array();
325 $year = getdate();
326 $year = $year['year'];
327 $nb = 0;
328 foreach ($this->educations as $id => $edu) {
329 if (
330 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
331 ||
332 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
333 ||
334 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
335 ||
336 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
337 ) {
338 $educations[$id] = $edu;
339 ++$nb;
340 }
341 if ($limit != null && $nb >= $limit) {
342 break;
343 }
9f21bd15 344 }
a060e1c3 345 return PlIteratorUtils::fromArray($educations);
9f21bd15
RB
346 }
347
348 public static function fetchData(array $pids, $visibility)
349 {
a060e1c3
RB
350 $data = XDB::iterator('SELECT id, pid, eduid, degreeid, fieldid,
351 entry_year, grad_year, program, flags
9f21bd15
RB
352 FROM profile_education
353 WHERE pid IN {?}
a060e1c3
RB
354 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
355 NOT FIND_IN_SET(\'primary\', flags), entry_year, id',
356 $pids);
9f21bd15
RB
357
358 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
359 }
360}
361// }}}
362// {{{ class ProfileMedals [ Field ]
363class ProfileMedals extends ProfileField
364{
365 public $medals = array();
366
56afc44b 367 public function __construct(PlIterator $it)
9f21bd15
RB
368 {
369 while ($medal = $it->next()) {
370 $this->medals[$medal['mid']] = $medal['gid'];
371 }
372 }
373
374 public static function fetchData(array $pids, $visibility)
375 {
376 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid
377 FROM profile_medals AS pm
378 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
379 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
380 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
381 XDB::formatArray($pids),
382 XDB::formatArray($visibility)
383 );
384
385 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
386 }
387}
388// }}}
389// {{{ class ProfileNetworking [ Field ]
390class ProfileNetworking extends ProfileField
391{
392 private $networks = array();
9f21bd15 393
56afc44b 394 public function __construct(PlIterator $it)
9f21bd15
RB
395 {
396 while ($network = $it->next()) {
397 $this->networks[$network['nwid']] = $network['address'];
9f21bd15
RB
398 }
399 }
400
401 public static function fetchData(array $pids, $visibility)
402 {
d4d395bb 403 $data = XDB::iterator('SELECT pid, nwid, address, network_type
9f21bd15
RB
404 FROM profile_networking
405 WHERE pid IN {?} AND pub IN {?}
d4d395bb
RB
406 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
407 network_type, nwid',
408 $pids, $visibility);
9f21bd15
RB
409
410 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
411 }
412
d4d395bb 413 public function get($flags, $limit = null)
9f21bd15
RB
414 {
415 $nws = array();
d4d395bb
RB
416 $nb = 0;
417 foreach ($this->networks as $id => $nw) {
418 // XXX hardcoded reference to web site index
419 if (
420 (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0)
421 ||
422 (! ($flags & self::NETWORKING_WEB))
423 ) {
424 $nws[$id] = $nw;
425 ++$nb;
426 }
427 if ($nb >= $limit) {
428 break;
9f21bd15
RB
429 }
430 }
d4d395bb 431 return PlIteratorUtils::fromArray($nws);
9f21bd15
RB
432 }
433}
434// }}}
435// {{{ class ProfilePhoto [ Field ]
436class ProfilePhoto extends ProfileField
437{
438 public $pic;
439
440 public function __construct(array $data)
441 {
442 if ($data == null || count($data) == 0) {
443 $this->pic = null;
444 } else {
445 $this->pid = $data['pid'];
446 $this->pic = PlImage::fromDATA($data['attach'],
447 $data['attachmime'],
448 $data['x'],
449 $data['y']);
450 }
451 }
452
453 public static function fetchData(array $pids, $visibility)
454 {
455 $data = XDB::iterator('SELECT *
456 FROM profile_photos
457 WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?}
458 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
0f9af912 459 $pids, $visibility);
9f21bd15
RB
460
461 return $data;
462 }
463}
464// }}}
465// {{{ class ProfileCorps [ Field ]
466class ProfileCorps extends ProfileField
467{
468 public $original;
469 public $current;
470 public $rank;
471
56afc44b 472 public function __construct(array $data)
9f21bd15
RB
473 {
474 $this->original = $data['original_corpsid'];
475 $this->current = $data['current_corpsid'];
476 $this->rank = $data['rankid'];
477 $this->visibility = $data['corps_pub'];
478 }
479
480 public static function fetchData(array $pids, $visibility)
481 {
482 $data = XDB::iterator('SELECT pid, original_corpsid, current_corpsid,
483 rankid
484 FROM profile_corps
485 WHERE pid IN {?} AND corps_pub IN {?}
486 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
487 XDB::formatArray($pids),
488 XDB::formatArray($visibility)
489 );
490
491 return $data;
492 }
493}
494// }}}
495
496/** Loading of data for a Profile :
497 * 1) load jobs, addresses, phones
498 * 2) attach phones to addresses, jobs and profiles
499 * 3) attach addresses to jobs and profiles
500 */
501
502// {{{ class ProfileAddresses [ Field ]
503class ProfileAddresses extends ProfileField
504{
505 private $addresses = array();
506
3bad2574 507 public function __construct(PlIterator $it)
9f21bd15 508 {
3bad2574
RB
509 if ($it instanceof PlInnerSubIterator) {
510 $this->pid = $it->value();
511 }
512
9f21bd15 513 while ($addr = $it->next()) {
3bad2574
RB
514 $this->addresses[] = new Address($addr);
515 }
516 }
517
518 public function get($flags, $limit = null)
519 {
520 $res = array();
521 $nb = 0;
522 foreach ($this->addresses as $addr) {
523 if ($addr->hasFlags($flags)) {
524 $res[] = $addr;
525 $nb++;
526 }
527 if ($limit != null && $nb == $limit) {
528 break;
529 }
9f21bd15 530 }
3bad2574 531 return PlIteratorUtils::fromArray($res);
9f21bd15
RB
532 }
533
534 public static function fetchData(array $pids, $visibility)
535 {
fc10e72b
RB
536 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
537 IF(pa.type = \'home\', pid, jobid) AS link_id,
538 pa.text, pa.postalCode, pa.latitude, pa.longitude,
539 gl.name AS locality, gas.name AS subAdministrativeArea,
540 ga.name AS administrativeArea, gc.countryFR AS country
541 FROM profile_addresses AS pa
542 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
543 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
544 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
545 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
546 WHERE pa.pid in {?} AND pa.pub IN {?}
9f21bd15 547 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
0f9af912 548 $pids, $visibility);
9f21bd15
RB
549
550 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
551 }
552
df85e426 553 public function addPhones(ProfilePhones $phones)
9f21bd15 554 {
df85e426
RB
555 $p = $phones->get(0);
556 while ($phone = $p->next()) {
949fc736
RB
557 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
558 $this->addresses[$phone->link_id]->addPhone($phone);
9f21bd15
RB
559 }
560 }
9f21bd15
RB
561 }
562}
563// }}}
564// {{{ class ProfilePhones [ Field ]
565class ProfilePhones extends ProfileField
566{
567 private $phones = array();
568
56afc44b 569 public function __construct(PlIterator $phones)
9f21bd15
RB
570 {
571 while ($phone = $it->next()) {
572 $this->phones[] = Phone::buildFromData($phone);
573 }
574 }
575
df85e426
RB
576 public function get($flags, $limit = null)
577 {
578 $phones = array();
579 $nb = 0;
580 foreach ($this->phones as $id => $phone) {
581 $phones[$id] = $phone;
582 ++$nb;
583 if ($limit != null && $nb == $limit) {
584 break;
585 }
586 }
587 return PlIteratorUtils::fromArray($phones);
588 }
589
9f21bd15
RB
590 public static function fetchData(array $pids, $visibility)
591 {
592 $data = XDB::iterator('SELECT type, search, display, link_type, comment
593 FROM profile_phones
594 WHERE pid IN {?} AND pub IN {?}
595 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
596 XDB::formatArray($pids),
597 XDB::formatArray($visibility)
598 );
599 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
600 }
601}
602// }}}
603// {{{ class ProfileJobs [ Field ]
604class ProfileJobs extends ProfileField
605{
606 private $jobs = array();
607
56afc44b 608 public function __construct(PlIterator $jobs)
9f21bd15
RB
609 {
610 while ($job = $jobs->next()) {
0907501b 611 $this->jobs[$job['id']] = new Job($job);
9f21bd15
RB
612 }
613 }
614
615 public static function fetchData(array $pids, $visibility)
616 {
02306579 617 CompanyList::preload($pids);
17982ebf
RB
618 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
619 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
620 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
621 pjssse.name AS subsubsector
622 FROM profile_job AS pj
623 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
624 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
625 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
626 WHERE pj.pid IN {?} AND pj.pub IN {?}
949fc736 627 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
17982ebf 628 pj.id',
949fc736 629 $visibility, $pids, $visibility);
9f21bd15
RB
630 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
631 }
632
949fc736
RB
633 public function get($flags, $limit = null)
634 {
635 $jobs = array();
636 $nb = 0;
637 foreach ($this->jobs as $id => $job) {
638 $jobs[$id] = $job;
639 ++$nb;
640 if ($limit != null && $nb >= $limit) {
641 break;
642 }
643 }
644 return PlIteratorUtils::fromArray($jobs);
645 }
646
df85e426 647 public function addPhones(ProfilePhones $phones)
9f21bd15 648 {
df85e426
RB
649 $p = $phones->get(0);
650 while ($phone = $p->next()) {
949fc736
RB
651 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
652 $this->jobs[$phone->link_id]->addPhones($phone);
9f21bd15
RB
653 }
654 }
9f21bd15
RB
655 }
656
df85e426 657 public static function addAddresses(ProfileAddresses $addresses)
9f21bd15 658 {
df85e426
RB
659 $a = $addresses->get(Profile::ADDRESS_PRO);
660 while ($address = $a->next()) {
949fc736
RB
661 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
662 $this->jobs[$address->link_id]->setAddress($address);
9f21bd15
RB
663 }
664 }
9f21bd15
RB
665 }
666
949fc736 667 public static function addCompanies(array $companies)
9f21bd15 668 {
949fc736 669 foreach ($this->jobs as $job)
9f21bd15 670 {
17982ebf 671 $job->company = $companies[$job->jobid];
9f21bd15 672 }
9f21bd15
RB
673 }
674}
675// }}}
676
677// {{{ class CompanyList
678class CompanyList
679{
680 static private $fullload = false;
681 static private $companies = array();
682
683 static public function preload($pids = array())
684 {
685 if (self::$fullload) {
686 return;
687 }
688 // Load raw data
689 if (count($pids)) {
02306579
RB
690 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
691 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
9f21bd15
RB
692 } else {
693 $join = '';
694 $where = '';
695 }
696
df85e426
RB
697 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
698 pa.flags, pa.text, pa.postalCode, pa.countryId,
699 pa.type, pa.pub
9f21bd15
RB
700 FROM profile_job_enum AS pje
701 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
702 ' . $join . '
703 ' . $where);
704 while ($row = $it->next()) {
df85e426
RB
705 $cp = new Company($row);
706 $addr = new Address($row);
9f21bd15
RB
707 $cp->setAddress($addr);
708 self::$companies[$row['id']] = $cp;
709 }
710
711 // TODO: add phones to addresses
712 if (count($pids) == 0) {
713 self::$fullload = true;
714 }
715 }
716
df85e426 717 static public function get($id)
9f21bd15
RB
718 {
719 if (!array_key_exists($id, self::$companies)) {
720 self::preload();
721 }
722 return self::$companies[$id];
723 }
724}
725
726// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
727?>