-
Notifications
You must be signed in to change notification settings - Fork 3
/
islandora_feeds.module
356 lines (328 loc) · 12.2 KB
/
islandora_feeds.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
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
<?php
/**
* @file
* Defines all the hooks this module implements.
*/
/**
* Implements hook_menu(). Much code borrowed from field_ui.module.
*/
function islandora_feeds_menu() {
// Create tabs for all possible bundles.
foreach (entity_get_info() as $entity_type => $entity_info) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
if (isset($bundle_info['admin'])) {
// Extract path information from the bundle.
$path = $bundle_info['admin']['path'];
if (isset($bundle_info['admin']['bundle argument'])) {
$bundle_arg = $bundle_info['admin']['bundle argument'];
$bundle_pos = (string) $bundle_arg;
}
else {
$bundle_arg = $bundle_name;
$bundle_pos = '0';
}
// Extract access information, providing defaults.
$access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
$access += array(
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
);
// We only want the node entities, not the comment node entities.
if (preg_match('/\/manage\/%node_type$/', $path)) {
// 'Islandora Feeds schema' tab.
$items["$path/islandora_feeds"] = array(
'title' => 'Islandora Feeds',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_feeds_helpers_form', $bundle_arg),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
) + $access;
}
}
}
}
return $items;
}
/**
* Form constructor for the Islandora Feeds XSD generation tool.
* No submit button or callback is required.
*/
function islandora_feeds_helpers_form($form, &$form_state, $bundle_arg) {
module_load_include('inc', 'islandora_feeds', 'includes/utilities');
$form['xmlform'] = array(
'#type' => 'textarea',
'#title' => t('Current XML Forms form definition file'),
'#default_value' => theme('xmlform', array('content_type' => $bundle_arg->type)),
'#rows' => 20,
'#description' => t("Copy this and upload it to the XML Forms Builder"),
);
$xml_forms_module_dir = drupal_get_path('module', 'islandora_xml_forms');
$form['schema'] = array(
'#type' => 'textarea',
'#title' => t('Current schema for the OBJ datastream'),
'#default_value' => theme('schema_xsd', array('content_type' => $bundle_arg->type)),
'#rows' => 20,
'#description' => t("Copy this into a file and upload it to %dir",
array("%dir" => $xml_forms_module_dir . '/xml')),
);
return $form;
}
/**
* Implements hook_feeds_plugins().
*/
function islandora_feeds_feeds_plugins() {
$info = array();
$info['IslandoraFeedsFeedsNodeProcessor'] = array(
'name' => 'Islandora Feeds node processor',
'description' => 'Creates Islandora objects. Corresponding nodes are not created.',
'handler' => array(
'parent' => 'FeedsProcessor',
'class' => 'IslandoraFeedsFeedsNodeProcessor',
'file' => 'IslandoraFeedsFeedsNodeProcessor.inc',
'path' => drupal_get_path('module', 'islandora_feeds'),
),
);
return $info;
}
/**
* Implements hook_feeds_before_import().
*
* Reinitialize session variables that keep track of which Islandora objects
* and their datastreams have been processed by the various derivative-managment
* code in islandora_feeds_derivs.module on during the current import. We need
* to do this to avoid the Islandora hook_derivative() code from reprocessing
* objects (which is desired in most cases, but not the ones presented by Islandora
* Feeds or Islandora Feeds Derivs).
*/
function islandora_feeds_feeds_before_import(FeedsSource $source) {
if (variable_get('islandora_feeds_derivs_is_active', 0)) {
// An simple array of PIDs.
$_SESSION['islandora_feeds_derivs_seen']['obj_replaced'] = array();
// An associative array in the form PID = array(DSID1, DSID2).
$_SESSION['islandora_feeds_derivs_seen']['deriv_generated'] = array();
// An simple array of PIDs.
$_SESSION['islandora_feeds_derivs_seen']['deriv_deleted'] = array();
}
}
/**
* Implements hook_theme().
*/
function islandora_feeds_theme($existing, $type, $theme, $path) {
return array(
'schema_xsd' => array(
'template' => 'theme/schema',
'variables' => array('content_type' => NULL),
),
'xmlform' => array(
'template' => 'theme/xmlform',
'variables' => array('content_type' => NULL),
),
'islandora_feeds_item_display' => array(
'template' => 'theme/item-display',
'variables' => array('islandora_object' => NULL),
),
'islandora_feeds_item_ingest' => array(
'template' => 'theme/item-ingest',
'variables' => array('node' => NULL),
),
);
}
/**
* Implements hook_preprocess_theme().
*
* @todo: Use standard Drupal table theming here.
*/
function islandora_feeds_preprocess_islandora_feeds_item_display(array &$variables) {
$islandora_object = $variables['islandora_object'];
if (isset($islandora_object['OBJ'])) {
$xsl_doc = new DOMDocument();
$path_to_xsl = drupal_get_path('module', 'islandora_feeds') . '/xml/xml2table.xsl';
$xsl_doc->load($path_to_xsl);
$xml_doc = new DOMDocument();
$xml_doc->loadXML($islandora_object['OBJ']->content);
$xslt_proc = new XSLTProcessor();
$xslt_proc->importStylesheet($xsl_doc);
$variables['islandora_content'] = $xslt_proc->transformToXML($xml_doc);
}
else {
$variables['islandora_content'] = t('There is no OBJ datastream for this object.');
}
}
/**
* Implements hook_preprocess_theme().
*/
function islandora_feeds_preprocess_islandora_feeds_item_ingest(array &$variables) {
$node = $variables['node'];
$variables['title'] = $node->title;
$object_fields = field_info_instances("node", $node->type);
// We remove this field because we don't want it in our object's XML field data.
unset($object_fields['field_tn']);
foreach ($object_fields as $field_name => $info) {
$trimmed_field_name = preg_replace('/^field_/', '', $field_name);
if (isset($info['field_name'])) {
$variables['field_data'][] = array('field_name' => $trimmed_field_name,
'field_label' => htmlspecialchars($info['label']),
'field_value' => $node->{$field_name}['und']);
}
}
}
/**
* Implements hook_preprocess_theme().
*/
function islandora_feeds_preprocess_schema_xsd(array &$variables) {
$content_type = $variables['content_type'];
$object_fields = field_info_instances("node", $content_type);
$fields = array('title');
foreach ($object_fields as $field_name => $info) {
// Remove 'field_'.
$field_name = preg_replace('/^field_/', '', $field_name);
$fields[] = $field_name;
}
$variables['fields'] = $fields;
$variables['documentation'] = "Generated by the Islandora Feeds module "
. date(DATE_RFC2822) . ".\n";
}
/**
* Implements hook_preprocess_theme().
*/
function islandora_feeds_preprocess_xmlform(array &$variables) {
$content_type = $variables['content_type'];
$object_fields = field_info_instances("node", $content_type);
$fields = array('title');
foreach ($object_fields as $field_name => $info) {
// Remove 'field_'.
$field_name = preg_replace('/^field_/', '', $field_name);
$fields[$field_name] = array(
'field_name' => $field_name,
'field_label' => $info['label'],
);
}
$variables['fields'] = $fields;
$variables['documentation'] = "Generated by the Islandora Feeds module "
. date(DATE_RFC2822) . ".\n";
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_feeds_islandora_feedsCModel_islandora_view_object($object, $page_number, $page_size) {
$output = theme('islandora_feeds_item_display', array('islandora_object' => $object));
return array('' => $output);
}
/**
* Implements hook_action_info().
*/
function islandora_feeds_action_info() {
return array(
'islandora_feeds_create_islandora_objects_action' => array(
'type' => 'node',
'label' => t('Create Islandora objects from nodes'),
'configurable' => TRUE,
'vbo_configurable' => TRUE,
'pass rows' => TRUE,
'triggers' => array('any'),
),
);
}
/**
* Form to configure the 'Create Islandora objects from nodes' action.
*/
function islandora_feeds_create_islandora_objects_action_form($context) {
$types = node_type_get_types();
$types_options = array();
foreach ($types as $type => $properties) {
$types_options[$type] = $properties->name;
}
$form['bundle'] = array(
'#type' => 'select',
'#title' => t('Bundle'),
'#options' => $types_options,
'#description' => t("The content type bundle that the objects will be created from."),
'#required' => TRUE,
);
module_load_include('inc', 'islandora_feeds', 'includes/utilities');
$collections = islandora_feeds_get_collections_or_content_models('collections');
$form['collection'] = array(
'#type' => 'select',
'#title' => t('Target collection'),
'#options' => $collections,
'#description' => t("The Islandora collection you want to import into."),
'#required' => TRUE,
);
$cmodels = islandora_feeds_get_collections_or_content_models('cmodels');
$form['cmodel'] = array(
'#type' => 'select',
'#title' => t('Target content model'),
'#options' => $cmodels,
'#default_value' => 'islandora:feedsCModel',
'#description' => t("The Islandora content model you want to import into."),
'#required' => TRUE,
);
$form['namespace'] = array(
'#type' => 'textfield',
'#title' => t('Namespace'),
'#description' => t('The PID namespace to use for the imported objects.'),
'#required' => TRUE,
);
$author = user_load_by_name('admin');
$form['author'] = array(
'#type' => 'textfield',
'#title' => t('Author'),
'#description' => t('Select the author of the nodes to be created - leave empty to assign "anonymous".'),
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => empty($author->name) ? 'anonymous' : check_plain($author->name),
);
$form['delete_source_node'] = array(
'#type' => 'checkbox',
'#title' => t('Delete nodes after creating Islandora objects'),
'#default_value' => FALSE,
'#description' => t('Check this option if you want to delete the nodes the
Islandora objects are created from.'),
);
return $form;
}
/**
* Custom action. Fired from Views Bulk Operations.
*/
function islandora_feeds_create_islandora_objects_action($object, $context) {
module_load_include('inc', 'islandora_feeds', 'includes/utilities');
islandora_feeds_ingest_object($object, $context['namespace'], $context['cmodel'],
$context['collection'], $context['author'], $context['delete_source_node']);
}
/**
* Action submit function.
*/
function islandora_feeds_create_islandora_objects_action_submit($form, $form_state) {
return array(
'namespace' => $form_state['values']['namespace'],
'bundle' => $form_state['values']['bundle'],
'collection' => $form_state['values']['collection'],
'cmodel' => $form_state['values']['cmodel'],
'author' => $form_state['values']['author'],
'delete_source_node' => $form_state['values']['delete_source_node'],
);
}
/**
* Implements hook_islandora_required_objects().
*/
function islandora_feeds_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'islandora_feeds');
$islandora_path = drupal_get_path('module', 'islandora');
$islandora_feeds_content_model = $connection->repository->constructObject('islandora:feedsCModel');
$islandora_feeds_content_model->owner = 'fedoraAdmin';
$islandora_feeds_content_model->label = 'Islandora Feeds Content Model';
$islandora_feeds_content_model->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL Datastream.
$datastream = $islandora_feeds_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'text/xml';
$datastream->setContentFromFile("$module_path/xml/islandora_feeds_ds_composite_model.xml", FALSE);
$islandora_feeds_content_model->ingestDatastream($datastream);
return array(
'islandora_feeds' => array(
'title' => 'Islandora Feeds',
'objects' => array(
$islandora_feeds_content_model,
),
),
);
}