Simplify code.
[platal.git] / classes / profile.php
CommitLineData
e7b93962
FB
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
22class Profile
23{
24 private $pid;
25 private $hrpid;
d5e60905 26 private $promo;
e7b93962 27
3e53a496
FB
28 private $data = array();
29
e7b93962
FB
30 private function __construct($login)
31 {
32 if ($login instanceof PlUser) {
d5e60905 33 $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display
e7b93962
FB
34 FROM account_profiles AS ap
35 INNER JOIN profiles AS p ON (p.pid = ap.pid)
d5e60905 36 INNER JOIN profile_display AS pd ON (pd.uid = p.pid)
e7b93962
FB
37 WHERE ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)',
38 $login->id());
39 } else if (is_numeric($login)) {
d5e60905 40 $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display
e7b93962 41 FROM profiles AS p
d5e60905 42 INNER JOIN profile_display AS pd ON (pd.uid = p.pid)
e7b93962
FB
43 WHERE p.pid = {?}',
44 $login);
45 } else {
d5e60905 46 $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display
e7b93962 47 FROM profiles AS p
d5e60905 48 INNER JOIN profile_display AS pd ON (pd.uid = p.pid)
e7b93962
FB
49 WHERE p.hrpid = {?}',
50 $login);
51 }
52 if ($res->numRows() != 1) {
53 throw new UserNotFoundException();
54 }
d5e60905 55 list($this->pid, $this->hrpid, $this->promo) = $res->fetchOneRow();
e7b93962
FB
56 }
57
58 public function id()
59 {
60 return $this->pid;
61 }
62
63 public function hrid()
64 {
65 return $this->hrpid;
66 }
67
d5e60905
FB
68 public function promo()
69 {
70 return $this->promo;
71 }
72
3e53a496
FB
73 public function __get($name)
74 {
75 if (property_exists($this, $name)) {
76 return $this->$name;
77 }
78
79 if (empty($this->data)) {
80 $this->data = XDB::fetchOneAssoc('SELECT *
81 FROM profiles
82 WHERE pid = {?}',
83 $this->id());
84 }
85 if (isset($this->data[$name])) {
86 return $this->data[$name];
87 }
88
89 return null;
90 }
91
92 public function __isset($name)
93 {
94 return property_exists($this, $name) || isset($this->data[$name]);
95 }
96
97
e7b93962
FB
98 public function owner()
99 {
100 return User::getSilent($this);
101 }
102
103 /** Return the profile associated with the given login.
104 */
105 public static function get($login) {
106 try {
107 return new Profile($login);
108 } catch (UserNotFoundException $e) {
3e53a496 109 return null;
e7b93962
FB
110 }
111 }
112}
113
114// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
115?>