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