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); |
c4b6b1f4 SJ |
284 | if (is_null($this->company)) { |
285 | require_once 'validations.inc.php'; | |
286 | $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise'); | |
287 | $this->company = new Company(array('name' => $entreprise[$this->id]->name)); | |
288 | } | |
9f21bd15 RB |
289 | } |
290 | ||
291 | public function phones() | |
292 | { | |
293 | return $this->phones; | |
294 | } | |
295 | ||
f2f314c4 RB |
296 | public function address() |
297 | { | |
298 | return $this->address; | |
299 | } | |
300 | ||
9f21bd15 RB |
301 | public function addPhone(Phone &$phone) |
302 | { | |
303 | if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) { | |
42adf96b | 304 | $this->phones[$phone->uid()] = $phone; |
9f21bd15 RB |
305 | } |
306 | } | |
307 | ||
308 | public function setAddress(Address $address) | |
309 | { | |
42adf96b | 310 | if ($address->link_type == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) { |
9f21bd15 RB |
311 | $this->address = $address; |
312 | } | |
313 | } | |
9f21bd15 RB |
314 | } |
315 | // }}} | |
316 | // {{{ class Address | |
317 | class Address | |
318 | { | |
319 | const LINK_JOB = 'job'; | |
320 | const LINK_COMPANY = 'hq'; | |
321 | const LINK_PROFILE = 'home'; | |
322 | ||
fc10e72b | 323 | public $flags; |
42adf96b RB |
324 | public $id; // The ID of the address among those associated with its link |
325 | public $link_id; // The ID of the object to which the address is linked (profile, job, company) | |
9f21bd15 RB |
326 | public $link_type; |
327 | ||
9f21bd15 | 328 | public $text; |
df85e426 | 329 | public $postalCode; |
fc10e72b RB |
330 | public $latitude; |
331 | public $longitude; | |
332 | ||
333 | public $locality; | |
334 | public $subAdministrativeArea; | |
335 | public $administrativeArea; | |
9f21bd15 RB |
336 | public $country; |
337 | ||
790c52a8 RB |
338 | public $comment; |
339 | ||
9f21bd15 RB |
340 | private $phones = array(); |
341 | ||
342 | /** Fields are: | |
343 | * pîd, id, link_id, link_type, flags, text, postcode, country | |
344 | */ | |
345 | public function __construct($data) | |
346 | { | |
347 | foreach ($data as $key => $val) { | |
348 | $this->$key = $val; | |
349 | } | |
790c52a8 | 350 | $this->flags = new PlFlagSet($this->flags); |
9f21bd15 RB |
351 | } |
352 | ||
42adf96b RB |
353 | public function uid() { |
354 | $uid = $this->link_type . '_'; | |
355 | if ($this->link_type != self::LINK_COMPANY) { | |
356 | $uid .= $this->pid . '_'; | |
357 | } | |
358 | $uid .= $this->link_id . '_' . $this->id; | |
359 | } | |
360 | ||
9f21bd15 RB |
361 | public function addPhone(Phone &$phone) |
362 | { | |
42adf96b RB |
363 | if ( |
364 | $phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && | |
365 | ($this->link_type == self::LINK_COMPANY || $phone->pid == $this->pid) ) { | |
366 | $this->phones[$phone->uid()] = $phone; | |
9f21bd15 RB |
367 | } |
368 | } | |
369 | ||
370 | public function phones() | |
371 | { | |
372 | return $this->phones; | |
373 | } | |
3bad2574 | 374 | |
790c52a8 | 375 | public function hasFlag($flag) |
3bad2574 | 376 | { |
da4dc2a9 FB |
377 | if (!$this->flags instanceof PlFlagSet) { |
378 | $this->flags = new PlFlagSet($this->flags); | |
379 | } | |
790c52a8 | 380 | return $this->flags->hasFlag($flag); |
3bad2574 | 381 | } |
9f21bd15 RB |
382 | } |
383 | // }}} | |
a060e1c3 RB |
384 | // {{{ class Education |
385 | class Education | |
386 | { | |
b7eec8d3 RB |
387 | public $id; |
388 | public $pid; | |
a060e1c3 RB |
389 | |
390 | public $entry_year; | |
391 | public $grad_year; | |
392 | public $program; | |
393 | public $flags; | |
394 | ||
b7eec8d3 RB |
395 | public $school; |
396 | public $school_short; | |
397 | public $school_url; | |
398 | public $country; | |
399 | ||
400 | public $degree; | |
401 | public $degree_short; | |
402 | public $degree_level; | |
403 | ||
a955e787 RB |
404 | public $field; |
405 | ||
a060e1c3 RB |
406 | public function __construct(array $data) |
407 | { | |
b7eec8d3 RB |
408 | foreach ($data as $key => $val) { |
409 | $this->$key = $val; | |
410 | } | |
411 | $this->flags = new PlFlagSet($this->flags); | |
a060e1c3 RB |
412 | } |
413 | } | |
414 | // }}} | |
9f21bd15 RB |
415 | |
416 | // {{{ class ProfileEducation [ Field ] | |
417 | class ProfileEducation extends ProfileField | |
418 | { | |
a060e1c3 | 419 | private $educations = array(); |
9f21bd15 | 420 | |
b7eec8d3 | 421 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 422 | { |
a060e1c3 | 423 | $this->pid = $it->value(); |
9f21bd15 | 424 | while ($edu = $it->next()) { |
a060e1c3 RB |
425 | $this->educations[$edu['id']] = new Education($edu); |
426 | } | |
427 | } | |
428 | ||
429 | public function get($flags, $limit) | |
430 | { | |
431 | $educations = array(); | |
432 | $year = getdate(); | |
433 | $year = $year['year']; | |
434 | $nb = 0; | |
435 | foreach ($this->educations as $id => $edu) { | |
436 | if ( | |
437 | (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary')) | |
438 | || | |
439 | (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary')) | |
440 | || | |
441 | (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year) | |
442 | || | |
443 | (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year) | |
ad336893 RB |
444 | || |
445 | ($flags & Profile::EDUCATION_ALL) | |
a060e1c3 RB |
446 | ) { |
447 | $educations[$id] = $edu; | |
448 | ++$nb; | |
449 | } | |
450 | if ($limit != null && $nb >= $limit) { | |
451 | break; | |
452 | } | |
9f21bd15 | 453 | } |
598c57f6 | 454 | return $educations; |
9f21bd15 RB |
455 | } |
456 | ||
1a9affb7 | 457 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 458 | { |
b7eec8d3 RB |
459 | $data = XDB::iterator('SELECT pe.id, pe.pid, |
460 | pe.entry_year, pe.grad_year, pe.program, pe.flags, | |
461 | pee.name AS school, pee.abbreviation AS school_short, | |
462 | pee.url AS school_url, gc.countryFR AS country, | |
463 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, | |
464 | pefe.field | |
465 | FROM profile_education AS pe | |
466 | LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid) | |
467 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
468 | LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid) | |
469 | LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid) | |
470 | WHERE pe.pid IN {?} | |
a060e1c3 | 471 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
b7eec8d3 | 472 | NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id', |
a060e1c3 | 473 | $pids); |
9f21bd15 RB |
474 | |
475 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
476 | } | |
477 | } | |
478 | // }}} | |
479 | // {{{ class ProfileMedals [ Field ] | |
480 | class ProfileMedals extends ProfileField | |
481 | { | |
482 | public $medals = array(); | |
483 | ||
f35e9630 | 484 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 485 | { |
f35e9630 | 486 | $this->pid = $it->value(); |
9f21bd15 | 487 | while ($medal = $it->next()) { |
cef9e8c8 | 488 | $this->medals[$medal['mid']] = $medal; |
9f21bd15 RB |
489 | } |
490 | } | |
491 | ||
1a9affb7 | 492 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 493 | { |
c6d16b24 | 494 | $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade |
9f21bd15 RB |
495 | FROM profile_medals AS pm |
496 | LEFT JOIN profiles AS p ON (pm.pid = p.pid) | |
cef9e8c8 | 497 | LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid) |
c6d16b24 | 498 | LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid) |
9f21bd15 RB |
499 | WHERE pm.pid IN {?} AND p.medals_pub IN {?} |
500 | ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids), | |
1a9affb7 | 501 | $pids, $visibility->levels()); |
9f21bd15 RB |
502 | |
503 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
504 | } | |
505 | } | |
506 | // }}} | |
507 | // {{{ class ProfileNetworking [ Field ] | |
508 | class ProfileNetworking extends ProfileField | |
509 | { | |
510 | private $networks = array(); | |
9f21bd15 | 511 | |
34de4b20 | 512 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 513 | { |
34de4b20 | 514 | $this->pid = $it->value(); |
9f21bd15 | 515 | while ($network = $it->next()) { |
34de4b20 PC |
516 | $network['network_type'] = new PlFlagSet($network['network_type']); |
517 | $this->networks[$network['id']] = $network; | |
9f21bd15 RB |
518 | } |
519 | } | |
520 | ||
1a9affb7 | 521 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 522 | { |
1f5cd004 | 523 | $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name |
34de4b20 PC |
524 | FROM profile_networking AS pn |
525 | LEFT JOIN profile_networking_enum AS pne USING(nwid) | |
9f21bd15 | 526 | WHERE pid IN {?} AND pub IN {?} |
d4d395bb | 527 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
34de4b20 | 528 | pn.nwid, id', |
1a9affb7 | 529 | $pids, $visibility->levels()); |
9f21bd15 RB |
530 | |
531 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
532 | } | |
533 | ||
d4d395bb | 534 | public function get($flags, $limit = null) |
9f21bd15 | 535 | { |
34de4b20 PC |
536 | if (!$flags) { |
537 | $flags = Profile::NETWORKING_ALL; | |
538 | } | |
9f21bd15 | 539 | $nws = array(); |
d4d395bb RB |
540 | $nb = 0; |
541 | foreach ($this->networks as $id => $nw) { | |
34de4b20 PC |
542 | if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') || |
543 | ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') || | |
544 | ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') || | |
545 | ($flags & Profile::NETWORKING_ALL)) { | |
d4d395bb RB |
546 | $nws[$id] = $nw; |
547 | ++$nb; | |
1f5cd004 | 548 | if (isset($limit) && $nb >= $limit) { |
34de4b20 PC |
549 | break; |
550 | } | |
9f21bd15 RB |
551 | } |
552 | } | |
598c57f6 | 553 | return $nws; |
9f21bd15 RB |
554 | } |
555 | } | |
556 | // }}} | |
9f21bd15 RB |
557 | // {{{ class ProfileCorps [ Field ] |
558 | class ProfileCorps extends ProfileField | |
559 | { | |
560 | public $original; | |
561 | public $current; | |
0396c259 RB |
562 | |
563 | public $original_name; | |
564 | public $original_abbrev; | |
565 | public $original_still_exists; | |
566 | ||
567 | public $current_name; | |
568 | public $current_abbrev; | |
569 | public $current_still_exists; | |
570 | public $current_rank; | |
571 | public $current_rank_abbrev; | |
9f21bd15 | 572 | |
56afc44b | 573 | public function __construct(array $data) |
9f21bd15 | 574 | { |
0396c259 RB |
575 | foreach ($data as $key => $val) { |
576 | $this->$key = $val; | |
577 | } | |
9f21bd15 RB |
578 | } |
579 | ||
1a9affb7 | 580 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 581 | { |
0396c259 RB |
582 | $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current, |
583 | pceo.name AS original_name, pceo.abbreviation AS original_abbrev, | |
584 | pceo.still_exists AS original_still_exists, | |
585 | pcec.name AS current_name, pcec.abbreviation AS current_abbrev, | |
586 | pcec.still_exists AS current_still_exists, | |
587 | pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev, | |
9f21bd15 | 588 | rankid |
0396c259 RB |
589 | FROM profile_corps AS pc |
590 | LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid) | |
591 | LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid) | |
592 | LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid) | |
23d0cc48 | 593 | WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1 |
9f21bd15 | 594 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
1a9affb7 | 595 | $pids, $visibility->levels()); |
9f21bd15 RB |
596 | |
597 | return $data; | |
598 | } | |
599 | } | |
600 | // }}} | |
4d1f0f6b RB |
601 | // {{{ class ProfileMentoringSectors [ Field ] |
602 | class ProfileMentoringSectors extends ProfileField | |
603 | { | |
604 | public $sectors = array(); | |
605 | ||
606 | public function __construct(PlInnerSubIterator $it) | |
607 | { | |
608 | $this->pid = $it->value(); | |
609 | while ($sector = $it->next()) { | |
610 | $this->sectors[] = $sector; | |
611 | } | |
612 | } | |
613 | ||
614 | public static function fetchData(array $pids, ProfileVisibility $visibility) | |
615 | { | |
616 | $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector | |
617 | FROM profile_mentor_sector AS pms | |
618 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid) | |
619 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid) | |
620 | WHERE pms.pid IN {?} | |
621 | ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids), | |
622 | $pids); | |
623 | ||
624 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
625 | } | |
626 | } | |
627 | // }}} | |
628 | // {{{ class ProfileMentoringCountries [ Field ] | |
629 | class ProfileMentoringCountries extends ProfileField | |
630 | { | |
631 | public $countries = array(); | |
632 | ||
633 | public function __construct(PlInnerSubIterator $it) | |
634 | { | |
635 | $this->pid = $it->value(); | |
636 | while ($country = $it->next()) { | |
637 | $this->countries[$country['id']] = $country['name']; | |
638 | } | |
639 | } | |
640 | ||
641 | public static function fetchData(array $pids, ProfileVisibility $visibility) | |
642 | { | |
643 | $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name | |
644 | FROM profile_mentor_country AS pmc | |
645 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country) | |
646 | WHERE pmc.pid IN {?} | |
647 | ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids), | |
648 | $pids); | |
649 | ||
650 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
651 | } | |
652 | } | |
653 | // }}} | |
9f21bd15 RB |
654 | |
655 | /** Loading of data for a Profile : | |
656 | * 1) load jobs, addresses, phones | |
657 | * 2) attach phones to addresses, jobs and profiles | |
658 | * 3) attach addresses to jobs and profiles | |
659 | */ | |
660 | ||
c91c11ba RB |
661 | // {{{ Database schema (profile_address, profile_phones, profile_jobs) |
662 | /** The database for this is very unclear, so here is a little schema : | |
663 | * profile_job describes a Job, links to: | |
664 | * - a Profile, through `pid` | |
665 | * - a Company, through `jobid` | |
666 | * The `id` field is the id of this job in the list of the jobs of its profile | |
667 | * | |
668 | * profile_addresses describes an Address, which | |
669 | * related to either a Profile, a Job or a Company: | |
670 | * - for a Profile: | |
671 | * - `type` is set to 'home' | |
672 | * - `pid` is set to the related profile pid | |
673 | * - `id` is the id of the address in the list of those related to that profile | |
674 | * - `jobid` is empty | |
675 | * | |
676 | * - for a Company: | |
677 | * - `type` is set to 'hq' | |
678 | * - `pid` is set to 0 | |
679 | * - `jobid` is set to the id of the company | |
680 | * - `id` is set to 0 (only one address per Company) | |
681 | * | |
682 | * - for a Job: | |
683 | * - `type` is set to 'job' | |
684 | * - `pid` is set to the pid of the Profile of the related Job | |
685 | * - `jobid` is set to the Company of the job (this information is redundant | |
686 | * with that of the row of profile_job for the related job) | |
687 | * - `id` is the id of the job to which we refer (i.e `profile_job.id`) | |
688 | * | |
689 | * profile_phone describes a Phone, which can be related to an Address, | |
690 | * a Job, a Profile or a Company: | |
691 | * - for a Profile: | |
692 | * - `link_type` is set to 'user' | |
693 | * - `link_id` is set to 0 | |
694 | * - `pid` is set to the id of the related Profile | |
695 | * | |
696 | * - for a Company: | |
697 | * - `link_type` is set to 'hq' | |
698 | * - `link_id` is set to the id of the related Company | |
699 | * - `pid` is set to 0 | |
700 | * | |
701 | * - for an Address (this is only possible for a *personal* address) | |
702 | * - `link_type` is set to 'address' | |
703 | * - `link_id` is set to the related Address `id` | |
704 | * - `pid` is set to the related Address `pid` | |
705 | * | |
706 | * - for a Job: | |
707 | * - `link_type` is set to 'pro' | |
708 | * - `link_id` is set to the related Job `id` (not `jobid`) | |
709 | * - `pid` is set to the related Job `pid` | |
710 | * | |
711 | * | |
712 | * The possible relations are as follow: | |
713 | * An Address can be linked to a Company, a Profile, a Job | |
714 | * A Job is linked to a Company and a Profile | |
715 | * A Phone can be linked to a Company, a Profile, a Job, or a Profile-related Address | |
716 | */ | |
717 | // }}} | |
718 | ||
9f21bd15 RB |
719 | // {{{ class ProfileAddresses [ Field ] |
720 | class ProfileAddresses extends ProfileField | |
721 | { | |
722 | private $addresses = array(); | |
723 | ||
3bad2574 | 724 | public function __construct(PlIterator $it) |
9f21bd15 | 725 | { |
3bad2574 RB |
726 | if ($it instanceof PlInnerSubIterator) { |
727 | $this->pid = $it->value(); | |
728 | } | |
729 | ||
9f21bd15 | 730 | while ($addr = $it->next()) { |
10e9d82b | 731 | $this->addresses[] = new Address($addr); |
3bad2574 RB |
732 | } |
733 | } | |
734 | ||
735 | public function get($flags, $limit = null) | |
736 | { | |
737 | $res = array(); | |
738 | $nb = 0; | |
739 | foreach ($this->addresses as $addr) { | |
790c52a8 | 740 | if ( |
790c52a8 RB |
741 | (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current')) |
742 | || | |
743 | (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail')) | |
744 | || | |
dc953a0c | 745 | (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE) |
790c52a8 | 746 | || |
dc953a0c | 747 | (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB) |
790c52a8 | 748 | ) { |
3bad2574 RB |
749 | $res[] = $addr; |
750 | $nb++; | |
751 | } | |
752 | if ($limit != null && $nb == $limit) { | |
753 | break; | |
754 | } | |
9f21bd15 | 755 | } |
598c57f6 | 756 | return $res; |
9f21bd15 RB |
757 | } |
758 | ||
1a9affb7 | 759 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 760 | { |
fc10e72b | 761 | $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type, |
42adf96b | 762 | IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id, |
790c52a8 | 763 | pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment, |
fc10e72b RB |
764 | gl.name AS locality, gas.name AS subAdministrativeArea, |
765 | ga.name AS administrativeArea, gc.countryFR AS country | |
766 | FROM profile_addresses AS pa | |
767 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
768 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
769 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
770 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
771 | WHERE pa.pid in {?} AND pa.pub IN {?} | |
9f21bd15 | 772 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
1a9affb7 | 773 | $pids, $visibility->levels()); |
9f21bd15 RB |
774 | |
775 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
776 | } | |
777 | ||
df85e426 | 778 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 779 | { |
dc953a0c | 780 | $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY); |
598c57f6 | 781 | foreach ($p as $phone) { |
949fc736 RB |
782 | if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) { |
783 | $this->addresses[$phone->link_id]->addPhone($phone); | |
9f21bd15 RB |
784 | } |
785 | } | |
9f21bd15 RB |
786 | } |
787 | } | |
788 | // }}} | |
789 | // {{{ class ProfilePhones [ Field ] | |
790 | class ProfilePhones extends ProfileField | |
791 | { | |
792 | private $phones = array(); | |
793 | ||
f35e9630 | 794 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 795 | { |
f35e9630 | 796 | $this->pid = $it->value(); |
9f21bd15 | 797 | while ($phone = $it->next()) { |
f35e9630 | 798 | $this->phones[] = new Phone($phone); |
9f21bd15 RB |
799 | } |
800 | } | |
801 | ||
df85e426 RB |
802 | public function get($flags, $limit = null) |
803 | { | |
804 | $phones = array(); | |
805 | $nb = 0; | |
806 | foreach ($this->phones as $id => $phone) { | |
3c985c08 RB |
807 | if ($phone->hasFlags($flags)) { |
808 | $phones[$id] = $phone; | |
809 | ++$nb; | |
810 | if ($limit != null && $nb == $limit) { | |
811 | break; | |
812 | } | |
df85e426 RB |
813 | } |
814 | } | |
598c57f6 | 815 | return $phones; |
df85e426 RB |
816 | } |
817 | ||
1a9affb7 | 818 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 819 | { |
dc953a0c | 820 | $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 |
821 | FROM profile_phones |
822 | WHERE pid IN {?} AND pub IN {?} | |
823 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), | |
1a9affb7 | 824 | $pids, $visibility->levels()); |
9f21bd15 RB |
825 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
826 | } | |
827 | } | |
828 | // }}} | |
829 | // {{{ class ProfileJobs [ Field ] | |
830 | class ProfileJobs extends ProfileField | |
831 | { | |
832 | private $jobs = array(); | |
833 | ||
a5a92ae7 | 834 | public function __construct(PlInnerSubIterator $jobs) |
9f21bd15 | 835 | { |
a5a92ae7 | 836 | $this->pid = $jobs->value(); |
9f21bd15 | 837 | while ($job = $jobs->next()) { |
0907501b | 838 | $this->jobs[$job['id']] = new Job($job); |
9f21bd15 RB |
839 | } |
840 | } | |
841 | ||
1a9affb7 | 842 | public static function fetchData(array $pids, ProfileVisibility $visibility) |
9f21bd15 | 843 | { |
02306579 | 844 | CompanyList::preload($pids); |
17982ebf RB |
845 | $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site, |
846 | IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email, | |
847 | pj.jobid, pjse.name AS sector, pjsse.name AS subsector, | |
848 | pjssse.name AS subsubsector | |
849 | FROM profile_job AS pj | |
850 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) | |
851 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) | |
852 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) | |
853 | WHERE pj.pid IN {?} AND pj.pub IN {?} | |
949fc736 | 854 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
17982ebf | 855 | pj.id', |
1a9affb7 | 856 | $visibility->levels(), $pids, $visibility->levels()); |
9f21bd15 RB |
857 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
858 | } | |
859 | ||
949fc736 RB |
860 | public function get($flags, $limit = null) |
861 | { | |
862 | $jobs = array(); | |
863 | $nb = 0; | |
864 | foreach ($this->jobs as $id => $job) { | |
865 | $jobs[$id] = $job; | |
866 | ++$nb; | |
867 | if ($limit != null && $nb >= $limit) { | |
868 | break; | |
869 | } | |
870 | } | |
598c57f6 | 871 | return $jobs; |
949fc736 RB |
872 | } |
873 | ||
df85e426 | 874 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 875 | { |
dc953a0c | 876 | $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY); |
598c57f6 | 877 | foreach ($p as $phone) { |
949fc736 | 878 | if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) { |
dc953a0c | 879 | $this->jobs[$phone->link_id]->addPhone($phone); |
9f21bd15 RB |
880 | } |
881 | } | |
9f21bd15 RB |
882 | } |
883 | ||
a1c2dd50 | 884 | public function addAddresses(ProfileAddresses $addresses) |
9f21bd15 | 885 | { |
df85e426 | 886 | $a = $addresses->get(Profile::ADDRESS_PRO); |
598c57f6 | 887 | foreach ($a as $address) { |
949fc736 RB |
888 | if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) { |
889 | $this->jobs[$address->link_id]->setAddress($address); | |
9f21bd15 RB |
890 | } |
891 | } | |
9f21bd15 RB |
892 | } |
893 | ||
a1c2dd50 | 894 | public function addCompanies(array $companies) |
9f21bd15 | 895 | { |
a1c2dd50 FB |
896 | foreach ($this->jobs as $job) { |
897 | $this->company = $companies[$job->jobid]; | |
9f21bd15 | 898 | } |
9f21bd15 RB |
899 | } |
900 | } | |
901 | // }}} | |
902 | ||
903 | // {{{ class CompanyList | |
904 | class CompanyList | |
905 | { | |
906 | static private $fullload = false; | |
907 | static private $companies = array(); | |
908 | ||
909 | static public function preload($pids = array()) | |
910 | { | |
911 | if (self::$fullload) { | |
912 | return; | |
913 | } | |
914 | // Load raw data | |
915 | if (count($pids)) { | |
02306579 RB |
916 | $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)'; |
917 | $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids); | |
9f21bd15 RB |
918 | } else { |
919 | $join = ''; | |
920 | $where = ''; | |
921 | } | |
922 | ||
df85e426 RB |
923 | $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url, |
924 | pa.flags, pa.text, pa.postalCode, pa.countryId, | |
925 | pa.type, pa.pub | |
9f21bd15 RB |
926 | FROM profile_job_enum AS pje |
927 | LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\') | |
928 | ' . $join . ' | |
929 | ' . $where); | |
dc953a0c | 930 | $newcompanies = array(); |
9f21bd15 | 931 | while ($row = $it->next()) { |
df85e426 RB |
932 | $cp = new Company($row); |
933 | $addr = new Address($row); | |
9f21bd15 | 934 | $cp->setAddress($addr); |
dc953a0c RB |
935 | if (!array_key_exists($row['id'], self::$companies)) { |
936 | $newcompanies[] = $row['id']; | |
937 | } | |
9f21bd15 RB |
938 | self::$companies[$row['id']] = $cp; |
939 | } | |
940 | ||
dc953a0c RB |
941 | // TODO: determine whether there can be phones attached to a hq's address |
942 | // Add phones to hq | |
b2307983 RB |
943 | if (count($newcompanies)) { |
944 | $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 | |
945 | FROM profile_phones | |
946 | WHERE link_id IN {?} AND link_type = \'hq\'', | |
947 | $newcompanies); | |
948 | while ($row = $it->next()) { | |
949 | $p = new Phone($row); | |
950 | self::$companies[$row['link_id']]->setPhone($p); | |
951 | } | |
dc953a0c RB |
952 | } |
953 | ||
9f21bd15 RB |
954 | if (count($pids) == 0) { |
955 | self::$fullload = true; | |
956 | } | |
957 | } | |
958 | ||
df85e426 | 959 | static public function get($id) |
9f21bd15 RB |
960 | { |
961 | if (!array_key_exists($id, self::$companies)) { | |
962 | self::preload(); | |
963 | } | |
964 | return self::$companies[$id]; | |
965 | } | |
966 | } | |
967 | ||
968 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
969 | ?> |