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