| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 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.*, p.sex = \'female\' AS sex, 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 | GROUP BY p.pid'); |
| 56 | if ($res->numRows() != 1) { |
| 57 | __autoload('PlUser'); |
| 58 | throw new UserNotFoundException(); |
| 59 | } |
| 60 | $this->data = $res->fetchOneAssoc(); |
| 61 | $this->pid = $this->data['pid']; |
| 62 | $this->hrpid = $this->data['hrpid']; |
| 63 | } |
| 64 | |
| 65 | public function id() |
| 66 | { |
| 67 | return $this->pid; |
| 68 | } |
| 69 | |
| 70 | public function hrid() |
| 71 | { |
| 72 | return $this->hrpid; |
| 73 | } |
| 74 | |
| 75 | public function promo() |
| 76 | { |
| 77 | return $this->promo; |
| 78 | } |
| 79 | |
| 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 | { |
| 115 | return $this->firstname; |
| 116 | } |
| 117 | |
| 118 | public function lastName() |
| 119 | { |
| 120 | return $this->lastname; |
| 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 | |
| 134 | public function __get($name) |
| 135 | { |
| 136 | if (property_exists($this, $name)) { |
| 137 | return $this->$name; |
| 138 | } |
| 139 | |
| 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 | |
| 153 | public function owner() |
| 154 | { |
| 155 | return User::getSilent($this); |
| 156 | } |
| 157 | |
| 158 | /** Return the profile associated with the given login. |
| 159 | */ |
| 160 | public static function get($login) |
| 161 | { |
| 162 | try { |
| 163 | return new Profile($login); |
| 164 | } catch (UserNotFoundException $e) { |
| 165 | /* Let say we can identify a profile using the identifiers of its owner. |
| 166 | */ |
| 167 | if (!($login instanceof PlUser)) { |
| 168 | $user = User::getSilent($login); |
| 169 | if ($user && $user->hasProfile()) { |
| 170 | return $user->profile(); |
| 171 | } |
| 172 | } |
| 173 | return null; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public static function getNameTypeId($type, $for_sql = false) |
| 178 | { |
| 179 | if (!S::has('name_types')) { |
| 180 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
| 181 | FROM profile_name_enum'); |
| 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 | } |
| 192 | } |
| 193 | |
| 194 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 195 | ?> |