26d73a1b4cf22515c7ad1adfaec4a3cfbefdcaed
[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, ProfileVisibility $visibility)
40 {
41 return PlIteratorUtils::emptyIterator();
42 }
43
44 public static function buildForPID($cls, $pid, ProfileVisibility $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, ProfileVisibility $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, ProfileVisibility $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, ProfileVisibility $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 while ($edu = $it->next()) {
331 $this->educations[$edu['id']] = new Education($edu);
332 }
333 }
334
335 public function get($flags, $limit)
336 {
337 $educations = array();
338 $year = getdate();
339 $year = $year['year'];
340 $nb = 0;
341 foreach ($this->educations as $id => $edu) {
342 if (
343 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
344 ||
345 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
346 ||
347 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
348 ||
349 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
350 ||
351 ($flags & Profile::EDUCATION_ALL)
352 ) {
353 $educations[$id] = $edu;
354 ++$nb;
355 }
356 if ($limit != null && $nb >= $limit) {
357 break;
358 }
359 }
360 return $educations;
361 }
362
363 public static function fetchData(array $pids, ProfileVisibility $visibility)
364 {
365 $data = XDB::iterator('SELECT pe.id, pe.pid,
366 pe.entry_year, pe.grad_year, pe.program, pe.flags,
367 pee.name AS school, pee.abbreviation AS school_short,
368 pee.url AS school_url, gc.countryFR AS country,
369 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
370 pefe.field
371 FROM profile_education AS pe
372 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
373 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
374 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
375 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
376 WHERE pe.pid IN {?}
377 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
378 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
379 $pids);
380
381 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
382 }
383 }
384 // }}}
385 // {{{ class ProfileMedals [ Field ]
386 class ProfileMedals extends ProfileField
387 {
388 public $medals = array();
389
390 public function __construct(PlInnerSubIterator $it)
391 {
392 $this->pid = $it->value();
393 while ($medal = $it->next()) {
394 $this->medals[$medal['mid']] = $medal;
395 }
396 }
397
398 public static function fetchData(array $pids, ProfileVisibility $visibility)
399 {
400 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img
401 FROM profile_medals AS pm
402 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
403 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
404 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
405 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
406 $pids, $visibility->levels());
407
408 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
409 }
410 }
411 // }}}
412 // {{{ class ProfileNetworking [ Field ]
413 class ProfileNetworking extends ProfileField
414 {
415 private $networks = array();
416
417 public function __construct(PlIterator $it)
418 {
419 while ($network = $it->next()) {
420 $this->networks[$network['nwid']] = $network['address'];
421 }
422 }
423
424 public static function fetchData(array $pids, ProfileVisibility $visibility)
425 {
426 $data = XDB::iterator('SELECT pid, nwid, address, network_type
427 FROM profile_networking
428 WHERE pid IN {?} AND pub IN {?}
429 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
430 network_type, nwid',
431 $pids, $visibility->levels());
432
433 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
434 }
435
436 public function get($flags, $limit = null)
437 {
438 $nws = array();
439 $nb = 0;
440 foreach ($this->networks as $id => $nw) {
441 // XXX hardcoded reference to web site index
442 if (
443 (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0)
444 ||
445 (! ($flags & self::NETWORKING_WEB))
446 ) {
447 $nws[$id] = $nw;
448 ++$nb;
449 }
450 if ($nb >= $limit) {
451 break;
452 }
453 }
454 return $nws;
455 }
456 }
457 // }}}
458 // {{{ class ProfileCorps [ Field ]
459 class ProfileCorps extends ProfileField
460 {
461 public $original;
462 public $current;
463
464 public $original_name;
465 public $original_abbrev;
466 public $original_still_exists;
467
468 public $current_name;
469 public $current_abbrev;
470 public $current_still_exists;
471 public $current_rank;
472 public $current_rank_abbrev;
473
474 public function __construct(array $data)
475 {
476 foreach ($data as $key => $val) {
477 $this->$key = $val;
478 }
479 }
480
481 public static function fetchData(array $pids, ProfileVisibility $visibility)
482 {
483 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
484 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
485 pceo.still_exists AS original_still_exists,
486 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
487 pcec.still_exists AS current_still_exists,
488 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
489 rankid
490 FROM profile_corps AS pc
491 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
492 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
493 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
494 WHERE pc.pid IN {?} AND pc.corps_pub IN {?}
495 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
496 $pids, $visibility->levels());
497
498 return $data;
499 }
500 }
501 // }}}
502
503 /** Loading of data for a Profile :
504 * 1) load jobs, addresses, phones
505 * 2) attach phones to addresses, jobs and profiles
506 * 3) attach addresses to jobs and profiles
507 */
508
509 // {{{ class ProfileAddresses [ Field ]
510 class ProfileAddresses extends ProfileField
511 {
512 private $addresses = array();
513
514 public function __construct(PlIterator $it)
515 {
516 if ($it instanceof PlInnerSubIterator) {
517 $this->pid = $it->value();
518 }
519
520 while ($addr = $it->next()) {
521 $this->addresses[] = new Address($addr);
522 }
523 }
524
525 public function get($flags, $limit = null)
526 {
527 $res = array();
528 $nb = 0;
529 foreach ($this->addresses as $addr) {
530 if (
531 ($flags & Profile::ADDRESS_ALL)
532 ||
533 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
534 ||
535 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
536 ||
537 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == 'home')
538 ||
539 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == 'job')
540 ) {
541 $res[] = $addr;
542 $nb++;
543 }
544 if ($limit != null && $nb == $limit) {
545 break;
546 }
547 }
548 return $res;
549 }
550
551 public static function fetchData(array $pids, ProfileVisibility $visibility)
552 {
553 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
554 IF(pa.type = \'home\', pid, jobid) AS link_id,
555 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
556 gl.name AS locality, gas.name AS subAdministrativeArea,
557 ga.name AS administrativeArea, gc.countryFR AS country
558 FROM profile_addresses AS pa
559 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
560 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
561 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
562 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
563 WHERE pa.pid in {?} AND pa.pub IN {?}
564 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
565 $pids, $visibility->levels());
566
567 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
568 }
569
570 public function addPhones(ProfilePhones $phones)
571 {
572 $p = $phones->get(0);
573 foreach ($p as $phone) {
574 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
575 $this->addresses[$phone->link_id]->addPhone($phone);
576 }
577 }
578 }
579 }
580 // }}}
581 // {{{ class ProfilePhones [ Field ]
582 class ProfilePhones extends ProfileField
583 {
584 private $phones = array();
585
586 public function __construct(PlInnerSubIterator $it)
587 {
588 $this->pid = $it->value();
589 while ($phone = $it->next()) {
590 $this->phones[] = new Phone($phone);
591 }
592 }
593
594 public function get($flags, $limit = null)
595 {
596 $phones = array();
597 $nb = 0;
598 foreach ($this->phones as $id => $phone) {
599 $phones[$id] = $phone;
600 ++$nb;
601 if ($limit != null && $nb == $limit) {
602 break;
603 }
604 }
605 return $phones;
606 }
607
608 public static function fetchData(array $pids, ProfileVisibility $visibility)
609 {
610 $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment
611 FROM profile_phones
612 WHERE pid IN {?} AND pub IN {?}
613 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
614 $pids, $visibility->levels());
615 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
616 }
617 }
618 // }}}
619 // {{{ class ProfileJobs [ Field ]
620 class ProfileJobs extends ProfileField
621 {
622 private $jobs = array();
623
624 public function __construct(PlInnerSubIterator $jobs)
625 {
626 $this->pid = $jobs->value();
627 while ($job = $jobs->next()) {
628 $this->jobs[$job['id']] = new Job($job);
629 }
630 }
631
632 public static function fetchData(array $pids, ProfileVisibility $visibility)
633 {
634 CompanyList::preload($pids);
635 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
636 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
637 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
638 pjssse.name AS subsubsector
639 FROM profile_job AS pj
640 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
641 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
642 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
643 WHERE pj.pid IN {?} AND pj.pub IN {?}
644 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
645 pj.id',
646 $visibility->levels(), $pids, $visibility->levels());
647 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
648 }
649
650 public function get($flags, $limit = null)
651 {
652 $jobs = array();
653 $nb = 0;
654 foreach ($this->jobs as $id => $job) {
655 $jobs[$id] = $job;
656 ++$nb;
657 if ($limit != null && $nb >= $limit) {
658 break;
659 }
660 }
661 return $jobs;
662 }
663
664 public function addPhones(ProfilePhones $phones)
665 {
666 $p = $phones->get(0);
667 foreach ($p as $phone) {
668 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
669 $this->jobs[$phone->link_id]->addPhones($phone);
670 }
671 }
672 }
673
674 public static function addAddresses(ProfileAddresses $addresses)
675 {
676 $a = $addresses->get(Profile::ADDRESS_PRO);
677 foreach ($a as $address) {
678 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
679 $this->jobs[$address->link_id]->setAddress($address);
680 }
681 }
682 }
683
684 public static function addCompanies(array $companies)
685 {
686 foreach ($this->jobs as $job)
687 {
688 $job->company = $companies[$job->jobid];
689 }
690 }
691 }
692 // }}}
693
694 // {{{ class CompanyList
695 class CompanyList
696 {
697 static private $fullload = false;
698 static private $companies = array();
699
700 static public function preload($pids = array())
701 {
702 if (self::$fullload) {
703 return;
704 }
705 // Load raw data
706 if (count($pids)) {
707 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
708 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
709 } else {
710 $join = '';
711 $where = '';
712 }
713
714 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
715 pa.flags, pa.text, pa.postalCode, pa.countryId,
716 pa.type, pa.pub
717 FROM profile_job_enum AS pje
718 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
719 ' . $join . '
720 ' . $where);
721 while ($row = $it->next()) {
722 $cp = new Company($row);
723 $addr = new Address($row);
724 $cp->setAddress($addr);
725 self::$companies[$row['id']] = $cp;
726 }
727
728 // TODO: add phones to addresses
729 if (count($pids) == 0) {
730 self::$fullload = true;
731 }
732 }
733
734 static public function get($id)
735 {
736 if (!array_key_exists($id, self::$companies)) {
737 self::preload();
738 }
739 return self::$companies[$id];
740 }
741 }
742
743 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
744 ?>