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() ? '•' : '', | |
133 | $this->first_name, $this->last_name, | |
134 | $this->full_name, $this->short_name, | |
135 | $this->promo), $format); | |
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 | ||
f5642983 FB |
216 | public function setVisibilityLevel($visibility) |
217 | { | |
0d906109 RB |
218 | if ($visibility != self::VISIBILITY_PRIVATE |
219 | && $visibility != self::VISIBILITY_AX | |
f5642983 FB |
220 | && $visibility != self::VISIBILITY_PUBLIC) { |
221 | Platal::page()->kill("Visibility invalide: " . $visibility); | |
222 | } | |
223 | $this->visibility = self::$v_values[$visibility]; | |
5c005b37 | 224 | if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) { |
46e9eb99 FB |
225 | unset($this->data['mobile']); |
226 | } | |
f5642983 FB |
227 | } |
228 | ||
4bc5b8f0 | 229 | |
833a6e86 FB |
230 | /* Photo |
231 | */ | |
232 | public function getPhoto($fallback = true) | |
233 | { | |
833a6e86 FB |
234 | $cond = ''; |
235 | if ($this->visibility) { | |
236 | $cond = ' AND pub IN ' . XDB::formatArray($this->visibility); | |
237 | } | |
5d83c3be | 238 | $res = XDB::query("SELECT * |
5c4ea53f | 239 | FROM profile_photos |
5d83c3be SJ |
240 | WHERE attachmime IN ('jpeg', 'png') |
241 | " . $cond . ' AND pid = {?}', | |
833a6e86 FB |
242 | $this->id()); |
243 | if ($res->numRows() > 0) { | |
244 | $photo = $res->fetchOneAssoc(); | |
245 | return PlImage::fromData($photo['attach'], 'image/' . $photo['attachmime'], | |
246 | $photo['x'], $photo['y']); | |
247 | } else if ($fallback) { | |
248 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png', | |
249 | 'image/png'); | |
250 | } | |
251 | return null; | |
252 | } | |
253 | ||
4bc5b8f0 FB |
254 | /* Addresses |
255 | */ | |
256 | public function getAddresses($flags, $limit = null) | |
f5642983 FB |
257 | { |
258 | $where = XDB::format('pa.pid = {?}', $this->id()); | |
259 | if ($flags & self::ADDRESS_MAIN) { | |
260 | $where .= ' AND FIND_IN_SET(\'current\', pa.flags)'; | |
261 | } | |
262 | if ($flags & self::ADDRESS_POSTAL) { | |
263 | $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)'; | |
264 | } | |
265 | if ($this->visibility) { | |
266 | $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility); | |
267 | } | |
268 | $type = array(); | |
269 | if ($flags & self::ADDRESS_PRO) { | |
270 | $type[] = 'job'; | |
271 | } | |
272 | if ($flags & self::ADDRESS_PERSO) { | |
273 | $type[] = 'home'; | |
274 | } | |
275 | if (count($type) > 0) { | |
276 | $where .= ' AND pa.type IN ' . XDB::formatArray($type); | |
277 | } | |
4bc5b8f0 | 278 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
f5642983 FB |
279 | return XDB::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude, |
280 | gl.name AS locality, gas.name AS subAdministrativeArea, | |
281 | ga.name AS administrativeArea, gc.countryFR AS country, | |
6b83ce06 | 282 | ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel, |
f5642983 FB |
283 | FIND_IN_SET(\'current\', pa.flags) AS current, |
284 | FIND_IN_SET(\'temporary\', pa.flags) AS temporary, | |
285 | FIND_IN_SET(\'secondary\', pa.flags) AS secondary, | |
286 | FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type | |
287 | FROM profile_addresses AS pa | |
288 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
289 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
290 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
291 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
ce0b2c6f FB |
292 | 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\') |
293 | 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 |
294 | WHERE ' . $where . ' |
295 | ORDER BY pa.id | |
296 | ' . $limit); | |
f5642983 FB |
297 | } |
298 | ||
299 | public function getMainAddress() | |
300 | { | |
301 | $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); | |
302 | if ($it->total() == 0) { | |
303 | return null; | |
304 | } else { | |
305 | return $it->next(); | |
306 | } | |
307 | } | |
3e53a496 | 308 | |
4bc5b8f0 FB |
309 | |
310 | /* Educations | |
311 | */ | |
312 | public function getEducations($flags, $limit = null) | |
313 | { | |
ce0b2c6f | 314 | $where = XDB::format('pe.pid = {?}', $this->id()); |
4bc5b8f0 FB |
315 | if ($flags & self::EDUCATION_MAIN) { |
316 | $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)'; | |
317 | } else if ($flags & self::EDUCATION_EXTRA) { | |
318 | $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)'; | |
319 | } else if ($flags & self::EDUCATION_FINISHED) { | |
320 | $where .= ' AND pe.grad_year <= YEAR(CURDATE())'; | |
321 | } else if ($flags & self::EDUCATION_CURRENT) { | |
322 | $where .= ' AND pe.grad_year > YEAR(CURDATE())'; | |
323 | } | |
324 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
325 | return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program, | |
326 | pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country, | |
327 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field, | |
328 | FIND_IN_SET(\'primary\', pe.flags) AS prim | |
329 | FROM profile_education AS pe | |
330 | INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) | |
331 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
332 | INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) | |
333 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) | |
334 | WHERE ' . $where . ' | |
335 | ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id | |
336 | ' . $limit); | |
337 | } | |
338 | ||
339 | public function getExtraEducations($limit = null) | |
340 | { | |
341 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
342 | } | |
343 | ||
344 | ||
04a94b1d FB |
345 | /** Networking |
346 | */ | |
347 | ||
348 | public function getNetworking($flags, $limit = null) | |
349 | { | |
ce0b2c6f | 350 | $where = XDB::format('pn.pid = {?}', $this->id()); |
04a94b1d FB |
351 | if ($flags & self::NETWORKING_WEB) { |
352 | $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index | |
353 | } | |
354 | if ($this->visibility) { | |
355 | $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility); | |
356 | } | |
357 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
358 | return XDB::iterator('SELECT pne.name, pne.icon, | |
359 | IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address), | |
360 | pn.address) AS address | |
361 | FROM profile_networking AS pn | |
362 | INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type) | |
363 | WHERE ' . $where . ' | |
364 | ORDER BY pn.network_type, pn.nwid | |
365 | ' . $limit); | |
366 | } | |
367 | ||
368 | public function getWebSite() | |
369 | { | |
370 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
371 | if ($site->total() != 1) { | |
372 | return null; | |
373 | } | |
374 | $site = $site->next(); | |
375 | return $site['address']; | |
376 | } | |
377 | ||
378 | ||
e718bd18 FB |
379 | /** Jobs |
380 | */ | |
381 | ||
382 | public function getJobs($flags, $limit = null) | |
383 | { | |
ce0b2c6f | 384 | $where = XDB::format('pj.pid = {?}', $this->id()); |
e718bd18 FB |
385 | $cond = 'TRUE'; |
386 | if ($this->visibility) { | |
387 | $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility); | |
388 | $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility); | |
389 | } | |
390 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
391 | return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code, | |
392 | pj.description, pj.url AS user_site, | |
393 | IF (' . $cond . ', pj.email, NULL) AS user_email, | |
83ff5281 SJ |
394 | pjse.name AS sector, pjsse.name AS subsector, |
395 | pjssse.name AS subsubsector | |
e718bd18 FB |
396 | FROM profile_job AS pj |
397 | INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid) | |
e718bd18 FB |
398 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) |
399 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) | |
400 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) | |
401 | WHERE ' . $where . ' | |
402 | ORDER BY pj.id | |
403 | ' . $limit); | |
404 | } | |
405 | ||
406 | public function getMailJob() | |
407 | { | |
408 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
409 | if ($job->total() != 1) { | |
410 | return null; | |
411 | } | |
412 | return $job->next(); | |
413 | } | |
414 | ||
5c005b37 RB |
415 | /* Binets |
416 | */ | |
417 | public function getBinets() | |
418 | { | |
419 | return XDB::fetchColumn('SELECT binet_id | |
5c8a71f2 | 420 | FROM profile_binets |
bdd977d7 | 421 | WHERE pid = {?}', $this->id()); |
5c005b37 RB |
422 | } |
423 | ||
e718bd18 | 424 | |
e7b93962 FB |
425 | public function owner() |
426 | { | |
427 | return User::getSilent($this); | |
428 | } | |
429 | ||
94590511 SJ |
430 | public function compareNames($firstname, $lastname) |
431 | { | |
432 | $_lastname = mb_strtoupper($this->lastName()); | |
433 | $_firstname = mb_strtoupper($this->firstName()); | |
434 | $lastname = mb_strtoupper($lastname); | |
435 | $firstname = mb_strtoupper($firstname); | |
436 | ||
437 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
438 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
439 | $maxlen = 0; | |
440 | ||
441 | foreach ($tokens as $str) { | |
442 | $isOk &= (strpos($_lastname, $str) !== false); | |
443 | $maxlen = max($maxlen, strlen($str)); | |
444 | } | |
445 | ||
446 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
447 | } | |
448 | ||
0d906109 | 449 | private static function fetchProfileData(array $pids, $respect_order = true) |
b774ddab FB |
450 | { |
451 | if (count($pids) == 0) { | |
452 | return array(); | |
453 | } | |
0d906109 RB |
454 | |
455 | if ($respect_order) { | |
456 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
457 | } else { | |
458 | $order = ''; | |
459 | } | |
460 | ||
461 | return XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, | |
462 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, | |
463 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, | |
464 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
465 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, | |
466 | pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub, | |
88c31faf FB |
467 | ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub, |
468 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, | |
469 | ap.uid AS owner_id | |
0d906109 RB |
470 | FROM profiles AS p |
471 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
ce0b2c6f | 472 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) |
0d906109 RB |
473 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
474 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
475 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
476 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
477 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
478 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
479 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
480 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
481 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
482 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
ce0b2c6f | 483 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') |
5c4ea53f | 484 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) |
0d906109 RB |
485 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
486 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
487 | GROUP BY p.pid | |
488 | ' . $order); | |
b774ddab FB |
489 | } |
490 | ||
491 | public static function getPID($login) | |
492 | { | |
493 | if ($login instanceof PlUser) { | |
494 | return XDB::fetchOneCell('SELECT pid | |
495 | FROM account_profiles | |
496 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
497 | $login->id()); | |
498 | } else if (ctype_digit($login)) { | |
499 | return XDB::fetchOneCell('SELECT pid | |
500 | FROM profiles | |
501 | WHERE pid = {?}', $login); | |
502 | } else { | |
503 | return XDB::fetchOneCell('SELECT pid | |
504 | FROM profiles | |
505 | WHERE hrpid = {?}', $login); | |
506 | } | |
507 | } | |
508 | ||
0d906109 RB |
509 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
510 | { | |
511 | if ($respect_order) { | |
512 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
513 | } else { | |
514 | $order = ''; | |
515 | } | |
516 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
517 | FROM account_profiles AS ap | |
518 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
519 | AND ap.uid IN ' . XDB::formatArray($uids) .' | |
520 | ' . $order); | |
521 | } | |
b774ddab | 522 | |
e7b93962 FB |
523 | /** Return the profile associated with the given login. |
524 | */ | |
a3118782 FB |
525 | public static function get($login) |
526 | { | |
641f2115 FB |
527 | if (is_array($login)) { |
528 | return new Profile($login); | |
529 | } | |
b774ddab FB |
530 | $pid = self::getPID($login); |
531 | if (!is_null($pid)) { | |
0d906109 RB |
532 | $it = self::iterOverPIDs(array($pid), false); |
533 | return $it->next(); | |
b774ddab | 534 | } else { |
efe597c5 FB |
535 | /* Let say we can identify a profile using the identifiers of its owner. |
536 | */ | |
455ea0c9 FB |
537 | if (!($login instanceof PlUser)) { |
538 | $user = User::getSilent($login); | |
539 | if ($user && $user->hasProfile()) { | |
540 | return $user->profile(); | |
541 | } | |
efe597c5 | 542 | } |
3e53a496 | 543 | return null; |
e7b93962 FB |
544 | } |
545 | } | |
a3118782 | 546 | |
0d906109 RB |
547 | public static function iterOverUIDs($uids, $respect_order = true) |
548 | { | |
549 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order); | |
550 | } | |
551 | ||
552 | public static function iterOverPIDs($pids, $respect_order = true) | |
553 | { | |
554 | return new ProfileIterator(self::fetchProfileData($pids, $respect_order)); | |
555 | } | |
556 | ||
b774ddab FB |
557 | /** Return profiles for the list of pids. |
558 | */ | |
559 | public static function getBulkProfilesWithPIDs(array $pids) | |
560 | { | |
561 | if (count($pids) == 0) { | |
562 | return array(); | |
563 | } | |
0d906109 | 564 | $it = self::iterOverPIDs($pids); |
b774ddab | 565 | $profiles = array(); |
0d906109 RB |
566 | while ($p = $it->next()) { |
567 | $profiles[$p->id()] = $p; | |
b774ddab FB |
568 | } |
569 | return $profiles; | |
570 | } | |
571 | ||
572 | /** Return profiles for uids. | |
573 | */ | |
574 | public static function getBulkProfilesWithUIDS(array $uids) | |
575 | { | |
576 | if (count($uids) == 0) { | |
577 | return array(); | |
578 | } | |
0d906109 | 579 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids)); |
b774ddab FB |
580 | } |
581 | ||
913a4e90 RB |
582 | public static function isDisplayName($name) |
583 | { | |
584 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
585 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
586 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
587 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
588 | } | |
589 | ||
a3118782 FB |
590 | public static function getNameTypeId($type, $for_sql = false) |
591 | { | |
592 | if (!S::has('name_types')) { | |
eb6207f7 | 593 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
32742f84 | 594 | FROM profile_name_enum'); |
a3118782 FB |
595 | S::set('name_types', $table); |
596 | } else { | |
597 | $table = S::v('name_types'); | |
598 | } | |
599 | if ($for_sql) { | |
600 | return XDB::escape($table[$type]); | |
601 | } else { | |
602 | return $table[$type]; | |
603 | } | |
604 | } | |
726eaf7a SJ |
605 | |
606 | public static function rebuildSearchTokens($pid) | |
607 | { | |
608 | XDB::execute('DELETE FROM search_name | |
6dbb167e | 609 | WHERE pid = {?}', |
726eaf7a SJ |
610 | $pid); |
611 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, | |
612 | FIND_IN_SET('public', e.flags) AS public | |
613 | FROM profile_name AS n | |
614 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
615 | WHERE n.pid = {?}", | |
616 | $pid); | |
617 | ||
618 | foreach ($keys as $i => $key) { | |
619 | if ($key['name'] == '') { | |
620 | continue; | |
621 | } | |
622 | $toks = preg_split('/[ \'\-]+/', $key['name']); | |
623 | $token = ''; | |
624 | $first = 5; | |
625 | while ($toks) { | |
626 | $token = strtolower(replace_accent(array_pop($toks) . $token)); | |
627 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); | |
6dbb167e | 628 | XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags) |
726eaf7a | 629 | VALUES ({?}, {?}, {?}, {?}, {?})', |
6dbb167e | 630 | $token, $pid, soundex_fr($token), $score, $key['public']); |
726eaf7a SJ |
631 | $first = 0; |
632 | } | |
633 | } | |
69b46857 SJ |
634 | } |
635 | ||
636 | /** The school identifier consists of 6 digits. The first 3 represent the | |
637 | * promotion entry year. The last 3 indicate the student's rank. | |
638 | * | |
639 | * Our identifier consists of 8 digits and both half have the same role. | |
640 | * This enables us to deal with bigger promotions and with a wider range | |
641 | * of promotions. | |
642 | * | |
643 | * getSchoolId returns a school identifier given one of ours. | |
644 | * getXorgId returns a X.org identifier given a school identifier. | |
645 | */ | |
646 | public static function getSchoolId($xorgId) | |
647 | { | |
648 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
649 | return null; | |
650 | } | |
651 | ||
652 | $year = intval(substr($xorgId, 0, 4)); | |
653 | $rank = intval(substr($xorgId, 5, 3)); | |
654 | if ($year < 1996) { | |
655 | return null; | |
656 | } elseif ($year < 2000) { | |
657 | $year = intval(substr(1900 - $year, 1, 3)); | |
658 | return sprintf('%02u0%03u', $year, $rank); | |
659 | } else { | |
660 | $year = intval(substr(1900 - $year, 1, 3)); | |
661 | return sprintf('%03u%03u', $year, $rank); | |
662 | } | |
663 | } | |
726eaf7a | 664 | |
69b46857 SJ |
665 | public static function getXorgId($schoolId) |
666 | { | |
94590511 | 667 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
668 | return null; |
669 | } | |
670 | ||
69b46857 SJ |
671 | $year = intval(substr($schoolId, 0, 3)); |
672 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 673 | |
69b46857 SJ |
674 | if ($year > 200) { |
675 | $year /= 10; | |
676 | } | |
677 | if ($year < 96) { | |
678 | return null; | |
679 | } else { | |
680 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
681 | } | |
726eaf7a | 682 | } |
e7b93962 FB |
683 | } |
684 | ||
0d906109 RB |
685 | /** Iterator over a set of Profiles |
686 | * @param an XDB::Iterator obtained from a Profile::fetchProfileData | |
687 | */ | |
688 | class ProfileIterator implements PlIterator | |
689 | { | |
690 | private $dbiter; | |
691 | ||
692 | public function __construct($dbiter) | |
693 | { | |
694 | $this->dbiter = $dbiter; | |
695 | } | |
696 | ||
697 | public function next() | |
698 | { | |
699 | $data = $this->dbiter->next(); | |
700 | if ($data == null) { | |
701 | return null; | |
702 | } else { | |
641f2115 | 703 | return Profile::get($data); |
0d906109 RB |
704 | } |
705 | } | |
706 | ||
707 | public function total() | |
708 | { | |
709 | return $this->dbiter->total(); | |
710 | } | |
711 | ||
712 | public function first() | |
713 | { | |
714 | return $this->dbiter->first(); | |
715 | } | |
716 | ||
717 | public function last() | |
718 | { | |
719 | return $this->dbiter->last(); | |
720 | } | |
721 | } | |
722 | ||
e7b93962 FB |
723 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
724 | ?> |