Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
832e6fcb | 3 | * Copyright (C) 2003-2008 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 | ||
e7b93962 FB |
28 | private function __construct($login) |
29 | { | |
30 | if ($login instanceof PlUser) { | |
832e6fcb FB |
31 | $from = 'account_profiles AS ap |
32 | INNER JOIN profiles AS p ON (p.pid = ap.pid)'; | |
33 | $where = XDB::format('ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id()); | |
e7b93962 | 34 | } else if (is_numeric($login)) { |
832e6fcb FB |
35 | $from = 'profiles AS p'; |
36 | $where = XDB::format('p.pid = {?}', $login); | |
e7b93962 | 37 | } else { |
832e6fcb FB |
38 | $from = 'profiles AS p'; |
39 | $where = XDB::format('p.hrpid = {?}', $login); | |
e7b93962 | 40 | } |
a3118782 FB |
41 | $res = XDB::query('SELECT p.*, pe.entry_year, pe.grad_year, |
42 | pns_f.name AS firstname, pns_l.name AS lastname, pns_n.name AS nickname, | |
43 | IF(pns_uf.name IS NULL, pns_f.name, pns_uf.name) AS firstname_usual, | |
44 | IF(pns_ul.name IS NULL, pns_l.name, pns_ul.name) AS lastname_usual, | |
45 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name | |
832e6fcb | 46 | FROM ' . $from . ' |
832e6fcb | 47 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) |
a3118782 FB |
48 | INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) |
49 | INNER JOIN profile_name_search AS pns_f ON (pns_f.pid = p.pid AND pns_f.typeid = ' . self::getNameTypeId('Nom patronymique', true) . ') | |
50 | INNER JOIN profile_name_search AS pns_l ON (pns_l.pid = p.pid AND pns_l.typeid = ' . self::getNameTypeId('Prénom', true) . ') | |
51 | LEFT JOIN profile_name_search AS pns_uf ON (pns_uf.pid = p.pid AND pns_uf.typeid = ' . self::getNameTypeId('Prénom usuel', true) . ') | |
52 | LEFT JOIN profile_name_search AS pns_ul ON (pns_ul.pid = p.pid AND pns_ul.typeid = ' . self::getNameTypeId('Nom usuel', true) . ') | |
53 | LEFT JOIN profile_name_search aS pns_n ON (pns_n.pid = p.pid AND pns_n.typeid = ' . self::getNameTypeId('Surnom', true) . ') | |
832e6fcb | 54 | WHERE ' . $where); |
e7b93962 FB |
55 | if ($res->numRows() != 1) { |
56 | throw new UserNotFoundException(); | |
57 | } | |
832e6fcb FB |
58 | $this->data = $res->fetchOneAssoc(); |
59 | $this->pid = $this->data['pid']; | |
60 | $this->hrpid = $this->data['hrpid']; | |
e7b93962 FB |
61 | } |
62 | ||
63 | public function id() | |
64 | { | |
65 | return $this->pid; | |
66 | } | |
67 | ||
68 | public function hrid() | |
69 | { | |
70 | return $this->hrpid; | |
71 | } | |
72 | ||
d5e60905 FB |
73 | public function promo() |
74 | { | |
75 | return $this->promo; | |
76 | } | |
77 | ||
94b72319 FB |
78 | /** Print a name with the given formatting: |
79 | * %s = • for women | |
80 | * %f = firstname | |
81 | * %l = lastname | |
82 | * %F = fullname | |
83 | * %S = shortname | |
84 | * %p = promo | |
85 | */ | |
86 | public function name($format) | |
87 | { | |
88 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
89 | array($this->isFemale() ? '•' : '', | |
90 | $this->first_name, $this->last_name, | |
91 | $this->full_name, $this->short_name, | |
92 | $this->promo), $format); | |
93 | } | |
94 | ||
95 | public function fullName($with_promo = false) | |
96 | { | |
97 | if ($with_promo) { | |
98 | return $this->full_name . ' (' . $this->promo . ')'; | |
99 | } | |
100 | return $this->full_name; | |
101 | } | |
102 | ||
103 | public function shortName($with_promo = false) | |
104 | { | |
105 | if ($with_promo) { | |
106 | return $this->short_name . ' (' . $this->promo . ')'; | |
107 | } | |
108 | return $this->short_name; | |
109 | } | |
110 | ||
111 | public function firstName() | |
112 | { | |
113 | return $this->first_name; | |
114 | } | |
115 | ||
116 | public function lastName() | |
117 | { | |
118 | return $this->last_name; | |
119 | } | |
120 | ||
121 | public function isFemale() | |
122 | { | |
123 | return $this->sex == PlUser::GENDER_FEMALE; | |
124 | } | |
125 | ||
126 | public function data() | |
127 | { | |
128 | $this->first_name; | |
129 | return $this->data; | |
130 | } | |
131 | ||
3e53a496 FB |
132 | public function __get($name) |
133 | { | |
134 | if (property_exists($this, $name)) { | |
135 | return $this->$name; | |
136 | } | |
137 | ||
3e53a496 FB |
138 | if (isset($this->data[$name])) { |
139 | return $this->data[$name]; | |
140 | } | |
141 | ||
142 | return null; | |
143 | } | |
144 | ||
145 | public function __isset($name) | |
146 | { | |
147 | return property_exists($this, $name) || isset($this->data[$name]); | |
148 | } | |
149 | ||
150 | ||
e7b93962 FB |
151 | public function owner() |
152 | { | |
153 | return User::getSilent($this); | |
154 | } | |
155 | ||
156 | /** Return the profile associated with the given login. | |
157 | */ | |
a3118782 FB |
158 | public static function get($login) |
159 | { | |
e7b93962 FB |
160 | try { |
161 | return new Profile($login); | |
162 | } catch (UserNotFoundException $e) { | |
efe597c5 FB |
163 | /* Let say we can identify a profile using the identifiers of its owner. |
164 | */ | |
165 | $user = User::getSilent($login); | |
166 | if ($user && $user->hasProfile()) { | |
167 | return $user->profile(); | |
168 | } | |
3e53a496 | 169 | return null; |
e7b93962 FB |
170 | } |
171 | } | |
a3118782 FB |
172 | |
173 | public static function getNameTypeId($type, $for_sql = false) | |
174 | { | |
175 | if (!S::has('name_types')) { | |
176 | $table = XDB::fetchAllAssoc('name', 'SELECT id, name | |
177 | FROM profile_name_search_enum'); | |
178 | S::set('name_types', $table); | |
179 | } else { | |
180 | $table = S::v('name_types'); | |
181 | } | |
182 | if ($for_sql) { | |
183 | return XDB::escape($table[$type]); | |
184 | } else { | |
185 | return $table[$type]; | |
186 | } | |
187 | } | |
e7b93962 FB |
188 | } |
189 | ||
190 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
191 | ?> |