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