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