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