-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathXDUserProfile.php
More file actions
225 lines (148 loc) · 6.22 KB
/
Copy pathXDUserProfile.php
File metadata and controls
225 lines (148 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
use CCR\DB;
// @class XDUserProfile
class XDUserProfile {
const TARGET_SCHEMA = 'moddb.UserProfiles';
/*
CREATE TABLE moddb.UserProfiles (
user_id INT(11), primary key(user_id),
serialized_profile_data BLOB
);
*/
// Class constants (used as keys for accessing/setting values in the profile) -------------
const INITIAL_TAB = 0;
const LAST_VISITED_TAB = 1;
const DASHBOARD_CHARTS = 2;
// Private variables --------------------------------
private $_pdo; // PDO Handle (set in __construct)
private $_user_id; // User ID (set in __construct)
private $_profile_data; // Represents cached profile data
// -----------------------------------------------------
// @function __construct()
//
// @param int $user_id [user id pertaining to the user whose profile you want to manage]
function __construct($user_id) {
$this->_pdo = DB::factory('database');
$this->_user_id = $user_id;
$this->_load();
}//__construct
// -----------------------------------------------------
// @function setValue()
//
// @param String $param [The parameter / key]
// @param Object $value [The object to be mapped to the parameter / key]
public function setValue($param, $value) {
$this->_profile_data[$param] = $value;
}//setValue
// -----------------------------------------------------
// @function getDump()
//
// NOTE: Should be used for debugging purposes only
public function getDump() {
print '<pre>';
print_r($this->_profile_data);
print '</pre>';
}//getDump
// -----------------------------------------------------
// @function valueExists()
//
// @param String $param
//
// @return boolean [Indicating whether a value already exists for a given parameter]
public function valueExists($param) {
return (isset($this->_profile_data[$param]));
}//valueExists
// -----------------------------------------------------
// @function fetchValue()
//
// @param String $param
//
// @return Object [The value of corresponding datatype mapped to the parameter of interest]
// If there is no mapping, then NULL is returned
public function fetchValue($param) {
return (isset($this->_profile_data[$param])) ? $this->_profile_data[$param] : NULL;
}//fetchValue
// -----------------------------------------------------
// @function dropValue()
//
// Removes an item from the local cache (made permanent by a subsequent call to save())
//
// @param String $param [The parameter identifying the item to be discarded]
public function dropValue($param) {
// NOTE: The actual value will stay in the profile cache until a call to save() is made.
if (isset($this->_profile_data[$param])) {
unset($this->_profile_data[$param]);
}
}//dropValue
// -----------------------------------------------------
// @function clear()
//
// Clear local cache as well as data in the persistence model (implicit save)
public function clear() {
$this->_profile_data = array();
$this->_pdo->execute(
'DELETE FROM '.self::TARGET_SCHEMA.' WHERE user_id=:user_id',
array(
'user_id' => $this->_user_id
)
);
}//clear
// -----------------------------------------------------
// @function reload()
//
// Re-Populate local cache with data currently in the persistence model
// NOTE: Any unsaved data will be lost as the result of calling reload()
public function reload() {
$this->_load();
}//reload
// -----------------------------------------------------
// @function _load()
//
// Discard what is in the local cache and (re-)populate it with data in the persistence model
// NOTE: Any unsaved data will be lost as the result of calling load()
private function _load() {
$this->_profile_data = array();
$results = $this->_pdo->query('SELECT serialized_profile_data FROM '.self::TARGET_SCHEMA.' WHERE user_id=:user_id',
array(
'user_id' => $this->_user_id
)
);
if (count($results) > 0) {
$this->_profile_data = @unserialize($results[0]['serialized_profile_data']);
if (!is_array($this->_profile_data)) {
// Unserialization failed -- initialize to an empty array
$this->_profile_data = array();
}
}//if (count($results) > 0)
}//_load
// -----------------------------------------------------
// @function save()
//
// Writes cached profile data back to the persistence model in a serialized format
//
// @throws Exception if there was an issue executing the SQL command to write the data to the DB
public function save() {
$this->_pdo->execute('LOCK TABLES '.self::TARGET_SCHEMA.' WRITE');
$this->_pdo->execute('BEGIN');
// BEGIN Critical Section ========================
$targetQuery = 'INSERT INTO '.self::TARGET_SCHEMA.' (user_id, serialized_profile_data) VALUES (:user_id, :serialized_profile_data)';
$targetQuery .= ' ON DUPLICATE KEY UPDATE serialized_profile_data=:serialized_profile_data';
try {
$serialized = serialize($this->_profile_data);
$this->_pdo->execute(
$targetQuery,
array(
'serialized_profile_data' => $serialized,
'user_id' => $this->_user_id
)
);
$this->_pdo->execute('COMMIT');
$this->_pdo->execute('UNLOCK TABLES');
}
catch(Exception $e) {
$this->_pdo->execute('ROLLBACK');
$this->_pdo->execute('UNLOCK TABLES');
throw new Exception($e->getMessage());
}
}//save
}//XDUserProfile