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