-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedoraobject.module
371 lines (324 loc) · 11.7 KB
/
fedoraobject.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
365
366
367
368
369
370
<?php
/**
* @file
* Defines the Drupal Entity that represents an object in a Fedora Commons repository
*/
/**
* Implements hook_entity_info.
*/
function fedoraobject_entity_info() {
$return['fedoraobject'] = array(
'label' => 'Fedora Object',
'controller class' => 'FedoraObjectController',
'base table' => 'fedoraobject',
'revision table' => 'fedoraobject_revision',
'uri callback' => 'fedoraobject_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'foid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'static cache' => TRUE,
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => FALSE,
),
),
);
foreach (fedoraobject_types() as $type => $info) {
$return['fedoraobject']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/fedoraobjects/manage/%fedoraobject_type',
'real path' => 'admin/structure/fedoraobjects/manage/' .
str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array('administer fedora objects'),
),
);
}
return $return;
}
/**
* Generate the URI for a given Fedora Object
* @param object $fedoraobject
* @return string
*/
function fedoraobject_uri($fedoraobject) {
return array(
'path' => 'fedora/object/' . $fedoraobject->pid,
);
}
function fedoraobject_types() {
$types = &drupal_static(__FUNCTION__);
if (empty($types)) {
$types['generic'] = (object)array(
'type' => 'generic',
'name' => 'Generic Fedora Object',
'description' => t('A standard Fedora Commons repository object.'),
);
}
return $types;
}
function fedoraobject_type_load($type) {
$types = fedoraobject_types();
$type = str_replace('_', '-', $type);
return isset($types[$type]) ? $types[$type] : FALSE;
}
function fedoraobject_menu() {
$items['admin/structure/fedoraobjects'] = array(
'title' => 'Manage Fedora objects',
'description' => 'Manage fedora objects',
'page callback' => 'fedoraobject_overview_types',
'access arguments' => array('administer fedora objects'),
);
$items['admin/structure/fedoraobjects/manage/%fedoraobject_type'] = array(
'title' => 'View fedora object type',
'title callback' => 'fedoraobject_type_page_title',
'title arguments' => array(4),
'page callback' => 'fedoraobject_information',
'page arguments' => array(4),
'access arguments' => array('administer fedora objects'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/structure/fedoraobjects/manage/%fedoraobject_type/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['fedoraobject/add'] = array(
'title' => 'Add new fedora object',
'page callback' => 'fedoraobject_add_page',
'access arguments' => array('create fedora objects'),
'weight' => 1,
'menu_name' => 'manaagement',
'file' => 'fedoraobject.pages.inc',
);
foreach (fedoraobject_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$items['fedoraobject/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'check_plain',
'page callback' => 'fedoraobject_add',
'page arguments' => array(2),
'access arguments' => array('create fedora objects'),
'description' => $type->description,
);
}
$items['fedoraobject/%fedoraobject'] = array(
'title callback' => 'fedoraobject_page_title',
'title arguments' => array(1),
'page callback' => 'fedoraobject_page_view',
'page arguments' => array(1),
'access arguments' => array('view fedora objects'),
'type' => MENU_CALLBACK,
);
$items['fedoraobject/%fedoraobject/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['fedoraobject/%fedoraobject/edit'] = array(
'title' => 'Edit',
'page callback' => 'fedoraobject_page_edit',
'page arguments' => array(1),
'access arguments' => array('update fedora objects'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['fedoraobject/%fedoraobject/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('fedoraobject_delete_confirm', 1),
'access arguments' => array('delete fedora objects'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
return $items;
}
function fedoraobject_overview_types() {
foreach (fedoraobject_types() as $type => $info) {
$type_url_str = str_replace('_', '-', $type);
$label = t('View @type', array('@type' => $info->name));
$items[] = l($label, 'admin/structure/fedoraobjects/manage/' . $type_url_str);
}
return theme('item_list', array('items' => $items));
}
function fedoraobject_page_title($fedoraobject) {
return $fedoraobject->title;
}
function fedoraobject_page_edit($fedoraobject) {
$types = fedoraobject_types();
drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $types[$fedoraobject->type]->name,
'@title' => $fedoraobject->title)), PASS_THROUGH);
return drupal_get_form($fedoraobject->type . '_fedoraobject_form', $fedoraobject);
}
function fedoraobject_page_view($fedoraobject, $view_mode = 'full') {
// Remove previously-built content, if it exists.
$fedoraobject->content = array();
if ($view_mode == 'teaser') {
$fedoraobject->content['title'] = array(
'#markup' => filter_xss($fedoraobject->title),
'#weight' => -5,
);
}
// Build fields content
field_attach_prepare_view('fedoraobject', array($fedoraobject->foid => $fedoraobject), $view_mode);
entity_prepare_view('fedoraobject', array($fedoraobject->foid => $fedoraobject));
$fedoraobject->content += field_attach_view('fedoraobject', $fedoraobject, $view_mode);
return $fedoraobject->content;
}
function fedoraobject_type_page_title($type) {
return t('Manage @type', array('@type' => $type->name));
}
function fedoraobject_information($fedoraobject_type) {
return $fedoraobject_type->name . ': ' . $fedoraobject_type->description;
}
function fedoraobject_add($type) {
global $user;
$types = fedoraobject_types();
$type = isset($type) ? str_replace('_', '-', $type) : NULL;
if (empty($types[$type])) {
return MENU_NOT_FOUND;
}
$fedoraobject = entity_get_controller('fedoraobject')->create($type);
drupal_set_title(t('Create %name', array('%name' => $types[$type]->name)), PASS_THROUGH);
return drupal_get_form($type . '_fedoraobject_form', $fedoraobject);
}
function fedoraobject_forms() {
$forms = array();
if ($types = fedoraobject_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_fedoraobject_form']['callback'] = 'fedoraobject_form';
}
}
return $forms;
}
function fedoraobject_form($form, &$form_state, $fedoraobject) {
// Set the id to identify this as a fedora object edit form
$form['#id'] = 'fedoraobject-form';
// Save the fedora object for later, in case we need it.
$form['#fedoraobject'] = $fedoraobject;
$form_state['fedoraobject'] = $fedoraobject;
// Common fields. We don't have many.
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $fedoraobject->title,
'#weight' => -5,
'#required' => TRUE,
);
$form['revision'] = array(
'#access' => user_access('administer fedora objects'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => 0,
);
// Add the buttons
$form['buttons'] = array();
$form['buttons']['#weight'] = 100;
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array('fedoraobject_form_submit'),
);
if (!empty($fedoraobject->foid)) {
$form['buttons']['delete'] = array(
'#access' => user_access('delete fedora objects'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => 'fedoraobject_form_delete_submit',
);
}
$form['#validate'][] = 'fedoraobject_form_validate';
field_attach_form('fedoraobject', $fedoraobject, $form, $form_state);
return $form;
}
function fedoraobject_delete_confirm($form, &$form_state, $fedoraobject) {
$form['#fedoraobject'] = $fedoraobject;
// Always provide entity id in the same form key as in the entity edit form.
$form['foid'] = array(
'#type' => 'value',
'#value' => $fedoraobject->foid,
);
return confirm_form($form, t('Are you sure you want to delete %title?', array('%title' => $fedoraobject->title)),
'fedoraobject/' . $fedoraobject->foid, t('This function cannot be undone.'), t('Delete'), t('Cancel'));
}
function fedoraobject_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$fedoraobject = fedoraobject_load($form_state['values']['foid']);
fedoraobject_delete($form_state['values']['foid']);
watchdog('fedoraobject', '@type: deleted %title', array('%type' => $fedoraobject->type, '%title' => $fedoraobject->title));
$types = fedoraobject_types();
drupal_set_message(t('%type %title has been deleted', array('%type' => $fedoraobject->type, '%title' => $fedoraobject->title)));
$form_state['redirect'] = '<front>';
}
}
function fedoraobject_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$fedoraobject = $form['fedoraobject'];
$form_state['redirect'] = array('fedoraobject/' . $fedoraobject->foid . '/delete', array('query' => 'destination'));
}
function fedoraobject_form_validate($form, &$form_state) {
$fedoraobject = $form_state['fedoraobject'];
// Field validation
field_attach_form_validate('fedoraobject', $fedoraobject, $form, $form_state);
}
function fedoraobject_form_submit($form, &$form_state) {
global $user;
$fedoraobject = $form_state['fedoraobject'];
// Set the owner id if the object is being created.
if (empty($fedoraobject->uid)) {
$fedoraobject->uid = $user->uid;
}
$fedoraobject->title = $form_state['values']['title'];
$fedoraobject->revision = $form_state['values']['revision'];
// Notify field widgets
field_attach_submit('fedoraobject', $fedoraobject, $form, $form_state);
// Save the fedora object
$saved = fedoraobject_save($fedoraobject);
// Notify the user
if (!$saved) {
drupal_set_message("There was an error while saving the object.", 'error');
}
else {
drupal_set_message(t('Fedora object saved.'));
}
$form_state['redirect'] = 'fedoraobject/' . $fedoraobject->foid;
}
function fedoraobject_delete($foid) {
return fedoraobject_delete_multiple(array($foid));
}
function fedoraobject_delete_multiple($foids) {
return entity_get_controller('fedoraobject')->delete($foids);
}
function fedoraobject_save($fedoraobject) {
return entity_get_controller('fedoraobject')->save($fedoraobject);
}
function fedoraobject_load($foid = NULL, $vid = NULL, $reset = FALSE) {
$foids = (isset($foid) ? array($foid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$fedoraobject = fedoraobject_load_multiple($foids, $conditions, $reset);
return $fedoraobject ? reset($fedoraobject) : FALSE;
}
function fedoraobject_load_multiple($foids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('fedoraobject', $foids, $conditions, $reset);
}