-
Notifications
You must be signed in to change notification settings - Fork 13
/
Collection.inc
347 lines (324 loc) · 10.4 KB
/
Collection.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
<?php
// $Id$
/**
* @file
*
*/
/**
*
*/
class Collection {
/**
* The pid of this Collection.
*
* @var string
*/
protected $pid;
/**
* Named default properties.
*
* @var array
*/
protected $default;
/**
* Create a Collection class.
*
* @param string $pid
*/
public function __construct($pid) {
$this->pid = $pid;
$sort = array(new stdClass(), new stdClass());
$sort[0]->property = 'label';
$sort[0]->direction = 'ASC';
$filter = array(new stdClass());
$filter[0]->property = 'label';
$filter[0]->value = NULL;
$this->default = array(
'sort' => $sort,
'filter' => $filter
);
}
/**
*
* @return array
*/
public function getMembers() {
$offset = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 7;
$filter = isset($_REQUEST['filter']) ? json_decode($_REQUEST['filter']) : $this->default['filter'];
$sort = isset($_REQUEST['sort']) ? json_decode($_REQUEST['sort']) : $this->default['sort'];
$results = $this->findMembers('all');
$results = $this->filter($results, $filter);
$results = $this->sort($results, $sort);
list($results, $count) = $this->limit($results, $offset, $limit);
$results = $this->populate($results);
return array($results, $count);
}
/**
* Find the members of this collection based on type
*
* @param string $type
*
* @return array
*/
private function findMembers($type) {
switch ($type) {
case 'collections':
return $this->findMemberCollections();
case 'objects':
return $this->findMemberObjects();
case 'all':
default:
return $this->findAllMembers();
}
}
/**
* Runs a SPARQL query and returns the results in a array.
*
* @return array
*/
private function findAllMembers() {
$query = 'select $object $title $model $parent_model $created from <#ri>
where ($object <info:fedora/fedora-system:def/model#label> $title
and $object <fedora-model:hasModel> $model
and $object <fedora-model:createdDate> $created
and $model <fedora-model:hasModel> $parent_model
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
and ($object <fedora-rels-ext:isMemberOfCollection> <info:fedora/' . $this->pid . '>
or $object <fedora-rels-ext:isMemberOf> <info:fedora/' . $this->pid . '>))
minus $model <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0>
order by $title';
return $this->query($query);
}
/**
* Runs a SPARQL query and returns the results in a array.
*
* @return array
*/
private function findMemberObjects() {
$query = 'select $object $title $model $parent_model $created from <#ri>
where ($object <info:fedora/fedora-system:def/model#label> $title
and $object <fedora-model:createdDate> $created
and $object <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> $parent_model
and $parent_model <mulgara:is> <info:fedora/fedora-system:ContentModel-3.0>
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
and ($object <fedora-rels-ext:isMemberOfCollection> <info:fedora/' . $this->pid . '>
or $object <fedora-rels-ext:isMemberOf> <info:fedora/' . $this->pid . '>))
minus $model <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0>
order by $title';
return $this->query($query);
}
/**
* Runs a SPARQL query and returns the results in a array.
*
* @return array
*/
private function findMemberCollections() {
$query = 'select $object $title $model $parent_model $created from <#ri>
where ($object <info:fedora/fedora-system:def/model#label> $title
and $object <fedora-model:createdDate> $created
and $object <fedora-model:hasModel> $model
and $model <fedora-model:hasModel> $parent_model
and $parent_model <mulgara:is> <info:fedora/islandora:collectionCModel>
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
and ($object <fedora-rels-ext:isMemberOfCollection> <info:fedora/' . $this->pid . '>
or $object <fedora-rels-ext:isMemberOf> <info:fedora/' . $this->pid . '>))
minus $model <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0>
order by $title';
return $this->query($query);
}
/**
* Launch a SPARQL query. Returns all members found up to 1,000,000 results.
*
* @param string $query
* SPARQL query.
*
* @return array
*/
private function query($query) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collection = new CollectionClass();
$results = $collection->getRelatedObjects($this->pid, 1000000, NULL, $query);
$results = trim($results);
$ret = array();
if ($results != '') {
$document = new DOMDocument();
$document->loadXML($results);
$path = new DOMXPath($document);
$path->registerNamespace('sparql', 'http://www.w3.org/2001/sw/DataAccess/rf1/result');
$results = $path->query('//sparql:result');
$count = $results->length;
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
$result = $results->item($i);
$pid = substr($path->query('sparql:object/@uri', $result)->item(0)->value, 12); // 12 characteres in "info:fedora/"
$label = $path->query('sparql:title', $result)->item(0)->textContent;
$created = $path->query('sparql:created', $result)->item(0)->textContent;
$ret[] = array('pid' => $pid, 'label' => $label, 'created' => $created);
}
}
return $ret;
}
return $ret;
}
/**
* Filter the results based on the search string.
*
* @param array $results
* @param string $search
*
* @return array
*/
private function filter(array $results, $filter) {
$search = isset($filter[0]->value) ? trim($filter[0]->value) : NULL;
if (isset($search) && $search != '') {
$ret = array();
foreach ($results as $result) {
$matches = preg_match("/^.*{$search}.*/i", $result['label']);
if ($matches > 0) {
$ret[] = $result;
}
}
return $ret;
}
return $results;
}
/**
* Sort the results.
*
* @param array $results
* @param string $sort
*
* @return array
*/
private function sort(array $results, $sort) {
switch ($sort[0]->property) {
case 'created':
if ($sort[0]->direction == 'ASC') {
usort($results, array($this, 'date_newest_sort'));
}
else {
usort($results, array($this, 'date_oldest_sort'));
}
break;
case 'label':
default:
if ($sort[0]->direction == 'ASC') {
usort($results, array($this, 'alpha_asc_sort'));
}
else {
usort($results, array($this, 'alpha_desc_sort'));
}
break;
}
return $results;
}
/**
* Sort by the title in ascending order.
*
* @param string $a
* @param string $b
*
* @return boolean
*/
private function alpha_asc_sort($a, $b) {
return strcmp($a['label'], $b['label']);
}
/**
* Sort by the title in descending order.
*
* @param string $a
* @param string $b
*
* @return boolean
*/
private function alpha_desc_sort($a, $b) {
return strcmp($b['label'], $a['label']);
}
/**
* Sort by the creation date from newest to oldest.
*
* @param string $a
* @param string $b
*
* @return boolean
*/
private function date_newest_sort($a, $b) {
$a = strtotime($a['created']);
$b = strtotime($b['created']);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
/**
* Sort by the creation date from oldest to newest.
*
* @param string $a
* @param string $b
*
* @return boolean
*/
private function date_oldest_sort($a, $b) {
$a = strtotime($a['created']);
$b = strtotime($b['created']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
/**
* Limit the results to a given range.
*
* @param array $results
* @param integer $offset
* @param integer $limit
*
* @return array
*/
private function limit(array $results, $offset, $limit) {
$count = count($results);
$data = array_slice($results, $offset, $limit);
return array($data, $count);
}
/**
* Fills out properties for each object.
*
* @param array $results
*
* @return array
*/
private function populate($results) {
global $base_url;
$module_path = drupal_get_path('module', 'content_model_viewer');
$image_path = '/' . $module_path . '/images';
foreach ($results as &$result) {
$pid = $result['pid'];
$content_model_pid = $this->getContentModelPid($pid);
$result['link'] = "$base_url/fedora/repository/$pid";
$result['tn'] = $image_path . '/object.png';
$result['description'] = 'Default Description, Check this object\'s metadata and ensure a description is present.';
}
return $results;
}
/**
* Get Content Model Pid.
*
* @param string $pid
*
* @return string
*/
private function getContentModelPid($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$rels_ext = $item->get_datastream_dissemination('RELS-EXT');
$document = new DOMDocument();
$document->loadXML($rels_ext);
$xpath = new DOMXPath($document);
$xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
$xpath->registerNamespace('fedora-model', "info:fedora/fedora-system:def/model#");
$results = $xpath->query('/rdf:RDF/rdf:Description/fedora-model:hasModel/@rdf:resource');
return substr($results->item(0)->value, 12); // 12 characteres in "info:fedora/"
}
}