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