22f4833c047f9b797f00689da44882c3fd0ec97f
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];
156 if ($this->mobile
&& !in_array($this->modbile_pub
, $this->visibility
)) {
157 unset($this->data
['mobile']);
164 public function getAddresses($flags, $limit = null
)
166 $where = XDB
::format('pa.pid = {?}', $this->id());
167 if ($flags & self
::ADDRESS_MAIN
) {
168 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
170 if ($flags & self
::ADDRESS_POSTAL
) {
171 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
173 if ($this->visibility
) {
174 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
177 if ($flags & self
::ADDRESS_PRO
) {
180 if ($flags & self
::ADDRESS_PERSO
) {
183 if (count($type) > 0) {
184 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
186 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
187 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
188 gl.name AS locality, gas.name AS subAdministrativeArea,
189 ga.name AS administrativeArea, gc.countryFR AS country,
190 FIND_IN_SET(\'current\', pa.flags) AS current,
191 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
192 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
193 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
194 FROM profile_addresses AS pa
195 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
196 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
197 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
198 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
204 public function getMainAddress()
206 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
207 if ($it->total() == 0) {
217 public function getEducations($flags, $limit = null
)
219 $where = XDB
::format('pe.uid = {?}', $this->id());
220 if ($flags & self
::EDUCATION_MAIN
) {
221 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
222 } else if ($flags & self
::EDUCATION_EXTRA
) {
223 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
224 } else if ($flags & self
::EDUCATION_FINISHED
) {
225 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
226 } else if ($flags & self
::EDUCATION_CURRENT
) {
227 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
229 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
230 return XDB
::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
231 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
232 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
233 FIND_IN_SET(\'primary\', pe.flags) AS prim
234 FROM profile_education AS pe
235 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
236 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
237 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
238 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
240 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
244 public function getExtraEducations($limit = null
)
246 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
250 public function owner()
252 return User
::getSilent($this);
255 private static function fetchProfileData(array $pids)
257 if (count($pids) == 0) {
260 return XDB
::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
261 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
262 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
263 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
264 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
265 pp.display_tel AS mobile, pp.pub AS mobile_pub
267 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
268 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
269 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
270 AND pn_f.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
271 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
272 AND pn_l.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
273 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
274 AND pn_uf.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
275 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
276 AND pn_ul.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
277 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
278 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
279 LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
280 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
284 public static function getPID($login)
286 if ($login instanceof PlUser
) {
287 return XDB
::fetchOneCell('SELECT pid
288 FROM account_profiles
289 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
291 } else if (ctype_digit($login)) {
292 return XDB
::fetchOneCell('SELECT pid
294 WHERE pid = {?}', $login);
296 return XDB
::fetchOneCell('SELECT pid
298 WHERE hrpid = {?}', $login);
303 /** Return the profile associated with the given login.
305 public static function get($login)
307 $pid = self
::getPID($login);
308 if (!is_null($pid)) {
309 $data = self
::fetchProfileData(array($pid));
310 return new Profile(array_pop($data));
312 /* Let say we can identify a profile using the identifiers of its owner.
314 if (!($login instanceof PlUser
)) {
315 $user = User
::getSilent($login);
316 if ($user && $user->hasProfile()) {
317 return $user->profile();
324 /** Return profiles for the list of pids.
326 public static function getBulkProfilesWithPIDs(array $pids)
328 if (count($pids) == 0) {
331 $data = self
::fetchProfileData($pids);
332 $inv = array_flip($pids);
334 foreach ($data AS $p) {
335 $p = new Profile($p);
336 $key = $inv[$p->id()];
337 $profiles[$key] = $p;
342 /** Return profiles for uids.
344 public static function getBulkProfilesWithUIDS(array $uids)
346 if (count($uids) == 0) {
349 $table = XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
350 FROM account_profiles AS ap
351 WHERE FIND_IN_SET(\'owner\', ap.perms)
352 AND ap.uid IN ' . XDB
::formatArray($uids));
353 return self
::getBulkProfilesWithPIDs($table);
356 public static function getNameTypeId($type, $for_sql = false
)
358 if (!S
::has('name_types')) {
359 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
360 FROM profile_name_enum');
361 S
::set('name_types', $table);
363 $table = S
::v('name_types');
366 return XDB
::escape($table[$type]);
368 return $table[$type];
373 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: