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