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 | */ |
b3c0f165 RB |
39 | public static function fetchData(array $pids, $visibility) |
40 | { | |
41 | return PlIteratorUtils::emptyIterator(); | |
42 | } | |
9f21bd15 RB |
43 | |
44 | public static function buildForPID($cls, $pid, $visibility) | |
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 | */ | |
56 | public static function buildFromPIDs($cls, array $pids, $visibility) | |
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 RB |
65 | |
66 | public static function getForPID($cls, $pid, $visibility) | |
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 | ||
80 | public function __construct($cls, array $pids, $visibility) | |
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 | ||
9f21bd15 RB |
216 | public function addPhone(Phone &$phone) |
217 | { | |
218 | if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) { | |
219 | $this->phones[] = $phone; | |
220 | } | |
221 | } | |
222 | ||
223 | public function setAddress(Address $address) | |
224 | { | |
225 | if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) { | |
226 | $this->address = $address; | |
227 | } | |
228 | } | |
9f21bd15 RB |
229 | } |
230 | // }}} | |
231 | // {{{ class Address | |
232 | class Address | |
233 | { | |
234 | const LINK_JOB = 'job'; | |
235 | const LINK_COMPANY = 'hq'; | |
236 | const LINK_PROFILE = 'home'; | |
237 | ||
fc10e72b | 238 | public $flags; |
9f21bd15 RB |
239 | public $link_id; |
240 | public $link_type; | |
241 | ||
9f21bd15 | 242 | public $text; |
df85e426 | 243 | public $postalCode; |
fc10e72b RB |
244 | public $latitude; |
245 | public $longitude; | |
246 | ||
247 | public $locality; | |
248 | public $subAdministrativeArea; | |
249 | public $administrativeArea; | |
9f21bd15 RB |
250 | public $country; |
251 | ||
252 | private $phones = array(); | |
253 | ||
254 | /** Fields are: | |
255 | * pîd, id, link_id, link_type, flags, text, postcode, country | |
256 | */ | |
257 | public function __construct($data) | |
258 | { | |
259 | foreach ($data as $key => $val) { | |
260 | $this->$key = $val; | |
261 | } | |
262 | } | |
263 | ||
264 | public function addPhone(Phone &$phone) | |
265 | { | |
266 | if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) { | |
267 | $this->phones[] = $phone; | |
268 | } | |
269 | } | |
270 | ||
271 | public function phones() | |
272 | { | |
273 | return $this->phones; | |
274 | } | |
3bad2574 RB |
275 | |
276 | public function hasFlags($flags) | |
277 | { | |
278 | return $flags & $this->flags; | |
279 | } | |
9f21bd15 RB |
280 | } |
281 | // }}} | |
a060e1c3 RB |
282 | // {{{ class Education |
283 | class Education | |
284 | { | |
b7eec8d3 RB |
285 | public $id; |
286 | public $pid; | |
a060e1c3 RB |
287 | |
288 | public $entry_year; | |
289 | public $grad_year; | |
290 | public $program; | |
291 | public $flags; | |
292 | ||
b7eec8d3 RB |
293 | public $school; |
294 | public $school_short; | |
295 | public $school_url; | |
296 | public $country; | |
297 | ||
298 | public $degree; | |
299 | public $degree_short; | |
300 | public $degree_level; | |
301 | ||
a060e1c3 RB |
302 | public function __construct(array $data) |
303 | { | |
b7eec8d3 RB |
304 | foreach ($data as $key => $val) { |
305 | $this->$key = $val; | |
306 | } | |
307 | $this->flags = new PlFlagSet($this->flags); | |
a060e1c3 RB |
308 | } |
309 | } | |
310 | // }}} | |
9f21bd15 RB |
311 | |
312 | // {{{ class ProfileEducation [ Field ] | |
313 | class ProfileEducation extends ProfileField | |
314 | { | |
a060e1c3 | 315 | private $educations = array(); |
9f21bd15 | 316 | |
b7eec8d3 | 317 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 318 | { |
a060e1c3 | 319 | $this->pid = $it->value(); |
9f21bd15 RB |
320 | $this->visibility = Profile::VISIBILITY_PUBLIC; |
321 | while ($edu = $it->next()) { | |
a060e1c3 RB |
322 | $this->educations[$edu['id']] = new Education($edu); |
323 | } | |
324 | } | |
325 | ||
326 | public function get($flags, $limit) | |
327 | { | |
328 | $educations = array(); | |
329 | $year = getdate(); | |
330 | $year = $year['year']; | |
331 | $nb = 0; | |
332 | foreach ($this->educations as $id => $edu) { | |
333 | if ( | |
334 | (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary')) | |
335 | || | |
336 | (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary')) | |
337 | || | |
338 | (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year) | |
339 | || | |
340 | (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year) | |
ad336893 RB |
341 | || |
342 | ($flags & Profile::EDUCATION_ALL) | |
a060e1c3 RB |
343 | ) { |
344 | $educations[$id] = $edu; | |
345 | ++$nb; | |
346 | } | |
347 | if ($limit != null && $nb >= $limit) { | |
348 | break; | |
349 | } | |
9f21bd15 | 350 | } |
b7eec8d3 | 351 | return PlIteratorUtils::fromArray($educations, 1, true); |
9f21bd15 RB |
352 | } |
353 | ||
354 | public static function fetchData(array $pids, $visibility) | |
355 | { | |
b7eec8d3 RB |
356 | $data = XDB::iterator('SELECT pe.id, pe.pid, |
357 | pe.entry_year, pe.grad_year, pe.program, pe.flags, | |
358 | pee.name AS school, pee.abbreviation AS school_short, | |
359 | pee.url AS school_url, gc.countryFR AS country, | |
360 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, | |
361 | pefe.field | |
362 | FROM profile_education AS pe | |
363 | LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid) | |
364 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
365 | LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid) | |
366 | LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid) | |
367 | WHERE pe.pid IN {?} | |
a060e1c3 | 368 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
b7eec8d3 | 369 | NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id', |
a060e1c3 | 370 | $pids); |
9f21bd15 RB |
371 | |
372 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
373 | } | |
374 | } | |
375 | // }}} | |
376 | // {{{ class ProfileMedals [ Field ] | |
377 | class ProfileMedals extends ProfileField | |
378 | { | |
379 | public $medals = array(); | |
380 | ||
f35e9630 | 381 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 382 | { |
f35e9630 | 383 | $this->pid = $it->value(); |
9f21bd15 | 384 | while ($medal = $it->next()) { |
cef9e8c8 | 385 | $this->medals[$medal['mid']] = $medal; |
9f21bd15 RB |
386 | } |
387 | } | |
388 | ||
389 | public static function fetchData(array $pids, $visibility) | |
390 | { | |
cef9e8c8 | 391 | $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img |
9f21bd15 RB |
392 | FROM profile_medals AS pm |
393 | LEFT JOIN profiles AS p ON (pm.pid = p.pid) | |
cef9e8c8 | 394 | LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid) |
9f21bd15 RB |
395 | WHERE pm.pid IN {?} AND p.medals_pub IN {?} |
396 | ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids), | |
f35e9630 | 397 | $pids, $visibility); |
9f21bd15 RB |
398 | |
399 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
400 | } | |
401 | } | |
402 | // }}} | |
403 | // {{{ class ProfileNetworking [ Field ] | |
404 | class ProfileNetworking extends ProfileField | |
405 | { | |
406 | private $networks = array(); | |
9f21bd15 | 407 | |
56afc44b | 408 | public function __construct(PlIterator $it) |
9f21bd15 RB |
409 | { |
410 | while ($network = $it->next()) { | |
411 | $this->networks[$network['nwid']] = $network['address']; | |
9f21bd15 RB |
412 | } |
413 | } | |
414 | ||
415 | public static function fetchData(array $pids, $visibility) | |
416 | { | |
d4d395bb | 417 | $data = XDB::iterator('SELECT pid, nwid, address, network_type |
9f21bd15 RB |
418 | FROM profile_networking |
419 | WHERE pid IN {?} AND pub IN {?} | |
d4d395bb RB |
420 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
421 | network_type, nwid', | |
422 | $pids, $visibility); | |
9f21bd15 RB |
423 | |
424 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
425 | } | |
426 | ||
d4d395bb | 427 | public function get($flags, $limit = null) |
9f21bd15 RB |
428 | { |
429 | $nws = array(); | |
d4d395bb RB |
430 | $nb = 0; |
431 | foreach ($this->networks as $id => $nw) { | |
432 | // XXX hardcoded reference to web site index | |
433 | if ( | |
434 | (($flags & self::NETWORKING_WEB) && $nw['network_type'] == 0) | |
435 | || | |
436 | (! ($flags & self::NETWORKING_WEB)) | |
437 | ) { | |
438 | $nws[$id] = $nw; | |
439 | ++$nb; | |
440 | } | |
441 | if ($nb >= $limit) { | |
442 | break; | |
9f21bd15 RB |
443 | } |
444 | } | |
a5a92ae7 | 445 | return PlIteratorUtils::fromArray($nws, 1, true); |
9f21bd15 RB |
446 | } |
447 | } | |
448 | // }}} | |
449 | // {{{ class ProfilePhoto [ Field ] | |
450 | class ProfilePhoto extends ProfileField | |
451 | { | |
452 | public $pic; | |
453 | ||
454 | public function __construct(array $data) | |
455 | { | |
456 | if ($data == null || count($data) == 0) { | |
457 | $this->pic = null; | |
458 | } else { | |
459 | $this->pid = $data['pid']; | |
460 | $this->pic = PlImage::fromDATA($data['attach'], | |
461 | $data['attachmime'], | |
462 | $data['x'], | |
463 | $data['y']); | |
464 | } | |
465 | } | |
466 | ||
467 | public static function fetchData(array $pids, $visibility) | |
468 | { | |
469 | $data = XDB::iterator('SELECT * | |
470 | FROM profile_photos | |
471 | WHERE pid IN {?} AND attachmime IN (\'jpeg\', \'png\') AND pub IN {?} | |
472 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), | |
0f9af912 | 473 | $pids, $visibility); |
9f21bd15 RB |
474 | |
475 | return $data; | |
476 | } | |
477 | } | |
478 | // }}} | |
479 | // {{{ class ProfileCorps [ Field ] | |
480 | class ProfileCorps extends ProfileField | |
481 | { | |
482 | public $original; | |
483 | public $current; | |
484 | public $rank; | |
485 | ||
56afc44b | 486 | public function __construct(array $data) |
9f21bd15 RB |
487 | { |
488 | $this->original = $data['original_corpsid']; | |
489 | $this->current = $data['current_corpsid']; | |
490 | $this->rank = $data['rankid']; | |
491 | $this->visibility = $data['corps_pub']; | |
492 | } | |
493 | ||
494 | public static function fetchData(array $pids, $visibility) | |
495 | { | |
496 | $data = XDB::iterator('SELECT pid, original_corpsid, current_corpsid, | |
497 | rankid | |
498 | FROM profile_corps | |
499 | WHERE pid IN {?} AND corps_pub IN {?} | |
500 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), | |
f35e9630 | 501 | $pids, $visibility); |
9f21bd15 RB |
502 | |
503 | return $data; | |
504 | } | |
505 | } | |
506 | // }}} | |
507 | ||
508 | /** Loading of data for a Profile : | |
509 | * 1) load jobs, addresses, phones | |
510 | * 2) attach phones to addresses, jobs and profiles | |
511 | * 3) attach addresses to jobs and profiles | |
512 | */ | |
513 | ||
514 | // {{{ class ProfileAddresses [ Field ] | |
515 | class ProfileAddresses extends ProfileField | |
516 | { | |
517 | private $addresses = array(); | |
518 | ||
3bad2574 | 519 | public function __construct(PlIterator $it) |
9f21bd15 | 520 | { |
3bad2574 RB |
521 | if ($it instanceof PlInnerSubIterator) { |
522 | $this->pid = $it->value(); | |
523 | } | |
524 | ||
9f21bd15 | 525 | while ($addr = $it->next()) { |
3bad2574 RB |
526 | $this->addresses[] = new Address($addr); |
527 | } | |
528 | } | |
529 | ||
530 | public function get($flags, $limit = null) | |
531 | { | |
532 | $res = array(); | |
533 | $nb = 0; | |
534 | foreach ($this->addresses as $addr) { | |
535 | if ($addr->hasFlags($flags)) { | |
536 | $res[] = $addr; | |
537 | $nb++; | |
538 | } | |
539 | if ($limit != null && $nb == $limit) { | |
540 | break; | |
541 | } | |
9f21bd15 | 542 | } |
a5a92ae7 | 543 | return PlIteratorUtils::fromArray($res, 1, true); |
9f21bd15 RB |
544 | } |
545 | ||
546 | public static function fetchData(array $pids, $visibility) | |
547 | { | |
fc10e72b RB |
548 | $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type, |
549 | IF(pa.type = \'home\', pid, jobid) AS link_id, | |
550 | pa.text, pa.postalCode, pa.latitude, pa.longitude, | |
551 | gl.name AS locality, gas.name AS subAdministrativeArea, | |
552 | ga.name AS administrativeArea, gc.countryFR AS country | |
553 | FROM profile_addresses AS pa | |
554 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
555 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
556 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
557 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
558 | WHERE pa.pid in {?} AND pa.pub IN {?} | |
9f21bd15 | 559 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
0f9af912 | 560 | $pids, $visibility); |
9f21bd15 RB |
561 | |
562 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
563 | } | |
564 | ||
df85e426 | 565 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 566 | { |
df85e426 RB |
567 | $p = $phones->get(0); |
568 | while ($phone = $p->next()) { | |
949fc736 RB |
569 | if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) { |
570 | $this->addresses[$phone->link_id]->addPhone($phone); | |
9f21bd15 RB |
571 | } |
572 | } | |
9f21bd15 RB |
573 | } |
574 | } | |
575 | // }}} | |
576 | // {{{ class ProfilePhones [ Field ] | |
577 | class ProfilePhones extends ProfileField | |
578 | { | |
579 | private $phones = array(); | |
580 | ||
f35e9630 | 581 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 582 | { |
f35e9630 | 583 | $this->pid = $it->value(); |
9f21bd15 | 584 | while ($phone = $it->next()) { |
f35e9630 | 585 | $this->phones[] = new Phone($phone); |
9f21bd15 RB |
586 | } |
587 | } | |
588 | ||
df85e426 RB |
589 | public function get($flags, $limit = null) |
590 | { | |
591 | $phones = array(); | |
592 | $nb = 0; | |
593 | foreach ($this->phones as $id => $phone) { | |
594 | $phones[$id] = $phone; | |
595 | ++$nb; | |
596 | if ($limit != null && $nb == $limit) { | |
597 | break; | |
598 | } | |
599 | } | |
a5a92ae7 | 600 | return PlIteratorUtils::fromArray($phones, 1, true); |
df85e426 RB |
601 | } |
602 | ||
9f21bd15 RB |
603 | public static function fetchData(array $pids, $visibility) |
604 | { | |
f35e9630 | 605 | $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment |
9f21bd15 RB |
606 | FROM profile_phones |
607 | WHERE pid IN {?} AND pub IN {?} | |
608 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), | |
f35e9630 | 609 | $pids, $visibility); |
9f21bd15 RB |
610 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
611 | } | |
612 | } | |
613 | // }}} | |
614 | // {{{ class ProfileJobs [ Field ] | |
615 | class ProfileJobs extends ProfileField | |
616 | { | |
617 | private $jobs = array(); | |
618 | ||
a5a92ae7 | 619 | public function __construct(PlInnerSubIterator $jobs) |
9f21bd15 | 620 | { |
a5a92ae7 | 621 | $this->pid = $jobs->value(); |
9f21bd15 | 622 | while ($job = $jobs->next()) { |
0907501b | 623 | $this->jobs[$job['id']] = new Job($job); |
9f21bd15 RB |
624 | } |
625 | } | |
626 | ||
627 | public static function fetchData(array $pids, $visibility) | |
628 | { | |
02306579 | 629 | CompanyList::preload($pids); |
17982ebf RB |
630 | $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site, |
631 | IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email, | |
632 | pj.jobid, pjse.name AS sector, pjsse.name AS subsector, | |
633 | pjssse.name AS subsubsector | |
634 | FROM profile_job AS pj | |
635 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) | |
636 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) | |
637 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) | |
638 | WHERE pj.pid IN {?} AND pj.pub IN {?} | |
949fc736 | 639 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
17982ebf | 640 | pj.id', |
949fc736 | 641 | $visibility, $pids, $visibility); |
9f21bd15 RB |
642 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
643 | } | |
644 | ||
949fc736 RB |
645 | public function get($flags, $limit = null) |
646 | { | |
647 | $jobs = array(); | |
648 | $nb = 0; | |
649 | foreach ($this->jobs as $id => $job) { | |
650 | $jobs[$id] = $job; | |
651 | ++$nb; | |
652 | if ($limit != null && $nb >= $limit) { | |
653 | break; | |
654 | } | |
655 | } | |
a5a92ae7 | 656 | return PlIteratorUtils::fromArray($jobs, 1, true); |
949fc736 RB |
657 | } |
658 | ||
df85e426 | 659 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 660 | { |
df85e426 RB |
661 | $p = $phones->get(0); |
662 | while ($phone = $p->next()) { | |
949fc736 RB |
663 | if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) { |
664 | $this->jobs[$phone->link_id]->addPhones($phone); | |
9f21bd15 RB |
665 | } |
666 | } | |
9f21bd15 RB |
667 | } |
668 | ||
df85e426 | 669 | public static function addAddresses(ProfileAddresses $addresses) |
9f21bd15 | 670 | { |
df85e426 RB |
671 | $a = $addresses->get(Profile::ADDRESS_PRO); |
672 | while ($address = $a->next()) { | |
949fc736 RB |
673 | if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) { |
674 | $this->jobs[$address->link_id]->setAddress($address); | |
9f21bd15 RB |
675 | } |
676 | } | |
9f21bd15 RB |
677 | } |
678 | ||
949fc736 | 679 | public static function addCompanies(array $companies) |
9f21bd15 | 680 | { |
949fc736 | 681 | foreach ($this->jobs as $job) |
9f21bd15 | 682 | { |
17982ebf | 683 | $job->company = $companies[$job->jobid]; |
9f21bd15 | 684 | } |
9f21bd15 RB |
685 | } |
686 | } | |
687 | // }}} | |
688 | ||
689 | // {{{ class CompanyList | |
690 | class CompanyList | |
691 | { | |
692 | static private $fullload = false; | |
693 | static private $companies = array(); | |
694 | ||
695 | static public function preload($pids = array()) | |
696 | { | |
697 | if (self::$fullload) { | |
698 | return; | |
699 | } | |
700 | // Load raw data | |
701 | if (count($pids)) { | |
02306579 RB |
702 | $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)'; |
703 | $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids); | |
9f21bd15 RB |
704 | } else { |
705 | $join = ''; | |
706 | $where = ''; | |
707 | } | |
708 | ||
df85e426 RB |
709 | $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url, |
710 | pa.flags, pa.text, pa.postalCode, pa.countryId, | |
711 | pa.type, pa.pub | |
9f21bd15 RB |
712 | FROM profile_job_enum AS pje |
713 | LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\') | |
714 | ' . $join . ' | |
715 | ' . $where); | |
716 | while ($row = $it->next()) { | |
df85e426 RB |
717 | $cp = new Company($row); |
718 | $addr = new Address($row); | |
9f21bd15 RB |
719 | $cp->setAddress($addr); |
720 | self::$companies[$row['id']] = $cp; | |
721 | } | |
722 | ||
723 | // TODO: add phones to addresses | |
724 | if (count($pids) == 0) { | |
725 | self::$fullload = true; | |
726 | } | |
727 | } | |
728 | ||
df85e426 | 729 | static public function get($id) |
9f21bd15 RB |
730 | { |
731 | if (!array_key_exists($id, self::$companies)) { | |
732 | self::preload(); | |
733 | } | |
734 | return self::$companies[$id]; | |
735 | } | |
736 | } | |
737 | ||
738 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
739 | ?> |