forked from Gizra/biblio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biblio.module
726 lines (655 loc) · 20.8 KB
/
biblio.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
<?php
/**
* biblio.module for Drupal
*
* Copyright (C) 2006-2011 Ron Jerome
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* Implements hook_hook_info().
*/
function biblio_hook_info() {
$hooks = array();
$hooks['biblio_fields_info'] = array(
'group' => 'biblio',
);
$hooks['biblio_types_info'] = array(
'group' => 'biblio',
);
return $hooks;
}
/**
* Implements hook_access().
*/
function biblio_access($op, $node = '') {
global $user;
switch ($op) {
case 'admin':
return user_access('administer biblio');
case 'import':
return user_access('import from file');
case 'export':
return user_access('show export links');
case 'edit_author':
if (user_access('administer biblio') || user_access('edit biblio authors')) return NODE_ACCESS_ALLOW;
break;
case 'download':
if (user_access('show download links') || (user_access('show own download links') && ($user->uid == $node->uid))) return NODE_ACCESS_ALLOW;
break;
case 'rss':
return variable_get('biblio_rss', 0);
default:
}
return NODE_ACCESS_IGNORE;
}
/**
* Implements hook_permission().
*
* Since we are limiting the ability to create new nodes to certain users,
* we need to define what those permissions are here. We also define a permission
* to allow users to edit the nodes they created.
*/
function biblio_permission() {
return array(
'administer biblio' => array(
'title' => t('Administer Biblio'),
'description' => t('Allows full control (create, update, delete) of all Biblio nodes'),
),
'access biblio content' => array(
'title' => t('Access Biblio content'),
'description' => t('Allows the user to view Biblio nodes'),
),
'create biblio content' => array(
'title' => t('Create Biblio'),
'description' => t('Allows the user to create new Biblios'),
),
'edit all biblio entries' => array(
'title' => t('Edit all Biblio entries'),
'description' => t('Allows the user to edit ALL biblio entries regardless of who "owns" them, otherwise they are restricted to on'),
),
'edit own biblio entries' => array(
'title' => t('Edit pwn Biblio entries'),
'description' => t('Allows the user to edit his own biblio entries'),
),
'delete biblio entries' => array(
'title' => t('Delete biblio entries'),
'description' => t('Allows the user to delete any biblio entities'),
),
'delete own biblio entries' => array(
'title' => t('Delete own biblio entries'),
'description' => t('Allows the user to delete his own biblio entities'),
),
'edit by all biblio authors' => array(
'title' => t('Edit by all Biblio authors'),
'description' => t('Allows any/all of the authors associated with a biblio entry to edit the biblio entry. This requires the Drupal UserID be mapped to a Biblio author ID'),
),
'edit biblio authors' => array(
'title' => t('Edit Biblio authors'),
'description' => t('Allows the user to edit author information'),
),
'manage biblio structure' => array(
'title' => t('Manage Biblio structure'),
'description' => t('This determines if the user will be able to modify field and display settings for biblio and contributor entities (admin/structure/biblio)'),
),
'manage biblio content' => array(
'title' => t('Manage Biblio content'),
'description' => t('This determines if the user will be able to access the managment interface for biblios and contributors (admin/content/biblio)'),
),
'import from file' => array(
'title' => t('Import from file'),
'description' => t('Allows the user to import bibliographic data from files such as BibTex, RIS, EndNote'),
),
'show export links' => array(
'title' => t('Show export links'),
'description' => t('Allows users to see links which allow export of bibliographic data for an individual entry or the entire result set'),
),
'show download links' => array(
'title' => t('Show download links'),
'description' => t('Allows users to see links to any attachements associated with the Biblio entry'),
),
'show own download links' => array(
'title' => t('Show own download links'),
'description' => t('Allows user to only see download links on entries for which they are the owner.'),
),
'show filter tab' => array(
'title' => t('Show filter tab'),
'description' => t('This determines if the "Filter" tab on the Biblio list page will be shown to the user'),
),
'show sort links' => array(
'title' => t('Show sort links'),
'description' => t('This determines if the "Sort" links on the Biblio list page will be shown to the user'),
),
'view full text' => array(
'title' => t('Show full text'),
'description' => t('This determines if the user will be able to access the "Full Text" of the article if it is available'),
),
);
}
/**
* Implements hook_ctools_plugin_api().
*/
function biblio_ctools_plugin_api($module, $api) {
if ($module == 'biblio' && $api == 'biblio_style') {
return array('version' => 1);
}
}
/**
* Implements hook_ctools_plugin_type().
*/
function biblio_ctools_plugin_type() {
$plugins['biblio_style'] = array(
'classes' => array('class'),
'process' => 'biblio_plugin_process',
);
return $plugins;
}
/**
* Add defaults values to the notifier plugins.
*
* - 'description': The description of the plugin.
*/
function biblio_plugin_process(&$plugin, $info) {
$plugin += array(
'description' => '',
'options' => array(),
'assets' => array(
'js' => array(),
'css' => array(),
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function biblio_ctools_plugin_directory($module, $plugin) {
if ($module == 'biblio') {
return 'plugins/' . $plugin;
}
}
/**
* Helper function to include CTools plugins and get a notifier plguin.
*
* @param $plugin_name
* The plugin that should be laoded.
*/
function biblio_get_biblio_style($style_name) {
ctools_include('plugins');
return ctools_get_plugins('biblio', 'biblio_style', $style_name);
}
/**
* Helper function to include CTools plugins and get all notifier plugins.
*/
function biblio_get_biblio_styles() {
ctools_include('plugins');
return ctools_get_plugins('biblio', 'biblio_style');
}
/**
* Helper function to return all notifiers as options for a select list.
*
* @return array
* An array of biblio types for use in a field option list
*/
function biblio_get_notifiers_as_options() {
$options = array();
foreach (biblio_get_biblio_styles() as $style_name => $style) {
$options[$style_name] = check_plain($style['title']);
}
return $options;
}
/**
* Implements hook_views_api().
*/
function biblio_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'biblio') . '/includes/views',
);
}
/**
* Implements hook_migrate_api().
*/
function biblio_migrate_api() {
$migrations = array();
if (db_table_exists('_biblio_1x')) {
$migrations['BiblioMigrateContributorCollections'] = array('class_name' => 'BiblioMigrateContributorCollections');
$migrations['BiblioMigrateTypes'] = array('class_name' => 'BiblioMigrateTypes');
$migrations['BiblioMigrateEntries'] = array('class_name' => 'BiblioMigrateEntries');
}
$api = array(
'api' => 2,
'migrations' => $migrations,
);
return $api;
}
/**
* Implements hook_entity_info().
*
* Inform the Drupal and the Field API about entity types.
* Uses the contrib Entity API module to create entities
*/
function biblio_entity_info() {
$return['biblio'] = array(
'label' => t('Biblio'),
'entity class' => 'Biblio',
'controller class' => 'EntityAPIController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'biblio',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'load hook' => 'biblio_load',
'view modes' => array(),
// Entity API label callback that takes a look at our entity class method defaultLabel()
'label callback' => 'entity_class_label',
// This is also a standard Entity API callback for uri.
// It executes our entity defaultUri() method
'uri callback' => 'entity_class_uri',
'module' => 'biblio',
'access callback' => 'biblio_entity_access',
'views controller class' => 'BiblioViewsController',
'metadata controller class' => 'BiblioMetadataController',
);
// @todo: Biblio 1.x had a biblio type called "Biblio" if type was 0.
$return['biblio']['bundles']['biblio'] = array('label' => 'Biblio');
if (db_table_exists('biblio_type') && $bundles = biblio_types()) {
foreach ($bundles as $bundle) {
$return['biblio']['bundles'][$bundle->type] = array('label' => $bundle->name);
}
}
$return['biblio_contributor'] = array(
'label' => t('Contributor'),
'entity class' => 'BiblioContributor',
'controller class' => 'EntityAPIController',
'base table' => 'biblio_contributor',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'cid',
'label' => 'name',
),
'bundles' => array(
'contributor' => array(
'label' => t('Contributor'),
),
),
'load hook' => 'biblio_contributor_load',
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'biblio',
'access callback' => 'biblio_entity_access',
'inline entity form' => array(
'controller' => 'BiblioContributorInlineEntityFormController',
),
);
return $return;
}
/**
* Get the bundles of the biblio.
*
* @param string $type
* Optional; A specific type name.
* @return array
* List of biblio types.
*/
function biblio_types($type = NULL) {
$results = db_select('biblio_type')
->fields('biblio_type')
->execute()
->fetchAllAssoc('type');
if (!empty($type)) {
return isset($results[$type]) ? $results[$type] : array();
}
return $results;
}
/**
* Save biblio type.
*
* @todo: When changing the machine name create a batch api that will update
* the biblio entries to the new bundle machine name.
*
* @param object $biblio_type
* The values of a biblio type.
*/
function biblio_type_save($biblio_type) {
if (biblio_types($biblio_type->type)) {
db_update('biblio_type')
->fields(array(
'name' => $biblio_type->name,
'description' => $biblio_type->description,
)
)
->condition('type', $biblio_type->type)
->execute();
}
else {
db_insert('biblio_type')
->fields((array)$biblio_type)
->execute();
// Attach the contributors field collection to the new biblio bundle.
biblio_create_field('contributor_field_collection', 'biblio', $biblio_type->type);
biblio_create_field('biblio_contributor_role', 'field_collection_item', 'contributor_field_collection');
biblio_create_field('biblio_contributor', 'field_collection_item', 'contributor_field_collection');
}
}
/**
* Delete a biblio bundle.
*
* @param string $type
* The value of a biblio type.
*/
function biblio_type_delete($type) {
$query = new entityFieldQuery();
$number = $query
->entityCondition('entity_type', 'biblio')
->propertyCondition('type', $type)
->count()
->execute();
if ($number) {
throw new Exception(t("You cannot delete the bundle @type because there are biblio entries of this bundle."));
}
db_delete('biblio_type')
->condition('type', $type)
->execute();
}
/**
* Access callback for the entity API.
*/
function biblio_entity_access($op, $type = NULL, $account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer biblio', $account)
|| user_access('create biblio', $account);
case 'view':
return user_access('administer biblio', $account)
|| user_access('access biblio content', $account);
case 'delete':
case 'edit':
return user_access('administer biblio')
|| user_access('edit all biblio entries');
}
}
/**
* Create a biblio entity object
*
* @param $type
* The publication type of the biblio to be created (bundle)
* @param array $values
* An associative array of any additional values to be set when creating this
* entity. These values will be carried throughout the biblio object's life.
* Example: $values['type'] => 'book';
* @return object
* The biblio object, with default values.
*/
function biblio_create($type, $values = array()) {
if (empty($account)) {
global $user;
$account = clone $user;
}
$values['type'] = $type;
$values['uid'] = $account->uid;
$values['title'] = '';
$values += array(
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
$values['type'] = $type;
return entity_create('biblio', $values);
}
/**
* Load a biblio object from the database.
*
* @param string $bid
* The biblio ID.
*
* @return object
* A fully-populated node object.
*/
function biblio_load($bid) {
return entity_load_single('biblio', $bid);
}
/**
* Load biblio entities from the database.
*
* This function should be used whenever you need to load more than one biblio
* from the database. biblios are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @see entity_load()
*
* @param array $bids
* An array of biblio IDs.
* @param bool $reset
* Whether to reset the internal entity_load cache.
*
* @return
* An array of biblio objects indexed by bid.
*/
function biblio_load_multiple($bids = array(), $reset = FALSE) {
return entity_load('biblio', $bids, array(), $reset);
}
/**
* Create a contributor entity object.
*
* @param string $name
* The name of the contributor. If given, this function will parse out the
* author name and automatically fill in any associated fields (first name,
* last name, initials, etc.) and the type
* @param array $values
*
* @return object
* The contributor entity object
*/
function biblio_contributor_create($values = array()) {
global $language;
$values['type'] = 'biblio_contributor';
$values += array(
'language' => $language->language,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'prefix' => '',
'lastname' => '',
'initials' => '',
'suffix' => '',
'firstname' => '',
);
return entity_create('biblio_contributor', $values);
}
/**
* Load a contributor.
*/
function biblio_contributor_load($cid) {
return entity_load_single('biblio_contributor', $cid);
}
/**
* Load multiple contributors based on certain conditions.
*/
function biblio_contributor_load_multiple($cids = array()) {
return entity_load('biblio_contributor', $cids);
}
/**
* Attach fields to bundles.
*
* @param $bundles
* Array with the bundles to process, or empty array to use all Biblio bundles.
*/
function biblio_create_fields_by_bundle($bundles = array(), $clear_cache = TRUE) {
$entity_info = entity_get_info('biblio');
$bundles = $bundles ? $bundles : array_keys($entity_info['bundles']);
$fields_info = biblio_fields_info();
foreach ($bundles as $bundle) {
foreach ($fields_info as $field_name => $field_info) {
if (in_array($field_name, array('biblio_contributor_role', 'biblio_contributor'))) {
// @todo: Add better way to distniush fields that don't need to be
// attached to the Biblio entity.
continue;
}
if (!empty($field_info['bundles_info']) && isset($field_info['bundles_info'][$bundle])) {
// Allow per bundle settings to override the instance's default settings.
$field_info['instance'] = drupal_array_merge_deep($field_info['instance'], $field_info['bundles_info'][$bundle]);
biblio_create_field($field_name, 'biblio', $bundle, $field_info, $clear_cache);
}
elseif (!isset($field_info['bundles_info'])) {
biblio_create_field($field_name, 'biblio', $bundle, $field_info, $clear_cache);
}
}
}
}
/**
* Create a biblio field in a bundle.
*
* @param string $field_name
* The field name
* @param string $entity_type
* The entity type
* @param string $bundle
* The bundle name.
* @param array $biblio_field
* Optional; Array with field definitions, to allow easier overriding by the
* caller. If empty, function will get the field info by calling
* biblio_fields_info() with the field name.
*/
function biblio_create_field($field_name, $entity_type, $bundle, $biblio_field = array(), $clear_cache = TRUE) {
if (!$biblio_field && !$biblio_field = biblio_fields_info($field_name)) {
return;
}
$field = field_info_field($field_name);
// Allow overriding the field name.
$biblio_field['field']['field_name'] = $field_name;
if (empty($field)) {
field_create_field($biblio_field['field']);
}
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (empty($instance)) {
$instance = $biblio_field['instance'];
$instance += array(
'field_name' => $field_name,
'bundle' => $bundle,
'entity_type' => $entity_type,
);
field_create_instance($instance);
if ($clear_cache) {
field_cache_clear();
entity_property_info_cache_clear();
}
}
}
/**
* Get the field info.
*
* @param string $field_name
* The field name.
*
* @return array
* An array with the field and instance definitions, or FALSE if not found.
*/
function biblio_fields_info($field_name = NULL) {
$return = &drupal_static(__FUNCTION__, array());
if (empty($return)) {
foreach (module_implements('biblio_fields_info') as $module) {
if ($fields = module_invoke($module, 'biblio_fields_info')) {
foreach ($fields as $key => $field) {
// Add default values.
$field += array(
'entity type' => array(),
'multiple' => FALSE,
'description' => '',
);
// Add the module information.
$return[$key] = array_merge($field, array('module' => $module));
}
}
}
// Allow other modules to alter the field info.
drupal_alter('biblio_fields_info', $return);
}
if (!empty($field_name)) {
return !empty($return[$field_name]) ? $return[$field_name] : FALSE;
}
return $return;
}
/**
* Create new Biblio bundles.
*
* @todo: Make the bundles configurable?
*/
function biblio_add_publication_types() {
foreach (biblio_get_types_info() as $values) {
$name = $values['name'];
$row = new stdClass();
$row->type = strtolower(str_replace(' ', '_', $name));
$row->name = $name;
$row->description = $values['description'];
biblio_type_save($row);
}
}
function biblio_get_types_info($type = NULL) {
$return = &drupal_static(__FUNCTION__, array());
if (empty($return)) {
foreach (module_implements('biblio_types_info') as $module) {
if ($types = module_invoke($module, 'biblio_types_info')) {
foreach ($types as $key => $value) {
// Add default values.
$value += array(
'description' => '',
'style_info' => array(),
);
// Add the module information.
$return[$key] = array_merge($value, array('module' => $module));
}
}
}
// Allow other modules to alter the field info.
drupal_alter('biblio_types_info', $return);
}
if (!empty($type)) {
return !empty($return[$type]) ? $return[$type] : FALSE;
}
return $return;
}
/**
* Implements hook_views_pre_render().
*
* Replace publication year dummy value (9999) with "In Press".
* We have added this dummy value, so it would be easier to group publications
* by In Press and than by the real publication year.
*/
function biblio_views_pre_render(&$view) {
if ($view->name != 'biblio_year') {
return;
}
foreach ($view->result as $key => $value) {
if (empty($value->field_biblio_year[0]['rendered']) || $value->field_biblio_year[0]['rendered']['#markup'] != '9999') {
continue;
}
$view->result[$key]->field_biblio_year[0]['rendered']['#markup'] = t('In Press');
}
}