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