Commit | Line | Data |
---|---|---|
9f21bd15 RB |
1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
9f21bd15 RB |
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', | |
4d1f0f6b | 36 | Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries', |
3ac45f10 PC |
37 | Profile::FETCH_JOB_TERMS => 'ProfileJobTerms', |
38 | Profile::FETCH_MENTOR_TERMS => 'ProfileMentoringTerms', | |
148af7a9 | 39 | Profile::FETCH_PARTNER => 'ProfilePartnerSharing', |
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 | */ |
22771578 | 54 | public static function fetchData(array $pids, Visibility $visibility) |
b3c0f165 RB |
55 | { |
56 | return PlIteratorUtils::emptyIterator(); | |
57 | } | |
9f21bd15 | 58 | |
22771578 | 59 | public static function buildForPID($cls, $pid, Visibility $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 | */ | |
22771578 | 71 | public static function buildFromPIDs($cls, array $pids, Visibility $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 | |
22771578 | 81 | public static function getForPID($cls, $pid, Visibility $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 | ||
22771578 | 95 | public function __construct($cls, array $pids, Visibility $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 | ||
26ba053e | 152 | public function setPhone(Phone $phone) |
9f21bd15 | 153 | { |
0b53817f | 154 | if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) { |
9f21bd15 RB |
155 | $this->phone = $phone; |
156 | } | |
157 | } | |
158 | ||
26ba053e | 159 | public function setAddress(Address $address) |
9f21bd15 | 160 | { |
eb54852e | 161 | if ($address->type == Address::LINK_COMPANY && $address->jobid == $this->id) { |
9f21bd15 RB |
162 | $this->address = $address; |
163 | } | |
164 | } | |
165 | ||
166 | } | |
167 | // }}} | |
168 | // {{{ class Job | |
eb54852e SJ |
169 | /** profile_job describes a Job, links to: |
170 | * - a Profile, through `pid` | |
171 | * - a Company, through `jobid` | |
172 | * The `id` field is the id of this job in the list of the jobs of its profile | |
173 | * | |
174 | * For the documentation of the phone table, please see classes/phone.php. | |
175 | * For the documentation of the address table, please see classes/address.php. | |
176 | * | |
177 | * The possible relations are as follow: | |
178 | * A Job is linked to a Company and a Profile | |
179 | */ | |
9f21bd15 RB |
180 | class Job |
181 | { | |
182 | public $pid; | |
183 | public $id; | |
184 | ||
17982ebf | 185 | public $company = null; |
fde60e9f SJ |
186 | public $phones = array(); |
187 | public $address = null; | |
3ac45f10 | 188 | public $terms = array(); |
9f21bd15 | 189 | |
17982ebf | 190 | public $jobid; |
9f21bd15 RB |
191 | |
192 | public $description; | |
17982ebf RB |
193 | public $user_site; |
194 | public $user_email; | |
195 | ||
db492b02 RB |
196 | public $pub; |
197 | public $email_pub; | |
198 | ||
9f21bd15 RB |
199 | /** Fields are: |
200 | * pid, id, company_id, description, url, email | |
201 | */ | |
202 | public function __construct($data) | |
203 | { | |
204 | foreach ($data as $key => $val) { | |
205 | $this->$key = $val; | |
206 | } | |
17982ebf | 207 | $this->company = CompanyList::get($this->jobid); |
c4b6b1f4 | 208 | if (is_null($this->company)) { |
328bd54a SJ |
209 | $entreprises = ProfileValidate::get_typed_requests($this->pid, 'entreprise'); |
210 | foreach ($entreprises as $entreprise) { | |
211 | if ($entreprise->id == $this->id) { | |
212 | $this->company = new Company(array('name' => $entreprise->name)); | |
213 | } | |
214 | } | |
c4b6b1f4 | 215 | } |
9f21bd15 RB |
216 | } |
217 | ||
218 | public function phones() | |
219 | { | |
220 | return $this->phones; | |
221 | } | |
222 | ||
f2f314c4 RB |
223 | public function address() |
224 | { | |
225 | return $this->address; | |
226 | } | |
227 | ||
26ba053e | 228 | public function addPhone(Phone $phone) |
9f21bd15 | 229 | { |
0b53817f | 230 | if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) { |
0b6c8b36 | 231 | $this->phones[$phone->uniqueId()] = $phone; |
9f21bd15 RB |
232 | } |
233 | } | |
234 | ||
235 | public function setAddress(Address $address) | |
236 | { | |
0c408dbc | 237 | if ($address->type == Address::LINK_JOB && $address->id == $this->id && $address->pid == $this->pid) { |
9f21bd15 RB |
238 | $this->address = $address; |
239 | } | |
240 | } | |
3ac45f10 | 241 | |
26ba053e | 242 | public function addTerm(JobTerm $term) |
3ac45f10 PC |
243 | { |
244 | $this->terms[$term->jtid] = $term; | |
245 | } | |
246 | } | |
247 | // }}} | |
248 | // {{{ class JobTerm | |
249 | class JobTerm | |
250 | { | |
251 | public $jtid; | |
252 | public $full_name; | |
253 | public $pid; | |
254 | public $jid; | |
255 | ||
256 | /** Fields are: | |
257 | * pid, jid, jtid, full_name | |
258 | */ | |
259 | public function __construct($data) | |
260 | { | |
261 | foreach ($data as $key => $val) { | |
262 | $this->$key = $val; | |
263 | } | |
264 | } | |
9f21bd15 RB |
265 | } |
266 | // }}} | |
a060e1c3 RB |
267 | // {{{ class Education |
268 | class Education | |
269 | { | |
b7eec8d3 RB |
270 | public $id; |
271 | public $pid; | |
a060e1c3 RB |
272 | |
273 | public $entry_year; | |
274 | public $grad_year; | |
275 | public $program; | |
276 | public $flags; | |
277 | ||
b7eec8d3 RB |
278 | public $school; |
279 | public $school_short; | |
280 | public $school_url; | |
281 | public $country; | |
282 | ||
283 | public $degree; | |
284 | public $degree_short; | |
285 | public $degree_level; | |
286 | ||
a955e787 RB |
287 | public $field; |
288 | ||
a060e1c3 RB |
289 | public function __construct(array $data) |
290 | { | |
b7eec8d3 RB |
291 | foreach ($data as $key => $val) { |
292 | $this->$key = $val; | |
293 | } | |
294 | $this->flags = new PlFlagSet($this->flags); | |
a060e1c3 RB |
295 | } |
296 | } | |
297 | // }}} | |
9f21bd15 RB |
298 | |
299 | // {{{ class ProfileEducation [ Field ] | |
300 | class ProfileEducation extends ProfileField | |
301 | { | |
a060e1c3 | 302 | private $educations = array(); |
9f21bd15 | 303 | |
b7eec8d3 | 304 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 305 | { |
a060e1c3 | 306 | $this->pid = $it->value(); |
9f21bd15 | 307 | while ($edu = $it->next()) { |
a060e1c3 RB |
308 | $this->educations[$edu['id']] = new Education($edu); |
309 | } | |
310 | } | |
311 | ||
312 | public function get($flags, $limit) | |
313 | { | |
314 | $educations = array(); | |
315 | $year = getdate(); | |
316 | $year = $year['year']; | |
317 | $nb = 0; | |
318 | foreach ($this->educations as $id => $edu) { | |
319 | if ( | |
320 | (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary')) | |
321 | || | |
322 | (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary')) | |
323 | || | |
324 | (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year) | |
325 | || | |
326 | (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year) | |
ad336893 RB |
327 | || |
328 | ($flags & Profile::EDUCATION_ALL) | |
a060e1c3 RB |
329 | ) { |
330 | $educations[$id] = $edu; | |
331 | ++$nb; | |
332 | } | |
333 | if ($limit != null && $nb >= $limit) { | |
334 | break; | |
335 | } | |
9f21bd15 | 336 | } |
598c57f6 | 337 | return $educations; |
9f21bd15 RB |
338 | } |
339 | ||
22771578 | 340 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 341 | { |
b7eec8d3 RB |
342 | $data = XDB::iterator('SELECT pe.id, pe.pid, |
343 | pe.entry_year, pe.grad_year, pe.program, pe.flags, | |
344 | pee.name AS school, pee.abbreviation AS school_short, | |
1c305d4c | 345 | pee.url AS school_url, gc.country, |
b7eec8d3 RB |
346 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, |
347 | pefe.field | |
348 | FROM profile_education AS pe | |
349 | LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid) | |
350 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
351 | LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid) | |
352 | LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid) | |
353 | WHERE pe.pid IN {?} | |
a060e1c3 | 354 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
b7eec8d3 | 355 | NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id', |
a060e1c3 | 356 | $pids); |
9f21bd15 RB |
357 | |
358 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
359 | } | |
360 | } | |
361 | // }}} | |
362 | // {{{ class ProfileMedals [ Field ] | |
363 | class ProfileMedals extends ProfileField | |
364 | { | |
365 | public $medals = array(); | |
366 | ||
f35e9630 | 367 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 368 | { |
f35e9630 | 369 | $this->pid = $it->value(); |
9f21bd15 | 370 | while ($medal = $it->next()) { |
cef9e8c8 | 371 | $this->medals[$medal['mid']] = $medal; |
9f21bd15 RB |
372 | } |
373 | } | |
374 | ||
22771578 | 375 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 376 | { |
c6d16b24 | 377 | $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade |
9f21bd15 RB |
378 | FROM profile_medals AS pm |
379 | LEFT JOIN profiles AS p ON (pm.pid = p.pid) | |
cef9e8c8 | 380 | LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid) |
c6d16b24 | 381 | LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid) |
22771578 RB |
382 | LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?}) |
383 | WHERE pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0 | |
9f21bd15 | 384 | ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids), |
22771578 | 385 | $visibility->level(), $pids); |
9f21bd15 RB |
386 | |
387 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
388 | } | |
389 | } | |
390 | // }}} | |
391 | // {{{ class ProfileNetworking [ Field ] | |
392 | class ProfileNetworking extends ProfileField | |
393 | { | |
394 | private $networks = array(); | |
9f21bd15 | 395 | |
34de4b20 | 396 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 397 | { |
34de4b20 | 398 | $this->pid = $it->value(); |
9f21bd15 | 399 | while ($network = $it->next()) { |
34de4b20 PC |
400 | $network['network_type'] = new PlFlagSet($network['network_type']); |
401 | $this->networks[$network['id']] = $network; | |
9f21bd15 RB |
402 | } |
403 | } | |
404 | ||
22771578 | 405 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 406 | { |
1f5cd004 | 407 | $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name |
34de4b20 PC |
408 | FROM profile_networking AS pn |
409 | LEFT JOIN profile_networking_enum AS pne USING(nwid) | |
22771578 RB |
410 | LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?}) |
411 | WHERE pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0 | |
d4d395bb | 412 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', |
34de4b20 | 413 | pn.nwid, id', |
22771578 | 414 | $visibility->level(), $pids); |
9f21bd15 RB |
415 | |
416 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
417 | } | |
418 | ||
d4d395bb | 419 | public function get($flags, $limit = null) |
9f21bd15 RB |
420 | { |
421 | $nws = array(); | |
d4d395bb RB |
422 | $nb = 0; |
423 | foreach ($this->networks as $id => $nw) { | |
34de4b20 PC |
424 | if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') || |
425 | ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') || | |
426 | ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') || | |
140526de | 427 | ($flags == Profile::NETWORKING_ALL)) { |
d4d395bb RB |
428 | $nws[$id] = $nw; |
429 | ++$nb; | |
1f5cd004 | 430 | if (isset($limit) && $nb >= $limit) { |
34de4b20 PC |
431 | break; |
432 | } | |
9f21bd15 RB |
433 | } |
434 | } | |
598c57f6 | 435 | return $nws; |
9f21bd15 RB |
436 | } |
437 | } | |
438 | // }}} | |
9f21bd15 RB |
439 | // {{{ class ProfileCorps [ Field ] |
440 | class ProfileCorps extends ProfileField | |
441 | { | |
442 | public $original; | |
443 | public $current; | |
0396c259 RB |
444 | |
445 | public $original_name; | |
446 | public $original_abbrev; | |
447 | public $original_still_exists; | |
448 | ||
449 | public $current_name; | |
450 | public $current_abbrev; | |
451 | public $current_still_exists; | |
452 | public $current_rank; | |
453 | public $current_rank_abbrev; | |
9f21bd15 | 454 | |
56afc44b | 455 | public function __construct(array $data) |
9f21bd15 | 456 | { |
0396c259 RB |
457 | foreach ($data as $key => $val) { |
458 | $this->$key = $val; | |
459 | } | |
9f21bd15 RB |
460 | } |
461 | ||
22771578 | 462 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 463 | { |
0396c259 RB |
464 | $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current, |
465 | pceo.name AS original_name, pceo.abbreviation AS original_abbrev, | |
466 | pceo.still_exists AS original_still_exists, | |
467 | pcec.name AS current_name, pcec.abbreviation AS current_abbrev, | |
468 | pcec.still_exists AS current_still_exists, | |
469 | pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev, | |
9f21bd15 | 470 | rankid |
0396c259 RB |
471 | FROM profile_corps AS pc |
472 | LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid) | |
473 | LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid) | |
474 | LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid) | |
22771578 RB |
475 | LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?}) |
476 | WHERE pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1 | |
9f21bd15 | 477 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids), |
22771578 | 478 | $visibility->level(), $pids); |
9f21bd15 RB |
479 | |
480 | return $data; | |
481 | } | |
482 | } | |
483 | // }}} | |
4d1f0f6b RB |
484 | // {{{ class ProfileMentoringCountries [ Field ] |
485 | class ProfileMentoringCountries extends ProfileField | |
486 | { | |
487 | public $countries = array(); | |
488 | ||
489 | public function __construct(PlInnerSubIterator $it) | |
490 | { | |
491 | $this->pid = $it->value(); | |
492 | while ($country = $it->next()) { | |
493 | $this->countries[$country['id']] = $country['name']; | |
494 | } | |
495 | } | |
496 | ||
22771578 | 497 | public static function fetchData(array $pids, Visibility $visibility) |
4d1f0f6b | 498 | { |
1c305d4c | 499 | $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name |
4d1f0f6b RB |
500 | FROM profile_mentor_country AS pmc |
501 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country) | |
502 | WHERE pmc.pid IN {?} | |
503 | ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids), | |
504 | $pids); | |
505 | ||
506 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
507 | } | |
508 | } | |
509 | // }}} | |
9f21bd15 RB |
510 | // {{{ class ProfileAddresses [ Field ] |
511 | class ProfileAddresses extends ProfileField | |
512 | { | |
513 | private $addresses = array(); | |
514 | ||
eb54852e | 515 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 516 | { |
eb54852e SJ |
517 | $this->pid = $it->value(); |
518 | while ($address = $it->next()) { | |
519 | $this->addresses[] = new Address($address); | |
3bad2574 RB |
520 | } |
521 | } | |
522 | ||
523 | public function get($flags, $limit = null) | |
524 | { | |
eb54852e | 525 | $addresses = array(); |
3bad2574 | 526 | $nb = 0; |
eb54852e SJ |
527 | foreach ($this->addresses as $address) { |
528 | if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current')) | |
529 | || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail')) | |
530 | || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE) | |
531 | || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB) | |
790c52a8 | 532 | ) { |
eb54852e SJ |
533 | $addresses[] = $address; |
534 | ++$nb; | |
3bad2574 RB |
535 | } |
536 | if ($limit != null && $nb == $limit) { | |
537 | break; | |
538 | } | |
9f21bd15 | 539 | } |
eb54852e | 540 | return $addresses; |
9f21bd15 RB |
541 | } |
542 | ||
22771578 | 543 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 544 | { |
22771578 | 545 | $it = Address::iterate($pids, array(), array(), $visibility); |
eb54852e | 546 | return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid')); |
9f21bd15 RB |
547 | } |
548 | ||
df85e426 | 549 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 550 | { |
dc953a0c | 551 | $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY); |
598c57f6 | 552 | foreach ($p as $phone) { |
d8476774 | 553 | /* We must iterate on the addresses because id is not uniq thus, |
0b53817f | 554 | * $this->addresse[$phone->link_id] is invalid. |
d8476774 FB |
555 | */ |
556 | foreach ($this->addresses as $address) { | |
0b53817f | 557 | if ($address->type == Address::LINK_PROFILE && $address->id == $phone->link_id) { |
d8476774 FB |
558 | $address->addPhone($phone); |
559 | } | |
9f21bd15 RB |
560 | } |
561 | } | |
9f21bd15 RB |
562 | } |
563 | } | |
564 | // }}} | |
565 | // {{{ class ProfilePhones [ Field ] | |
566 | class ProfilePhones extends ProfileField | |
567 | { | |
568 | private $phones = array(); | |
569 | ||
f35e9630 | 570 | public function __construct(PlInnerSubIterator $it) |
9f21bd15 | 571 | { |
f35e9630 | 572 | $this->pid = $it->value(); |
9f21bd15 | 573 | while ($phone = $it->next()) { |
f35e9630 | 574 | $this->phones[] = new Phone($phone); |
9f21bd15 RB |
575 | } |
576 | } | |
577 | ||
df85e426 RB |
578 | public function get($flags, $limit = null) |
579 | { | |
580 | $phones = array(); | |
581 | $nb = 0; | |
582 | foreach ($this->phones as $id => $phone) { | |
3c985c08 RB |
583 | if ($phone->hasFlags($flags)) { |
584 | $phones[$id] = $phone; | |
585 | ++$nb; | |
586 | if ($limit != null && $nb == $limit) { | |
587 | break; | |
588 | } | |
df85e426 RB |
589 | } |
590 | } | |
598c57f6 | 591 | return $phones; |
df85e426 RB |
592 | } |
593 | ||
22771578 | 594 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 595 | { |
22771578 | 596 | $it = Phone::iterate($pids, array(), array(), $visibility); |
0b6c8b36 | 597 | return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid')); |
9f21bd15 RB |
598 | } |
599 | } | |
600 | // }}} | |
601 | // {{{ class ProfileJobs [ Field ] | |
602 | class ProfileJobs extends ProfileField | |
603 | { | |
604 | private $jobs = array(); | |
605 | ||
a5a92ae7 | 606 | public function __construct(PlInnerSubIterator $jobs) |
9f21bd15 | 607 | { |
a5a92ae7 | 608 | $this->pid = $jobs->value(); |
9f21bd15 | 609 | while ($job = $jobs->next()) { |
0907501b | 610 | $this->jobs[$job['id']] = new Job($job); |
9f21bd15 RB |
611 | } |
612 | } | |
613 | ||
22771578 | 614 | public static function fetchData(array $pids, Visibility $visibility) |
9f21bd15 | 615 | { |
02306579 | 616 | CompanyList::preload($pids); |
db492b02 | 617 | $data = XDB::iterator('SELECT id, pid, description, url as user_site, jobid, pub, IF(pve.best_display_level + 0 <= email_pub + 0, email, NULL) AS user_email, email_pub |
52deb3ae | 618 | FROM profile_job |
22771578 RB |
619 | LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?}) |
620 | WHERE pid IN {?} AND pve.best_display_level + 0 <= pub + 0 | |
52deb3ae | 621 | ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', id', |
22771578 | 622 | $visibility->level(), $pids); |
9f21bd15 RB |
623 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
624 | } | |
625 | ||
949fc736 RB |
626 | public function get($flags, $limit = null) |
627 | { | |
628 | $jobs = array(); | |
629 | $nb = 0; | |
630 | foreach ($this->jobs as $id => $job) { | |
631 | $jobs[$id] = $job; | |
632 | ++$nb; | |
633 | if ($limit != null && $nb >= $limit) { | |
634 | break; | |
635 | } | |
636 | } | |
598c57f6 | 637 | return $jobs; |
949fc736 RB |
638 | } |
639 | ||
df85e426 | 640 | public function addPhones(ProfilePhones $phones) |
9f21bd15 | 641 | { |
dc953a0c | 642 | $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY); |
598c57f6 | 643 | foreach ($p as $phone) { |
0b53817f SJ |
644 | if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) { |
645 | $this->jobs[$phone->link_id]->addPhone($phone); | |
9f21bd15 RB |
646 | } |
647 | } | |
9f21bd15 RB |
648 | } |
649 | ||
a1c2dd50 | 650 | public function addAddresses(ProfileAddresses $addresses) |
9f21bd15 | 651 | { |
df85e426 | 652 | $a = $addresses->get(Profile::ADDRESS_PRO); |
598c57f6 | 653 | foreach ($a as $address) { |
e1ff3b4f | 654 | if ($address->type == Address::LINK_JOB && array_key_exists($address->id, $this->jobs)) { |
0c408dbc | 655 | $this->jobs[$address->id]->setAddress($address); |
9f21bd15 RB |
656 | } |
657 | } | |
9f21bd15 RB |
658 | } |
659 | ||
a1c2dd50 | 660 | public function addCompanies(array $companies) |
9f21bd15 | 661 | { |
a1c2dd50 FB |
662 | foreach ($this->jobs as $job) { |
663 | $this->company = $companies[$job->jobid]; | |
9f21bd15 | 664 | } |
9f21bd15 | 665 | } |
3ac45f10 PC |
666 | |
667 | public function addJobTerms(ProfileJobTerms $jobterms) | |
668 | { | |
669 | $terms = $jobterms->get(); | |
670 | foreach ($terms as $term) { | |
671 | if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) { | |
26ba053e | 672 | $this->jobs[$term->jid]->addTerm($term); |
3ac45f10 PC |
673 | } |
674 | } | |
675 | } | |
9f21bd15 RB |
676 | } |
677 | // }}} | |
3ac45f10 PC |
678 | // {{{ class ProfileJobTerms [ Field ] |
679 | class ProfileJobTerms extends ProfileField | |
680 | { | |
681 | private $jobterms = array(); | |
9f21bd15 | 682 | |
3ac45f10 PC |
683 | public function __construct(PlInnerSubIterator $it) |
684 | { | |
685 | $this->pid = $it->value(); | |
686 | while ($term = $it->next()) { | |
687 | $this->jobterms[] = new JobTerm($term); | |
688 | } | |
689 | } | |
690 | ||
691 | public function get() | |
692 | { | |
693 | return $this->jobterms; | |
694 | } | |
695 | ||
22771578 | 696 | public static function fetchData(array $pids, Visibility $visibility) |
3ac45f10 PC |
697 | { |
698 | $data = XDB::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid | |
699 | FROM profile_job_term AS jt | |
700 | INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id) | |
701 | LEFT JOIN profile_job_term_enum AS jte USING(jtid) | |
22771578 RB |
702 | LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?}) |
703 | WHERE jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0 | |
3ac45f10 | 704 | ORDER BY ' . XDB::formatCustomOrder('jt.pid', $pids), |
22771578 | 705 | $visibility->level(), $pids); |
3ac45f10 PC |
706 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); |
707 | } | |
708 | } | |
709 | // }}} | |
710 | // {{{ class ProfileMentoringTerms [ Field ] | |
711 | class ProfileMentoringTerms extends ProfileJobTerms | |
712 | { | |
22771578 | 713 | public static function fetchData(array $pids, Visibility $visibility) |
3ac45f10 PC |
714 | { |
715 | $data = XDB::iterator('SELECT mt.jtid, jte.full_name, mt.pid | |
716 | FROM profile_mentor_term AS mt | |
717 | LEFT JOIN profile_job_term_enum AS jte USING(jtid) | |
718 | WHERE mt.pid IN {?} | |
719 | ORDER BY ' . XDB::formatCustomOrder('mt.pid', $pids), | |
720 | $pids); | |
721 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
722 | } | |
723 | } | |
724 | // }}} | |
148af7a9 RB |
725 | // {{{ class ProfilePartnerSharing [ Field ] |
726 | class ProfilePartnerSharing extends ProfileField | |
727 | { | |
728 | public function __construct(PlInnerSubIterator $it) | |
729 | { | |
730 | require_once 'partnersharing.inc.php'; | |
731 | ||
732 | $this->pid = $it->value(); | |
733 | while ($partner_settings = $it->next()) { | |
734 | $settings = new PartnerSettings($partner_settings); | |
735 | $this->partners_settings[$settings->partner->id] = $settings; | |
736 | } | |
737 | } | |
738 | ||
739 | public static function fetchData(array $pids, Visibility $visibility) | |
740 | { | |
741 | $data = XDB::iterator('SELECT ppss.pid, ppss.exposed_uid, ppss.sharing_level, | |
742 | ppss.allow_email, ppss.partner_id, | |
743 | ppse.shortname AS partner_shortname, | |
744 | ppse.name AS partner_name, | |
745 | ppse.url AS partner_url | |
746 | FROM profile_partnersharing_settings AS ppss | |
747 | LEFT JOIN profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id) | |
748 | WHERE ppss.pid IN {?} | |
749 | ORDER BY ' . XDB::formatCustomOrder('ppss.pid', $pids), | |
750 | $pids); | |
751 | return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid')); | |
752 | } | |
753 | ||
754 | public function get($partner_id) | |
755 | { | |
756 | if (isset($this->partners_settings[$partner_id])) { | |
757 | return $this->partners_settings[$partner_id]; | |
758 | } else { | |
759 | return PartnerSettings::getEmpty($partner_id); | |
760 | } | |
761 | } | |
762 | } | |
763 | // }}} | |
9f21bd15 RB |
764 | // {{{ class CompanyList |
765 | class CompanyList | |
766 | { | |
767 | static private $fullload = false; | |
768 | static private $companies = array(); | |
769 | ||
770 | static public function preload($pids = array()) | |
771 | { | |
772 | if (self::$fullload) { | |
773 | return; | |
774 | } | |
775 | // Load raw data | |
776 | if (count($pids)) { | |
02306579 RB |
777 | $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)'; |
778 | $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids); | |
9f21bd15 RB |
779 | } else { |
780 | $join = ''; | |
781 | $where = ''; | |
782 | } | |
783 | ||
df85e426 | 784 | $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url, |
3a2985f9 | 785 | pa.flags, pa.text, pa.type, pa.pub |
9f21bd15 RB |
786 | FROM profile_job_enum AS pje |
787 | LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\') | |
788 | ' . $join . ' | |
789 | ' . $where); | |
dc953a0c | 790 | $newcompanies = array(); |
9f21bd15 | 791 | while ($row = $it->next()) { |
df85e426 RB |
792 | $cp = new Company($row); |
793 | $addr = new Address($row); | |
9f21bd15 | 794 | $cp->setAddress($addr); |
dc953a0c RB |
795 | if (!array_key_exists($row['id'], self::$companies)) { |
796 | $newcompanies[] = $row['id']; | |
797 | } | |
9f21bd15 RB |
798 | self::$companies[$row['id']] = $cp; |
799 | } | |
800 | ||
dc953a0c | 801 | // Add phones to hq |
b2307983 | 802 | if (count($newcompanies)) { |
22771578 | 803 | $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies, Visibility::defaultForRead()); |
0b6c8b36 | 804 | while ($phone = $it->next()) { |
0b53817f | 805 | self::$companies[$phone->link_id]->setPhone($phone); |
b2307983 | 806 | } |
dc953a0c RB |
807 | } |
808 | ||
9f21bd15 RB |
809 | if (count($pids) == 0) { |
810 | self::$fullload = true; | |
811 | } | |
812 | } | |
813 | ||
df85e426 | 814 | static public function get($id) |
9f21bd15 RB |
815 | { |
816 | if (!array_key_exists($id, self::$companies)) { | |
817 | self::preload(); | |
818 | } | |
8654c3fa SJ |
819 | if (isset(self::$companies[$id])) { |
820 | return self::$companies[$id]; | |
821 | } | |
822 | return null; | |
9f21bd15 RB |
823 | } |
824 | } | |
148af7a9 | 825 | // }}} |
9f21bd15 RB |
826 | |
827 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
828 | ?> |