No need for batch loading of Photo data
[platal.git] / include / profilefields.inc.php
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 */
26 abstract 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)"
37 * XXX MUST be reimplemented for each kind of ProfileField
38 */
39 public static function fetchData(array $pids, $visibility)
40 {
41 return PlIteratorUtils::emptyIterator();
42 }
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 }
65
66 public static function getForPID($cls, $pid, $visibility)
67 {
68 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
69 return $it->next();
70 }
71 }
72 // }}}
73
74 // {{{ class ProfileFieldIterator
75 class 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
115 class 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
145 class 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 */
157 public function __construct($data)
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
181 class Job
182 {
183 public $pid;
184 public $id;
185
186 public $company = null;
187 private $phones = array();
188 private $address = null;
189
190 public $jobid;
191
192 public $description;
193 public $user_site;
194 public $user_email;
195
196 public $sector;
197 public $subsector;
198 public $subsubsector;
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 }
208 $this->company = CompanyList::get($this->jobid);
209 }
210
211 public function phones()
212 {
213 return $this->phones;
214 }
215
216 public function address()
217 {
218 return $this->address;
219 }
220
221 public function addPhone(Phone &$phone)
222 {
223 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
224 $this->phones[] = $phone;
225 }
226 }
227
228 public function setAddress(Address $address)
229 {
230 if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
231 $this->address = $address;
232 }
233 }
234 }
235 // }}}
236 // {{{ class Address
237 class Address
238 {
239 const LINK_JOB = 'job';
240 const LINK_COMPANY = 'hq';
241 const LINK_PROFILE = 'home';
242
243 public $flags;
244 public $link_id;
245 public $link_type;
246
247 public $text;
248 public $postalCode;
249 public $latitude;
250 public $longitude;
251
252 public $locality;
253 public $subAdministrativeArea;
254 public $administrativeArea;
255 public $country;
256
257 public $comment;
258
259 private $phones = array();
260
261 /** Fields are:
262 * pîd, id, link_id, link_type, flags, text, postcode, country
263 */
264 public function __construct($data)
265 {
266 foreach ($data as $key => $val) {
267 $this->$key = $val;
268 }
269 $this->flags = new PlFlagSet($this->flags);
270 }
271
272 public function addPhone(Phone &$phone)
273 {
274 if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
275 $this->phones[] = $phone;
276 }
277 }
278
279 public function phones()
280 {
281 return $this->phones;
282 }
283
284 public function hasFlag($flag)
285 {
286 return $this->flags->hasFlag($flag);
287 }
288 }
289 // }}}
290 // {{{ class Education
291 class Education
292 {
293 public $id;
294 public $pid;
295
296 public $entry_year;
297 public $grad_year;
298 public $program;
299 public $flags;
300
301 public $school;
302 public $school_short;
303 public $school_url;
304 public $country;
305
306 public $degree;
307 public $degree_short;
308 public $degree_level;
309
310 public $field;
311
312 public function __construct(array $data)
313 {
314 foreach ($data as $key => $val) {
315 $this->$key = $val;
316 }
317 $this->flags = new PlFlagSet($this->flags);
318 }
319 }
320 // }}}
321
322 // {{{ class ProfileEducation [ Field ]
323 class ProfileEducation extends ProfileField
324 {
325 private $educations = array();
326
327 public function __construct(PlInnerSubIterator $it)
328 {
329 $this->pid = $it->value();
330 $this->visibility = Profile::VISIBILITY_PUBLIC;
331 while ($edu = $it->next()) {
332 $this->educations[$edu['id']] = new Education($edu);
333 }
334 }
335
336 public function get($flags, $limit)
337 {
338 $educations = array();
339 $year = getdate();
340 $year = $year['year'];
341 $nb = 0;
342 foreach ($this->educations as $id => $edu) {
343 if (
344 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
345 ||
346 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
347 ||
348 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
349 ||
350 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
351 ||
352 ($flags & Profile::EDUCATION_ALL)
353 ) {
354 $educations[$id] = $edu;
355 ++$nb;
356 }
357 if ($limit != null && $nb >= $limit) {
358 break;
359 }
360 }
361 return $educations;
362 }
363
364 public static function fetchData(array $pids, $visibility)
365 {
366 $data = XDB::iterator('SELECT pe.id, pe.pid,
367 pe.entry_year, pe.grad_year, pe.program, pe.flags,
368 pee.name AS school, pee.abbreviation AS school_short,
369 pee.url AS school_url, gc.countryFR AS country,
370 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
371 pefe.field
372 FROM profile_education AS pe
373 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
374 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
375 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
376 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
377 WHERE pe.pid IN {?}
378 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
379 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
380 $pids);
381
382 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
383 }
384 }
385 // }}}
386 // {{{ class ProfileMedals [ Field ]
387 class ProfileMedals extends ProfileField
388 {
389 public $medals = array();
390
391 public function __construct(PlInnerSubIterator $it)
392 {
393 $this->pid = $it->value();
394 while ($medal = $it->next()) {
395 $this->medals[$medal['mid']] = $medal;
396 }
397 }
398
399 public static function fetchData(array $pids, $visibility)
400 {
401 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img
402 FROM profile_medals AS pm
403 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
404 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
405 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
406 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
407 $pids, $visibility);
408
409 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
410 }
411 }
412 // }}}
413 // {{{ class ProfileNetworking [ Field ]
414 class ProfileNetworking extends ProfileField
415 {
416 private $networks = array();
417
418 public function __construct(PlIterator $it)
419 {
420 while ($network = $it->next()) {
421 $this->networks[$network['nwid']] = $network['address'];
422 }
423 }
424
425 public static function fetchData(array $pids, $visibility)
426 {
427 $data = XDB::iterator('SELECT pid, nwid, address, network_type
428 FROM profile_networking
429 WHERE pid IN {?} AND pub IN {?}
430 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
431 network_type, nwid',
432 $pids, $visibility);
433
434 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
435 }
436
437 public function get($flags, $limit = null)
438 {
439 $nws = array();
440 $nb = 0;
441 foreach ($this->networks as $id => $nw) {
442 // XXX hardcoded reference to web site index
443 if (
444 (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0)
445 ||
446 (! ($flags & self::NETWORKING_WEB))
447 ) {
448 $nws[$id] = $nw;
449 ++$nb;
450 }
451 if ($nb >= $limit) {
452 break;
453 }
454 }
455 return $nws;
456 }
457 }
458 // }}}
459 // {{{ class ProfileCorps [ Field ]
460 class ProfileCorps extends ProfileField
461 {
462 public $original;
463 public $current;
464
465 public $original_name;
466 public $original_abbrev;
467 public $original_still_exists;
468
469 public $current_name;
470 public $current_abbrev;
471 public $current_still_exists;
472 public $current_rank;
473 public $current_rank_abbrev;
474
475 public function __construct(array $data)
476 {
477 foreach ($data as $key => $val) {
478 $this->$key = $val;
479 }
480 }
481
482 public static function fetchData(array $pids, $visibility)
483 {
484 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
485 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
486 pceo.still_exists AS original_still_exists,
487 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
488 pcec.still_exists AS current_still_exists,
489 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
490 rankid
491 FROM profile_corps AS pc
492 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
493 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
494 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
495 WHERE pc.pid IN {?} AND pc.corps_pub IN {?}
496 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
497 $pids, $visibility);
498
499 return $data;
500 }
501 }
502 // }}}
503
504 /** Loading of data for a Profile :
505 * 1) load jobs, addresses, phones
506 * 2) attach phones to addresses, jobs and profiles
507 * 3) attach addresses to jobs and profiles
508 */
509
510 // {{{ class ProfileAddresses [ Field ]
511 class ProfileAddresses extends ProfileField
512 {
513 private $addresses = array();
514
515 public function __construct(PlIterator $it)
516 {
517 if ($it instanceof PlInnerSubIterator) {
518 $this->pid = $it->value();
519 }
520
521 while ($addr = $it->next()) {
522 $this->addresses[] = new Address($addr);
523 }
524 }
525
526 public function get($flags, $limit = null)
527 {
528 $res = array();
529 $nb = 0;
530 foreach ($this->addresses as $addr) {
531 if (
532 ($flags & Profile::ADDRESS_ALL)
533 ||
534 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
535 ||
536 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
537 ||
538 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == 'home')
539 ||
540 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == 'job')
541 ) {
542 $res[] = $addr;
543 $nb++;
544 }
545 if ($limit != null && $nb == $limit) {
546 break;
547 }
548 }
549 return $res;
550 }
551
552 public static function fetchData(array $pids, $visibility)
553 {
554 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
555 IF(pa.type = \'home\', pid, jobid) AS link_id,
556 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
557 gl.name AS locality, gas.name AS subAdministrativeArea,
558 ga.name AS administrativeArea, gc.countryFR AS country
559 FROM profile_addresses AS pa
560 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
561 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
562 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
563 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
564 WHERE pa.pid in {?} AND pa.pub IN {?}
565 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
566 $pids, $visibility);
567
568 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
569 }
570
571 public function addPhones(ProfilePhones $phones)
572 {
573 $p = $phones->get(0);
574 foreach ($p as $phone) {
575 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
576 $this->addresses[$phone->link_id]->addPhone($phone);
577 }
578 }
579 }
580 }
581 // }}}
582 // {{{ class ProfilePhones [ Field ]
583 class ProfilePhones extends ProfileField
584 {
585 private $phones = array();
586
587 public function __construct(PlInnerSubIterator $it)
588 {
589 $this->pid = $it->value();
590 while ($phone = $it->next()) {
591 $this->phones[] = new Phone($phone);
592 }
593 }
594
595 public function get($flags, $limit = null)
596 {
597 $phones = array();
598 $nb = 0;
599 foreach ($this->phones as $id => $phone) {
600 $phones[$id] = $phone;
601 ++$nb;
602 if ($limit != null && $nb == $limit) {
603 break;
604 }
605 }
606 return $phones;
607 }
608
609 public static function fetchData(array $pids, $visibility)
610 {
611 $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment
612 FROM profile_phones
613 WHERE pid IN {?} AND pub IN {?}
614 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
615 $pids, $visibility);
616 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
617 }
618 }
619 // }}}
620 // {{{ class ProfileJobs [ Field ]
621 class ProfileJobs extends ProfileField
622 {
623 private $jobs = array();
624
625 public function __construct(PlInnerSubIterator $jobs)
626 {
627 $this->pid = $jobs->value();
628 while ($job = $jobs->next()) {
629 $this->jobs[$job['id']] = new Job($job);
630 }
631 }
632
633 public static function fetchData(array $pids, $visibility)
634 {
635 CompanyList::preload($pids);
636 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
637 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
638 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
639 pjssse.name AS subsubsector
640 FROM profile_job AS pj
641 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
642 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
643 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
644 WHERE pj.pid IN {?} AND pj.pub IN {?}
645 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
646 pj.id',
647 $visibility, $pids, $visibility);
648 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
649 }
650
651 public function get($flags, $limit = null)
652 {
653 $jobs = array();
654 $nb = 0;
655 foreach ($this->jobs as $id => $job) {
656 $jobs[$id] = $job;
657 ++$nb;
658 if ($limit != null && $nb >= $limit) {
659 break;
660 }
661 }
662 return $jobs;
663 }
664
665 public function addPhones(ProfilePhones $phones)
666 {
667 $p = $phones->get(0);
668 foreach ($p as $phone) {
669 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
670 $this->jobs[$phone->link_id]->addPhones($phone);
671 }
672 }
673 }
674
675 public static function addAddresses(ProfileAddresses $addresses)
676 {
677 $a = $addresses->get(Profile::ADDRESS_PRO);
678 foreach ($a as $address) {
679 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
680 $this->jobs[$address->link_id]->setAddress($address);
681 }
682 }
683 }
684
685 public static function addCompanies(array $companies)
686 {
687 foreach ($this->jobs as $job)
688 {
689 $job->company = $companies[$job->jobid];
690 }
691 }
692 }
693 // }}}
694
695 // {{{ class CompanyList
696 class CompanyList
697 {
698 static private $fullload = false;
699 static private $companies = array();
700
701 static public function preload($pids = array())
702 {
703 if (self::$fullload) {
704 return;
705 }
706 // Load raw data
707 if (count($pids)) {
708 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
709 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
710 } else {
711 $join = '';
712 $where = '';
713 }
714
715 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
716 pa.flags, pa.text, pa.postalCode, pa.countryId,
717 pa.type, pa.pub
718 FROM profile_job_enum AS pje
719 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
720 ' . $join . '
721 ' . $where);
722 while ($row = $it->next()) {
723 $cp = new Company($row);
724 $addr = new Address($row);
725 $cp->setAddress($addr);
726 self::$companies[$row['id']] = $cp;
727 }
728
729 // TODO: add phones to addresses
730 if (count($pids) == 0) {
731 self::$fullload = true;
732 }
733 }
734
735 static public function get($id)
736 {
737 if (!array_key_exists($id, self::$companies)) {
738 self::preload();
739 }
740 return self::$companies[$id];
741 }
742 }
743
744 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
745 ?>