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