forked from Islandora/islandora_pathauto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathislandora_pathauto.module
253 lines (238 loc) · 7.11 KB
/
islandora_pathauto.module
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
<?php
/**
* @file
* Exposes Islandora object urls to pathauto,
* so that if a pattern is configured, then
* aliases are updated automatically on object
* ingest/modify/purge.
*
* Also enables batch creation/updates of aliases.
*/
/**
* Implements hook_menu().
*/
function islandora_pathauto_menu() {
$items['admin/islandora/tools/islandora-pathauto'] = array(
'title' => 'Pathauto',
'description' => 'Configure settings for the Islandora Pathauto module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_pathauto_admin_settings'),
'access arguments' => array('administer islandora_pathauto'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_pathauto_permission() {
return array(
'administer islandora_pathauto' => array(
'title' => t('Administer Islandora Pathauto'),
'description' => t('Select content models available for custom Islandora Pathauto patterns.'),
),
);
}
/**
* Implements hook_theme().
*/
function islandora_pathauto_theme() {
return array(
'islandora_pathauto_admin_table' => array(
'render element' => 'form',
),
);
}
/**
* Implements hook_pathauto().
*/
function islandora_pathauto_pathauto($op) {
module_load_include('inc', 'islandora', 'includes/utilities');
$settings = new stdClass();
$settings->module = 'islandora';
$settings->groupheader = t('Islandora object paths');
$settings->patterndescr = t('All Islandora objects (except those with custom paths specified below)');
$settings->patterndefault = '';
$settings->token_type = 'fedora';
$settings->patternitems = array();
$settings->batch_update_callback = 'islandora_pathauto_pathauto_bulkupdate';
$all_cmodels = islandora_get_content_models();
$pathauto_cmodels = variable_get('islandora_pathauto_selected_cmodels', array());
$cmodels = array();
foreach ($all_cmodels as $key => $value) {
if (in_array($key, $pathauto_cmodels)) {
$cmodels[$key] = $value;
}
}
foreach ($cmodels as $cmodel) {
$settings->patternitems[$cmodel['pid']] = t('Custom pattern for @cmodel objects', array("@cmodel" => $cmodel['label']));
}
return $settings;
}
/**
* Wrapper to pathauto_create_alias().
*
* @param FedoraObject $object
* an Islandora object
*
* @param string $op
* The operation to perform: 'insert' or 'update'.
*
* @return int
* Returns the number of aliases generated from this object;
* may be >1 if the object has multiple content models.
*/
function islandora_pathauto_create_alias($object, $op) {
module_load_include('inc', 'pathauto');
$path = 'islandora/object/' . $object->id;
$count = 0;
// Give priority to any cmodels that are enabled for pathauto.
$all_enabled = variable_get('islandora_pathauto_selected_cmodels', array());
$models = array_intersect($object->models, $all_enabled);
if (!$models) {
$models = $object->models;
}
foreach ($models as $cmodel) {
$result = pathauto_create_alias('islandora', $op, $path, array('fedora' => $object), $cmodel);
if ($result != '') {
$count++;
}
}
return $count;
}
/**
* Implements hook_islandora_object_ingested().
*/
function islandora_pathauto_islandora_object_ingested($object) {
islandora_pathauto_create_alias($object, 'insert');
}
/**
* Implements hook_islandora_object_alter().
*/
function islandora_pathauto_islandora_object_alter($original, $context) {
$object = (object) array(
'id' => $original->id,
'label' => $original->label,
'models' => $original->models,
);
// The object does not have the new label yet.
// This is a nasty fix to get it from $context.
if (isset($context['action']) and $context['action'] == 'modify') {
if (isset($context['params']['label'])) {
$object->label = $context['params']['label'];
}
}
islandora_pathauto_create_alias($object, 'update');
}
/**
* Implements hook_islandora_object_purged().
*/
function islandora_pathauto_islandora_object_purged($pid) {
pathauto_path_delete_all('islandora/object/' . $pid);
}
/**
* Implements hook_pathauto_bulkupdate().
*/
function islandora_pathauto_pathauto_bulkupdate() {
module_load_include('inc', 'islandora', 'includes/utilities');
$query = 'SELECT $object $title $cmodel
FROM <#ri>
WHERE {
$object <fedora-model:label> $title ;
<fedora-model:hasModel> $cmodel ;
<fedora-model:state> <fedora-model:Active> .
FILTER(!sameTerm($cmodel, <info:fedora/fedora-system:FedoraObject-3.0>))
}
ORDER BY $title';
$tuque = islandora_get_tuque_connection();
if ($tuque) {
try {
$results = $tuque->repository->ri->query($query, 'sparql');
$count = 0;
foreach ($results as $result) {
$pid = $result['object']['value'];
$object = islandora_object_load($pid);
$result = islandora_pathauto_create_alias($object, 'bulkupdate');
if ($result != '') {
$count += $result;
}
}
drupal_set_message($count . ' islandora aliases were updated.');
}
catch (Exception $e) {
if ($e->getCode() == '404') {
return FALSE;
}
else {
return NULL;
}
}
}
else {
IslandoraTuque::getError();
}
// Assuming access denied in all other cases for now.
return NULL;
}
/**
* Implements hook_path_alias_types().
*/
function islandora_pathauto_path_alias_types() {
$objects['islandora/object/'] = t('Islandora');
return $objects;
}
/**
* Implements hook_token_info().
*/
function islandora_pathauto_token_info() {
$info['types']['fedora'] = array(
'name' => 'Fedora object tokens',
'description' => t('Tokens for islandora relating to fedora objects'),
'needs-data' => 'fedora',
);
$info['tokens']['fedora']['pid'] = array(
'name' => t('Object PID'),
'description' => t('Full PID of object in Fedora repository'),
);
$info['tokens']['fedora']['label'] = array(
'name' => t('Object label'),
'description' => t('Fedora object label'),
);
$info['tokens']['fedora']['shortpid'] = array(
'name' => t('Short PID'),
'description' => t('Fedora object pid without namespace'),
);
$info['tokens']['fedora']['namespace'] = array(
'name' => t('Namespace'),
'description' => t('Fedora object namespace'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function islandora_pathauto_tokens($type, $tokens, array $data = array(), array $options = array()) {
if ($type == 'fedora' & !empty($data['fedora'])) {
$object = $data['fedora'];
$replacements = array();
foreach ($tokens as $name => $original) {
if ($name == 'pid') {
$replacements[$original] = $object->id;
}
if ($name == 'shortpid') {
$temp = explode(':', $object->id, 2);
$replacements[$original] = $temp[1];
}
if ($name == 'namespace') {
$temp = explode(':', $object->id, 2);
$replacements[$original] = $temp[0];
}
if ($name == 'label') {
$replacements[$original] = $object->label;
}
}
return $replacements;
}
return array();
}