Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
d4c08d89 | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
e7b93962 FB |
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 Profile | |
23 | { | |
f5642983 FB |
24 | static private $v_values = array('public' => array('public'), |
25 | 'ax' => array('ax', 'public'), | |
26 | 'private' => array('private', 'ax', 'public')); | |
913a4e90 | 27 | |
f5642983 FB |
28 | const VISIBILITY_PUBLIC = 'public'; |
29 | const VISIBILITY_AX = 'ax'; | |
30 | const VISIBILITY_PRIVATE = 'private'; | |
31 | ||
913a4e90 RB |
32 | /* name tokens */ |
33 | const LASTNAME = 'lastname'; | |
34 | const FIRSTNAME = 'firstname'; | |
35 | const NICKNAME = 'nickname'; | |
36 | const PSEUDONYM = 'pseudonym'; | |
37 | const NAME = 'name'; | |
38 | /* name variants */ | |
39 | const VN_MARITAL = 'marital'; | |
40 | const VN_ORDINARY = 'ordinary'; | |
41 | const VN_OTHER = 'other'; | |
42 | const VN_INI = 'ini'; | |
43 | /* display names */ | |
44 | const DN_FULL = 'directory_name'; | |
45 | const DN_DISPLAY = 'yourself'; | |
46 | const DN_YOURSELF = 'yourself'; | |
47 | const DN_DIRECTORY = 'directory_name'; | |
48 | const DN_PRIVATE = 'private_name'; | |
49 | const DN_PUBLIC = 'public_name'; | |
50 | const DN_SHORT = 'short_name'; | |
51 | const DN_SORT = 'sort_name'; | |
e02d9fbb SJ |
52 | /* education related names */ |
53 | const EDU_X = 'École polytechnique'; | |
54 | const DEGREE_X = 'Ingénieur'; | |
55 | const DEGREE_M = 'Master'; | |
56 | const DEGREE_D = 'Doctorat'; | |
913a4e90 RB |
57 | |
58 | static public $name_variants = array( | |
59 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
60 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
61 | ); | |
62 | ||
f5642983 FB |
63 | const ADDRESS_MAIN = 0x000001; |
64 | const ADDRESS_PERSO = 0x000002; | |
65 | const ADDRESS_PRO = 0x000004; | |
66 | const ADDRESS_ALL = 0x000006; | |
67 | const ADDRESS_POSTAL = 0x000008; | |
68 | ||
69 | const EDUCATION_MAIN = 0x000010; | |
4bc5b8f0 FB |
70 | const EDUCATION_EXTRA = 0x000020; |
71 | const EDUCATION_ALL = 0x000040; | |
72 | const EDUCATION_FINISHED = 0x000080; | |
73 | const EDUCATION_CURRENT = 0x000100; | |
f5642983 | 74 | |
4bc5b8f0 FB |
75 | const JOBS_MAIN = 0x001000; |
76 | const JOBS_ALL = 0x002000; | |
77 | const JOBS_FINISHED = 0x004000; | |
78 | const JOBS_CURRENT = 0x008000; | |
f5642983 | 79 | |
04a94b1d FB |
80 | const NETWORKING_ALL = 0x000000; |
81 | const NETWORKING_WEB = 0x010000; | |
82 | const NETWORKING_IM = 0x020000; | |
83 | const NETWORKING_SOCIAL = 0x040000; | |
84 | ||
e7b93962 FB |
85 | private $pid; |
86 | private $hrpid; | |
3e53a496 FB |
87 | private $data = array(); |
88 | ||
f5642983 FB |
89 | private $visibility = null; |
90 | ||
b774ddab | 91 | private function __construct(array $data) |
e7b93962 | 92 | { |
b774ddab | 93 | $this->data = $data; |
832e6fcb FB |
94 | $this->pid = $this->data['pid']; |
95 | $this->hrpid = $this->data['hrpid']; | |
f74fb084 FB |
96 | if (!S::logged()) { |
97 | $this->setVisibilityLevel(self::VISIBILITY_PUBLIC); | |
98 | } | |
e7b93962 FB |
99 | } |
100 | ||
101 | public function id() | |
102 | { | |
103 | return $this->pid; | |
104 | } | |
105 | ||
106 | public function hrid() | |
107 | { | |
108 | return $this->hrpid; | |
109 | } | |
110 | ||
d5e60905 FB |
111 | public function promo() |
112 | { | |
113 | return $this->promo; | |
114 | } | |
115 | ||
845f0a4d SJ |
116 | public function yearpromo() |
117 | { | |
118 | return intval(substr($this->promo, 1, 4)); | |
119 | } | |
120 | ||
94b72319 FB |
121 | /** Print a name with the given formatting: |
122 | * %s = • for women | |
123 | * %f = firstname | |
124 | * %l = lastname | |
125 | * %F = fullname | |
126 | * %S = shortname | |
127 | * %p = promo | |
128 | */ | |
129 | public function name($format) | |
130 | { | |
131 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
132 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
133 | $this->firstName(), $this->lastName(), |
134 | $this->fullName(), $this->shortName(), | |
135 | $this->promo()), $format); | |
94b72319 FB |
136 | } |
137 | ||
138 | public function fullName($with_promo = false) | |
139 | { | |
140 | if ($with_promo) { | |
141 | return $this->full_name . ' (' . $this->promo . ')'; | |
142 | } | |
143 | return $this->full_name; | |
144 | } | |
145 | ||
146 | public function shortName($with_promo = false) | |
147 | { | |
148 | if ($with_promo) { | |
149 | return $this->short_name . ' (' . $this->promo . ')'; | |
150 | } | |
151 | return $this->short_name; | |
152 | } | |
153 | ||
154 | public function firstName() | |
155 | { | |
08c91036 | 156 | return $this->firstname; |
94b72319 FB |
157 | } |
158 | ||
5c005b37 RB |
159 | public function firstNames() |
160 | { | |
161 | return $this->nameVariants(self::FIRSTNAME); | |
162 | } | |
163 | ||
94b72319 FB |
164 | public function lastName() |
165 | { | |
08c91036 | 166 | return $this->lastname; |
94b72319 FB |
167 | } |
168 | ||
5c005b37 RB |
169 | public function lastNames() |
170 | { | |
171 | return $this->nameVariants(self::LASTNAME); | |
172 | } | |
173 | ||
94b72319 FB |
174 | public function isFemale() |
175 | { | |
176 | return $this->sex == PlUser::GENDER_FEMALE; | |
177 | } | |
178 | ||
179 | public function data() | |
180 | { | |
181 | $this->first_name; | |
182 | return $this->data; | |
183 | } | |
184 | ||
5c005b37 RB |
185 | private function nameVariants($type) |
186 | { | |
187 | $vals = array($this->$type); | |
188 | foreach (self::$name_variants[$type] as $var) { | |
189 | $vartype = $type . '_' . $var; | |
190 | $varname = $this->$vartype; | |
191 | if ($varname != null && $varname != "") { | |
192 | $vals[] = $varname; | |
193 | } | |
194 | } | |
195 | return array_unique($vals); | |
196 | } | |
197 | ||
3e53a496 FB |
198 | public function __get($name) |
199 | { | |
200 | if (property_exists($this, $name)) { | |
201 | return $this->$name; | |
202 | } | |
203 | ||
3e53a496 FB |
204 | if (isset($this->data[$name])) { |
205 | return $this->data[$name]; | |
206 | } | |
207 | ||
208 | return null; | |
209 | } | |
210 | ||
211 | public function __isset($name) | |
212 | { | |
213 | return property_exists($this, $name) || isset($this->data[$name]); | |
214 | } | |
215 | ||
c159e50f RB |
216 | /** Sets the level of visibility of the profile |
217 | * Sets $this->visibility to a list of valid visibilities. | |
218 | * @param one of the self::VISIBILITY_* values | |
219 | */ | |
f5642983 FB |
220 | public function setVisibilityLevel($visibility) |
221 | { | |
0d906109 RB |
222 | if ($visibility != self::VISIBILITY_PRIVATE |
223 | && $visibility != self::VISIBILITY_AX | |
f5642983 FB |
224 | && $visibility != self::VISIBILITY_PUBLIC) { |
225 | Platal::page()->kill("Visibility invalide: " . $visibility); | |
226 | } | |
227 | $this->visibility = self::$v_values[$visibility]; | |
5c005b37 | 228 | if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) { |
46e9eb99 FB |
229 | unset($this->data['mobile']); |
230 | } | |
f5642983 FB |
231 | } |
232 | ||
c159e50f RB |
233 | /** Determine whether an item with visibility $visibility can be displayed |
234 | * with the current level of visibility of the profile | |
235 | * @param $visibility The level of visibility to be checked | |
236 | */ | |
237 | public function isVisible($visibility) | |
238 | { | |
239 | return in_array($visibility, $this->visibility); | |
240 | } | |
4bc5b8f0 | 241 | |
833a6e86 FB |
242 | /* Photo |
243 | */ | |
244 | public function getPhoto($fallback = true) | |
245 | { | |
833a6e86 FB |
246 | $cond = ''; |
247 | if ($this->visibility) { | |
248 | $cond = ' AND pub IN ' . XDB::formatArray($this->visibility); | |
249 | } | |
5d83c3be | 250 | $res = XDB::query("SELECT * |
5c4ea53f | 251 | FROM profile_photos |
5d83c3be SJ |
252 | WHERE attachmime IN ('jpeg', 'png') |
253 | " . $cond . ' AND pid = {?}', | |
833a6e86 FB |
254 | $this->id()); |
255 | if ($res->numRows() > 0) { | |
256 | $photo = $res->fetchOneAssoc(); | |
257 | return PlImage::fromData($photo['attach'], 'image/' . $photo['attachmime'], | |
258 | $photo['x'], $photo['y']); | |
259 | } else if ($fallback) { | |
260 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png', | |
261 | 'image/png'); | |
262 | } | |
263 | return null; | |
264 | } | |
265 | ||
4bc5b8f0 FB |
266 | /* Addresses |
267 | */ | |
268 | public function getAddresses($flags, $limit = null) | |
f5642983 FB |
269 | { |
270 | $where = XDB::format('pa.pid = {?}', $this->id()); | |
271 | if ($flags & self::ADDRESS_MAIN) { | |
272 | $where .= ' AND FIND_IN_SET(\'current\', pa.flags)'; | |
273 | } | |
274 | if ($flags & self::ADDRESS_POSTAL) { | |
275 | $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)'; | |
276 | } | |
277 | if ($this->visibility) { | |
278 | $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility); | |
279 | } | |
280 | $type = array(); | |
281 | if ($flags & self::ADDRESS_PRO) { | |
282 | $type[] = 'job'; | |
283 | } | |
284 | if ($flags & self::ADDRESS_PERSO) { | |
285 | $type[] = 'home'; | |
286 | } | |
287 | if (count($type) > 0) { | |
288 | $where .= ' AND pa.type IN ' . XDB::formatArray($type); | |
289 | } | |
4bc5b8f0 | 290 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
f5642983 FB |
291 | return XDB::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude, |
292 | gl.name AS locality, gas.name AS subAdministrativeArea, | |
293 | ga.name AS administrativeArea, gc.countryFR AS country, | |
6b83ce06 | 294 | ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel, |
f5642983 FB |
295 | FIND_IN_SET(\'current\', pa.flags) AS current, |
296 | FIND_IN_SET(\'temporary\', pa.flags) AS temporary, | |
297 | FIND_IN_SET(\'secondary\', pa.flags) AS secondary, | |
298 | FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type | |
299 | FROM profile_addresses AS pa | |
300 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
301 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
302 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
303 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
ce0b2c6f FB |
304 | LEFT JOIN profile_phones AS ppfix ON (ppfix.link_type = \'address\' AND ppfix.pid = pa.pid AND ppfix.link_id = pa.id AND ppfix.tel_type = \'fixed\') |
305 | LEFT JOIN profile_phones AS ppfax ON (ppfax.link_type = \'address\' AND ppfax.pid = pa.pid AND ppfax.link_id = pa.id AND ppfax.tel_type = \'fax\') | |
4bc5b8f0 FB |
306 | WHERE ' . $where . ' |
307 | ORDER BY pa.id | |
308 | ' . $limit); | |
f5642983 FB |
309 | } |
310 | ||
311 | public function getMainAddress() | |
312 | { | |
313 | $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); | |
314 | if ($it->total() == 0) { | |
315 | return null; | |
316 | } else { | |
317 | return $it->next(); | |
318 | } | |
319 | } | |
3e53a496 | 320 | |
4bc5b8f0 FB |
321 | |
322 | /* Educations | |
323 | */ | |
324 | public function getEducations($flags, $limit = null) | |
325 | { | |
ce0b2c6f | 326 | $where = XDB::format('pe.pid = {?}', $this->id()); |
4bc5b8f0 FB |
327 | if ($flags & self::EDUCATION_MAIN) { |
328 | $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)'; | |
329 | } else if ($flags & self::EDUCATION_EXTRA) { | |
330 | $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)'; | |
331 | } else if ($flags & self::EDUCATION_FINISHED) { | |
332 | $where .= ' AND pe.grad_year <= YEAR(CURDATE())'; | |
333 | } else if ($flags & self::EDUCATION_CURRENT) { | |
334 | $where .= ' AND pe.grad_year > YEAR(CURDATE())'; | |
335 | } | |
336 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
337 | return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program, | |
338 | pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country, | |
339 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field, | |
340 | FIND_IN_SET(\'primary\', pe.flags) AS prim | |
341 | FROM profile_education AS pe | |
342 | INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) | |
343 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
344 | INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) | |
345 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) | |
346 | WHERE ' . $where . ' | |
347 | ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id | |
348 | ' . $limit); | |
349 | } | |
350 | ||
351 | public function getExtraEducations($limit = null) | |
352 | { | |
353 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
354 | } | |
355 | ||
356 | ||
04a94b1d FB |
357 | /** Networking |
358 | */ | |
359 | ||
360 | public function getNetworking($flags, $limit = null) | |
361 | { | |
ce0b2c6f | 362 | $where = XDB::format('pn.pid = {?}', $this->id()); |
04a94b1d FB |
363 | if ($flags & self::NETWORKING_WEB) { |
364 | $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index | |
365 | } | |
366 | if ($this->visibility) { | |
367 | $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility); | |
368 | } | |
369 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
370 | return XDB::iterator('SELECT pne.name, pne.icon, | |
371 | IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address), | |
372 | pn.address) AS address | |
373 | FROM profile_networking AS pn | |
374 | INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type) | |
375 | WHERE ' . $where . ' | |
376 | ORDER BY pn.network_type, pn.nwid | |
377 | ' . $limit); | |
378 | } | |
379 | ||
380 | public function getWebSite() | |
381 | { | |
382 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
383 | if ($site->total() != 1) { | |
384 | return null; | |
385 | } | |
386 | $site = $site->next(); | |
387 | return $site['address']; | |
388 | } | |
389 | ||
390 | ||
e718bd18 FB |
391 | /** Jobs |
392 | */ | |
393 | ||
394 | public function getJobs($flags, $limit = null) | |
395 | { | |
ce0b2c6f | 396 | $where = XDB::format('pj.pid = {?}', $this->id()); |
e718bd18 FB |
397 | $cond = 'TRUE'; |
398 | if ($this->visibility) { | |
399 | $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility); | |
400 | $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility); | |
401 | } | |
402 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
403 | return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code, | |
404 | pj.description, pj.url AS user_site, | |
405 | IF (' . $cond . ', pj.email, NULL) AS user_email, | |
83ff5281 SJ |
406 | pjse.name AS sector, pjsse.name AS subsector, |
407 | pjssse.name AS subsubsector | |
e718bd18 FB |
408 | FROM profile_job AS pj |
409 | INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid) | |
e718bd18 FB |
410 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) |
411 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) | |
412 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) | |
413 | WHERE ' . $where . ' | |
414 | ORDER BY pj.id | |
415 | ' . $limit); | |
416 | } | |
417 | ||
418 | public function getMailJob() | |
419 | { | |
420 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
421 | if ($job->total() != 1) { | |
422 | return null; | |
423 | } | |
424 | return $job->next(); | |
425 | } | |
426 | ||
5c005b37 RB |
427 | /* Binets |
428 | */ | |
429 | public function getBinets() | |
430 | { | |
431 | return XDB::fetchColumn('SELECT binet_id | |
5c8a71f2 | 432 | FROM profile_binets |
bdd977d7 | 433 | WHERE pid = {?}', $this->id()); |
5c005b37 RB |
434 | } |
435 | ||
e718bd18 | 436 | |
e7b93962 FB |
437 | public function owner() |
438 | { | |
439 | return User::getSilent($this); | |
440 | } | |
441 | ||
94590511 SJ |
442 | public function compareNames($firstname, $lastname) |
443 | { | |
444 | $_lastname = mb_strtoupper($this->lastName()); | |
445 | $_firstname = mb_strtoupper($this->firstName()); | |
446 | $lastname = mb_strtoupper($lastname); | |
447 | $firstname = mb_strtoupper($firstname); | |
448 | ||
449 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
450 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
451 | $maxlen = 0; | |
452 | ||
453 | foreach ($tokens as $str) { | |
454 | $isOk &= (strpos($_lastname, $str) !== false); | |
455 | $maxlen = max($maxlen, strlen($str)); | |
456 | } | |
457 | ||
458 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
459 | } | |
460 | ||
0d906109 | 461 | private static function fetchProfileData(array $pids, $respect_order = true) |
b774ddab FB |
462 | { |
463 | if (count($pids) == 0) { | |
464 | return array(); | |
465 | } | |
0d906109 RB |
466 | |
467 | if ($respect_order) { | |
468 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
469 | } else { | |
470 | $order = ''; | |
471 | } | |
472 | ||
473 | return XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, | |
474 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, | |
475 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, | |
476 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
477 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, | |
478 | pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub, | |
88c31faf FB |
479 | ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub, |
480 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, | |
481 | ap.uid AS owner_id | |
0d906109 RB |
482 | FROM profiles AS p |
483 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
ce0b2c6f | 484 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) |
0d906109 RB |
485 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
486 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
487 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
488 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
489 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
490 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
491 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
492 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
493 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
494 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
ce0b2c6f | 495 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') |
5c4ea53f | 496 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) |
0d906109 RB |
497 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
498 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
499 | GROUP BY p.pid | |
500 | ' . $order); | |
b774ddab FB |
501 | } |
502 | ||
503 | public static function getPID($login) | |
504 | { | |
505 | if ($login instanceof PlUser) { | |
506 | return XDB::fetchOneCell('SELECT pid | |
507 | FROM account_profiles | |
508 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
509 | $login->id()); | |
510 | } else if (ctype_digit($login)) { | |
511 | return XDB::fetchOneCell('SELECT pid | |
512 | FROM profiles | |
513 | WHERE pid = {?}', $login); | |
514 | } else { | |
515 | return XDB::fetchOneCell('SELECT pid | |
516 | FROM profiles | |
517 | WHERE hrpid = {?}', $login); | |
518 | } | |
519 | } | |
520 | ||
0d906109 RB |
521 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
522 | { | |
523 | if ($respect_order) { | |
524 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
525 | } else { | |
526 | $order = ''; | |
527 | } | |
528 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
529 | FROM account_profiles AS ap | |
530 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
531 | AND ap.uid IN ' . XDB::formatArray($uids) .' | |
532 | ' . $order); | |
533 | } | |
b774ddab | 534 | |
e7b93962 FB |
535 | /** Return the profile associated with the given login. |
536 | */ | |
a3118782 FB |
537 | public static function get($login) |
538 | { | |
641f2115 FB |
539 | if (is_array($login)) { |
540 | return new Profile($login); | |
541 | } | |
b774ddab FB |
542 | $pid = self::getPID($login); |
543 | if (!is_null($pid)) { | |
0d906109 RB |
544 | $it = self::iterOverPIDs(array($pid), false); |
545 | return $it->next(); | |
b774ddab | 546 | } else { |
efe597c5 FB |
547 | /* Let say we can identify a profile using the identifiers of its owner. |
548 | */ | |
455ea0c9 FB |
549 | if (!($login instanceof PlUser)) { |
550 | $user = User::getSilent($login); | |
551 | if ($user && $user->hasProfile()) { | |
552 | return $user->profile(); | |
553 | } | |
efe597c5 | 554 | } |
3e53a496 | 555 | return null; |
e7b93962 FB |
556 | } |
557 | } | |
a3118782 | 558 | |
0d906109 RB |
559 | public static function iterOverUIDs($uids, $respect_order = true) |
560 | { | |
561 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order); | |
562 | } | |
563 | ||
564 | public static function iterOverPIDs($pids, $respect_order = true) | |
565 | { | |
566 | return new ProfileIterator(self::fetchProfileData($pids, $respect_order)); | |
567 | } | |
568 | ||
b774ddab FB |
569 | /** Return profiles for the list of pids. |
570 | */ | |
571 | public static function getBulkProfilesWithPIDs(array $pids) | |
572 | { | |
573 | if (count($pids) == 0) { | |
574 | return array(); | |
575 | } | |
0d906109 | 576 | $it = self::iterOverPIDs($pids); |
b774ddab | 577 | $profiles = array(); |
0d906109 RB |
578 | while ($p = $it->next()) { |
579 | $profiles[$p->id()] = $p; | |
b774ddab FB |
580 | } |
581 | return $profiles; | |
582 | } | |
583 | ||
584 | /** Return profiles for uids. | |
585 | */ | |
586 | public static function getBulkProfilesWithUIDS(array $uids) | |
587 | { | |
588 | if (count($uids) == 0) { | |
589 | return array(); | |
590 | } | |
0d906109 | 591 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids)); |
b774ddab FB |
592 | } |
593 | ||
913a4e90 RB |
594 | public static function isDisplayName($name) |
595 | { | |
596 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
597 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
598 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
599 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
600 | } | |
601 | ||
a3118782 FB |
602 | public static function getNameTypeId($type, $for_sql = false) |
603 | { | |
604 | if (!S::has('name_types')) { | |
eb6207f7 | 605 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
32742f84 | 606 | FROM profile_name_enum'); |
a3118782 FB |
607 | S::set('name_types', $table); |
608 | } else { | |
609 | $table = S::v('name_types'); | |
610 | } | |
611 | if ($for_sql) { | |
612 | return XDB::escape($table[$type]); | |
613 | } else { | |
614 | return $table[$type]; | |
615 | } | |
616 | } | |
726eaf7a SJ |
617 | |
618 | public static function rebuildSearchTokens($pid) | |
619 | { | |
620 | XDB::execute('DELETE FROM search_name | |
6dbb167e | 621 | WHERE pid = {?}', |
726eaf7a SJ |
622 | $pid); |
623 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, | |
624 | FIND_IN_SET('public', e.flags) AS public | |
625 | FROM profile_name AS n | |
626 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
627 | WHERE n.pid = {?}", | |
628 | $pid); | |
629 | ||
630 | foreach ($keys as $i => $key) { | |
631 | if ($key['name'] == '') { | |
632 | continue; | |
633 | } | |
634 | $toks = preg_split('/[ \'\-]+/', $key['name']); | |
635 | $token = ''; | |
636 | $first = 5; | |
637 | while ($toks) { | |
638 | $token = strtolower(replace_accent(array_pop($toks) . $token)); | |
639 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); | |
6dbb167e | 640 | XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags) |
726eaf7a | 641 | VALUES ({?}, {?}, {?}, {?}, {?})', |
6dbb167e | 642 | $token, $pid, soundex_fr($token), $score, $key['public']); |
726eaf7a SJ |
643 | $first = 0; |
644 | } | |
645 | } | |
69b46857 SJ |
646 | } |
647 | ||
648 | /** The school identifier consists of 6 digits. The first 3 represent the | |
649 | * promotion entry year. The last 3 indicate the student's rank. | |
650 | * | |
651 | * Our identifier consists of 8 digits and both half have the same role. | |
652 | * This enables us to deal with bigger promotions and with a wider range | |
653 | * of promotions. | |
654 | * | |
655 | * getSchoolId returns a school identifier given one of ours. | |
656 | * getXorgId returns a X.org identifier given a school identifier. | |
657 | */ | |
658 | public static function getSchoolId($xorgId) | |
659 | { | |
660 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
661 | return null; | |
662 | } | |
663 | ||
664 | $year = intval(substr($xorgId, 0, 4)); | |
665 | $rank = intval(substr($xorgId, 5, 3)); | |
666 | if ($year < 1996) { | |
667 | return null; | |
668 | } elseif ($year < 2000) { | |
669 | $year = intval(substr(1900 - $year, 1, 3)); | |
670 | return sprintf('%02u0%03u', $year, $rank); | |
671 | } else { | |
672 | $year = intval(substr(1900 - $year, 1, 3)); | |
673 | return sprintf('%03u%03u', $year, $rank); | |
674 | } | |
675 | } | |
726eaf7a | 676 | |
69b46857 SJ |
677 | public static function getXorgId($schoolId) |
678 | { | |
94590511 | 679 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
680 | return null; |
681 | } | |
682 | ||
69b46857 SJ |
683 | $year = intval(substr($schoolId, 0, 3)); |
684 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 685 | |
69b46857 SJ |
686 | if ($year > 200) { |
687 | $year /= 10; | |
688 | } | |
689 | if ($year < 96) { | |
690 | return null; | |
691 | } else { | |
692 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
693 | } | |
726eaf7a | 694 | } |
e7b93962 FB |
695 | } |
696 | ||
0d906109 RB |
697 | /** Iterator over a set of Profiles |
698 | * @param an XDB::Iterator obtained from a Profile::fetchProfileData | |
699 | */ | |
700 | class ProfileIterator implements PlIterator | |
701 | { | |
702 | private $dbiter; | |
703 | ||
704 | public function __construct($dbiter) | |
705 | { | |
706 | $this->dbiter = $dbiter; | |
707 | } | |
708 | ||
709 | public function next() | |
710 | { | |
711 | $data = $this->dbiter->next(); | |
712 | if ($data == null) { | |
713 | return null; | |
714 | } else { | |
641f2115 | 715 | return Profile::get($data); |
0d906109 RB |
716 | } |
717 | } | |
718 | ||
719 | public function total() | |
720 | { | |
721 | return $this->dbiter->total(); | |
722 | } | |
723 | ||
724 | public function first() | |
725 | { | |
726 | return $this->dbiter->first(); | |
727 | } | |
728 | ||
729 | public function last() | |
730 | { | |
731 | return $this->dbiter->last(); | |
732 | } | |
733 | } | |
734 | ||
e7b93962 FB |
735 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
736 | ?> |