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