-
Notifications
You must be signed in to change notification settings - Fork 3
/
AjaxHandler.inc
422 lines (390 loc) · 13.3 KB
/
AjaxHandler.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
// $Id$
/**
* @file
*
* Defines a class that handles Ajax Requests.
*/
/**
* Handles Ajax requests.
*/
class AjaxHandler {
/**
* Fedora Object PID.
*
* @var string
*/
protected $pid;
/**
* Fedora Object
*
* @var Fedora_Item
*/
protected $item;
/**
* FOXML repersention of the Fedora Object identified by $pid.
*
* Access through the function AjaxHandler::getFoxmlDocument().
* Do not access directly as it may not exist.
*
* @var DOMDocument
*/
protected $foxml;
/**
* XPath for the FOXML document identified by $foxml.
*
* Access through the function AjaxHandler::getFoxmlXPath().
* Do not directly as it may not exist.
*
* @var DOMDocument
*/
protected $xpath;
/**
* This fedora objects content model.
*
* Access through the function AjaxHandler::getContentModel().
* Do not directly as it may not exist.
*
* @var ContentModel
*/
protected $contentModel;
/**
* Create a Ajax request handler.
*
* This class provides methods for accesing/modifiying infomation about the
* fedora objected identified by $pid. These methods are general accessed by
* Ajax requests, from the ADR Basic Viewer.
*
* @param string $pid
* Fedora Object PID.
*/
public function __construct($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$this->pid = $pid;
$this->item = new Fedora_Item($pid);
$dsinfo = $this->item->find_objects_by_pattern($this->item->pid, 'pid', 1, array('ownerId','state'));
$this->extraInfo = $dsinfo[$this->item->pid];
}
/**
*
* @return array
*/
public function getAllowedMIMETypes() {
$content_model = &$this->getContentModel();
$mime_types = $content_model->getMimetypes();
$response = array();
foreach ($mime_types as $type) {
$response[] = array('mime' => $type);
}
return $response;
}
/**
*
* @return array
*/
public function getDescription() {
module_load_include('inc', 'adr_basic_viewer', 'ObjectViewDescription');
$object_view = new ObjectViewDescription($this->item, $this->getContentModel());
$description = $object_view->getDescription();
$content = '';
foreach ($description as $label => $value) {
$value = filter_xss($value, array('a', 'br'));
$content .= '<tr>';
$content .= "<td style='padding: 5px; background:#cccccc; font-size: 1em; font-weight:bold; vertical-align:top;'>$label</td>";
$content .= "<td style='padding:5px;font-size:1.2em; vertical-align: middle;'>$value</td>";
$content .= '</tr>';
}
$created = $this->getObjectCreationDate();
$modified = $this->getObjectLastModifiedDate();
// @TODO use classes instead of inlined values.
$footer = "<tr><td><br/></td></tr>";
$footer .= "<tr><td style='text-align: center;font-size:0.9em;'>";
$footer .= "This object was added {$created}. ";
$footer .= "This object was last updated {$modified}.</td></tr>";
return "<table>{$content}</table><table>{$footer}</table>";
}
/**
*
* @return array
*/
public function getDatastreams($start, $limit) {
$data = array();
$count = 0;
$end = (isset($limit)) ? $start + $limit : NULL;
module_load_include('inc', 'adr_basic_viewer', 'ObjectViewDescription');
module_load_include('inc', 'islandora_xacml_api', 'Xacml');
module_load_include('inc', 'islandora_xacml_api', 'XacmlException');
module_load_include('inc', 'adr_basic_viewer', 'Thumbnail');
$xacml = Xacml::constructFromPid($this->pid);
$object_view = new ObjectViewDescription($this->item, $this->getContentModel(), $xacml);
$datastreams = $this->item->datastreams;
foreach ($datastreams as $dsid => $attr) {
if ($count >= $start && (empty($end) || $count < $end)) {
$thumbnail = new Thumbnail($this->item, $dsid);
$stream['dsid'] = $dsid;
$stream['viewable_dsid'] = $object_view->getViewableDSID($dsid);
$stream['label'] = $attr['label'];
$stream['img_url'] = $thumbnail->getUrl();
$stream['download_url'] = $this->getDatastreamDownloadURL($dsid, $attr['label']);
$stream['mime'] = $attr['MIMEType'];
$data[] = $stream;
}
$count++;
}
return array("success" => TRUE, "data" => $data, "total" => $count);
}
/**
*
* @param int $start
* @param int $limit
* @return array
*/
public function getOverviewDatastreams($start, $limit) {
$data = array();
module_load_include('inc', 'adr_basic_viewer', 'ObjectViewDescription');
module_load_include('inc', 'adr_basic_viewer', 'Thumbnail');
module_load_include('inc', 'islandora_xacml_api', 'Xacml');
module_load_include('inc', 'islandora_xacml_api', 'XacmlException');
$xacml = Xacml::constructFromPid($this->pid);
$object_view = new ObjectViewDescription($this->item, $this->getContentModel(), $xacml);
$count = 0;
$end = $start + $limit;
$datastreams = $this->item->datastreams;
foreach ($datastreams as $key => $row) {
$datastreams_sorted[$key] = $row['label'];
}
$datastreams_sorted = array_map('strtolower', $datastreams_sorted);
array_multisort($datastreams_sorted, SORT_ASC, $datastreams);
foreach ($datastreams as $dsid => $attr) {
list($viewable, $downloadable) = $object_view->isViewableAndDownloadable($dsid);
if ($viewable || $downloadable) {
$within_range = $count >= $start && $count < $end;
if ($within_range) {
$thumbnail = new Thumbnail($this->item, $dsid);
// it is possible that we can get this far and the datastream MIMEtype is viewable
// but there may be no viewer installed for the MIMEtype. This can happen when
// the object has derived datastreams that have viewers even if the main datastream
// does not have a viewer. In this case we must find the DSID of the viewable
// datastream to send to the viewer.
$stream['dsid'] = $dsid;
$stream['viewable_dsid'] = ($viewable) ? $object_view->getViewableDSID($dsid) : NULL;
$stream['label'] = $attr['label'];
$stream['img_url'] = $thumbnail->getUrl();
$stream['download_url'] = ($downloadable) ? $this->getDatastreamDownloadURL($dsid, $attr['label']) : NULL;
$stream['mime'] = $attr['MIMEType'];
$data[] = $stream;
}
$count++;
}
}
return array("success" => TRUE, "data" => $data, "total" => $count);
}
/**
*
* @global string $base_url
* @param string $dsid
*/
private function getDatastreamDownloadURL($dsid, $label) {
global $base_url;
return "$base_url/fedora/repository/object_download/" . drupal_urlencode($this->pid) . '/' . $dsid . '/' . drupal_urlencode(preg_replace('/\//i', '${1}_', $dsid));
}
/**
*
* @param <type> $values
* @return <type>
*/
public function addDatastream($values) {
$controls = array(
'Inline XML (X)' => "X",
'Managed Resource (M)' => "M",
'Redirect (R)' => "R",
'External Reference (E)' => "E",
);
$dsid = $values['dsid'];
$label = $values['label'];
$control = $controls[$values['control_group']];
$mime = $values['mime_type'];
$file = array_shift($_FILES);
if ($file['type'] != $mime) {
return array('success' => false, 'msg' => 'Mime type of file does not match the mime type specified.');
}
else {
if ($this->item->add_datastream_from_file($file['tmp_name'], $dsid, $label, $mime, $control)) {
return array('success' => true, 'msg' => 'Successfully added datastream.');
}
else {
return array('success' => false, 'msg' => 'Failed to add datastream.');
}
}
}
/**
* Deletes datastreams from the object.
*
* @param string $dsid
* @return array
*/
public function deleteDatastream($dsid) {
if ($this->item->purge_datastream($dsid)) {
return json_encode(array('success' => true, 'msg' => 'Successfully Deleted datastream.'));
}
else {
return json_encode(array('success' => false, 'msg' => 'Failed to deleted file.'));
}
}
/**
* Gets the fedora object's properties.
*
* @return array
*/
public function getObjectProperties() {
$object_properties = array(
'pid' => $this->pid,
'label' => $this->getObjectLabel(),
'created' => $this->getObjectCreationDate(),
'modified' => $this->getObjectLastModifiedDate(),
'owner' => $this->getObjectOwner(),
'state' => $this->getObjectState()
);
return array($object_properties);
}
/**
* Sets the fedora object's properties.
*
* Input is expected to be from the form defined on the manage tab,
* of the ADR Basic viewer.
*
* @return array
* Indicates the success of this function as well as the fedora
* objects current settings.
*/
public function setObjectProperties($values) {
$states = array(
'Active' => 'A',
'Inactive' => 'I',
'Deleted' => 'D',
);
$label = $values['label'];
$owner = $values['owner'];
$state = $values['state'];
if ($this->item->modify_object($label, $states[$state], $owner)) {
$object_properties = $this->getObjectProperties();
return array('success' => true, 'msg' => 'Successfully Edited Object', 'data' => $object_properties);
}
else {
$object_properties = $this->getObjectProperties();
return array('success' => false, 'msg' => 'Failed to Edit Object', 'data' => $object_properties);
}
}
/**
* Marks the fedora object in question as being deleted.
*
* @return array
*/
public function deleteObject() {
// mark the object as deleted in Fedora without purging the object
$get_properties = $this->getObjectProperties();
$object_properties = $get_properties[0];
$object_properties['state'] = "Deleted";
$set_properties = $this->setObjectProperties($object_properties);
if ($set_properties['success']) {
drupal_set_message("Successfully Deleted Object: {$this->pid}");
return array('success' => true, 'msg' => 'Successfully Deleted Object');
}
else {
return array('success' => false, 'msg' => 'Failed to Delete Object');
}
}
/**
* Sets the label of the specified datastream given the user's input.
*
* @param array $values
* Contains the pid, label text and the dsid of the object being modified.
*
* @return array
* Indicates whether the label was updated correctly, failed, or unchanged.
*/
public function setDatastreamLabel($values) {
module_load_include('inc', 'fedora_repository', 'api/tuque');
global $user;
// Tuque is only used here to get around the restrictions in fedora_item,
// when attempting to update a label. At some point it may be prudent to
// go back to change everything to implement Tuque.
try {
$connection = new IslandoraTuque($user);
$dsarray = $connection->api->a->listDatastreams($values['pid']);
$object = islandora_object_load($values['pid']);
$ds = $object->getDatastream($values['ds']);
} catch (RepositoryException $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
$object_properties = $this->getObjectProperties();
return array('success' => false, 'msg' => 'Failed to Edit Object', 'data' => $object_properties);
}
if ($dsarray[$values['ds']]['label'] != $values['label']) {
try {
$ds->label = $values['label'];
$object_properties = $this->getObjectProperties();
return array('success' => true, 'msg' => 'Successfully Edited Object', 'data' => $object_properties);
} catch (RepositoryException $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
$object_properties = $this->getObjectProperties();
} return array('success' => false, 'msg' => 'Failed to Edit Object', 'data' => $object_properties);
}
else {
return array('success' => true, 'msg' => 'Not updated');
}
}
/**
* Gets the fedora object's label.
*
* @return string
*/
private function getObjectLabel() {
return $this->item->objectProfile->objLabel;
}
/**
* Gets the fedora object's creation date in the format "Month Day, Year".
*
* @return string
*/
private function getObjectCreationDate() {
$date_created = new DateTime($this->item->objectProfile->objCreateDate);
return $date_created->format('F j, Y');
}
/**
* Gets the fedora object's last modified date in the format "Month Day, Year".
*
* @return string
*/
private function getObjectLastModifiedDate() {
$date_modified = new DateTime($this->item->objectProfile->objLastModDate);
return $date_modified->format('F j, Y');
}
/**
* Gets the fedora object's owner id.
*
* @return string
*/
private function getObjectOwner() {
return $this->extraInfo['ownerId'];
}
/**
* Gets the fedora object's current state.
*
* @return string
*/
private function getObjectState() {
return $this->extraInfo['state'];
}
/**
*
*
* @return ContentModel
*/
private function &getContentModel() {
if (empty($this->contentModel)) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
$this->contentModel = ContentModel::loadFromObject($this->pid);
}
return $this->contentModel;
}
}