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