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