Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
34ade5a6 | 3 | * Copyright (C) 2003-2009 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 | { | |
24 | private $pid; | |
25 | private $hrpid; | |
3e53a496 FB |
26 | private $data = array(); |
27 | ||
b774ddab | 28 | private function __construct(array $data) |
e7b93962 | 29 | { |
b774ddab | 30 | $this->data = $data; |
832e6fcb FB |
31 | $this->pid = $this->data['pid']; |
32 | $this->hrpid = $this->data['hrpid']; | |
e7b93962 FB |
33 | } |
34 | ||
35 | public function id() | |
36 | { | |
37 | return $this->pid; | |
38 | } | |
39 | ||
40 | public function hrid() | |
41 | { | |
42 | return $this->hrpid; | |
43 | } | |
44 | ||
d5e60905 FB |
45 | public function promo() |
46 | { | |
47 | return $this->promo; | |
48 | } | |
49 | ||
94b72319 FB |
50 | /** Print a name with the given formatting: |
51 | * %s = • for women | |
52 | * %f = firstname | |
53 | * %l = lastname | |
54 | * %F = fullname | |
55 | * %S = shortname | |
56 | * %p = promo | |
57 | */ | |
58 | public function name($format) | |
59 | { | |
60 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
61 | array($this->isFemale() ? '•' : '', | |
62 | $this->first_name, $this->last_name, | |
63 | $this->full_name, $this->short_name, | |
64 | $this->promo), $format); | |
65 | } | |
66 | ||
67 | public function fullName($with_promo = false) | |
68 | { | |
69 | if ($with_promo) { | |
70 | return $this->full_name . ' (' . $this->promo . ')'; | |
71 | } | |
72 | return $this->full_name; | |
73 | } | |
74 | ||
75 | public function shortName($with_promo = false) | |
76 | { | |
77 | if ($with_promo) { | |
78 | return $this->short_name . ' (' . $this->promo . ')'; | |
79 | } | |
80 | return $this->short_name; | |
81 | } | |
82 | ||
83 | public function firstName() | |
84 | { | |
08c91036 | 85 | return $this->firstname; |
94b72319 FB |
86 | } |
87 | ||
88 | public function lastName() | |
89 | { | |
08c91036 | 90 | return $this->lastname; |
94b72319 FB |
91 | } |
92 | ||
93 | public function isFemale() | |
94 | { | |
95 | return $this->sex == PlUser::GENDER_FEMALE; | |
96 | } | |
97 | ||
98 | public function data() | |
99 | { | |
100 | $this->first_name; | |
101 | return $this->data; | |
102 | } | |
103 | ||
3e53a496 FB |
104 | public function __get($name) |
105 | { | |
106 | if (property_exists($this, $name)) { | |
107 | return $this->$name; | |
108 | } | |
109 | ||
3e53a496 FB |
110 | if (isset($this->data[$name])) { |
111 | return $this->data[$name]; | |
112 | } | |
113 | ||
114 | return null; | |
115 | } | |
116 | ||
117 | public function __isset($name) | |
118 | { | |
119 | return property_exists($this, $name) || isset($this->data[$name]); | |
120 | } | |
121 | ||
122 | ||
e7b93962 FB |
123 | public function owner() |
124 | { | |
125 | return User::getSilent($this); | |
126 | } | |
127 | ||
b774ddab FB |
128 | private static function fetchProfileData(array $pids) |
129 | { | |
130 | if (count($pids) == 0) { | |
131 | return array(); | |
132 | } | |
133 | return XDB::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, | |
134 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, | |
135 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual, | |
136 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual, | |
137 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name | |
138 | FROM profiles AS p | |
139 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
140 | INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
141 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid | |
142 | AND pn_f.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
143 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
144 | AND pn_l.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
145 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
146 | AND pn_uf.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
147 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
148 | AND pn_ul.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
149 | LEFT JOIN profile_name aS pn_n ON (pn_n.pid = p.pid | |
150 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
151 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
152 | GROUP BY p.pid'); | |
153 | } | |
154 | ||
155 | public static function getPID($login) | |
156 | { | |
157 | if ($login instanceof PlUser) { | |
158 | return XDB::fetchOneCell('SELECT pid | |
159 | FROM account_profiles | |
160 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
161 | $login->id()); | |
162 | } else if (ctype_digit($login)) { | |
163 | return XDB::fetchOneCell('SELECT pid | |
164 | FROM profiles | |
165 | WHERE pid = {?}', $login); | |
166 | } else { | |
167 | return XDB::fetchOneCell('SELECT pid | |
168 | FROM profiles | |
169 | WHERE hrpid = {?}', $login); | |
170 | } | |
171 | } | |
172 | ||
173 | ||
e7b93962 FB |
174 | /** Return the profile associated with the given login. |
175 | */ | |
a3118782 FB |
176 | public static function get($login) |
177 | { | |
b774ddab FB |
178 | $pid = self::getPID($login); |
179 | if (!is_null($pid)) { | |
180 | $data = self::fetchProfileData(array($pid)); | |
181 | return new Profile(array_pop($data)); | |
182 | } else { | |
efe597c5 FB |
183 | /* Let say we can identify a profile using the identifiers of its owner. |
184 | */ | |
455ea0c9 FB |
185 | if (!($login instanceof PlUser)) { |
186 | $user = User::getSilent($login); | |
187 | if ($user && $user->hasProfile()) { | |
188 | return $user->profile(); | |
189 | } | |
efe597c5 | 190 | } |
3e53a496 | 191 | return null; |
e7b93962 FB |
192 | } |
193 | } | |
a3118782 | 194 | |
b774ddab FB |
195 | /** Return profiles for the list of pids. |
196 | */ | |
197 | public static function getBulkProfilesWithPIDs(array $pids) | |
198 | { | |
199 | if (count($pids) == 0) { | |
200 | return array(); | |
201 | } | |
202 | $data = self::fetchProfileData($pids); | |
203 | $inv = array_flip($pids); | |
204 | $profiles = array(); | |
205 | foreach ($data AS $p) { | |
206 | $p = new Profile($p); | |
207 | $key = $inv[$p->id()]; | |
208 | $profiles[$key] = $p; | |
209 | } | |
210 | return $profiles; | |
211 | } | |
212 | ||
213 | /** Return profiles for uids. | |
214 | */ | |
215 | public static function getBulkProfilesWithUIDS(array $uids) | |
216 | { | |
217 | if (count($uids) == 0) { | |
218 | return array(); | |
219 | } | |
220 | $table = XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
221 | FROM account_profiles AS ap | |
222 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
223 | AND ap.uid IN ' . XDB::formatArray($uids)); | |
224 | return self::getBulkProfilesWithPIDs($table); | |
225 | } | |
226 | ||
a3118782 FB |
227 | public static function getNameTypeId($type, $for_sql = false) |
228 | { | |
229 | if (!S::has('name_types')) { | |
eb6207f7 | 230 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
32742f84 | 231 | FROM profile_name_enum'); |
a3118782 FB |
232 | S::set('name_types', $table); |
233 | } else { | |
234 | $table = S::v('name_types'); | |
235 | } | |
236 | if ($for_sql) { | |
237 | return XDB::escape($table[$type]); | |
238 | } else { | |
239 | return $table[$type]; | |
240 | } | |
241 | } | |
e7b93962 FB |
242 | } |
243 | ||
244 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
245 | ?> |