Start porting xnetgrp.
[platal.git] / classes / profile.php
CommitLineData
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
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 }
832e6fcb
FB
41 // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory
42 $res = XDB::query('SELECT p.*, u.prenom AS first_name,
43 IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name,
44 pd.promo AS promo,
45 CONCAT(u.prenom, " ", u.nom) AS short_name,
46 IF(u.nom_usage != "",
47 CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom),
48 CONCAT(u.nom, ", ", u.prenom)) AS full_name
49 FROM ' . $from . '
50 INNER JOIN auth_user_md5 AS u ON (u.user_id = p.pid)
51 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
52 WHERE ' . $where);
e7b93962
FB
53 if ($res->numRows() != 1) {
54 throw new UserNotFoundException();
55 }
832e6fcb
FB
56 $this->data = $res->fetchOneAssoc();
57 $this->pid = $this->data['pid'];
58 $this->hrpid = $this->data['hrpid'];
e7b93962
FB
59 }
60
61 public function id()
62 {
63 return $this->pid;
64 }
65
66 public function hrid()
67 {
68 return $this->hrpid;
69 }
70
d5e60905
FB
71 public function promo()
72 {
73 return $this->promo;
74 }
75
94b72319
FB
76 /** Print a name with the given formatting:
77 * %s = • for women
78 * %f = firstname
79 * %l = lastname
80 * %F = fullname
81 * %S = shortname
82 * %p = promo
83 */
84 public function name($format)
85 {
86 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
87 array($this->isFemale() ? '•' : '',
88 $this->first_name, $this->last_name,
89 $this->full_name, $this->short_name,
90 $this->promo), $format);
91 }
92
93 public function fullName($with_promo = false)
94 {
95 if ($with_promo) {
96 return $this->full_name . ' (' . $this->promo . ')';
97 }
98 return $this->full_name;
99 }
100
101 public function shortName($with_promo = false)
102 {
103 if ($with_promo) {
104 return $this->short_name . ' (' . $this->promo . ')';
105 }
106 return $this->short_name;
107 }
108
109 public function firstName()
110 {
111 return $this->first_name;
112 }
113
114 public function lastName()
115 {
116 return $this->last_name;
117 }
118
119 public function isFemale()
120 {
121 return $this->sex == PlUser::GENDER_FEMALE;
122 }
123
124 public function data()
125 {
126 $this->first_name;
127 return $this->data;
128 }
129
3e53a496
FB
130 public function __get($name)
131 {
132 if (property_exists($this, $name)) {
133 return $this->$name;
134 }
135
3e53a496
FB
136 if (isset($this->data[$name])) {
137 return $this->data[$name];
138 }
139
140 return null;
141 }
142
143 public function __isset($name)
144 {
145 return property_exists($this, $name) || isset($this->data[$name]);
146 }
147
148
e7b93962
FB
149 public function owner()
150 {
151 return User::getSilent($this);
152 }
153
154 /** Return the profile associated with the given login.
155 */
156 public static function get($login) {
157 try {
158 return new Profile($login);
159 } catch (UserNotFoundException $e) {
efe597c5
FB
160 /* Let say we can identify a profile using the identifiers of its owner.
161 */
162 $user = User::getSilent($login);
163 if ($user && $user->hasProfile()) {
164 return $user->profile();
165 }
3e53a496 166 return null;
e7b93962
FB
167 }
168 }
169}
170
171// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
172?>