2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
27 const VISIBILITY_PUBLIC
= 'public';
28 const VISIBILITY_AX
= 'ax';
29 const VISIBILITY_PRIVATE
= 'private';
31 const ADDRESS_MAIN
= 0x000001;
32 const ADDRESS_PERSO
= 0x000002;
33 const ADDRESS_PRO
= 0x000004;
34 const ADDRESS_ALL
= 0x000006;
35 const ADDRESS_POSTAL
= 0x000008;
37 const EDUCATION_MAIN
= 0x000010;
38 const EDUCATION_EXTRA
= 0x000020;
39 const EDUCATION_ALL
= 0x000040;
40 const EDUCATION_FINISHED
= 0x000080;
41 const EDUCATION_CURRENT
= 0x000100;
43 const JOBS_MAIN
= 0x001000;
44 const JOBS_ALL
= 0x002000;
45 const JOBS_FINISHED
= 0x004000;
46 const JOBS_CURRENT
= 0x008000;
50 private $data = array();
52 private $visibility = null
;
54 private function __construct(array $data)
57 $this->pid
= $this->data
['pid'];
58 $this->hrpid
= $this->data
['hrpid'];
66 public function hrid()
71 public function promo()
76 /** Print a name with the given formatting:
84 public function name($format)
86 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
87 array($this->isFemale() ?
'•' : '',
88 $this->first_name
, $this->last_name
,
89 $this->full_name
, $this->short_name
,
90 $this->promo
), $format);
93 public function fullName($with_promo = false
)
96 return $this->full_name
. ' (' . $this->promo
. ')';
98 return $this->full_name
;
101 public function shortName($with_promo = false
)
104 return $this->short_name
. ' (' . $this->promo
. ')';
106 return $this->short_name
;
109 public function firstName()
111 return $this->firstname
;
114 public function lastName()
116 return $this->lastname
;
119 public function isFemale()
121 return $this->sex
== PlUser
::GENDER_FEMALE
;
124 public function data()
130 public function __get($name)
132 if (property_exists($this, $name)) {
136 if (isset($this->data
[$name])) {
137 return $this->data
[$name];
143 public function __isset($name)
145 return property_exists($this, $name) ||
isset($this->data
[$name]);
148 public function setVisibilityLevel($visibility)
150 if ($visibility != self
::VISIBILITY_PRIVATE
151 && $visibility != self
::VISIBILITY_AX
152 && $visibility != self
::VISIBILITY_PUBLIC
) {
153 Platal
::page()->kill("Visibility invalide: " . $visibility);
155 $this->visibility
= self
::$v_values[$visibility];
161 public function getAddresses($flags, $limit = null
)
163 $where = XDB
::format('pa.pid = {?}', $this->id());
164 if ($flags & self
::ADDRESS_MAIN
) {
165 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
167 if ($flags & self
::ADDRESS_POSTAL
) {
168 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
170 if ($this->visibility
) {
171 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
174 if ($flags & self
::ADDRESS_PRO
) {
177 if ($flags & self
::ADDRESS_PERSO
) {
180 if (count($type) > 0) {
181 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
183 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
184 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
185 gl.name AS locality, gas.name AS subAdministrativeArea,
186 ga.name AS administrativeArea, gc.countryFR AS country,
187 FIND_IN_SET(\'current\', pa.flags) AS current,
188 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
189 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
190 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
191 FROM profile_addresses AS pa
192 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
193 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
194 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
195 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
201 public function getMainAddress()
203 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
204 if ($it->total() == 0) {
214 public function getEducations($flags, $limit = null
)
216 $where = XDB
::format('pe.uid = {?}', $this->id());
217 if ($flags & self
::EDUCATION_MAIN
) {
218 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
219 } else if ($flags & self
::EDUCATION_EXTRA
) {
220 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
221 } else if ($flags & self
::EDUCATION_FINISHED
) {
222 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
223 } else if ($flags & self
::EDUCATION_CURRENT
) {
224 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
226 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
227 return XDB
::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
228 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
229 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
230 FIND_IN_SET(\'primary\', pe.flags) AS prim
231 FROM profile_education AS pe
232 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
233 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
234 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
235 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
237 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
241 public function getExtraEducations($limit = null
)
243 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
247 public function owner()
249 return User
::getSilent($this);
252 private static function fetchProfileData(array $pids)
254 if (count($pids) == 0) {
257 return XDB
::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
258 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
259 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
260 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
261 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name
263 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
264 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
265 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
266 AND pn_f.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
267 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
268 AND pn_l.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
269 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
270 AND pn_uf.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
271 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
272 AND pn_ul.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
273 LEFT JOIN profile_name aS pn_n ON (pn_n.pid = p.pid
274 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
275 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
279 public static function getPID($login)
281 if ($login instanceof PlUser
) {
282 return XDB
::fetchOneCell('SELECT pid
283 FROM account_profiles
284 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
286 } else if (ctype_digit($login)) {
287 return XDB
::fetchOneCell('SELECT pid
289 WHERE pid = {?}', $login);
291 return XDB
::fetchOneCell('SELECT pid
293 WHERE hrpid = {?}', $login);
298 /** Return the profile associated with the given login.
300 public static function get($login)
302 $pid = self
::getPID($login);
303 if (!is_null($pid)) {
304 $data = self
::fetchProfileData(array($pid));
305 return new Profile(array_pop($data));
307 /* Let say we can identify a profile using the identifiers of its owner.
309 if (!($login instanceof PlUser
)) {
310 $user = User
::getSilent($login);
311 if ($user && $user->hasProfile()) {
312 return $user->profile();
319 /** Return profiles for the list of pids.
321 public static function getBulkProfilesWithPIDs(array $pids)
323 if (count($pids) == 0) {
326 $data = self
::fetchProfileData($pids);
327 $inv = array_flip($pids);
329 foreach ($data AS $p) {
330 $p = new Profile($p);
331 $key = $inv[$p->id()];
332 $profiles[$key] = $p;
337 /** Return profiles for uids.
339 public static function getBulkProfilesWithUIDS(array $uids)
341 if (count($uids) == 0) {
344 $table = XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
345 FROM account_profiles AS ap
346 WHERE FIND_IN_SET(\'owner\', ap.perms)
347 AND ap.uid IN ' . XDB
::formatArray($uids));
348 return self
::getBulkProfilesWithPIDs($table);
351 public static function getNameTypeId($type, $for_sql = false
)
353 if (!S
::has('name_types')) {
354 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
355 FROM profile_name_enum');
356 S
::set('name_types', $table);
358 $table = S
::v('name_types');
361 return XDB
::escape($table[$type]);
363 return $table[$type];
368 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: