Mercurial > hg > MPIWG-drupal-modules
comparison sites/all/modules/custom/digitalobjects/digitalobjects.module @ 0:015d06b10d37 default tip
initial
author | dwinter |
---|---|
date | Wed, 31 Jul 2013 13:49:13 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:015d06b10d37 |
---|---|
1 <?php | |
2 /* Implements hook_menu */ | |
3 | |
4 | |
5 /* TODOS | |
6 * | |
7 * | |
8 */ | |
9 include('digitalobject_reader.php'); | |
10 include('digitalobjects.import.php'); | |
11 | |
12 /** | |
13 * Implementation of hook_enable(). | |
14 */ | |
15 function digitalobjects_enable() { | |
16 | |
17 #adds a field for the current collection | |
18 // Check if our field is not already created. | |
19 if (!field_info_field('field_digitalobjects_cid')) { | |
20 $field = array( | |
21 'field_name' => 'field_digitalobjects_cid', | |
22 'type' => 'digitalobjects_digitalcollection', | |
23 ); | |
24 field_create_field($field); | |
25 | |
26 // Create the instance on the bundle. | |
27 $instance = array( | |
28 'field_name' => 'field_digitalobjects_cid', | |
29 'entity_type' => 'user', | |
30 'label' => 'Current collection', | |
31 'bundle' => 'user', | |
32 // If you don't set the "required" property then the field wont be required by default. | |
33 'required' => False, | |
34 'settings' => array( | |
35 // Here you inform either or not you want this field showing up on the registration form. | |
36 'user_register_form' => 1, | |
37 ), | |
38 'widget' => array( | |
39 'type' => 'digitalcollection_default_textfield', | |
40 ), | |
41 ); | |
42 field_create_instance($instance); | |
43 } | |
44 } | |
45 | |
46 function digitalobjects_menu(){ | |
47 | |
48 $items['digitalcollections/manageCurrent'] = array( | |
49 'description' => 'show an object.', | |
50 'page callback' => 'digitalobjects_digitalcollectionsManage', | |
51 'access arguments' => array('access content'), | |
52 'file' => 'digitalobjects.collections.inc', | |
53 | |
54 ); | |
55 | |
56 | |
57 $items['digitalobject'] = array( | |
58 'description' => 'show an object.', | |
59 'page callback' => 'digitalobjects_page', | |
60 'access arguments' => array('access content'), | |
61 | |
62 ); | |
63 $items['admin/config/media/digitalobjects'] = array( | |
64 'title' => 'Digitalobject', | |
65 'description' => 'Set path to the metadaprovider for digitalobjects', | |
66 'weight' => 10, | |
67 'page callback' => 'drupal_get_form', | |
68 'page arguments' => array('digitalobjects_settings'), | |
69 'access arguments' => array('administrate digitalobjects'), | |
70 'file' => 'digitalobjects.admin.inc', | |
71 | |
72 ); | |
73 | |
74 | |
75 | |
76 $items['user/%/collections'] = array( | |
77 'title' => 'Collections', | |
78 'description' => 'Set path to the metadaprovider for digitalobjects', | |
79 'weight' => 10, | |
80 'page callback' => 'digitalobjects_user_admin_collection', | |
81 'page arguments' => array(1), | |
82 'access arguments' => array('manage private collections'), | |
83 'file' => 'digitalobjects.collections.inc', | |
84 'type' => MENU_LOCAL_TASK, | |
85 ); | |
86 | |
87 | |
88 | |
89 $items['digitalobject/%/view'] = array( | |
90 'title' => 'View', | |
91 'description' => 'show an object.', | |
92 'page callback' => 'digitalobjects_page', | |
93 'access arguments' => array('access content'), | |
94 'page arguments' => array(1), | |
95 | |
96 'access arguments' => array('access content'), | |
97 'type' => MENU_LOCAL_TASK, | |
98 | |
99 ); | |
100 | |
101 $items['digitalobject/%/edit'] = array( | |
102 'title' => 'Edit', | |
103 'description' => 'Set path to the metadaprovider for digitalobjects', | |
104 'weight' => 10, | |
105 'page callback' => 'digitalobjects_edit', | |
106 'page arguments' => array(1), | |
107 | |
108 'access arguments' => array('administrate digitalobjects'), | |
109 'type' => MENU_LOCAL_TASK, | |
110 | |
111 ); | |
112 | |
113 $items['digitalobject/%/usage'] = array( | |
114 'title' => 'Usage', | |
115 'description' => 'Set path to the metadaprovider for digitalobjects', | |
116 'weight' => 10, | |
117 'page callback' => 'digitalobjects_show_usage', | |
118 'page arguments' => array(1), | |
119 | |
120 'access arguments' => array('access content'), | |
121 'type' => MENU_LOCAL_TASK, | |
122 | |
123 ); | |
124 | |
125 $items['digitalobject/%/add'] = array( | |
126 'title' => 'Add', | |
127 'description' => 'Add an object to the current collection', | |
128 'weight' => 10, | |
129 'page callback' => 'digitalobjects_addToCurrentCollection', | |
130 'page arguments' => array(1), | |
131 | |
132 'access arguments' => array('administrate digitalobjects'), | |
133 'file' => 'digitalobjects.collections.inc', | |
134 'type' => MENU_LOCAL_TASK, | |
135 | |
136 ); | |
137 return $items; | |
138 | |
139 } | |
140 | |
141 | |
142 function digitalobjects_create_primary_node($objid){ | |
143 $node = new stdClass(); | |
144 $node->type = 'digitalobject'; | |
145 node_object_prepare($node); | |
146 global $user; | |
147 #dpm($user); | |
148 #$node->uid=$user->uid; | |
149 $node->field_single_objid['und'][0]['objid']=$objid; | |
150 | |
151 $node->title = $objid; | |
152 | |
153 $node->language = LANGUAGE_NONE; | |
154 #$node = node_submit($node); | |
155 | |
156 #$node->field_object_type['und'][0]=array('tid'=> '6'); | |
157 | |
158 $foo = taxonomy_get_term_by_name('primary'); | |
159 foreach($foo as $term) { | |
160 | |
161 if(($term->vocabulary_machine_name) == 'digitalobject_types') { | |
162 | |
163 $node->field_object_type['und'][]['tid'] = $term->tid; | |
164 } | |
165 } | |
166 $node = node_submit($node); | |
167 | |
168 node_save($node); | |
169 | |
170 return $node; | |
171 } | |
172 | |
173 function digitalobjects_edit($objid){ | |
174 #suche ob es schon ein locales object zu dieser id gibt. | |
175 $query = new EntityFieldQuery(); | |
176 $entities = $query->entityCondition('entity_type', 'node') | |
177 ->entityCondition('bundle', 'digitalobject') | |
178 ->fieldCondition('field_single_objid', 'objid', $objid,"=") | |
179 ->execute(); | |
180 | |
181 if ($entities == null) #existiert noch nicht, dann anlegen | |
182 { | |
183 $node =digitalobjects_create_primary_node($objid); | |
184 #$node->field_object_type['und'][0]=array('tid'=> 'autocreate', | |
185 #'name' => "primary", | |
186 #'vocabulary_machine_name' => 'digitialobject_types'); | |
187 | |
188 } else { | |
189 | |
190 | |
191 $nodes = node_load_multiple(array_keys($entities['node'])); | |
192 | |
193 #gibt es eins, dann gib dieses zum editieren zurück | |
194 #TODO falls mehrere existieren, was dann?? | |
195 | |
196 #gehe durch alle gefundene | |
197 foreach ($nodes as $node){ | |
198 #$typeTag = $entity->object_type['und'] | |
199 | |
200 if (isset($node->field_object_type['und'])){ | |
201 $tid = $node->field_object_type['und'][0]['tid']; | |
202 $tax =taxonomy_term_load($tid); | |
203 if ($tax->name == "primary"){ #'primary gefunden' | |
204 module_load_include('inc', 'node', 'node.pages'); | |
205 return drupal_get_form('digitalobject_node_form', $node); | |
206 }} | |
207 } # keine primary dann lege diese an: | |
208 | |
209 $node =digitalobjects_create_primary_node($objid); | |
210 | |
211 #$vals = array_values($nodes); | |
212 #$node= array_shift($vals); | |
213 } | |
214 | |
215 $node = node_submit($node); | |
216 | |
217 #node_save($node); | |
218 module_load_include('inc', 'node', 'node.pages'); | |
219 | |
220 return drupal_get_form('digitalobject_node_form', $node); | |
221 | |
222 } | |
223 | |
224 | |
225 function digitalobjects_show_usage($objid){ | |
226 $query = new EntityFieldQuery(); | |
227 $entities = $query->entityCondition('entity_type', 'node') | |
228 ->fieldCondition('field_objid', 'objid', $objid,"=") | |
229 ->execute(); | |
230 | |
231 | |
232 $data = array(); | |
233 | |
234 #treffer gefunden | |
235 if (isset($entities['node'])){ | |
236 | |
237 $nodes = node_load_multiple(array_keys($entities['node'])); | |
238 | |
239 | |
240 foreach ($nodes as $node){ | |
241 $data[$node->nid] = $node->title; | |
242 | |
243 } | |
244 | |
245 | |
246 $output['digitalobjects_item_usage']= array( | |
247 '#theme' => 'digitalobjects_item_usage', | |
248 '#data' => $data); | |
249 | |
250 return $output; | |
251 } | |
252 | |
253 return "<h2> object not used in a collection </h2>"; | |
254 } | |
255 | |
256 | |
257 function digitalobjects_page($keys = ''){ | |
258 | |
259 | |
260 #suche ob es schon ein locales object zu dieser id gibt. | |
261 $query = new EntityFieldQuery(); | |
262 $entities = $query->entityCondition('entity_type', 'node') | |
263 ->entityCondition('bundle', 'digitalobject') | |
264 ->fieldCondition('field_single_objid', 'objid', $keys,"=") | |
265 ->execute(); | |
266 | |
267 if ($entities == null) #existiert noch nicht, dann generische Anzeige | |
268 { | |
269 | |
270 $output['digitalobjects_item']= array( | |
271 '#theme' => 'digitalobjects_item', | |
272 '#objid' => $keys); | |
273 | |
274 return $output; | |
275 } else { | |
276 $nodes = node_load_multiple(array_keys($entities['node'])); | |
277 | |
278 #gibt es eins, dann gib dieses zum editieren zurück | |
279 #TODO falls mehrere existieren, was dann?? | |
280 | |
281 #gehe durch alle gefundene | |
282 foreach ($nodes as $node){ | |
283 if (isset($node->field_object_type['und'])){ | |
284 $tid = $node->field_object_type['und'][0]['tid']; | |
285 $tax =taxonomy_term_load($tid); | |
286 | |
287 if ($tax->name == "primary"){ #'primary gefunden' | |
288 return node_view($node, $view_mode = 'full'); | |
289 } | |
290 } | |
291 } | |
292 #keine primary node gefunden, generische ausgabe | |
293 $output['digitalobjects_item']= array( | |
294 '#theme' => 'digitalobjects_item', | |
295 '#objid' => $keys); | |
296 return $output; | |
297 } | |
298 } | |
299 | |
300 /* implements digitalobject_theme */ | |
301 | |
302 function digitalobjects_theme(){ | |
303 | |
304 return array( | |
305 'digitalobjects_item' => array( | |
306 'variables' => array('objid' => NULL), | |
307 'file' => 'digitalobjects.item.inc', | |
308 'template' => 'digitalobjects-item', | |
309 ), | |
310 'digitalobjects_item_short' => array( | |
311 'variables' => array('objid' => NULL), | |
312 'file' => 'digitalobjects.item.inc', | |
313 'template' => 'digitalobjects-item-short', | |
314 ), | |
315 'digitalobjects_item_tools' => array( | |
316 'variables' => array('objid' => NULL), | |
317 'template' => 'digitalobjects-item-tools', | |
318 ), | |
319 'digitalobjects_item_usage' => array( | |
320 'variables' => array('data' => NULL), | |
321 'template' => 'digitalobjects-item-usage', | |
322 ), | |
323 | |
324 'digitalobjects_currentCollection_block' => array( | |
325 'variables' => array('node' => NULL), | |
326 'template' => 'digitalobjects-current-collection', | |
327 ), | |
328 /* 'digitalobjects_user_admin_page' => array( | |
329 'variables' => array('digitalobjects_user_admin','uid' =>1), | |
330 'template' => 'digitalobjects-user-admin-form' | |
331 ),*/ | |
332 ); | |
333 } | |
334 | |
335 function digitalobjects_forms($form_id, $args) { | |
336 | |
337 $forms['digitalobjects_user_admin_form']= array( | |
338 'callback' => 'digitalobjects_user_admin', | |
339 'callback arguments' => array('digitalobjects_user_admin_page'), | |
340 ); | |
341 return $forms; | |
342 }; | |
343 | |
344 | |
345 | |
346 | |
347 function digitalobjects_user_admin(array $form, array &$form_state) { | |
348 | |
349 $form['digitalcollection'] = array( | |
350 '#type' => 'textfield', | |
351 '#title' => t('Active collection'), | |
352 '#size' => 15, | |
353 '#default_value' => 'xxxx', | |
354 '#attributes' => array('title' => t('Enter the terms id of the current collection.')), | |
355 ); | |
356 | |
357 | |
358 $form['userid'] = array( | |
359 '#type' => 'hidden', | |
360 ); | |
361 #$form['actions'] = array('#type' => 'actions'); | |
362 $form['submit'] = array('#type' => 'submit', '#value' => t('Save')); | |
363 $form['submit']['#submit'][] = 'digitalobjects_user_admin_form_submit'; | |
364 | |
365 return $form; | |
366 } | |
367 /* implements a new field type for the digitalobject id */ | |
368 | |
369 function digitalobjects_field_info(){ | |
370 | |
371 return array('digitalobjects_digitalobject' => array ( | |
372 'label' => t('Digital Object ID'), | |
373 'description' => t('This field stores a digital object ID'), | |
374 'settings' => array('max_length' =>20), | |
375 'instance_settings' => array('text_processing' => 0), | |
376 'default_widget' => 'digitalobject_default_textfield', | |
377 'default_formatter' => 'digitalobjects_fullmetadata', | |
378 ), | |
379 'digitalobjects_digitalcollection' => array ( | |
380 'label' => t('Digital Collection ID'), | |
381 'description' => t('This field stores a digital object ID'), | |
382 'settings' => array('max_length' =>20), | |
383 'instance_settings' => array('text_processing' => 0), | |
384 'default_widget' => 'digitalcollection_default_textfield', | |
385 'default_formatter' => 'digitalcollection_fullmetadata', | |
386 ), | |
387 | |
388 ); | |
389 } | |
390 | |
391 function digitalobjects_field_formatter_info() { | |
392 | |
393 return array( | |
394 // This formatter shows the obejct with fullmetadata | |
395 'digitalobjects_fullmetadata' => array( | |
396 'label' => t('Fullmetadata formatter'), | |
397 'field types' => array('digitalobjects_digitalobject'), | |
398 ), | |
399 'digitalobjects_label' => array( | |
400 'label' => t('Lable formatter'), | |
401 'field types' => array('digitalobjects_digitalobject'), | |
402 ) | |
403 ); | |
404 } | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 function digitalobjects_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
412 $element = array(); | |
413 | |
414 | |
415 switch ($display['type']) { | |
416 // This formatter simply outputs the field as text and with a color. | |
417 case 'digitalobjects_fullmetadata': | |
418 foreach ($items as $delta => $item) { | |
419 if ($item['objid']!=""){ | |
420 $element[$delta]['#markup'] = theme('digitalobjects_item_tools', array("objid" => $item['objid'])); | |
421 $element[$delta]['#markup'] .= theme('digitalobjects_item', array("objid" => $item['objid'])); | |
422 | |
423 } | |
424 } | |
425 break; | |
426 | |
427 case 'digitalobjects_label': | |
428 foreach ($items as $delta => $item) { | |
429 if ($item['objid']!=""){ | |
430 | |
431 $element[$delta]['#markup'] = theme('digitalobjects_item_short', array("objid" => $item['objid'])); | |
432 $element[$delta]['#markup'] .= theme('digitalobjects_item_tools', array("objid" => $item['objid'])); | |
433 } | |
434 } | |
435 break; | |
436 } | |
437 return $element; | |
438 } | |
439 | |
440 | |
441 function digitalobjects_field_widget_info() { | |
442 #TODO:both types should check if field is valid | |
443 return array( | |
444 'digitalobject_default_textfield' => array( | |
445 'label' => t('digitalobject id'), | |
446 'field types' => array('digitalobjects_digitalobject'), | |
447 ), | |
448 'digitalcollection_default_textfield' => array( | |
449 'label' => t('digitalobject id'), | |
450 'field types' => array('digitalobjects_digitalcollection'), | |
451 ) | |
452 ); | |
453 } | |
454 | |
455 function digitalobjects_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { | |
456 | |
457 | |
458 $field_name = $field['field_name']; | |
459 $field_type = $field['type']; | |
460 | |
461 | |
462 $value = isset($items[$delta]['objid']) ? $items[$delta]['objid'] : ''; | |
463 | |
464 $widget = $element; | |
465 $widget['#delta'] = $delta; | |
466 | |
467 switch ($instance['widget']['type']) { | |
468 | |
469 | |
470 | |
471 case 'digitalobject_default_textfield': | |
472 $widget += array( | |
473 '#type' => 'textfield', | |
474 '#default_value' => $value, | |
475 '#size' => 20, | |
476 '#maxlength' => 20, | |
477 ); | |
478 $element['objid'] = $widget; | |
479 break; | |
480 | |
481 case 'digitalcollection_default_textfield': | |
482 $widget += array( | |
483 '#type' => 'textfield', | |
484 '#default_value' => $value, | |
485 '#size' => 20, | |
486 '#maxlength' => 20, | |
487 ); | |
488 break; | |
489 | |
490 | |
491 } | |
492 | |
493 | |
494 | |
495 | |
496 return $element; | |
497 } | |
498 | |
499 | |
500 function digitalobjects_field_is_empty($item, $field){ | |
501 return empty($item['objid']); | |
502 } | |
503 | |
504 | |
505 | |
506 function digitalobjects_user_admin_form_submit($form,$formstate){ | |
507 $value = $formstate['values']['digitalcollection']; | |
508 $userid = $formstate['values']['userid']; | |
509 | |
510 $user=user_load($userid); | |
511 | |
512 | |
513 $user->field_digitalobjects_cid['und'][0]['objid'] = $value; | |
514 | |
515 user_save($user); | |
516 | |
517 } | |
518 | |
519 function digitalobjects_block_info(){ | |
520 $blocks['digitalobjects_currentCollection'] = array( | |
521 'info' => t('Current Collection'), | |
522 'visibility' => BLOCK_VISIBILITY_PHP, | |
523 'pages' => '<?php global $user; if ($user->uid == 0) return FALSE; else return TRUE; ?>' | |
524 ); | |
525 return $blocks; | |
526 } | |
527 | |
528 function digitalobjects_block_view($delta){ | |
529 | |
530 switch($delta){ | |
531 | |
532 case 'digitalobjects_currentCollection': | |
533 /* display current cullection */ | |
534 | |
535 global $user; | |
536 | |
537 $user_full=user_load($user->uid); | |
538 | |
539 if (isset($user_full->field_digitalobjects_cid['und'][0]['objid'])){ | |
540 $val = $user_full->field_digitalobjects_cid['und'][0]['objid']; | |
541 } else { | |
542 return; | |
543 } | |
544 $node = node_load($val); | |
545 | |
546 #$title = $node=>title; | |
547 | |
548 $block['content'] = theme('digitalobjects_currentCollection_block',array('node' => $node)); | |
549 | |
550 $form['submit'] = array('#type' => 'submit', '#value' => t('Save')); | |
551 $form['submit']['#submit'][] = 'digitalobjects_user_admin_collection'; | |
552 | |
553 $submitForm=drupal_get_form('digitalobjects_change_collection_button_form'); | |
554 $block['content'] .= drupal_render($submitForm); | |
555 } | |
556 | |
557 | |
558 return $block; | |
559 } | |
560 | |
561 function digitalobjects_change_collection_button_form(){ | |
562 include_once 'digitalobjects.collections.inc'; | |
563 global $user; | |
564 $form['userid'] = array( | |
565 | |
566 '#type' => 'hidden', | |
567 '#value' => $user->uid | |
568 ); | |
569 | |
570 $form['submit'] = array('#type' => 'submit', '#value' => t('change')); | |
571 $form['submit']['#submit'][] = 'digitalobjects_user_admin_collection_submit'; | |
572 | |
573 return $form; | |
574 } | |
575 | |
576 function digitalobjects_permission() { | |
577 return array( | |
578 'manage private collections' => array( | |
579 'title' => t('Manage private collections'), | |
580 'description' => t('Allow users create and manage private collections.'), | |
581 ), | |
582 'administrate digitalobjects' => array( | |
583 'title' => t('Administrate digitalobjects'), | |
584 'description' => t('Can add commentaries to digital objects.'), | |
585 ), | |
586 ); | |
587 } | |
588 | |
589 | |
590 function digitalobjects_pathologic_alter(&$url_params, $parts, $settings){ | |
591 #bilder ohne jegliche pfad angaben werden auf sites/default/files/.. | |
592 if (preg_match('~^([^/]*)\.(png|gif|jpe?g)$~', $url_params['path'])){ | |
593 | |
594 $url_params['path'] = 'sites/default/files/' . $url_params['path']; | |
595 } | |
596 | |
597 if (preg_match('~^./([^/]*)\.pt$~', $url_params['path'],$matches)){ | |
598 | |
599 $url_params['path'] = 'harriot/maps/' . $matches[1] . '.pt'; | |
600 } | |
601 | |
602 } |