Fix display of the type of the phone numbers in the pdf. (Closes #1151)
[platal.git] / include / profilefields.inc.php
CommitLineData
9f21bd15
RB
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 */
26abstract class ProfileField
27{
1f8250e4 28 public static $fields = array(
4d1f0f6b
RB
29 Profile::FETCH_ADDRESSES => 'ProfileAddresses',
30 Profile::FETCH_CORPS => 'ProfileCorps',
31 Profile::FETCH_EDU => 'ProfileEducation',
32 Profile::FETCH_JOBS => 'ProfileJobs',
33 Profile::FETCH_MEDALS => 'ProfileMedals',
34 Profile::FETCH_NETWORKING => 'ProfileNetworking',
35 Profile::FETCH_PHONES => 'ProfilePhones',
36 Profile::FETCH_MENTOR_SECTOR => 'ProfileMentoringSectors',
37 Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
1f8250e4
RB
38 );
39
9f21bd15
RB
40 /** The profile to which this field belongs
41 */
42 public $pid;
43
44 /** Fetches data from the database for the given pids, compatible with
45 * the visibility context.
46 * @param $pids An array of pids
47 * @param $visibility The level of visibility fetched fields must have
48 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
b3c0f165 49 * XXX MUST be reimplemented for each kind of ProfileField
9f21bd15 50 */
1a9affb7 51 public static function fetchData(array $pids, ProfileVisibility $visibility)
b3c0f165
RB
52 {
53 return PlIteratorUtils::emptyIterator();
54 }
9f21bd15 55
1a9affb7 56 public static function buildForPID($cls, $pid, ProfileVisibility $visibility)
9f21bd15
RB
57 {
58 $res = self::buildFromPIDs($cls, array($pid), $visibility);
59 return array_pop($res);
60 }
61
62 /** Build a list of ProfileFields from a set of pids
63 * @param $cls The name of the field to create ('ProfileMedals', ...)
64 * @param $pids An array of pids
65 * @param $visibility An array of allowed visibility contexts
66 * @return An array of $pid => ProfileField
67 */
1a9affb7 68 public static function buildFromPIDs($cls, array $pids, ProfileVisibility $visibility)
9f21bd15
RB
69 {
70 $it = new ProfileFieldIterator($cls, $pids, $visibility);
71 $res = array();
72 while ($pf = $it->next()) {
73 $res[$pf->pid] = $pf;
74 }
75 return $res;
76 }
990cb17b 77
1a9affb7 78 public static function getForPID($cls, $pid, ProfileVisibility $visibility)
990cb17b
RB
79 {
80 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
81 return $it->next();
82 }
9f21bd15
RB
83}
84// }}}
85
86// {{{ class ProfileFieldIterator
87class ProfileFieldIterator implements PlIterator
88{
89 private $data;
90 private $cls;
91
1a9affb7 92 public function __construct($cls, array $pids, ProfileVisibility $visibility)
9f21bd15
RB
93 {
94 $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
95 $this->cls = $cls;
96 }
97
98 public function next()
99 {
100 $d = $this->data->next();
101 if ($d == null) {
102 return null;
103 } else {
104 $cls = $this->cls;
105 return new $cls($d);
106 }
107 }
108
109 public function total()
110 {
111 return $this->data->total();
112 }
113
114 public function first()
115 {
116 return $this->data->first();
117 }
118
119 public function last()
120 {
121 return $this->data->last();
122 }
123}
124// }}}
125
126// {{{ class Phone
127class Phone
128{
129 const TYPE_FAX = 'fax';
130 const TYPE_FIXED = 'fixed';
131 const TYPE_MOBILE = 'mobile';
132 public $type;
133
134 public $search;
135 public $display;
136 public $comment = '';
137
dc953a0c 138 const LINK_JOB = 'pro';
9f21bd15
RB
139 const LINK_ADDRESS = 'address';
140 const LINK_PROFILE = 'user';
141 const LINK_COMPANY = 'hq';
142 public $link_type;
143 public $link_id;
144
dc953a0c
RB
145 public $id;
146
9f21bd15
RB
147 /** Fields are :
148 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
149 */
150 public function __construct($data)
151 {
152 foreach ($data as $key => $val) {
153 $this->$key = $val;
154 }
155 }
3c985c08
RB
156
157 public function hasFlags($flags) {
158 return $this->hasType($flags) && $this->hasLink($flags);
159 }
160
161 /** Returns true if this phone's type matches the flags
162 */
163 public function hasType($flags) {
164 $flags = $flags & Profile::PHONE_TYPE_ANY;
165 return (
166 ($flags == Profile::PHONE_TYPE_ANY)
167 ||
168 (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
169 ||
170 (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
171 ||
172 (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
173 );
174 }
175
89d17c5a
FB
176 /** User accessible version of the type
177 */
178 public function displayType($short = false)
179 {
180 switch ($this->type) {
181 case Phone::TYPE_FIXED:
182 return $short ? 'Tél' : 'Fixe';
183 case Phone::TYPE_FAX:
184 return 'Fax';
185 case Phone::TYPE_MOBILE:
186 return $short ? 'Mob' : 'Mobile';
187 default:
188 return $this->type;
189 }
190 }
191
3c985c08
RB
192 /** Returns true if this phone's link matches the flags
193 */
194 public function hasLink($flags) {
195 $flags = $flags & Profile::PHONE_LINK_ANY;
196 return (
197 ($flags == Profile::PHONE_LINK_ANY)
198 ||
199 (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
200 ||
201 (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
202 ||
203 (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
204 ||
205 (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
206 );
207 }
9f21bd15
RB
208}
209// }}}
210// {{{ class Company
211class Company
212{
213 public $id;
214 public $name;
215 public $acronym;
216 public $url;
217 public $phone = null;
218 public $address = null;
219
220 /** Fields are:
221 * $id, $name, $acronym, $url
222 */
df85e426 223 public function __construct($data)
9f21bd15
RB
224 {
225 foreach ($data as $key => $val) {
226 $this->$key = $val;
227 }
228 }
229
230 public function setPhone(Phone &$phone)
231 {
232 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
233 $this->phone = $phone;
234 }
235 }
236
237 public function setAddress(Address &$address)
238 {
239 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
240 $this->address = $address;
241 }
242 }
243
244}
245// }}}
246// {{{ class Job
247class Job
248{
249 public $pid;
250 public $id;
251
17982ebf 252 public $company = null;
9f21bd15
RB
253 private $phones = array();
254 private $address = null;
255
17982ebf 256 public $jobid;
9f21bd15
RB
257
258 public $description;
17982ebf
RB
259 public $user_site;
260 public $user_email;
261
262 public $sector;
263 public $subsector;
264 public $subsubsector;
9f21bd15
RB
265
266 /** Fields are:
267 * pid, id, company_id, description, url, email
268 */
269 public function __construct($data)
270 {
271 foreach ($data as $key => $val) {
272 $this->$key = $val;
273 }
17982ebf 274 $this->company = CompanyList::get($this->jobid);
9f21bd15
RB
275 }
276
277 public function phones()
278 {
279 return $this->phones;
280 }
281
f2f314c4
RB
282 public function address()
283 {
284 return $this->address;
285 }
286
9f21bd15
RB
287 public function addPhone(Phone &$phone)
288 {
289 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
290 $this->phones[] = $phone;
291 }
292 }
293
294 public function setAddress(Address $address)
295 {
296 if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
297 $this->address = $address;
298 }
299 }
9f21bd15
RB
300}
301// }}}
302// {{{ class Address
303class Address
304{
305 const LINK_JOB = 'job';
306 const LINK_COMPANY = 'hq';
307 const LINK_PROFILE = 'home';
308
fc10e72b 309 public $flags;
9f21bd15
RB
310 public $link_id;
311 public $link_type;
312
9f21bd15 313 public $text;
df85e426 314 public $postalCode;
fc10e72b
RB
315 public $latitude;
316 public $longitude;
317
318 public $locality;
319 public $subAdministrativeArea;
320 public $administrativeArea;
9f21bd15
RB
321 public $country;
322
790c52a8
RB
323 public $comment;
324
9f21bd15
RB
325 private $phones = array();
326
327 /** Fields are:
328 * pîd, id, link_id, link_type, flags, text, postcode, country
329 */
330 public function __construct($data)
331 {
332 foreach ($data as $key => $val) {
333 $this->$key = $val;
334 }
790c52a8 335 $this->flags = new PlFlagSet($this->flags);
9f21bd15
RB
336 }
337
338 public function addPhone(Phone &$phone)
339 {
340 if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
dc953a0c 341 $this->phones[$phone->id] = $phone;
9f21bd15
RB
342 }
343 }
344
345 public function phones()
346 {
347 return $this->phones;
348 }
3bad2574 349
790c52a8 350 public function hasFlag($flag)
3bad2574 351 {
da4dc2a9
FB
352 if (!$this->flags instanceof PlFlagSet) {
353 $this->flags = new PlFlagSet($this->flags);
354 }
790c52a8 355 return $this->flags->hasFlag($flag);
3bad2574 356 }
9f21bd15
RB
357}
358// }}}
a060e1c3
RB
359// {{{ class Education
360class Education
361{
b7eec8d3
RB
362 public $id;
363 public $pid;
a060e1c3
RB
364
365 public $entry_year;
366 public $grad_year;
367 public $program;
368 public $flags;
369
b7eec8d3
RB
370 public $school;
371 public $school_short;
372 public $school_url;
373 public $country;
374
375 public $degree;
376 public $degree_short;
377 public $degree_level;
378
a955e787
RB
379 public $field;
380
a060e1c3
RB
381 public function __construct(array $data)
382 {
b7eec8d3
RB
383 foreach ($data as $key => $val) {
384 $this->$key = $val;
385 }
386 $this->flags = new PlFlagSet($this->flags);
a060e1c3
RB
387 }
388}
389// }}}
9f21bd15
RB
390
391// {{{ class ProfileEducation [ Field ]
392class ProfileEducation extends ProfileField
393{
a060e1c3 394 private $educations = array();
9f21bd15 395
b7eec8d3 396 public function __construct(PlInnerSubIterator $it)
9f21bd15 397 {
a060e1c3 398 $this->pid = $it->value();
9f21bd15 399 while ($edu = $it->next()) {
a060e1c3
RB
400 $this->educations[$edu['id']] = new Education($edu);
401 }
402 }
403
404 public function get($flags, $limit)
405 {
406 $educations = array();
407 $year = getdate();
408 $year = $year['year'];
409 $nb = 0;
410 foreach ($this->educations as $id => $edu) {
411 if (
412 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
413 ||
414 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
415 ||
416 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
417 ||
418 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
ad336893
RB
419 ||
420 ($flags & Profile::EDUCATION_ALL)
a060e1c3
RB
421 ) {
422 $educations[$id] = $edu;
423 ++$nb;
424 }
425 if ($limit != null && $nb >= $limit) {
426 break;
427 }
9f21bd15 428 }
598c57f6 429 return $educations;
9f21bd15
RB
430 }
431
1a9affb7 432 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 433 {
b7eec8d3
RB
434 $data = XDB::iterator('SELECT pe.id, pe.pid,
435 pe.entry_year, pe.grad_year, pe.program, pe.flags,
436 pee.name AS school, pee.abbreviation AS school_short,
437 pee.url AS school_url, gc.countryFR AS country,
438 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
439 pefe.field
440 FROM profile_education AS pe
441 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
442 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
443 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
444 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
445 WHERE pe.pid IN {?}
a060e1c3 446 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
b7eec8d3 447 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
a060e1c3 448 $pids);
9f21bd15
RB
449
450 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
451 }
452}
453// }}}
454// {{{ class ProfileMedals [ Field ]
455class ProfileMedals extends ProfileField
456{
457 public $medals = array();
458
f35e9630 459 public function __construct(PlInnerSubIterator $it)
9f21bd15 460 {
f35e9630 461 $this->pid = $it->value();
9f21bd15 462 while ($medal = $it->next()) {
cef9e8c8 463 $this->medals[$medal['mid']] = $medal;
9f21bd15
RB
464 }
465 }
466
1a9affb7 467 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 468 {
c6d16b24 469 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
9f21bd15
RB
470 FROM profile_medals AS pm
471 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
cef9e8c8 472 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
c6d16b24 473 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
9f21bd15
RB
474 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
475 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
1a9affb7 476 $pids, $visibility->levels());
9f21bd15
RB
477
478 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
479 }
480}
481// }}}
482// {{{ class ProfileNetworking [ Field ]
483class ProfileNetworking extends ProfileField
484{
485 private $networks = array();
9f21bd15 486
34de4b20 487 public function __construct(PlInnerSubIterator $it)
9f21bd15 488 {
34de4b20 489 $this->pid = $it->value();
9f21bd15 490 while ($network = $it->next()) {
34de4b20
PC
491 $network['network_type'] = new PlFlagSet($network['network_type']);
492 $this->networks[$network['id']] = $network;
9f21bd15
RB
493 }
494 }
495
1a9affb7 496 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 497 {
1f5cd004 498 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
34de4b20
PC
499 FROM profile_networking AS pn
500 LEFT JOIN profile_networking_enum AS pne USING(nwid)
9f21bd15 501 WHERE pid IN {?} AND pub IN {?}
d4d395bb 502 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
34de4b20 503 pn.nwid, id',
1a9affb7 504 $pids, $visibility->levels());
9f21bd15
RB
505
506 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
507 }
508
d4d395bb 509 public function get($flags, $limit = null)
9f21bd15 510 {
34de4b20
PC
511 if (!$flags) {
512 $flags = Profile::NETWORKING_ALL;
513 }
9f21bd15 514 $nws = array();
d4d395bb
RB
515 $nb = 0;
516 foreach ($this->networks as $id => $nw) {
34de4b20
PC
517 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
518 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
519 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
520 ($flags & Profile::NETWORKING_ALL)) {
d4d395bb
RB
521 $nws[$id] = $nw;
522 ++$nb;
1f5cd004 523 if (isset($limit) && $nb >= $limit) {
34de4b20
PC
524 break;
525 }
9f21bd15
RB
526 }
527 }
598c57f6 528 return $nws;
9f21bd15
RB
529 }
530}
531// }}}
9f21bd15
RB
532// {{{ class ProfileCorps [ Field ]
533class ProfileCorps extends ProfileField
534{
535 public $original;
536 public $current;
0396c259
RB
537
538 public $original_name;
539 public $original_abbrev;
540 public $original_still_exists;
541
542 public $current_name;
543 public $current_abbrev;
544 public $current_still_exists;
545 public $current_rank;
546 public $current_rank_abbrev;
9f21bd15 547
56afc44b 548 public function __construct(array $data)
9f21bd15 549 {
0396c259
RB
550 foreach ($data as $key => $val) {
551 $this->$key = $val;
552 }
9f21bd15
RB
553 }
554
1a9affb7 555 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 556 {
0396c259
RB
557 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
558 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
559 pceo.still_exists AS original_still_exists,
560 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
561 pcec.still_exists AS current_still_exists,
562 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
9f21bd15 563 rankid
0396c259
RB
564 FROM profile_corps AS pc
565 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
566 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
567 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
23d0cc48 568 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
9f21bd15 569 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
1a9affb7 570 $pids, $visibility->levels());
9f21bd15
RB
571
572 return $data;
573 }
574}
575// }}}
4d1f0f6b
RB
576// {{{ class ProfileMentoringSectors [ Field ]
577class ProfileMentoringSectors extends ProfileField
578{
579 public $sectors = array();
580
581 public function __construct(PlInnerSubIterator $it)
582 {
583 $this->pid = $it->value();
584 while ($sector = $it->next()) {
585 $this->sectors[] = $sector;
586 }
587 }
588
589 public static function fetchData(array $pids, ProfileVisibility $visibility)
590 {
591 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
592 FROM profile_mentor_sector AS pms
593 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
594 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
595 WHERE pms.pid IN {?}
596 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
597 $pids);
598
599 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
600 }
601}
602// }}}
603// {{{ class ProfileMentoringCountries [ Field ]
604class ProfileMentoringCountries extends ProfileField
605{
606 public $countries = array();
607
608 public function __construct(PlInnerSubIterator $it)
609 {
610 $this->pid = $it->value();
611 while ($country = $it->next()) {
612 $this->countries[$country['id']] = $country['name'];
613 }
614 }
615
616 public static function fetchData(array $pids, ProfileVisibility $visibility)
617 {
618 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
619 FROM profile_mentor_country AS pmc
620 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
621 WHERE pmc.pid IN {?}
622 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
623 $pids);
624
625 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
626 }
627}
628// }}}
9f21bd15
RB
629
630/** Loading of data for a Profile :
631 * 1) load jobs, addresses, phones
632 * 2) attach phones to addresses, jobs and profiles
633 * 3) attach addresses to jobs and profiles
634 */
635
636// {{{ class ProfileAddresses [ Field ]
637class ProfileAddresses extends ProfileField
638{
639 private $addresses = array();
640
3bad2574 641 public function __construct(PlIterator $it)
9f21bd15 642 {
3bad2574
RB
643 if ($it instanceof PlInnerSubIterator) {
644 $this->pid = $it->value();
645 }
646
9f21bd15 647 while ($addr = $it->next()) {
dc953a0c 648 $this->addresses[$addr['id']] = new Address($addr);
3bad2574
RB
649 }
650 }
651
652 public function get($flags, $limit = null)
653 {
654 $res = array();
655 $nb = 0;
656 foreach ($this->addresses as $addr) {
790c52a8 657 if (
790c52a8
RB
658 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
659 ||
660 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
661 ||
dc953a0c 662 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE)
790c52a8 663 ||
dc953a0c 664 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
790c52a8 665 ) {
3bad2574
RB
666 $res[] = $addr;
667 $nb++;
668 }
669 if ($limit != null && $nb == $limit) {
670 break;
671 }
9f21bd15 672 }
598c57f6 673 return $res;
9f21bd15
RB
674 }
675
1a9affb7 676 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 677 {
fc10e72b
RB
678 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
679 IF(pa.type = \'home\', pid, jobid) AS link_id,
790c52a8 680 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
fc10e72b
RB
681 gl.name AS locality, gas.name AS subAdministrativeArea,
682 ga.name AS administrativeArea, gc.countryFR AS country
683 FROM profile_addresses AS pa
684 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
685 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
686 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
687 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
688 WHERE pa.pid in {?} AND pa.pub IN {?}
9f21bd15 689 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
1a9affb7 690 $pids, $visibility->levels());
9f21bd15
RB
691
692 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
693 }
694
df85e426 695 public function addPhones(ProfilePhones $phones)
9f21bd15 696 {
dc953a0c 697 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
598c57f6 698 foreach ($p as $phone) {
949fc736
RB
699 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
700 $this->addresses[$phone->link_id]->addPhone($phone);
9f21bd15
RB
701 }
702 }
9f21bd15
RB
703 }
704}
705// }}}
706// {{{ class ProfilePhones [ Field ]
707class ProfilePhones extends ProfileField
708{
709 private $phones = array();
710
f35e9630 711 public function __construct(PlInnerSubIterator $it)
9f21bd15 712 {
f35e9630 713 $this->pid = $it->value();
9f21bd15 714 while ($phone = $it->next()) {
f35e9630 715 $this->phones[] = new Phone($phone);
9f21bd15
RB
716 }
717 }
718
df85e426
RB
719 public function get($flags, $limit = null)
720 {
721 $phones = array();
722 $nb = 0;
723 foreach ($this->phones as $id => $phone) {
3c985c08
RB
724 if ($phone->hasFlags($flags)) {
725 $phones[$id] = $phone;
726 ++$nb;
727 if ($limit != null && $nb == $limit) {
728 break;
729 }
df85e426
RB
730 }
731 }
598c57f6 732 return $phones;
df85e426
RB
733 }
734
1a9affb7 735 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 736 {
dc953a0c 737 $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid, link_id, tel_id AS id
9f21bd15
RB
738 FROM profile_phones
739 WHERE pid IN {?} AND pub IN {?}
740 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
1a9affb7 741 $pids, $visibility->levels());
9f21bd15
RB
742 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
743 }
744}
745// }}}
746// {{{ class ProfileJobs [ Field ]
747class ProfileJobs extends ProfileField
748{
749 private $jobs = array();
750
a5a92ae7 751 public function __construct(PlInnerSubIterator $jobs)
9f21bd15 752 {
a5a92ae7 753 $this->pid = $jobs->value();
9f21bd15 754 while ($job = $jobs->next()) {
0907501b 755 $this->jobs[$job['id']] = new Job($job);
9f21bd15
RB
756 }
757 }
758
1a9affb7 759 public static function fetchData(array $pids, ProfileVisibility $visibility)
9f21bd15 760 {
02306579 761 CompanyList::preload($pids);
17982ebf
RB
762 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
763 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
764 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
765 pjssse.name AS subsubsector
766 FROM profile_job AS pj
767 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
768 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
769 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
770 WHERE pj.pid IN {?} AND pj.pub IN {?}
949fc736 771 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
17982ebf 772 pj.id',
1a9affb7 773 $visibility->levels(), $pids, $visibility->levels());
9f21bd15
RB
774 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
775 }
776
949fc736
RB
777 public function get($flags, $limit = null)
778 {
779 $jobs = array();
780 $nb = 0;
781 foreach ($this->jobs as $id => $job) {
782 $jobs[$id] = $job;
783 ++$nb;
784 if ($limit != null && $nb >= $limit) {
785 break;
786 }
787 }
598c57f6 788 return $jobs;
949fc736
RB
789 }
790
df85e426 791 public function addPhones(ProfilePhones $phones)
9f21bd15 792 {
dc953a0c 793 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
598c57f6 794 foreach ($p as $phone) {
949fc736 795 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
dc953a0c 796 $this->jobs[$phone->link_id]->addPhone($phone);
9f21bd15
RB
797 }
798 }
9f21bd15
RB
799 }
800
a1c2dd50 801 public function addAddresses(ProfileAddresses $addresses)
9f21bd15 802 {
df85e426 803 $a = $addresses->get(Profile::ADDRESS_PRO);
598c57f6 804 foreach ($a as $address) {
949fc736
RB
805 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
806 $this->jobs[$address->link_id]->setAddress($address);
9f21bd15
RB
807 }
808 }
9f21bd15
RB
809 }
810
a1c2dd50 811 public function addCompanies(array $companies)
9f21bd15 812 {
a1c2dd50
FB
813 foreach ($this->jobs as $job) {
814 $this->company = $companies[$job->jobid];
9f21bd15 815 }
9f21bd15
RB
816 }
817}
818// }}}
819
820// {{{ class CompanyList
821class CompanyList
822{
823 static private $fullload = false;
824 static private $companies = array();
825
826 static public function preload($pids = array())
827 {
828 if (self::$fullload) {
829 return;
830 }
831 // Load raw data
832 if (count($pids)) {
02306579
RB
833 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
834 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
9f21bd15
RB
835 } else {
836 $join = '';
837 $where = '';
838 }
839
df85e426
RB
840 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
841 pa.flags, pa.text, pa.postalCode, pa.countryId,
842 pa.type, pa.pub
9f21bd15
RB
843 FROM profile_job_enum AS pje
844 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
845 ' . $join . '
846 ' . $where);
dc953a0c 847 $newcompanies = array();
9f21bd15 848 while ($row = $it->next()) {
df85e426
RB
849 $cp = new Company($row);
850 $addr = new Address($row);
9f21bd15 851 $cp->setAddress($addr);
dc953a0c
RB
852 if (!array_key_exists($row['id'], self::$companies)) {
853 $newcompanies[] = $row['id'];
854 }
9f21bd15
RB
855 self::$companies[$row['id']] = $cp;
856 }
857
dc953a0c
RB
858 // TODO: determine whether there can be phones attached to a hq's address
859 // Add phones to hq
b2307983
RB
860 if (count($newcompanies)) {
861 $it = XDB::iterator('SELECT search_tel AS search, display_tel AS display, comment, link_id, tel_type AS type, link_type, tel_id AS id
862 FROM profile_phones
863 WHERE link_id IN {?} AND link_type = \'hq\'',
864 $newcompanies);
865 while ($row = $it->next()) {
866 $p = new Phone($row);
867 self::$companies[$row['link_id']]->setPhone($p);
868 }
dc953a0c
RB
869 }
870
9f21bd15
RB
871 if (count($pids) == 0) {
872 self::$fullload = true;
873 }
874 }
875
df85e426 876 static public function get($id)
9f21bd15
RB
877 {
878 if (!array_key_exists($id, self::$companies)) {
879 self::preload();
880 }
881 return self::$companies[$id];
882 }
883}
884
885// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
886?>