-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathislandora_cwrc_document.module
364 lines (341 loc) · 14.5 KB
/
islandora_cwrc_document.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
356
357
358
359
360
361
362
363
364
<?php
/**
* @file
* Defines hooks and required objects for the module.
*/
// Objects.
define('ISLANDORA_CWRC_DOCUMENT_COLLECTION_PID', 'cwrc:documentCollection');
define('ISLANDORA_CWRC_DOCUMENT_TEMPLATE_COLLECTION_PID', 'cwrc:documentTemplateCollection');
define('ISLANDORA_CWRC_DOCUMENT_CONTENT_MODEL', 'cwrc:documentCModel');
define('ISLANDORA_CWRC_DOCUMENT_TEMPLATE_CONTENT_MODEL', 'cwrc:documentTemplateCModel');
// Permissions.
define('ISLANDORA_CWRC_DOCUMENT_VIEW_CWRC', 'view CWRC datastreams');
define('ISLANDORA_CWRC_DOCUMENT_ANNOTATE_CWRC', 'annotate CWRC datastreams');
define('ISLANDORA_CWRC_DOCUMENT_EDIT_CWRC', 'edit CWRC datastreams');
/**
* Implements hook_menu().
*/
function islandora_cwrc_document_menu() {
$items = array();
$items['islandora/object/%islandora_object/edit-cwrc'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'islandora_cwrc_document_display_cwrc_writer',
2,
array('initial_mode' => 'edit'),
),
'access callback' => 'islandora_cwrc_document_access_callback',
'access arguments' => array(ISLANDORA_CWRC_DOCUMENT_EDIT_CWRC, 2),
'load arguments' => array(2),
'file' => 'includes/utilities.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
$items['islandora/object/%islandora_object/annotate-cwrc'] = array(
'title' => 'Annotate',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'islandora_cwrc_document_display_cwrc_writer',
2,
array(
'initial_mode' => 'annotate',
'readonly' => TRUE,
),
),
'access callback' => 'islandora_cwrc_document_access_callback',
'access arguments' => array(ISLANDORA_CWRC_DOCUMENT_ANNOTATE_CWRC, 2),
'load arguments' => array(2),
'file' => 'includes/utilities.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
$items['islandora/object/%islandora_object/note.htm'] = array(
'title' => 'CWRC Writer Note',
'page callback' => 'islandora_cwrc_document_display_cwrc_writer_note',
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_CWRC_DOCUMENT_EDIT_CWRC, 2),
'file' => 'includes/utilities.inc',
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function islandora_cwrc_document_menu_alter(&$items) {
$path = drupal_get_path('module', 'islandora_cwrc_document');
$items['islandora/object/%islandora_tokened_object/datastream/%islandora_tokened_datastream/view'] = array(
'title' => 'View datastream',
'page callback' => 'islandora_cwrc_document_view_datastream',
'page arguments' => array(4, FALSE, NULL),
'type' => MENU_CALLBACK,
'file' => "$path/includes/utilities.inc",
'load arguments' => array('%map'),
'access callback' => 'islandora_object_datastream_tokened_access_callback',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2, 4),
);
$items['islandora/object/%islandora_object/datastream/%islandora_datastream/version/%/view'] = array(
'title' => 'View datastream version',
'page callback' => 'islandora_cwrc_document_view_datastream',
'page arguments' => array(4, FALSE, 6),
'type' => MENU_CALLBACK,
'file' => "$path/includes/utilities.inc",
'access callback' => 'islandora_datastream_access',
'access arguments' => array(ISLANDORA_VIEW_DATASTREAM_HISTORY, 4),
'load_arguments' => array(2),
);
$items['islandora/object/%islandora_object/datastream/%islandora_datastream/download'] = array(
'title' => 'Download datastream',
'page callback' => 'islandora_cwrc_document_download_datastream',
'page arguments' => array(4),
'type' => MENU_CALLBACK,
'file' => "$path/includes/utilities.inc",
'access callback' => 'islandora_datastream_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 4),
'load arguemnts' => array(2),
);
}
/**
* Checks whether the user can access the given object's CWRC-Writer tab.
*
* Checks for object existance, accessiblitly, namespace permissions,
* and user permissions
*
* @param string $op
* The permission to test for.
* @param AbstractObject $object
* The object to test, if NULL given the object doesn't exist or is
* inaccessible.
*
* @return bool
* TRUE if the user is allowed to access this object, FALSE otherwise.
*/
function islandora_cwrc_document_access_callback($op, $object = NULL) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (!$object && !islandora_describe_repository()) {
islandora_display_repository_inaccessible_message();
return FALSE;
}
if (!in_array(ISLANDORA_CWRC_DOCUMENT_CONTENT_MODEL, $object->models)) {
return FALSE;
}
return isset($object['CWRC']) &&
islandora_datastream_access($op, $object['CWRC']) &&
islandora_datastream_access(ISLANDORA_METADATA_EDIT, $object['CWRC']) &&
user_access('add or remove islandora object lock');
}
/**
* Implements hook_islandora_required_objects().
*/
function islandora_cwrc_document_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'islandora_cwrc_document');
$islandora_path = drupal_get_path('module', 'islandora');
$repository = $connection->repository;
$root_pid = variable_get('islandora_repository_pid', 'islandora:root');
module_load_include('inc', 'islandora_cwrc_writer', 'includes/utilities');
module_load_include('inc', 'islandora_cwrc_document', 'includes/utilities');
// Collection object.
$document_collection = $repository->constructObject(ISLANDORA_CWRC_DOCUMENT_COLLECTION_PID);
$document_collection->owner = 'fedoraAdmin';
$document_collection->label = 'CWRC Document Collection';
$document_collection->models = 'islandora:collectionCModel';
$document_collection->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', $root_pid);
// Collection policy.
$cp = $document_collection->constructDatastream('COLLECTION_POLICY', 'X');
$cp->label = 'Collection policy';
$cp->mimetype = 'application/xml';
$cp->setContentFromFile("$module_path/xml/cwrc_document_collection_policy.xml", FALSE);
$document_collection->ingestDatastream($cp);
// Thumbnail.
$tn = $document_collection->constructDatastream('TN', 'M');
$tn->label = 'Thumbnail';
$tn->mimetype = 'image/png';
$tn->setcontentFromFile("$islandora_path/images/folder.png", FALSE);
$document_collection->ingestDatastream($tn);
// Document content model.
$document_cmodel = $repository->constructObject(ISLANDORA_CWRC_DOCUMENT_CONTENT_MODEL);
$document_cmodel->owner = 'fedoraAdmin';
$document_cmodel->label = 'CWRC Document Content Model';
$document_cmodel->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL.
$ds = $document_cmodel->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$ds->label = "DS-COMPOSITE-MODEL";
$ds->mimetype = 'application/xml';
$ds->setContentFromFile("$module_path/xml/cwrc_document_ds_composite_model.xml", FALSE);
$document_cmodel->ingestDatastream($ds);
// Template collection object.
$template_collection = $repository->constructObject(ISLANDORA_CWRC_DOCUMENT_TEMPLATE_COLLECTION_PID);
$template_collection->owner = 'fedoraAdmin';
$template_collection->label = 'CWRC Document Template Collection';
$template_collection->models = 'islandora:collectionCModel';
$template_collection->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', $root_pid);
// Template collection policy.
$cp = $template_collection->constructDatastream('COLLECTION_POLICY', 'X');
$cp->label = 'Collection policy';
$cp->mimetype = 'application/xml';
$cp->setContentFromFile("$module_path/xml/cwrc_document_template_collection_policy.xml", FALSE);
$template_collection->ingestDatastream($cp);
// Thumbnail.
$tn = $template_collection->constructDatastream('TN', 'M');
$tn->label = 'Thumbnail';
$tn->mimetype = 'image/png';
$tn->setContentFromFile("$islandora_path/images/folder.png", FALSE);
$template_collection->ingestDatastream($tn);
// Document template content model.
$template_cmodel = $repository->constructObject(ISLANDORA_CWRC_DOCUMENT_TEMPLATE_CONTENT_MODEL);
$template_cmodel->owner = 'fedoraAdmin';
$template_cmodel->label = 'CWRC Document Template Content Model';
$template_cmodel->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL.
$ds = $template_cmodel->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$ds->label = 'DS-COMPOSITE-MODEL';
$ds->mimetype = 'application/xml';
$ds->setContentFromFile("$module_path/xml/cwrc_document_template_ds_composite_model.xml", FALSE);
$template_cmodel->ingestDatastream($ds);
$objects = array(
'islandora_cwrc_document' => array(
'title' => 'Islandora CWRC Document',
'objects' => array(
$document_collection,
$document_cmodel,
$template_collection,
$template_cmodel,
),
),
);
// Standard templates.
foreach (islandora_cwrc_document_get_stock_templates() as $stock_template) {
// Template object.
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']] = $repository->constructObject($stock_template['pid']);
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->owner = 'fedoraAdmin';
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->label = $stock_template['label'];
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->models = ISLANDORA_CWRC_DOCUMENT_TEMPLATE_CONTENT_MODEL;
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', ISLANDORA_CWRC_DOCUMENT_TEMPLATE_COLLECTION_PID);
// TEMPLATE datastream.
$td = $objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->constructDatastream('TEMPLATE', 'M');
$td->label = "{$stock_template['label']} TEI";
$td->mimetype = 'application/xml';
$td->setContentFromFile("$module_path/xml/templates/{$stock_template['filename']}", FALSE);
$objects['islandora_cwrc_document']['objects'][$stock_template['pid']]->ingestDatastream($td);
// Add a reference to the schema.
$schema = islandora_object_load($stock_template['schema']);
islandora_cwrc_writer_set_document_schema($objects['islandora_cwrc_document']['objects'][$stock_template['pid']], $schema);
}
return $objects;
}
/**
* Implements hook_CMODEL_PID_islandora_ingest_steps().
*/
function islandora_cwrc_document_cwrc_documentCModel_islandora_ingest_steps() {
return array(
'islandora_cwrc_document_upload' => array(
'weight' => 10,
'type' => 'form',
'form_id' => 'islandora_cwrc_document_upload_form',
'module' => 'islandora_cwrc_document',
'file' => 'includes/upload.form.inc',
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_ingest_steps().
*/
function islandora_cwrc_document_cwrc_documentTemplateCModel_islandora_ingest_steps() {
return array(
'islandora_cwrc_document_template_upload' => array(
'weight' => 10,
'type' => 'form',
'form_id' => 'islandora_cwrc_document_template_upload_form',
'module' => 'islandora_cwrc_document',
'file' => 'includes/upload.form.inc',
),
);
}
/**
* Implements hook_islandora_edit_datastream_registry().
*/
function islandora_cwrc_document_islandora_edit_datastream_registry(AbstractObject $object, AbstractDatastream $ds) {
if (in_array('cwrc:documentCModel', $object->models) && $ds->id == 'CWRC') {
return array(
array(
'name' => t('Islandora CWRC Document'),
'url' => "islandora/object/{$object->id}/cwrc-writer",
),
);
}
}
/**
* Implements hook_permission().
*/
function islandora_cwrc_document_permission() {
return array(
ISLANDORA_CWRC_DOCUMENT_VIEW_CWRC => array(
'title' => t('View CWRC documents'),
'description' => t('View CWRC documents inside the CWRC-Writer'),
),
ISLANDORA_CWRC_DOCUMENT_ANNOTATE_CWRC => array(
'title' => t('Annotate CWRC documents'),
'description' => t('Annotate CWRC documents inside the CWRC-Writer'),
),
ISLANDORA_CWRC_DOCUMENT_EDIT_CWRC => array(
'title' => t('Edit CWRC documents'),
'description' => t('Modify CWRC documents inside the CWRC-Writer'),
),
);
}
/**
* Implements hook_form_alter().
*/
function islandora_cwrc_document_form_islandora_datastream_version_replace_form_alter(&$form, &$form_state, $form_id) {
// On Datastream replace for CWRC datastream modify valid file extensions and
// add a validation function to check that the uploaded xml file is
// well-formed.
if ($form_state['dsid'] == 'CWRC') {
$form['dsid_fieldset']['file']['#upload_validators']['file_validate_extensions'] = array('xml');
$form['#validate'][] = 'islandora_cwrc_document_xml_validate';
}
}
/**
* Custom form validation for CWRC datastream replace to uploaded xml.
*/
function islandora_cwrc_document_xml_validate($form, &$form_state) {
module_load_include('inc', 'islandora_cwrc_document', 'includes/utilities');
$file = file_load($form_state['values']['file']);
if ($file === FALSE) {
return;
}
if (!islandora_cwrc_document_check_xml($file)) {
form_set_error('dsid_fieldset][file', t('The uploaded file @file is not well-formed XML.', array('@file' => $file->filename)));
}
}
/**
* Implements hook_preprocess_islandora_cwrc_writer().
*/
function islandora_cwrc_document_preprocess_islandora_cwrc_writer(&$variables) {
// Establish the set buttons.
$variables['settings']['buttons1'] = 'schematags,|,addperson,addplace,adddate,addorg,addcitation,addnote,addtitle,addcorrection,addkeyword,addlink,|,editTag,removeTag,|,addtriple,|,';
if (isset($variables['config']['initial_mode']) && $variables['config']['initial_mode'] == 'edit') {
$variables['settings']['buttons1'] .= 'viewsource,editsource,|,';
}
$variables['settings']['buttons1'] .= 'validate,savebutton,saveexitbutton';
// Remove the header links if necessary.
if (isset($variables['config']['readonly']) && isset($variables['header']['header_buttons']) && $variables['config']['readonly'] == TRUE) {
unset($variables['header']['header_buttons']);
}
$variables['settings'] += isset($variables['config']) ? (array) $variables['config'] : array();
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_cwrc_document_cwrc_documentCModel_islandora_view_object($object) {
if (user_access(ISLANDORA_CWRC_DOCUMENT_VIEW_CWRC)) {
module_load_include('inc', 'islandora_cwrc_document', 'includes/utilities');
$config = array(
'readonly' => TRUE,
'initial_mode' => 'read',
);
return array(
'cwrc_writer' => drupal_get_form('islandora_cwrc_document_display_cwrc_writer', $object, $config),
);
}
}