forked from Islandora/islandora_solution_pack_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.inc
265 lines (238 loc) · 9.08 KB
/
blocks.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
<?php
/**
* @file
* Provides block functionality for the basic collection module.
*/
/**
* Constructs the contents of the object count block.
*
* @return string
* The markup to be rendered inside the block.
*/
function islandora_basic_collection_object_count_listing_content() {
$block = array();
$id = drupal_html_id('islandora_basic_collection_object_count_block_count');
$block['counts'] = array(
'#attached' => array(
'js' => array(
drupal_get_path('module', 'islandora_basic_collection') . '/js/collection-block-count.js',
array(
'type' => 'setting',
'data' => array(
'islandora_basic_collection' => array(
'count_block' => array(
'id' => $id,
'callback' => url('islandora/collection/count_ajax'),
),
),
),
),
),
),
'#prefix' => "<span id='$id'>",
'#suffix' => '</span>',
'#markup' => variable_get('islandora_basic_collection_object_count_listing_placeholder', 'Counting items in collections...'),
);
return $block;
}
/**
* Build an ID for caching.
*
* Grabbed the internals of _block_get_cache_id(): Since we're not setting
* the general block cache to be used, we cannot depend call it.
*
* @return string
* The ID to use for the object count block.
*/
function islandora_basic_collection_get_object_count_block_cache_id() {
$block = block_load('islandora_basic_collection', 'collection_object_count');
$cid_parts[] = $block->module;
$cid_parts[] = $block->delta;
$cid_parts = array_merge($cid_parts, drupal_render_cid_parts($block->cache));
return implode(':', $cid_parts);
}
/**
* Clear the cache of content for the object count block.
*/
function islandora_basic_collection_clear_object_count_cache() {
$block = block_load('islandora_basic_collection', 'collection_object_count');
$cid_parts[] = $block->module;
$cid_parts[] = $block->delta;
cache_clear_all(implode(':', $cid_parts), 'cache', TRUE);
}
/**
* AJAX callback to get info about the count of objects and collections.
*
* Outputs a JSON string to the buffer.
*/
function islandora_basic_collection_object_count_callback() {
$count_method = variable_get('islandora_basic_collection_count_method', 'SPARQL');
$cid = islandora_basic_collection_get_object_count_block_cache_id();
if ($value = cache_get($cid, 'cache')) {
$substitutions = $value->data;
}
else {
$tuque = islandora_get_tuque_connection();
$objects_query_array = islandora_basic_collection_get_query_info(array(
'object' => islandora_object_load(variable_get('islandora_repository_pid', 'islandora:root')),
'collection_listing' => TRUE,
'all_objects' => TRUE,
));
$collection_query_array = islandora_basic_collection_get_query_info(array(
'object' => islandora_object_load(variable_get('islandora_repository_pid', 'islandora:root')),
'collection_listing' => TRUE,
'all_objects' => FALSE,
));
// Setup Solr query for collection and object counts.
if ($count_method == "Solr" && module_exists('islandora_solr')) {
$cmodel_field = variable_get('islandora_solr_content_model_field', 'RELS_EXT_hasModel_uri_ms');
$all_cmodels = islandora_get_content_models();
$models_to_exclude = variable_get('islandora_basic_collection_object_count_listing_content_models_to_restrict', FALSE);
$cmodels_to_count = array();
foreach ($all_cmodels AS $cmodel) {
if ($models_to_exclude[$cmodel['pid']] != '0') {
$cmodels_to_count[] = $cmodel['pid'];
}
}
// Collection count Solr query.
$collection_query = $cmodel_field . ':"info:fedora/islandora:collectionCModel"';
$qp = new IslandoraSolrQueryProcessor();
$qp->buildQuery($collection_query);
$qp->solrParams['fl'] = "PID";
$qp->solrLimit = -1;
$qp->executeQuery(FALSE);
try {
$results = $qp->islandoraSolrResult['response']['numFound'];
}
catch (Exception $e) {
watchdog_exception('Islandora Basic Collection', $e, 'Got an exception counting Collections.', array(), WATCHDOG_ERROR);
$results = array();
}
$collection_count = $results;
// Solr query for object count.
$object_query = "";
$i = 0;
foreach ($cmodels_to_count AS $model) {
if ($i == 0) {
$object_query = $cmodel_field . ':"info:fedora/' . $model . '"';
}
else {
$object_query = $object_query . ' OR ' . $cmodel_field . ':"info:fedora/' . $model . '"';
}
$i++;
}
$qp = new IslandoraSolrQueryProcessor();
$qp->buildQuery($object_query);
$qp->solrParams['fl'] = "PID";
$qp->solrLimit = -1;
$qp->executeQuery(FALSE);
try {
$results = $qp->islandoraSolrResult['response']['numFound'];
}
catch (Exception $e) {
watchdog_exception('Islandora Basic Collection', $e, 'Got an exception counting Objects.', array(), WATCHDOG_ERROR);
$results = array();
}
$object_count = $results;
$total_collection_count = $collection_count;
$total_object_count = $object_count;
}
else {
$collection_objects = $tuque->repository->ri->sparqlQuery($collection_query_array['query'], $collection_query_array['type']);
$total_object_count = $tuque->repository->ri->countQuery($objects_query_array['query'], $objects_query_array['type']);
$collections = array();
foreach ($collection_objects as $collection) {
$collections[$collection['object']['value']] = $collection['object']['value'];
}
$models_to_exclude = variable_get('islandora_basic_collection_object_count_listing_content_models_to_restrict', FALSE);
if ($models_to_exclude) {
$collections = islandora_basic_collection_filter_collection_by_cmodel($collections, array_filter($models_to_exclude));
}
$total_collection_count = count($collections);
}
$substitutions = array(
'!objects' => $total_object_count,
'!collections' => $total_collection_count,
);
cache_set($cid, $substitutions, 'cache');
}
$title_phrase = variable_get('islandora_basic_collection_object_count_listing_phrase', '!objects Items in !collections Collections');
$text = format_string($title_phrase, $substitutions);
drupal_json_output($text);
}
/**
* Constructs the content of the collection listing block.
*
* @return string
* The markup to be rendered in the block.
*/
function islandora_basic_collection_collection_listing_content() {
$block = array();
$tuque = islandora_get_tuque_connection();
$collection_query_array = islandora_basic_collection_get_query_info(array(
'object' => islandora_object_load('islandora:root'),
'collection_listing' => TRUE,
'all_objects' => FALSE,
));
$collections_to_display = variable_get('islandora_basic_collection_listing_block_links_to_render', 10);
$collection_objects = $tuque->repository->ri->sparqlQuery($collection_query_array['query'], $collection_query_array['type']);
$collections = array();
$stored_collections = array();
foreach ($collection_objects as $collection) {
$pid = $collection['object']['value'];
$collections[$pid] = $pid;
$stored_collections[$pid] = $collection['title']['value'];
}
$models_to_exclude = variable_get('islandora_basic_collection_listing_block_content_models_to_restrict', FALSE);
if ($models_to_exclude) {
$collections = islandora_basic_collection_filter_collection_by_cmodel($collections, array_filter($models_to_exclude));
}
$formatted_objects = array();
foreach ($collections as $pid) {
$formatted_objects[$pid] = l($stored_collections[$pid], "islandora/object/$pid");
}
$block['collections'] = array(
'#theme' => 'item_list',
'#items' => array_slice($formatted_objects, 0, $collections_to_display),
'#type' => 'ul',
);
$root_pid = variable_get('islandora_repository_pid', 'islandora:root');
$block['browse_repository'] = array(
'#type' => 'item',
'#markup' => l(t('Browse repository'), "islandora/object/$root_pid"),
'#attributes' => array(
'class' => array('islandora-basic-collection-read-more'),
),
);
return drupal_render($block);
}
/**
* Filters out collections that only contain included content models.
*
* @param array $collections
* An array of PIDs representing collection objects.
* @param array $models
* An array of content models to be included.
*
* @return array
* An array of collections to be used.
*/
function islandora_basic_collection_filter_collection_by_cmodel($collections, $models) {
$collection_filter = function ($collection) use ($models) {
$tuque = islandora_get_tuque_connection();
$models_query = <<<EOQ
SELECT DISTINCT ?model from <#ri> where {
?object <fedora-model:hasModel> ?model ;
<fedora-rels-ext:isMemberOfCollection> <info:fedora/{$collection}>
}
EOQ;
$models_results = $tuque->repository->ri->sparqlQuery($models_query);
$collection_models = array();
foreach ($models_results as $result) {
$collection_models[$result['model']['value']] = $result['model']['value'];
}
return array_intersect($collection_models, $models);
};
$filtered_collections = array_filter($collections, $collection_filter);
return $filtered_collections;
}