Only remains promo importer to rewrite in modules/admin.php.
[platal.git] / classes / profile.php
CommitLineData
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
22class 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 }
c52d86d1 41 $res = XDB::query('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
eb6207f7
FB
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,
a3118782 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 48 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
eb6207f7
FB
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) . ')
dfcccfc0
FB
54 WHERE ' . $where . '
55 GROUP BY p.pid');
e7b93962 56 if ($res->numRows() != 1) {
32742f84 57 __autoload('PlUser');
e7b93962
FB
58 throw new UserNotFoundException();
59 }
832e6fcb
FB
60 $this->data = $res->fetchOneAssoc();
61 $this->pid = $this->data['pid'];
62 $this->hrpid = $this->data['hrpid'];
e7b93962
FB
63 }
64
65 public function id()
66 {
67 return $this->pid;
68 }
69
70 public function hrid()
71 {
72 return $this->hrpid;
73 }
74
d5e60905
FB
75 public function promo()
76 {
77 return $this->promo;
78 }
79
94b72319
FB
80 /** Print a name with the given formatting:
81 * %s = • for women
82 * %f = firstname
83 * %l = lastname
84 * %F = fullname
85 * %S = shortname
86 * %p = promo
87 */
88 public function name($format)
89 {
90 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
91 array($this->isFemale() ? '•' : '',
92 $this->first_name, $this->last_name,
93 $this->full_name, $this->short_name,
94 $this->promo), $format);
95 }
96
97 public function fullName($with_promo = false)
98 {
99 if ($with_promo) {
100 return $this->full_name . ' (' . $this->promo . ')';
101 }
102 return $this->full_name;
103 }
104
105 public function shortName($with_promo = false)
106 {
107 if ($with_promo) {
108 return $this->short_name . ' (' . $this->promo . ')';
109 }
110 return $this->short_name;
111 }
112
113 public function firstName()
114 {
08c91036 115 return $this->firstname;
94b72319
FB
116 }
117
118 public function lastName()
119 {
08c91036 120 return $this->lastname;
94b72319
FB
121 }
122
123 public function isFemale()
124 {
125 return $this->sex == PlUser::GENDER_FEMALE;
126 }
127
128 public function data()
129 {
130 $this->first_name;
131 return $this->data;
132 }
133
3e53a496
FB
134 public function __get($name)
135 {
136 if (property_exists($this, $name)) {
137 return $this->$name;
138 }
139
3e53a496
FB
140 if (isset($this->data[$name])) {
141 return $this->data[$name];
142 }
143
144 return null;
145 }
146
147 public function __isset($name)
148 {
149 return property_exists($this, $name) || isset($this->data[$name]);
150 }
151
152
e7b93962
FB
153 public function owner()
154 {
155 return User::getSilent($this);
156 }
157
158 /** Return the profile associated with the given login.
159 */
a3118782
FB
160 public static function get($login)
161 {
e7b93962
FB
162 try {
163 return new Profile($login);
164 } catch (UserNotFoundException $e) {
efe597c5
FB
165 /* Let say we can identify a profile using the identifiers of its owner.
166 */
455ea0c9
FB
167 if (!($login instanceof PlUser)) {
168 $user = User::getSilent($login);
169 if ($user && $user->hasProfile()) {
170 return $user->profile();
171 }
efe597c5 172 }
3e53a496 173 return null;
e7b93962
FB
174 }
175 }
a3118782
FB
176
177 public static function getNameTypeId($type, $for_sql = false)
178 {
179 if (!S::has('name_types')) {
eb6207f7 180 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
32742f84 181 FROM profile_name_enum');
a3118782
FB
182 S::set('name_types', $table);
183 } else {
184 $table = S::v('name_types');
185 }
186 if ($for_sql) {
187 return XDB::escape($table[$type]);
188 } else {
189 return $table[$type];
190 }
191 }
e7b93962
FB
192}
193
194// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
195?>