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