forked from discoverygarden/islandora_pid_list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidlist.inc
353 lines (315 loc) · 9.5 KB
/
pidlist.inc
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* @file
* Definition of PidList.
*
*/
/**
* Class that defines a PidList object. A PidList object needs only to be
* constructed such that it contains a listname and listid for uniqueness.
*
*/
class PidList {
/**
* The list of users who belong to this object.
*
* @var array
*/
public $userlist = array();
/**
* The list of pids that belong to this object.
*
* @var array
*/
public $pidlist = array();
/**
* The name of the object.
*
* @var string
*/
public $listname;
/**
* The unique ID of the object. This is generated through the database or
* explicity assigned when adding objects to the session.
*
* @var integer
*/
public $listid;
/**
* The owner of the object.
*
* @var integer
*/
public $listowner;
/**
* The user id of the current user accessing the object.
*
* @var integer
*/
public $useruid;
/**
* Whether the object is created or retrieved through the session.
*
* @var boolean
*/
public $session = FALSE;
/**
* Constructor for the PidList object.
*
* @param array $params
*/
public function __construct($params = array()) {
$this->userlist = $params['listusers'];
$this->useruid = $params['useruid'];
if (!empty($params['session'])) {
$this->session = $params['session'];
}
if (!empty($params['listpids'])) {
$this->pidlist = $params['listpids'];
}
if (!empty($params['listname'])) {
$this->listname = $params['listname'];
}
if (!empty($params['listid'])) {
$this->listid = $params['listid'];
}
if (!$params['retrieve']) {
$this->create_list();
}
else {
$this->listowner = $params['listowner'];
}
}
/**
* Removes the specified pids from the object and either
* the database or session.
*
* @param array $pids
* An array of pids.
*/
public function remove_pids($pids) {
$this->pidlist = array_diff($this->pidlist, $pids);
if (!$this->session) {
if ($this->management_access()) {
foreach ($pids as $value) {
$this->remove_pid_db($value);
}
}
else {
throw new Exception(t("You do not have access to remove pids from the list $this->listname"));
}
}
else {
$superserial = unserialize($_SESSION['islandora_pid_list'][$this->listid]);
foreach ($superserial->pidlist as $key => $value) {
if (in_array($value, $pids)) {
unset($superserial->pidlist[$key]);
$_SESSION['islandora_pid_list'][$this->listid] = serialize($superserial);
}
}
}
}
/**
* Removes the specified users from the object and either the database
* or session.
*
* @global type $user
*
* @param array $users
* An array of users.
*/
public function remove_users($users) {
global $user;
$this->userlist = array_diff($this->userlist, $users);
if (!$this->session) {
if ($this->management_access()) {
foreach ($users as $value) {
$this->remove_user_db($value);
}
if (in_array($this->listowner, $users)) {
db_query("UPDATE {islandora_pid_list_names} SET listowner = NULL WHERE listid = %d", $this->listid);
}
}
elseif (in_array($this->useruid, $users)) {
$this->remove_user_db($this->useruid);
}
else {
throw new Exception(t("You do not have access to remove users from the list $this->listname"));
}
}
else {
$superserial = unserialize($_SESSION['islandora_pid_list'][$this->listid]);
foreach ($superserial->userlist as $key => $value) {
if (in_array($value, $users)) {
unset($superserial->userlist[$key]);
$_SESSION['islandora_pid_list'][$this->listid] = serialize($superserial);
}
}
}
}
/**
* Adds the specified pids to the object and either the database or session.
*
* @param array $pids
* An array of pids.
*
* @throws Exception
*
*/
public function add_pids($pids) {
module_load_include('inc', 'fedora_repository', 'api/tuque');
foreach ($pids as $key => $value) {
if (islandora_object_load($value)) {
if (!in_array($value, $this->pidlist)) {
array_push($this->pidlist, $value);
if (!$this->session) {
if ($this->management_access()) {
$this->add_pid_db($value);
}
else {
throw new Exception(t("You do not have access to add pids to the list $this->listname"));
}
}
else {
$superserial = unserialize($_SESSION['islandora_pid_list'][$this->listid]);
$superserial->pidlist[] = $value;
$_SESSION['islandora_pid_list'][$this->listid] = serialize($superserial);
}
}
}
else {
throw new Exception(t("Could not add the pid '" . $value ."' to '". $this->listname . "' as the pid doesn't exist!"));
}
}
}
/**
* Adds the specified users to the object and either the database or session.
*
* @param type $users
* An array of users.
*/
public function add_users($users) {
foreach ($users as $key => $value) {
if (!in_array($value, $this->userlist)) {
array_push($this->userlist, $value);
if (!$this->session) {
if ($this->management_access()) {
$this->add_user_db($value);
}
else {
throw new Exception(t("You do not have access to add users to the list $this->listname"));
}
}
else {
$superserial = unserialize($_SESSION['islandora_pid_list'][$this->listid]);
$superserial->userlist[] = $value;
$_SESSION['islandora_pid_list'][$this->listid] = serialize($superserial);
}
}
}
}
/**
* Inserts a record into the database or adds the object to the session.
*/
public function create_list() {
$this->listowner = $this->useruid;
if (!$this->session) {
db_query("INSERT INTO {islandora_pid_list_names} (listname, listowner) VALUES ('%s',%d)", $this->listname, $this->useruid);
$this->listid = db_last_insert_id('{islandora_pid_list_names}', 'listid');
foreach ($this->userlist as $key => $value) {
db_query("INSERT INTO {islandora_pid_list_lists} (uid, listid) VALUES (%d,%d)", $value, $this->listid);
}
foreach ($this->pidlist as $key => $value) {
db_query("INSERT INTO {islandora_pid_list_pids} (listid, pidid) VALUES (%d,'%s')", $this->listid, $value);
}
}
else {
// Add to session
$index = count($_SESSION['islandora_pid_list']);
$this->listid = 'session_' . $index;
$_SESSION['islandora_pid_list'][$this->listid] = serialize($this);
}
}
/**
* Removes the record from the database or the object from the session.
*/
public function remove_list() {
if (!$this->session) {
if ($this->management_access()) {
db_query("DELETE FROM {islandora_pid_list_names} WHERE listid = %d", $this->listid);
foreach ($this->userlist as $key => $value) {
db_query("DELETE FROM {islandora_pid_list_lists} WHERE listid = %d", $this->listid);
}
foreach ($this->pidlist as $key => $value) {
db_query("DELETE FROM {islandora_pid_list_pids} WHERE listid = %d", $this->listid);
}
}
else {
throw new Exception(t("You do not have access to remove the list $this->listname"));
}
}
else {
unset($_SESSION['islandora_pid_list'][$this->listid]);
}
}
/**
* Removes the specified user from the object's entry in the database.
*
* @param integer $userdelete
* The user to be removed.
*/
private function remove_user_db($userdelete) {
db_query("DELETE FROM {islandora_pid_list_lists} WHERE uid = %d AND listid = %d", $userdelete, $this->listid);
}
/**
* Adds the specified user to the object's entry in the database.
*
* @param integer $useradd
* The user to be added.
*/
private function add_user_db($useradd) {
db_query("INSERT INTO {islandora_pid_list_lists} (uid, listid) VALUES (%d,%d)", $useradd, $this->listid);
}
/**
* Removes the specified pid from the object's entry in the database.
*
* @param string $pid
* The pid to be removed.
*/
private function remove_pid_db($pid) {
db_query("DELETE FROM {islandora_pid_list_pids} WHERE pidid = '%s' AND listid = %d", $pid, $this->listid);
}
/**
* Add the specified pid to the object's entry in the database.
*
* @param string $pid
* The user to be added.
*/
private function add_pid_db($pid) {
db_query("INSERT INTO {islandora_pid_list_pids} (listid, pidid) VALUES (%d, '%s')", $this->listid, $pid);
}
/**
* Changes the name of the object in the session or updates the object's
* entry in the database
*
* @param string $name
* The new name of the object.
*/
public function change_list_name($name) {
$this->listname = $name;
if (!$this->session) {
db_query("UPDATE {islandora_pid_list_names} SET listname = '%s' WHERE listid = %d", $name, $this->listid);
}
else {
$_SESSION['islandora_pid_list'][$this->listid] = serialize($this);
}
}
public function management_access() {
if ($this->useruid === $this->listowner || user_access("Manage other users' pidlists")) {
return TRUE;
}
else {
return FALSE;
}
}
}