7828cebe131911f6981e031d5aa1be32a21e3357
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_ALL
= 0x000020;
39 const EDUCATION_FINISHED
= 0x000040;
40 const EDUCATION_CURRENT
= 0x000080;
42 const JOBS_MAIN
= 0x000100;
43 const JOBS_ALL
= 0x000200;
44 const JOBS_FINISHED
= 0x000400;
45 const JOBS_CURRENT
= 0x000800;
49 private $data = array();
51 private $visibility = null
;
53 private function __construct(array $data)
56 $this->pid
= $this->data
['pid'];
57 $this->hrpid
= $this->data
['hrpid'];
65 public function hrid()
70 public function promo()
75 /** Print a name with the given formatting:
83 public function name($format)
85 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
86 array($this->isFemale() ?
'•' : '',
87 $this->first_name
, $this->last_name
,
88 $this->full_name
, $this->short_name
,
89 $this->promo
), $format);
92 public function fullName($with_promo = false
)
95 return $this->full_name
. ' (' . $this->promo
. ')';
97 return $this->full_name
;
100 public function shortName($with_promo = false
)
103 return $this->short_name
. ' (' . $this->promo
. ')';
105 return $this->short_name
;
108 public function firstName()
110 return $this->firstname
;
113 public function lastName()
115 return $this->lastname
;
118 public function isFemale()
120 return $this->sex
== PlUser
::GENDER_FEMALE
;
123 public function data()
129 public function __get($name)
131 if (property_exists($this, $name)) {
135 if (isset($this->data
[$name])) {
136 return $this->data
[$name];
142 public function __isset($name)
144 return property_exists($this, $name) ||
isset($this->data
[$name]);
147 public function setVisibilityLevel($visibility)
149 if ($visibility != self
::VISIBILITY_PRIVATE
150 && $visibility != self
::VISIBILITY_AX
151 && $visibility != self
::VISIBILITY_PUBLIC
) {
152 Platal
::page()->kill("Visibility invalide: " . $visibility);
154 $this->visibility
= self
::$v_values[$visibility];
157 public function getAddresses($flags)
159 $where = XDB
::format('pa.pid = {?}', $this->id());
160 if ($flags & self
::ADDRESS_MAIN
) {
161 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
163 if ($flags & self
::ADDRESS_POSTAL
) {
164 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
166 if ($this->visibility
) {
167 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
170 if ($flags & self
::ADDRESS_PRO
) {
173 if ($flags & self
::ADDRESS_PERSO
) {
176 if (count($type) > 0) {
177 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
179 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
180 gl.name AS locality, gas.name AS subAdministrativeArea,
181 ga.name AS administrativeArea, gc.countryFR AS country,
182 FIND_IN_SET(\'current\', pa.flags) AS current,
183 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
184 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
185 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
186 FROM profile_addresses AS pa
187 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
188 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
189 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
190 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
194 public function getMainAddress()
196 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
197 if ($it->total() == 0) {
204 public function owner()
206 return User
::getSilent($this);
209 private static function fetchProfileData(array $pids)
211 if (count($pids) == 0) {
214 return XDB
::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
215 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
216 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
217 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
218 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name
220 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
221 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
222 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
223 AND pn_f.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
224 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
225 AND pn_l.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
226 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
227 AND pn_uf.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
228 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
229 AND pn_ul.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
230 LEFT JOIN profile_name aS pn_n ON (pn_n.pid = p.pid
231 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
232 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
236 public static function getPID($login)
238 if ($login instanceof PlUser
) {
239 return XDB
::fetchOneCell('SELECT pid
240 FROM account_profiles
241 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
243 } else if (ctype_digit($login)) {
244 return XDB
::fetchOneCell('SELECT pid
246 WHERE pid = {?}', $login);
248 return XDB
::fetchOneCell('SELECT pid
250 WHERE hrpid = {?}', $login);
255 /** Return the profile associated with the given login.
257 public static function get($login)
259 $pid = self
::getPID($login);
260 if (!is_null($pid)) {
261 $data = self
::fetchProfileData(array($pid));
262 return new Profile(array_pop($data));
264 /* Let say we can identify a profile using the identifiers of its owner.
266 if (!($login instanceof PlUser
)) {
267 $user = User
::getSilent($login);
268 if ($user && $user->hasProfile()) {
269 return $user->profile();
276 /** Return profiles for the list of pids.
278 public static function getBulkProfilesWithPIDs(array $pids)
280 if (count($pids) == 0) {
283 $data = self
::fetchProfileData($pids);
284 $inv = array_flip($pids);
286 foreach ($data AS $p) {
287 $p = new Profile($p);
288 $key = $inv[$p->id()];
289 $profiles[$key] = $p;
294 /** Return profiles for uids.
296 public static function getBulkProfilesWithUIDS(array $uids)
298 if (count($uids) == 0) {
301 $table = XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
302 FROM account_profiles AS ap
303 WHERE FIND_IN_SET(\'owner\', ap.perms)
304 AND ap.uid IN ' . XDB
::formatArray($uids));
305 return self
::getBulkProfilesWithPIDs($table);
308 public static function getNameTypeId($type, $for_sql = false
)
310 if (!S
::has('name_types')) {
311 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
312 FROM profile_name_enum');
313 S
::set('name_types', $table);
315 $table = S
::v('name_types');
318 return XDB
::escape($table[$type]);
320 return $table[$type];
325 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: