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