View source
<?php
define('BIBLIO_VERSION', '7.x-2.x-dev');
function _biblio_get_auth_types($auth_category, $biblio_type) {
static $auth_types = array();
if (empty($auth_types)) {
$db_res = db_query("SELECT * FROM {biblio_contributor_type}");
foreach ($db_res as $row) {
$auth_types[$row->auth_category][$row->biblio_type][] = $row->auth_type;
}
}
$result = isset($auth_types[$auth_category][$biblio_type]) ? $auth_types[$auth_category][$biblio_type] : @$auth_types[$auth_category][0];
return $result;
}
function _biblio_get_auth_type($auth_category, $biblio_type) {
$result = (array) _biblio_get_auth_types($auth_category, $biblio_type);
return empty($result) ? NULL : current($result);
}
function _biblio_localize_fields(&$fields) {
if (module_exists('i18nstrings')) {
foreach ($fields as $key => $row) {
$fields[$key]['title'] = tt("biblio:field:{$row['ftdid']}:title", $fields[$key]['title']);
$fields[$key]['hint'] = tt("biblio:field:{$row['ftdid']}:hint", $fields[$key]['hint']);
}
}
}
function _biblio_localize_type($publication_type, $field = 'name') {
if (module_exists('i18nstrings')) {
return tt("biblio:type:{$field}", $publication_type);
}
return $publication_type;
}
function biblio_locale($op = 'groups', $group = NULL) {
switch ($op) {
case 'groups':
return array(
'biblio' => t('Biblio'),
);
case 'refresh':
if ($group == 'biblio') {
biblio_locale_refresh_fields();
biblio_locale_refresh_types();
}
break;
}
}
function biblio_locale_refresh_fields($tid = NULL) {
if (module_exists('i18nstrings')) {
if (isset($tid)) {
$result = db_query('SELECT d.* FROM {biblio_field_type} b INNER JOIN {biblio_field_type_data} d ON b.ftdid = d.ftdid WHERE tid = :tid', array(
':tid' => $tid,
));
}
else {
$result = db_query('SELECT * FROM {biblio_field_type_data}');
}
foreach ($result as $row) {
tt("biblio:field:{$row->ftdid}:title", $row->title, NULL, TRUE);
tt("biblio:field:{$row->ftdid}:hint", $row->hint, NULL, TRUE);
}
}
}
function biblio_locale_refresh_types($tid = NULL) {
if (module_exists('i18nstrings')) {
if (isset($tid)) {
$result = db_query('SELECT * FROM {biblio_types} WHERE tid = :tid', array(
':tid',
$tid,
));
}
else {
$result = db_query('SELECT * FROM {biblio_types} WHERE tid > 0');
}
foreach ($result as $row) {
tt("biblio:type:{$row->tid}:name", $row->name, NULL, TRUE);
tt("biblio:type:{$row->tid}:description", $row->description, NULL, TRUE);
}
}
}
function biblio_init() {
global $user, $conf;
drupal_add_css(drupal_get_path('module', 'biblio') . '/biblio.css');
if ($user->uid === 0) {
$base = variable_get('biblio_base', 'biblio');
if (drupal_match_path($_GET['q'], "{$base}\n{$base}/*")) {
$conf['cache'] = FALSE;
}
}
}
function biblio_cron() {
require_once drupal_get_path('module', 'biblio') . '/includes/biblio.contributors.inc';
require_once drupal_get_path('module', 'biblio') . '/includes/biblio.keywords.inc';
$interval = variable_get('biblio_orphan_clean_interval', 24 * 60 * 60);
if (time() >= variable_get('biblio_orphan_clean_next_execution', 0)) {
biblio_delete_orphan_authors();
biblio_delete_orphan_keywords();
variable_set('biblio_orphan_clean_next_execution', time() + $interval);
}
}
function biblio_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'biblio');
return array(
'biblio_alpha_line' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'type' => 'author',
'current' => NULL,
'path' => NULL,
),
),
'biblio_admin_keyword_edit_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_author_types_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_type_mapper_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_io_mapper_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_io_mapper_add_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_field_mapper_form' => array(
'file' => '/includes/biblio.admin.inc',
'render element' => 'form',
),
'biblio_admin_types_edit_form' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_admin_types_form' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_field_tab' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_admin_author_edit_form' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_openurl' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'openURL',
),
),
'biblio_style' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'biblio' => NULL,
'style_name' => 'classic',
),
),
'biblio_long' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node' => NULL,
'base' => 'biblio',
'style_name' => 'classic',
),
),
'biblio_tabular' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node' => NULL,
'base' => 'biblio',
'teaser' => FALSE,
),
),
'biblio_entry' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node',
'style_name' => 'classic',
),
),
'biblio_format_authors' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'contributors' => NULL,
'options' => array(),
),
),
'biblio_page_number' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'orig_page_info' => NULL,
'page_range_delim' => "-",
'single_page_prefix' => '',
'page_range_prefix' => '',
'total_pages_prefix' => '',
'single_page_suffix' => '',
'page_range_suffix' => '',
'total_pages_suffix' => '',
'shorten_page_range_end' => FALSE,
),
),
'biblio_author_link' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'author',
),
),
'biblio_filters' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'form_filter' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_export_links' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node' => NULL,
'filter' => array(),
),
),
'biblio_download_links' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node',
),
),
'google_scholar_link' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'node',
),
),
'biblio_contributors' => array(
'file' => '/includes/biblio.theme.inc',
'render element' => 'form',
),
'biblio_admin_overview' => array(
'file' => '/includes/biblio.theme.inc',
'variables' => array(
'name' => NULL,
'type' => NULL,
'description' => NULL,
),
),
);
}
function biblio_autocomplete($field, $string = '') {
$matches = array();
switch ($field) {
case 'contributor':
case 'a':
$result = db_query_range("SELECT biblio_contributor_name_value FROM {field_data_biblio_contributor_name} WHERE LOWER(biblio_contributor_name_value) LIKE LOWER(:name) OR LOWER(biblio_contributor_name_value) LIKE LOWER(:firstname) ORDER BY biblio_contributor_name_value ASC ", 0, 10, array(
':name' => $string . '%',
':firstname' => $string . '%',
));
foreach ($result as $data) {
$matches[$data->biblio_contributor_name_value] = check_plain($data->biblio_contributor_name_value);
}
break;
case 'biblio_keywords':
case 'k':
$sep = check_plain(variable_get('biblio_keyword_sep', ','));
$sep_pos = strrpos($string, $sep);
$start = trim(drupal_substr($string, 0, $sep_pos));
$end_sep = $sep_pos ? $sep_pos + 1 : $sep_pos;
$end = trim(drupal_substr($string, $end_sep)) . '%';
$result = db_query_range("SELECT * FROM {biblio_keyword_data} WHERE LOWER(word) LIKE LOWER(:end) ORDER BY word ASC ", 0, 10, array(
':end' => $end,
));
foreach ($result as $data) {
$keywords = $sep_pos ? $start . ', ' . check_plain($data->word) : check_plain($data->word);
$matches[$keywords] = $keywords;
}
break;
default:
$field = drupal_strtolower($field);
$string = '%' . drupal_strtolower($string) . '%';
$query = db_select('biblio', 'b')
->fields('b', array(
$field,
))
->condition($field, $string, 'LIKE')
->orderBy($field, 'ASC')
->range(0, 10);
$result = $query
->execute();
foreach ($result as $data) {
$matches[$data->{$field}] = check_plain($data->{$field});
}
}
print drupal_json_encode($matches);
exit;
}
function biblio_help_page() {
$base = variable_get('biblio_base', 'biblio');
$text = "<h3>" . t('General:') . "</h3>";
$text .= "<p>" . t('By default, the !url page will list all of the entries in the database sorted by Year in descending order. If you wish to sort by "Title" or "Type", you may do so by clicking on the appropriate links at the top of the page. To reverse the sort order, simply click the link a second time.', array(
'!url' => l('', $base),
)) . "</p>";
$text .= "<h3>" . t('Filtering Search Results:') . "</h3>";
$text .= "<p>" . t('If you wish to filter the results, click on the "Filter" tab at the top of the page. To add a filter, click the radio button to the left of the filter type you wish to apply, then select the filter criteria from the drop down list on the right, then click the filter button.') . "</p>";
$text .= "<p>" . t('It is possible to create complex filters by returning to the <i>Filter</i> tab and adding additional filters. Simply follow the steps outlined above and press the "Refine" button.') . "</p>";
$text .= "<p>" . t('All filters can be removed by clicking the <i>Clear All Filters</i> link at the top of the result page, or on the <i>Filter</i> tab they can be removed one at a time using the <i>Undo</i> button, or you can remove them all using the <i>Clear All</i> button.') . "</p>";
$text .= "<p>" . t('You may also construct URLs which filter. For example, /biblio/year/2005 will show all of the entries for 2005. /biblio/year/2005/author/smith will show all of entries from 2005 for smith.') . "</p>";
$text .= "<h3>" . t('Exporting Search Results:') . "</h3>";
$text .= "<p>" . t('Assuming this option has been enabled by the administrator, you can export search results directly into EndNote. The link at the top of the result page will export all of the search results, and the links on individual entries will export the information related to that single entry.') . "</p>";
$text .= "<p>" . t('The information is exported in EndNote "Tagged" format similar to this...') . "<pre>" . t('
%0 Book
%A John Smith
%D 1959
%T The Works of John Smith
...') . '</pre></p>';
$text .= "<p>" . t('Clicking on one of the export links should cause your browser to ask you whether you want to Open, or Save To Disk, the file endnote.enw. If you choose to open it, Endnote should start and ask you which library you would like store the results in. Alternatively, you can save the file to disk and manually import it into EndNote.') . "</p>";
return $text;
}
function biblio_help($path, $arg) {
switch ($path) {
case 'admin/help#biblio':
return biblio_help_page();
case 'admin/structure/biblio':
return t("Here, you can manage the field and display settings for each of Biblio's publication types, similar to admin/structure/types. If a <small>(<span class=\"admin-disabled\">Missing Field Instances</span>)</small> label appears next to a publication type, don't panic. It simply means that the default Biblio field instances have not been created yet for that publication type. You can create the default field instances by navigating to the " . l('Biblio Add page', 'admin/content/biblio/add') . " and selecting the publication type for which to create field instances. (note that it may take a while for field instance creation to execute.)");
case 'admin/modules#description':
return t('Manages a list of scholarly papers on your site');
case 'node/add#biblio':
return t('This allows you to add a bibliographic entry to the database');
case 'admin/content/biblio/add':
return t('Create a new Biblio from scratch, or input a record from a file if you have a Biblio sub-module enabled. <i>Please note</i> that the first time you select a particular publication type, it will take a few seconds to build the field instances. (It won\'t happen more than once per publication type)');
}
}
function biblio_node_access($node, $op, $account) {
if (is_string($node)) {
return NODE_ACCESS_IGNORE;
}
if ($node->type != 'biblio') {
return NODE_ACCESS_IGNORE;
}
switch ($op) {
case 'view':
if (variable_get('biblio_view_only_own', 0) && $account->uid != $node->uid || !user_access('access biblio content')) {
return NODE_ACCESS_DENY;
}
break;
case 'update':
case 'delete':
if (user_access('edit by all biblio authors') && isset($node->biblio_contributors) && is_array($node->biblio_contributors)) {
foreach ($node->biblio_contributors as $key => $author) {
if (isset($author['drupal_uid']) && $author['drupal_uid'] == $account->uid || isset($account->data['biblio_contributor_id']) && $author['cid'] == $account->data['biblio_contributor_id']) {
return NODE_ACCESS_ALLOW;
}
}
}
break;
default:
}
return NODE_ACCESS_IGNORE;
}
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;
}
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' => 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'),
),
'delete biblios' => array(
'title' => t('Delete Biblios'),
'description' => t('Allows the user to delete any 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'),
),
'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'),
),
'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)'),
),
);
}
function biblio_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$account = $form['#user'];
include_once drupal_get_path('module', 'biblio') . '/includes/biblio.admin.inc';
$show_form = variable_get('biblio_show_user_profile_form', '1') || variable_get('biblio_show_crossref_profile_form', '1') || variable_get('biblio_show_openurl_profile_form', '1');
$admin_show_form = $account->uid == 1 || user_access('administer users') && user_access('administer biblio') ? TRUE : FALSE;
if ($admin_show_form || $show_form) {
$form['biblio_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Biblio settings'),
'#weight' => 5,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
if ($admin_show_form || variable_get('biblio_show_user_profile_form', '1')) {
$form['biblio_fieldset'] += _biblio_get_user_profile_form($account);
}
if ($admin_show_form || variable_get('biblio_show_openurl_profile_form', '1')) {
$form['biblio_fieldset'] += _biblio_get_user_openurl_form($account);
}
if ($admin_show_form || variable_get('biblio_show_crossref_profile_form', '1')) {
$form['biblio_fieldset'] += _biblio_get_user_doi_form($account);
}
}
}
function biblio_forms() {
$forms['biblio_admin_author_types_form_new'] = array(
'callback' => 'biblio_admin_author_types_form',
);
$forms['biblio_admin_author_types_form_edit'] = array(
'callback' => 'biblio_admin_author_types_form',
);
return $forms;
}
function biblio_menu() {
global $user;
$items = array();
$base = variable_get('biblio_base', 'biblio');
$base_title = check_plain(variable_get('biblio_base_title', 'Biblio'));
$items[$base] = array(
'title' => $base_title,
'page callback' => 'biblio_page',
'access callback' => 'user_access',
'access arguments' => array(
'access biblio content',
),
'file' => '/includes/biblio.pages.inc',
);
$items[$base . "/%biblio"] = array(
'title callback' => 'biblio_page_title',
'title arguments' => array(
1,
),
'page callback' => 'biblio_page_view',
'page arguments' => array(
1,
),
'access arguments' => array(
'access biblio content',
),
'type' => MENU_CALLBACK,
);
$items[$base . "/%biblio/view"] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'access arguments' => array(
'access biblio content',
),
);
$items[$base . "/%biblio/edit"] = array(
'title' => 'Edit',
'page callback' => 'biblio_page_edit',
'page arguments' => array(
1,
),
'access arguments' => array(
'edit all biblio entries',
),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'includes/biblio.admin.inc',
);
$items[$base . "/%biblio/delete"] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_delete_confirm',
1,
),
'access arguments' => array(
'edit all biblio entries',
),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'includes/biblio.admin.inc',
);
$items[$base . "/contributor/%biblio_contributor"] = array(
'title callback' => 'entity_label',
'title arguments' => array(
'biblio_contributor',
2,
),
'page callback' => 'biblio_contributor_page_view',
'page arguments' => array(
2,
),
'access arguments' => array(
'access biblio content',
),
);
$items[$base . "/contributor/%biblio_contributor/view"] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'access arguments' => array(
'access biblio content',
),
);
$items[$base . "/contributor/%biblio_contributor/edit"] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_contributor_form',
2,
),
'access arguments' => array(
'edit all biblio entries',
),
'file' => 'includes/biblio.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items[$base . "/contributor/%biblio_contributor/delete"] = array(
'title' => 'Delete Contributor',
'title callback' => 'biblio_contributor_label',
'title arguments' => array(
2,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_contributor_delete_confirm',
2,
),
'access arguments' => array(
'edit all biblio entries',
),
);
$items[$base . "/import"] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_import_form',
),
'file' => '/includes/biblio.import.export.inc',
'access callback' => 'user_access',
'access arguments' => array(
'import from file',
),
'weight' => 10,
);
$items[$base . "/export"] = array(
'title' => '',
'page callback' => 'biblio_export',
'access callback' => 'user_access',
'access arguments' => array(
'show export links',
),
'file' => '/includes/biblio.import.export.inc',
'type' => MENU_CALLBACK,
);
$items[$base . "/pot"] = array(
'title' => '',
'page callback' => 'biblio_dump_db_data_for_pot',
'access callback' => 'user_access',
'access arguments' => array(
'export pot data',
),
'type' => MENU_CALLBACK,
);
$items['admin/content/biblio'] = array(
'title' => 'Biblio',
'description' => 'List and edit Biblio entries.
Use Biblio for scholarly content, such as journal papers and books.',
'page callback' => 'biblio_admin_view',
'page arguments' => array(
'biblio_admin',
'page',
),
'access arguments' => array(
'manage biblio content',
),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
$items["admin/content/biblio/biblio-list"] = array(
'title' => 'Biblios',
'description' => 'View, Edit, and Delete Biblios',
'access arguments' => array(
'manage biblio content',
),
'weight' => 0,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/content/biblio/add'] = array(
'title' => 'Add Biblio',
'access arguments' => array(
'create biblio',
),
'page callback' => 'biblio_add',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/content/biblio/contributor'] = array(
'title' => 'Contributors',
'page callback' => 'biblio_admin_view',
'page arguments' => array(
'biblio_admin_contributors',
'page',
),
'access arguments' => array(
'manage biblio content',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$items['admin/content/biblio/contributor/add'] = array(
'title' => 'Add Contributor',
'page callback' => 'biblio_contributor_add',
'access arguments' => array(
'create biblio',
),
'file' => 'includes/biblio.admin.inc',
);
$items["admin/structure/biblio"] = array(
'title' => 'Biblio',
'description' => 'Manage Fields and Dislplay for Biblios and Contributors',
'access arguments' => array(
'administer biblio',
),
'page callback' => 'biblio_overview_types',
'page arguments' => array(
'biblio',
),
'file' => 'includes/biblio.admin.inc',
'access arguments' => array(
'manage biblio structure',
),
);
$items["admin/structure/biblio/publication-types"] = array(
'title' => 'Publication Types',
'description' => 'Manage Biblio Publication Types',
'access arguments' => array(
'manage biblio structure',
),
'weight' => 0,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
foreach (biblio_types('biblio') as $type) {
$bundle = biblio_bundle_name($type->entity);
$type_url_str = str_replace('_', '-', $type->{$bundle});
$entity_url_str = str_replace('_', '-', $type->entity);
$url = 'admin/structure/biblio/publication-types/' . $type_url_str;
$items[$url] = array(
'title' => 'Edit ' . $type->name,
'title callback' => 'check_plain',
'page callback' => 'biblio_field_settings',
'file' => '/includes/biblio.admin.inc',
'page arguments' => array(
4,
),
'access arguments' => array(
'manage biblio structure',
),
);
}
$items["admin/structure/biblio/contributors"] = array(
'title' => 'Contributors',
'description' => 'Manage Biblio Contributor Fields/Display',
'page callback' => 'biblio_overview_types',
'page arguments' => array(
'biblio_contributor',
),
'file' => '/includes/biblio.admin.inc',
'access arguments' => array(
'manage biblio structure',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
foreach (biblio_types('biblio_contributor') as $type) {
$bundle = biblio_bundle_name($type->entity);
$type_url_str = str_replace('_', '-', $type->{$bundle});
$entity_url_str = str_replace('_', '-', $type->entity);
$url = 'admin/structure/biblio/contributors/' . $type_url_str;
$items[$url] = array(
'title' => 'Edit ' . $type->name,
'title callback' => 'check_plain',
'page callback' => 'biblio_field_settings',
'file' => '/includes/biblio.admin.inc',
'page arguments' => array(
4,
),
'access arguments' => array(
'manage biblio structure',
),
);
}
$items['admin/config/content/biblio'] = array(
'title' => 'Biblio settings',
'description' => 'Configure default behavior of the Biblio module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_admin_settings',
),
'access arguments' => array(
'administer biblio',
),
'file' => '/includes/biblio.admin.inc',
);
$items['admin/config/content/biblio/preferences'] = array(
'title' => 'Preferences',
'description' => 'Configure default behavior of the Biblio module.',
'access arguments' => array(
'administer biblio',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/content/biblio/iomap'] = array(
'title' => 'Import/Export Mapping',
'page callback' => 'biblio_admin_io_mapper_page',
'access arguments' => array(
'administer biblio',
),
'file' => '/includes/biblio.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/content/biblio/iomap/formats'] = array(
'title' => 'Import/Export Mapping',
'page callback' => 'biblio_admin_io_mapper_page',
'access arguments' => array(
'administer biblio',
),
'file' => '/includes/biblio.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -100,
);
$formats = module_invoke_all('biblio_mapper_options');
foreach ($formats as $key => $format) {
$items['admin/config/content/biblio/iomap/edit/' . $key] = array(
'title' => $format['title'],
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_admin_io_mapper_form',
6,
),
'access arguments' => array(
'administer biblio',
),
'file' => '/includes/biblio.admin.inc',
'tab_parent' => 'admin/config/content/biblio/iomap',
'type' => MENU_LOCAL_TASK,
'weight' => -1,
);
}
$items['admin/config/content/biblio/iomap/%/%/add'] = array(
'title' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'biblio_admin_io_mapper_add_form',
5,
6,
),
'access arguments' => array(
'administer biblio',
),
'tab_parent' => 'admin/config/content/biblio/iomap',
'file' => '/includes/biblio.admin.inc',
'type' => MENU_CALLBACK,
'weight' => -1,
);
$items['biblio/autocomplete'] = array(
'title' => 'Autocomplete ',
'page callback' => 'biblio_autocomplete',
'access callback' => 'user_access',
'access arguments' => array(
'access biblio content',
),
'type' => MENU_CALLBACK,
);
$items["user/%user/" . $base] = array(
'title' => 'Publications',
'page callback' => 'biblio_profile_page',
'page arguments' => array(
1,
'profile',
'no_filters',
),
'access callback' => '_biblio_profile_access',
'access arguments' => array(
1,
'profile',
),
'file' => '/includes/biblio.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items[$base . "/user/%"] = array(
'title' => 'My publications',
'page callback' => 'biblio_profile_page',
'page arguments' => array(
2,
),
'access callback' => '_biblio_profile_access',
'access arguments' => array(
2,
'menu',
),
'parent' => '',
'file' => '/includes/biblio.pages.inc',
);
$items[$base . "/help"] = array(
'title' => 'Help',
'page callback' => 'biblio_help_page',
'access callback' => 'user_access',
'access arguments' => array(
'access biblio content',
),
'type' => MENU_CALLBACK,
);
$items[$base . "/citekey"] = array(
'title' => '',
'page callback' => 'biblio_citekey_view',
'access arguments' => array(
'access biblio content',
),
'file' => '/includes/biblio.pages.inc',
'type' => MENU_CALLBACK,
);
$items[$base . "/recent/rss.xml"] = array(
'title' => 'RSS feed',
'page callback' => 'biblio_recent_feed',
'access callback' => 'biblio_access',
'access arguments' => array(
'rss',
),
'type' => MENU_CALLBACK,
);
biblio_menu_links();
return $items;
}
function biblio_menu_links() {
$result = db_select('menu_links', 'm')
->fields('m', array(
'menu_name',
'mlid',
'plid',
'link_path',
))
->condition('link_path', '%biblio%', 'like')
->execute();
$add_link_exists = FALSE;
$biblio_root_mlid = 0;
while ($row = $result
->fetchObject()) {
switch ($row->link_path) {
case 'biblio':
if ($row->menu_name == 'navigation') {
$biblio_root_mlid = $row->mlid;
}
break;
case 'admin/content/biblio/add':
if ($row->plid == $biblio_root_mlid && $row->menu_name == 'navigation') {
$add_link_exists = TRUE;
}
break;
}
}
if (!$add_link_exists) {
$add_link = array(
'link_path' => drupal_get_normal_path('admin/content/biblio/add'),
'menu_name' => 'navigation',
'plid' => $biblio_root_mlid,
'link_title' => t('Add a new Biblio'),
);
menu_link_save($add_link);
}
}
function biblio_filter_clear() {
$options = array();
$_SESSION['biblio_filter'] = array();
$base = variable_get('biblio_base', 'biblio');
if (isset($_GET['sort'])) {
$options['sort'] = $_GET['sort'];
}
if (isset($_GET['order'])) {
$options['order'] = $_GET['order'];
}
drupal_goto($base, $options);
}
function biblio_remove_brace($title_string) {
$matchpattern = '/\\{\\$(?:(?!\\$\\}).)*\\$\\}|(\\{[^}]*\\})/';
$output = preg_replace_callback($matchpattern, 'biblio_remove_brace_callback', $title_string);
return $output;
}
function biblio_remove_brace_callback($match) {
if (isset($match[1])) {
$braceless = str_replace('{', '', $match[1]);
$braceless = str_replace('}', '', $braceless);
return $braceless;
}
return $match[0];
}
function biblio_node_revision_delete($node) {
if ($node->type == 'biblio') {
db_delete('biblio')
->condition('vid', $node->vid)
->execute();
db_delete('biblio_contributor')
->condition(db_and()
->condition('nid', $node->nid)
->condition('vid', $node->vid))
->execute();
db_delete('biblio_keyword')
->condition(db_and()
->condition('nid', $node->nid)
->condition('vid', $node->vid))
->execute();
}
}
function biblio_node_insert($node) {
if ($node->type == 'biblio') {
if (variable_get('biblio_index', 0)) {
_node_index_node($node);
search_update_totals();
}
}
}
function biblio_node_update($node) {
if ($node->type == 'biblio') {
if (variable_get('biblio_index', 0)) {
$mynode = node_load($node->nid, NULL, TRUE);
_node_index_node($mynode);
search_update_totals();
}
}
}
function biblio_node_view($node, $view_mode) {
if ($node->type == 'biblio') {
switch ($view_mode) {
case 'full':
if (variable_get('biblio_hide_bibtex_braces', 0) && !empty($a4)) {
drupal_set_title(filter_xss($node->title, biblio_get_allowed_tags()));
}
case 'teaser':
$show_link = variable_get('biblio_lookup_links', array(
'google' => TRUE,
));
if (!empty($show_link['google'])) {
$node->content['links']['biblio_google_scholar'] = array(
'#links' => array(
theme('google_scholar_link', array(
'node' => $node,
)),
),
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
);
}
}
}
}
function biblio_query_node_access_alter(QueryAlterableInterface $query) {
global $user;
if (user_access('access biblio content', $user)) {
return;
}
$tables = $query
->getTables();
foreach ($tables as $alias => $table_info) {
if (!$table_info instanceof SelectQueryInterface) {
if ($table_info['table'] == 'node') {
$query
->condition($alias . '.type', 'biblio', '<>');
break;
}
}
}
}
function biblio_user_presave(&$edit, $account, $catagory) {
$keys = array_keys($edit);
foreach ($keys as $key) {
if (strpos($key, 'biblio_') !== FALSE && isset($edit[$key])) {
if (isset($account->data['biblio_id_change_count']) && $account->data['biblio_id_change_count'] > 2 && $key == 'biblio_contributor_id' && $edit[$key] != 0) {
$edit[$key] = 0;
}
$edit['data'][$key] = $edit[$key];
if ($key == 'biblio_contributor_id') {
if ($edit[$key] != 0 && $edit[$key] != $account->data[$key]) {
$edit['biblio_id_change_count']++;
}
db_update('biblio_contributor_data')
->condition('drupal_uid', $account->uid)
->fields(array(
'drupal_uid' => 0,
))
->execute();
db_update('biblio_contributor_data')
->condition('cid', $edit['biblio_contributor_id'])
->fields(array(
'drupal_uid' => $account->uid,
))
->execute();
}
}
}
}
function biblio_form($form, &$form_state, $biblio = NULL) {
global $user;
$fields = array();
$form['#id'] = 'biblio-form';
if (isset($form_state['biblio_imported'])) {
$biblio = $form_state['biblio_imported'];
}
$form['biblio_tabs'] = $tabs = array();
$publication_type = !empty($form_state['biblio_type']) ? $form_state['biblio_type'] : (isset($biblio->publication_type) ? $biblio->publication_type : '');
if (isset($_COOKIE['has_js']) && !$_COOKIE['has_js']) {
unset($form['biblio_next']['#attributes']);
}
$step_two = FALSE;
if (!empty($publication_type) && $publication_type != 'select_type' || isset($biblio)) {
$step_two = TRUE;
}
$param['options'] = array(
"enctype" => "multipart/form-data",
);
foreach (biblio_types() as $machine_name => $info) {
$type_options[$machine_name] = $info->name;
}
asort($type_options);
$options['select_type'] = t('Select Type...');
$options += $type_options;
if (!$step_two) {
$form['biblio_type'] = array(
'#type' => 'select',
'#title' => t('Publication Type'),
'#default_value' => $publication_type,
'#options' => $options,
'#description' => NULL,
'#weight' => -15,
'#attributes' => array(
'onchange' => 'document.getElementById(\'edit-biblio-next\').click();
document.getElementById(\'edit-biblio-next\').disabled="true"',
),
'#multiple' => FALSE,
'#required' => TRUE,
);
$form['biblio_next'] = array(
'#type' => 'submit',
'#value' => $step_two ? t('Change Publication Type') : t('Next'),
'#limit_validation_errors' => array(),
'#weight' => -10,
'#submit' => array(),
);
}
else {
module_load_include('inc', 'biblio', 'includes/biblio.fields');
if (biblio_field_instances_missing($publication_type)) {
biblio_add_field_instances('biblio', $publication_type);
}
if (!isset($biblio)) {
$biblio = biblio_create($publication_type);
}
$wrapper = biblio_wrapper($biblio);
drupal_set_title(t('Create @name', array(
'@name' => $options[$publication_type],
)), PASS_THROUGH);
$instance_info = field_info_instances('biblio', $biblio->publication_type);
$fielded_form = $form;
field_attach_form('biblio', $biblio, $fielded_form, $form_state);
$tabs = array(
'#type' => 'vertical_tabs',
'#weight' => 3,
);
$tabs += biblio_form_vtabs();
$tabs['contributors'] += biblio_contributor_widget($biblio, $form_state);
if (isset($form_state['biblio_imported']) && isset($fielded_form['biblio_keywords'])) {
$langcode = $fielded_form['biblio_keywords']['#language'];
$fielded_form['biblio_keywords'][$langcode]['#default_value'] = implode(', ', $biblio->biblio_keywords);
}
foreach ($fielded_form as $field => $data) {
$vtab = isset($instance_info[$field]['settings']['vtab']) ? $instance_info[$field]['settings']['vtab'] : FALSE;
if ($vtab !== FALSE) {
if ($vtab == 'none' || $vtab == 'None') {
$form[$field] = $data;
}
else {
$tabs[$vtab][$field] = $data;
}
}
else {
if (biblio_is_field_instance($field, 'biblio', $biblio->publication_type)) {
$form[$field] = $data;
}
}
}
$form_state['biblio_fields'] = $fields;
foreach (element_children($tabs) as $key) {
$tab_children = element_children($tabs[$key]);
if (empty($tab_children) && $key != 'biblio_full_text') {
unset($tabs[$key]);
}
}
$form['buttons'] = array();
$form['buttons']['#weight'] = 100;
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array(
'biblio_form_submit',
),
);
if (!empty($biblio->bid)) {
$form['buttons']['delete'] = array(
'#access' => user_access('delete biblios'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array(
'biblio_form_delete_submit',
),
);
}
$form_state['biblio'] = $biblio;
}
$form['#validate'] = array(
'biblio_form_validate',
);
$form['#cache'] = TRUE;
$form['biblio_tabs'] += $tabs;
biblio_hide_form_fields($form, $form_state);
return $form;
}
function biblio_form_vtabs() {
$vtabs = biblio_form_vtab_info();
foreach ($vtabs as $tab) {
$form[$tab['tab_id']] = array(
'#type' => 'fieldset',
'#group' => 'biblio_tabs',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t($tab['title']),
'#description' => '',
'#weight' => $tab['weight'],
);
}
return $form;
}
function biblio_form_vtab_info() {
return array(
array(
'tab_id' => 'contributors',
'weight' => 9,
'title' => 'Contributors',
'description' => 'Enter a single name per line using a format such as "Smith, John K" or "John K Smith" or "J.K. Smith"',
),
array(
'tab_id' => 'abstract',
'weight' => 10,
'title' => 'Abstract',
'description' => '',
),
array(
'tab_id' => 'full_text',
'weight' => 11,
'title' => 'Full text',
'description' => '',
),
array(
'tab_id' => 'publication',
'weight' => 12,
'title' => 'Publication',
'description' => '',
),
array(
'tab_id' => 'publisher',
'weight' => 13,
'title' => 'Publisher',
'description' => '',
),
array(
'tab_id' => 'identifiers',
'weight' => 14,
'title' => 'Identifiers',
'description' => '',
),
array(
'tab_id' => 'locators',
'weight' => 15,
'title' => 'Locators',
'description' => 'URL\'s etc',
),
array(
'tab_id' => 'keywords',
'weight' => 16,
'title' => 'Keywords',
'description' => '',
),
array(
'tab_id' => 'notes',
'weight' => 17,
'title' => 'Notes',
'description' => '',
),
array(
'tab_id' => 'alternate_titles',
'weight' => 18,
'title' => 'Alternate Titles',
'description' => '',
),
array(
'tab_id' => 'other',
'weight' => 19,
'title' => 'Other',
'description' => '',
),
);
}
function biblio_contributor_widget($biblio, &$form_state) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
$categories = biblio_contributor_categories();
$init_count = variable_get('biblio_init_auth_count', 4);
$contributor_count = 0;
$contributors = biblio_get_contributors($biblio);
if (isset($form_state['biblio_imported_contributors'])) {
foreach ($form_state['biblio_imported_contributors'] as $author_data) {
$contributors[] = $author_data;
}
}
$wrapper = array();
$wrapper['biblio_contributors'] = array(
'#tree' => TRUE,
'#weight' => 0,
'#theme' => 'biblio_contributors',
'#prefix' => '<div id="biblio-contributors-wrapper">',
'#suffix' => '</div>',
);
foreach ($contributors as $cid => $info) {
$wrapper['biblio_contributors'][] = array();
end($wrapper['biblio_contributors']);
$delta = key($wrapper['biblio_contributors']);
$wrapper['biblio_contributors'][$delta] = biblio_contributor_biblio_form($info, $biblio, $form_state);
$contributor_count++;
}
if (isset($form_state['biblio_contrib_count'])) {
$form_count = max(max($init_count, $contributor_count), $form_state['biblio_contrib_count']);
}
else {
$form_count = max($init_count, $contributor_count);
$form_state['biblio_contrib_count'] = $form_count;
}
if ($form_count > $contributor_count) {
$author = array();
for ($i = 0; $i < $form_count - $contributor_count; $i++) {
$wrapper['biblio_contributors'][] = biblio_contributor_biblio_form(array(), $biblio, $form_state);
}
}
$wrapper['add_more'] = array(
'#type' => 'submit',
'#value' => t('More contributors'),
'#description' => t("If there aren't enough boxes above, click here to add more."),
'#weight' => 0.1,
'#submit' => array(
'biblio_contributors_add_more',
),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'biblio_contributors_add_more_callback',
'wrapper' => 'biblio-contributors-wrapper',
),
);
return $wrapper;
}
function biblio_get_contributor_options() {
$options = array(
1 => t('Primary'),
2 => t('Secondary'),
3 => t('Tertiary'),
4 => t('Subsidiary'),
5 => t('Corporate/Institutional'),
);
return $options;
}
function biblio_contributor_biblio_form($contributor, $biblio, &$form_state) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
foreach (biblio_contributor_categories($biblio->publication_type) as $category => $info) {
$category_options[$category] = $info['label'];
}
$form = array(
'#tree' => TRUE,
);
$form['name'] = array(
'#type' => 'textfield',
'#autocomplete_path' => 'biblio/autocomplete/contributor',
'#default_value' => isset($contributor['name']) ? $contributor['name'] : '',
);
$form['category'] = array(
'#type' => 'select',
'#default_value' => isset($contributor['category']) ? $contributor['category'] : '',
'#options' => $category_options,
'#multiple' => FALSE,
);
$form['cid'] = array(
'#type' => 'hidden',
'#default_value' => isset($contributor['cid']) ? $contributor['cid'] : '',
);
$form['rank'] = array(
'#type' => 'textfield',
'#size' => 6,
'#default_value' => isset($contributor['rank']) ? $contributor['rank'] : '',
);
return $form;
}
function biblio_contributors_add_more_callback($form, &$form_state) {
return $form['biblio_tabs']['contributors']['biblio_contributors'];
}
function biblio_contributors_add_more($form, &$form_state) {
$form_state['biblio_contrib_count'] += variable_get('biblio_contrib_fields_delta', 2);
$form_state['rebuild'] = TRUE;
}
function biblio_hide_form_fields(&$form, &$form_state) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
$elements_to_hide = array();
$biblio = isset($form_state['biblio']) ? $form_state['biblio'] : FALSE;
if ($biblio) {
$instances = field_info_instances('biblio', $biblio->publication_type);
foreach (biblio_contributor_categories() as $category => $info) {
$vtab = $instances[$info['field']]['settings']['vtab'];
$elements_to_hide[$vtab][] = $info['field'];
}
}
foreach ($elements_to_hide as $vtab => $fields) {
foreach ($fields as $field) {
if (isset($form['biblio_tabs'][$vtab][$field])) {
$form['biblio_tabs'][$vtab][$field]['#type'] = 'hidden';
}
else {
if (isset($form[$field])) {
$form[$field]['#type'] = 'hidden';
}
}
}
}
}
function biblio_form_node_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'biblio_node_form') {
$form['#pre_render'][] = 'biblio_node_form_pre_render';
if (!isset($form['biblio_tabs']['biblio_full_text']) && isset($form['body'])) {
unset($form['body']);
}
if (empty($form_state['biblio_type']) && empty($form['vid']['#value'])) {
foreach (element_children($form) as $form_element) {
if (strstr($form_element, 'biblio_')) {
continue;
}
if (strstr($form_element, 'form_')) {
continue;
}
if (isset($form[$form_element]['#type']) && $form[$form_element]['#type'] != 'value') {
$form[$form_element]['#access'] = FALSE;
}
}
}
}
}
function biblio_node_form_pre_render($form) {
if (isset($form['biblio_tabs']['biblio_full_text']) && isset($form['body'])) {
$form['biblio_tabs']['biblio_full_text']['body'] = $form['body'];
unset($form['body']);
}
return $form;
}
function biblio_node_form_validate($form, &$form_state) {
if ($form_state['triggering_element']['#value'] == t('Next') || $form_state['triggering_element']['#value'] == t('Change Publication Type')) {
$form_state['rebuild'] = TRUE;
$form_state['biblio_type'] = $form_state['values']['biblio_type'];
if ($form_state['values']['biblio_type'] == 0) {
form_set_error('biblio_type', t('Please select a publication type.'));
}
return;
}
$format = new stdClass();
foreach (_biblio_get_formatted_fields() as $field) {
if (isset($form_state['values'][$field]['format'])) {
$format->format = $form_state['values'][$field]['format'];
if (!filter_access($format)) {
form_set_error($field, t('You do not have access to the !format format', array(
'!format' => $format->format,
)));
}
}
}
if (isset($form_state['values']['biblio_keywords'])) {
require_once drupal_get_path('module', 'biblio') . '/includes/biblio.keywords.inc';
if (!is_array($form_state['values']['biblio_keywords'])) {
$form_state['values']['biblio_keywords'] = biblio_explode_keywords($form_state['values']['biblio_keywords']);
}
foreach ($form_state['values']['biblio_keywords'] as $keyword) {
if (strlen($keyword) > 255) {
form_set_error('biblio_keywords', t('No single keyword can be greater than 255 characters in length, the word: @kw exceeds this length', array(
'@kw' => $keyword,
)));
}
}
}
if (isset($form_state['biblio_fields'])) {
$vtabs = biblio_node_form_vtab_info();
foreach ($vtabs as $tab) {
$tabs[$tab['tab_id']] = $tab['title'];
}
foreach ($form_state['biblio_fields'] as $key => $fld) {
if ($fld['required'] && isset($form_state['values'][$key]) && empty($form_state['values'][$key])) {
$tab = $tabs[$fld['vtab']];
form_set_error($key, t('The <b><u>@fld</u></b> field (on the <b><i>@tab</i></b> tab) is required', array(
'@fld' => $fld['title'],
'@tab' => $tab,
)));
}
}
}
}
function _biblio_numeric_year($year) {
if (!is_numeric($year)) {
if (drupal_strtoupper($year) == drupal_strtoupper(t("In Press"))) {
return 9998;
}
if (drupal_strtoupper($year) == drupal_strtoupper(t("Submitted"))) {
return 9999;
}
return 0;
}
else {
return $year;
}
}
function _biblio_text_year($year) {
if ($year == 9998) {
return check_plain(variable_get('biblio_inpress_year_text', t('In Press')));
}
if ($year == 9999) {
return check_plain(variable_get('biblio_no_year_text', t('Submitted')));
}
return $year;
}
function _biblio_get_formatted_fields() {
$fields =& drupal_static(__FUNCTION__);
if (!isset($fields)) {
$query = db_select('biblio_fields', 'bf');
$result = $query
->fields('bf', array(
'name',
))
->condition('type', 'text_format')
->execute();
foreach ($result as $field) {
$fields[] = $field->name;
}
}
return (array) $fields;
}
function _biblio_prepare_submit(&$biblio) {
module_load_include('inc', 'biblio', 'includes/biblio.util');
$wrapper = biblio_wrapper($biblio);
if (!isset($biblio->publication_type)) {
$biblio->publication_type = 'miscellaneous';
}
$title = $wrapper->biblio_title
->value();
$biblio->biblio_sort_title = biblio_normalize_title($title);
if (!isset($biblio->biblio_year)) {
$wrapper->biblio_year
->set(9999);
}
$wrapper->biblio_year
->set(_biblio_numeric_year($wrapper->biblio_year
->value()));
if (variable_get('biblio_auto_citekey', 1) && empty($biblio->biblio_citekey)) {
$biblio->biblio_citekey = biblio_citekey_generate($biblio);
}
foreach (_biblio_get_formatted_fields() as $field) {
if (isset($biblio->{$field}) && is_array($biblio->{$field})) {
$biblio->biblio_formats[$field] = $biblio->{$field}['format'];
$biblio->{$field} = $biblio->{$field}['value'];
}
}
$wrapper->biblio_coins
->set(biblio_coins($biblio));
}
function biblio_update($node) {
module_load_include('inc', 'biblio', 'includes/biblio.util');
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
module_load_include('inc', 'biblio', 'includes/biblio.keywords');
_biblio_prepare_submit($node);
biblio_update_contributors($node);
biblio_update_keywords($node);
$node->biblio_coins = biblio_coins($node);
if (!empty($node->revision)) {
drupal_write_record('biblio', $node);
}
else {
drupal_write_record('biblio', $node, 'vid');
}
}
function biblio_load_old($nodes) {
module_load_include('inc', 'biblio', 'includes/biblio.util');
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
module_load_include('inc', 'biblio', 'includes/biblio.keywords');
$vids = array();
foreach ($nodes as $nid => $node) {
$vids[] = $node->vid;
}
$result = db_query('SELECT b.*, bt.name as biblio_type_name
FROM {biblio} b
LEFT JOIN {biblio_types} bt on b.biblio_type = bt.tid
WHERE b.vid IN (:vids)', array(
':vids' => $vids,
), array(
'fetch' => PDO::FETCH_ASSOC,
));
$contributors = biblio_load_contributors_multiple($vids);
$keywords = biblio_load_keywords_multiple($vids);
foreach ($result as $record) {
if ((isset($record['biblio_url']) || isset($record['biblio_accession_number'])) && variable_get('biblio_fix_isi_links', 0)) {
biblio_fix_isi_links($record);
}
foreach ($record as $key => $value) {
$nodes[$record['nid']]->{$key} = $value;
}
$nodes[$record['nid']]->biblio_year = _biblio_text_year($record['biblio_year']);
$nodes[$record['nid']]->biblio_contributors = isset($contributors[$record['vid']]) ? $contributors[$record['vid']] : array();
$nodes[$record['nid']]->biblio_keywords = isset($keywords[$record['vid']]) ? $keywords[$record['vid']] : array();
if (empty($record['biblio_coins'])) {
$nodes[$record['nid']]->biblio_coins = biblio_coins($nodes[$record['nid']]);
}
if ($record['biblio_formats'] != NULL) {
$nodes[$record['nid']]->biblio_formats = unserialize($record['biblio_formats']);
}
}
}
function biblio_citekey_generate($biblio) {
$php = check_plain(variable_get('biblio_citekey_phpcode', ''));
if (empty($php)) {
$prefix = variable_get('biblio_citekey_prefix', '');
$primary_field = variable_get('biblio_citekey_field1', 'nid');
$secondary_field = variable_get('biblio_citekey_field2', 'nid');
$citekey = !empty($biblio->{$primary_field}) ? $biblio->{$primary_field} : (!empty($biblio->{$secondary_field}) ? $biblio->{$secondary_field} : $biblio->bid);
return check_plain($prefix . $citekey);
}
else {
ob_start();
$return = eval($php);
ob_end_clean();
return check_plain(strip_tags((string) $return));
}
}
function biblio_block_info() {
$blocks['recent'] = array(
'info' => t('Recent publications'),
);
return $blocks;
}
function biblio_block_configure($delta = '') {
$form = array();
$form['block'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#title' => t('Options'),
'#description' => '',
);
$form['block']['biblio_rowsperblock'] = array(
'#type' => 'textfield',
'#title' => t('Number of results in the "Recent Publications" block'),
'#default_value' => variable_get('biblio_rowsperblock', 4),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('This sets the number of results that will be displayed in the "New Publications" block.'),
);
$form['block']['biblio_block_order'] = array(
'#type' => 'radios',
'#title' => t('Order by'),
'#default_value' => variable_get('biblio_block_order', 'n.created'),
'#options' => array(
'n.created' => t('Date Created'),
'b.biblio_year' => t('Year Published'),
),
);
return $form;
}
function biblio_block_save($delta = '', $edit = array()) {
if ($delta == 'recent') {
variable_set('biblio_rowsperblock', $edit['biblio_rowsperblock']);
variable_set('biblio_block_order', $edit['biblio_block_order']);
}
}
function biblio_block_view($delta = '') {
switch ($delta) {
case 'recent':
$num_in_block = variable_get('biblio_rowsperblock', 4);
$block_order = variable_get('biblio_block_order', 'n.created');
$query = db_select('node', 'n')
->fields('n', array(
'nid',
'title',
))
->condition(db_and()
->condition('n.type', 'biblio')
->condition('n.status', 1))
->orderBy($block_order, 'DESC')
->range(0, $num_in_block);
if ($block_order == 'b.biblio_year') {
$query
->leftJoin('biblio', 'b', 'n.vid=b.vid');
}
$result = $query
->execute();
$base = variable_get('biblio_base', 'biblio');
$block['subject'] = t('Recent Publications');
$block['content'] = '<div class="item-list"><ul>';
$options['html'] = TRUE;
foreach ($result as $pub) {
$block['content'] .= '<li >' . l(filter_xss($pub->title, biblio_get_allowed_tags()), "node/{$pub->nid}", $options) . '</li>';
}
$block['content'] .= '</ul>';
if (variable_get('biblio_rss', 0)) {
$block['content'] .= theme('feed_icon', array(
'url' => url("{$base}/recent/rss.xml", array(
'absolute' => TRUE,
)),
'title' => t('Recent Publications'),
));
}
$block['content'] .= l(t('More...'), $base);
$block['content'] .= '</div>';
return $block;
break;
}
}
function biblio_recent_feed() {
$numberInFeed = variable_get('biblio_rss_number_of_entries', 10);
$query = db_select('node', 'n')
->fields('n', array(
'nid',
'title',
))
->condition(db_and()
->condition('n.type', 'biblio')
->condition('n.status', 1))
->orderBy('n.created', 'DESC')
->range(0, $numberInFeed);
$result = $query
->execute();
$siteName = variable_get('site_name', 'Drupal');
$base = variable_get('biblio_base', 'biblio');
$channel['title'] = $siteName . ' - ' . t("Recently Added Publications");
$channel['link'] = url($base, array(
'absolute' => TRUE,
));
$channel['description'] = t("This feed lists the %num most recently added publications on %site", array(
'%num' => $numberInFeed,
'%site' => $siteName,
));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
node_feed($nids, $channel);
}
function biblio_filter_feed($rss_info, $nids) {
$base = variable_get('biblio_base', 'biblio');
$channel['title'] = $rss_info['title'];
$channel['link'] = url($base . $rss_info['link'], array(
'absolute' => TRUE,
));
$channel['description'] = $rss_info['description'];
node_feed($nids, $channel);
}
function biblio_get_db_fields() {
$fields = array();
$fields[] = 'nid';
$fields[] = 'vid';
$fields[] = 'biblio_type';
$result = db_query('SELECT name FROM {biblio_fields} ', array(), array(
'fetch' => PDO::FETCH_ASSOC,
));
foreach ($result as $field) {
$fields[] = $field['name'];
}
return $fields;
}
function _biblio_citekey_print($citekey) {
$nid = db_query("SELECT nid FROM {biblio} WHERE biblio_citekey = :key", array(
':key' => $citekey,
))
->fetchObject();
if ($nid->nid > 0) {
$style = biblio_get_style();
$base = variable_get('biblio_base', 'biblio');
$node = node_load($nid->nid);
return theme('biblio_style', array(
'node' => $node,
'base' => $base,
'style' => $style,
));
}
else {
return t("Citekey @cite not found", array(
'@cite' => $citekey,
));
}
}
function biblio_filter_info() {
$filters['biblio_filter_reference'] = array(
'title' => t('Biblio module references <bib> <i>or</i> [bib]'),
'description' => t('Use <bib>citekey</bib> or [bib]citebkey[/bib]to insert automatically numbered references.'),
'prepare callback' => '_biblio_filter_reference_prepare',
'process callback' => '_biblio_filter_reference_process',
'tips callback' => '_biblio_filter_reference_tips',
);
$filters['biblio_filter_inline_reference'] = array(
'title' => t('Biblio module inline references <ibib> <i>or</i> [ibib]'),
'description' => t('Use <bib>citekey</bib> or [bib]citebkey[/bib]to insert automatically numbered references.'),
'prepare callback' => '_biblio_filter_inline_reference_prepare',
'process callback' => '_biblio_filter_inline_reference_process',
'tips callback' => '_biblio_filter_inline_reference_tips',
);
return $filters;
}
function _biblio_filter_reference_tips($filter, $format, $long = FALSE) {
if (!$long) {
return t('Use <bib><i>citekey</i></bib> <b><i>or</i></b> [bib]<i>citekey</i>[/bib] to insert automatically numbered references.');
}
else {
return t('You can cite references directly into texts with <code><bib><i>citekey</i></bib> <b><i>or</i></b> [bib]<i>citekey</i>[/bib]</code>. This will be replaced with a running number (the publication reference) and the publication referenced by the citekey within the <bib> tags will be printed at the bottom of the page (the reference).');
}
}
function _biblio_filter_inline_reference_tips($filter, $format, $long = FALSE) {
return t('This creates an in line reference to another publication.');
}
function _biblio_filter_reference_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
return $text;
}
function _biblio_filter_inline_reference_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
return $text;
}
function _biblio_filter_reference_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$pattern = array(
'|\\[bib](.*?)\\[/bib]|s',
'|<bib>(.*?)</bib>|s',
);
if (variable_get('biblio_footnotes_integration', 0) && module_exists('footnotes')) {
$text = preg_replace_callback($pattern, '_biblio_filter_footnote_callback', $text);
return $text;
}
else {
$text = preg_replace_callback($pattern, '_biblio_filter_replace_callback', $text);
$footer = '';
$footer = _biblio_filter_replace_callback(NULL, 'output footer');
if (preg_match('/<bibliography(\\/( )?)?>/', $text) > 0) {
$text = preg_replace('/<bibliography(\\/( )?)?>/', $footer, $text, 1);
return $text;
}
else {
return $text . "\n\n" . $footer;
}
}
}
function _biblio_filter_inline_reference_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$pattern = array(
'|\\[ibib](.*?)\\[/ibib]|s',
'|<ibib>(.*?)</ibib>|s',
);
$text = preg_replace_callback($pattern, '_biblio_inline_filter_replace_callback', $text);
return $text;
}
function _biblio_inline_filter_replace_callback($matches) {
$text = _biblio_citekey_print($matches[1]);
return $text;
}
function _biblio_filter_footnote_callback($matches, $square_brackets = FALSE) {
if ($square_brackets) {
$text = '[fn]' . _biblio_citekey_print($matches[1]) . "</fn>";
}
else {
$text = '<fn>' . _biblio_citekey_print($matches[1]) . "</fn>";
}
return $text;
}
function _biblio_filter_replace_callback($matches, $op = '') {
static $n = 0;
static $store_matches = array();
$str = '';
if ($op == 'output footer') {
if ($n > 0) {
$str = '<hr /><h3>' . t('References') . '</h3>';
$str .= '<div class="references"><ol>';
for ($m = 1; $m <= $n; $m++) {
$str .= '<li id="reference' . $m . '"><a name="ref' . $m . '">' . _biblio_citekey_print($store_matches[$m - 1]) . " </a></li>\n\n";
}
$str .= '</ol></div>';
}
$n = 0;
$store_matches = array();
return $str;
}
$ref = array_search($matches[1], $store_matches);
if ($ref === FALSE) {
$n++;
array_push($store_matches, $matches[1]);
$ref = $n;
}
else {
$ref++;
}
$allowed_tags = array();
$title = filter_xss($matches[1], biblio_get_allowed_tags());
$title = str_replace('"', """, $title);
$title = str_replace("\n", " ", $title);
$title = str_replace("\r", "", $title);
$text = '<span hovertip="reference' . $ref . '"><a href="#ref' . $ref . '">[' . $ref . ']</a></span>';
if (module_exists('hovertip')) {
$text = '<span hovertip="reference' . $ref . '"><a href="#ref' . $ref . '">[' . $ref . ']</a></span>';
$text .= '<span id="reference' . $ref . '" class="hovertip">' . _biblio_citekey_print($title) . '</span>';
}
else {
$text = '<a href="#ref' . $ref . '" title="Reference ' . $ref . '">[' . $ref . ']</a>';
}
return $text;
}
function hook_taxonomy_vocabulary_delete($vocabulary) {
if ($vocabulary->vid == variable_get('biblio_keyword_vocabulary', FALSE)) {
variable_del('biblio_keyword_freetagging');
variable_del('biblio_keyword_vocabulary');
}
}
function biblio_term_path($term) {
$base = variable_get('biblio_base', 'biblio');
if ($term->vid == variable_get('biblio_collection_vocabulary', 0)) {
return "{$base}/collection/{$term->name}";
}
elseif ($term->vid == variable_get('biblio_keyword_vocabulary', 0)) {
return "{$base}/term_id/{$term->tid}";
}
else {
return;
}
}
function biblio_hash($node) {
static $sums = array();
$duplicate = NULL;
if (empty($sums)) {
$res = db_query("SELECT nid, biblio_md5 FROM {biblio} ");
foreach ($res as $md5) {
$sums[$md5->biblio_md5] = $md5->nid;
}
}
$hash_string = str_replace(' ', '', drupal_strtolower($node->title));
if (isset($node->biblio_contributors[0]['lastname'])) {
$hash_string .= str_replace(' ', '', drupal_strtolower($node->biblio_contributors[0]['lastname']));
}
$hash_string .= $node->biblio_year;
$sum = md5($hash_string);
if (isset($sums[$sum])) {
$duplicate = $sums[$sum];
}
else {
$sums[$sum] = $node->nid;
}
$node->biblio_md5 = $sum;
return $duplicate;
}
function biblio_token_info() {
$node['biblio_year'] = array(
'name' => t("Biblio: Publication year"),
'description' => '',
);
$node['biblio_authors'] = array(
'name' => t("Biblio: Authors"),
'description' => '',
);
$node['biblio_type_id'] = array(
'name' => t("Biblio: Type ID (e.g.: 100)"),
'description' => '',
);
$node['biblio_type'] = array(
'name' => t("Biblio: Type Name (e.g.: book)"),
'description' => '',
);
$node['biblio_citekey'] = array(
'name' => t("Biblio: Cite Key often used in BibTex files"),
'description' => '',
);
return array(
'tokens' => array(
'node' => $node,
),
);
}
function biblio_tokens($type, $tokens, $data = array(), $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'biblio') {
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'biblio_year':
$replacements[$original] = check_plain($node->biblio_year);
break;
case 'biblio_authors':
$replacements[$original] = check_plain($node->biblio_contributors[0]['lastname']);
break;
case 'biblio_type_id':
$replacements[$original] = check_plain($node->biblio_type);
break;
case 'biblio_citekey':
$replacements[$original] = check_plain($node->biblio_citekey);
break;
case 'biblio_type':
$type = db_query('SELECT name FROM {biblio_types} as t WHERE t.tid = :tid', array(
':tid' => $node->biblio_type,
))
->fetchField();
$replacements[$original] = check_plain($type);
break;
}
}
}
return $replacements;
}
function _biblio_profile_access($user, $type = 'profile') {
if ($type == 'profile') {
$key = 'biblio_show_profile';
}
elseif ($type == 'menu' && !empty($user) && $user->uid > 0) {
$key = 'biblio_my_pubs_menu';
}
else {
return FALSE;
}
if (!variable_get('biblio_show_user_profile_form', '1') || !isset($user->data[$key])) {
return variable_get($key, '0');
}
else {
return $user->data[$key];
}
}
function biblio_get_style() {
global $user;
if (isset($user->data['biblio_user_style']) && $user->data['biblio_user_style'] != "system") {
return $user->data['biblio_user_style'];
}
return module_exists('biblio_citeproc') ? variable_get('biblio_citeproc_style', 'cse.csl') : variable_get('biblio_style', 'cse');
}
function biblio_get_styles() {
$styles = array();
if (module_exists('biblio_citeproc')) {
$result = db_select('biblio_citeproc_styles', 'csl')
->fields('csl', array(
'filename',
'title',
))
->orderBy('title', 'ASC')
->execute();
foreach ($result as $style) {
$styles[$style->filename] = $style->title;
}
}
else {
$dir = drupal_get_path('module', 'biblio') . '/styles';
$files = file_scan_directory($dir, '/biblio_style_..*.inc$/');
foreach ($files as $file) {
include_once $file->uri;
$function = $file->name . '_info';
if (function_exists($function)) {
$styles = array_merge($styles, call_user_func($function));
}
}
ksort($styles);
}
return $styles;
}
function biblio_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'biblio') . '/views',
);
}
function biblio_views_handlers() {
return array(
'handlers' => array(
'views_handler_field',
'views_handler_field_field',
'entity_views_handler_field_field',
),
);
}
function biblio_fix_isi_links(&$node) {
$isi = check_plain(variable_get('biblio_isi_url', 'http://apps.isiknowledge.com/InboundService.do?Func=Frame&product=WOS&action=retrieve&SrcApp=EndNote&Init=Yes&SrcAuth=ResearchSoft&mode=FullRecord&UT='));
if (isset($node['biblio_url']) && preg_match('/Go\\s*to\\s*ISI/', $node['biblio_url'])) {
$node['biblio_url'] = str_replace('<Go to ISI>://', $isi, $node['biblio_url']);
}
if (isset($node['biblio_accession_number']) && preg_match('/^ISI:/', $node['biblio_accession_number'])) {
$node['biblio_accession_number'] = str_replace("ISI:", $isi, $node['biblio_accession_number']);
}
}
function biblio_get_allowed_tags() {
return array(
'a',
'b',
'i',
'u',
'sub',
'sup',
'span',
);
}
function biblio_get_title_url_info($biblio) {
return array(
'link' => variable_get('biblio_link_title_url', 0) && !empty($biblio->biblio_url) ? $biblio->biblio_url : "biblio/{$biblio->bid}",
'options' => array(
'attributes' => variable_get('biblio_links_target_new_window', FALSE) ? array(
'target' => '_blank',
) : array(),
'html' => TRUE,
),
);
}
function biblio_get_map($type, $format) {
$result = db_select('biblio_type_maps', 'btm')
->fields('btm', array(
$type,
))
->condition('format', $format)
->execute()
->fetchField();
$map = unserialize($result);
if ($type == 'export_map' && empty($map)) {
$schema = drupal_get_schema('biblio');
$fieldnames = array_keys($schema['fields']);
asort($fieldnames);
$map = array_fill_keys($fieldnames, 1);
}
return $map;
}
function biblio_save_map($maps) {
db_insert('biblio_type_maps')
->fields($maps)
->execute();
}
function biblio_set_map($type, $format, $map) {
$map[$type] = serialize($map);
$map['format'] = $format;
drupal_write_record('biblio_type_maps', $map, 'format');
}
function biblio_reset_map($type, $format) {
module_invoke_all($format . '_map_reset', $type);
}
function biblio_field_extra_fields() {
module_load_include('inc', 'biblio', 'includes/biblio.fields');
return _biblio_field_extra_fields();
}
function biblio_node_export_node_alter($node, $original_node) {
if ($node->type == 'biblio' && isset($node->biblio_contributors) && is_array($node->biblio_contributors)) {
foreach ($node->biblio_contributors as $n => $value) {
unset($node->biblio_contributors[$n]['cid']);
}
}
}
function biblio_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
module_load_include('inc', 'biblio', 'includes/biblio.feeds');
return _biblio_feeds_processor_targets_alter($targets, $entity_type, $bundle_name);
}
function biblio_feeds_importer_default() {
module_load_include('inc', 'biblio', 'includes/biblio.feeds');
$defaults = array();
if (module_exists('feeds_oai_pmh')) {
$defaults += _biblio_feeds_oai_importer_default();
}
return $defaults;
}
function biblio_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == "feeds" && $api == "feeds_importer_default") {
return array(
"version" => 1,
);
}
}
function biblio_entity_info() {
$return['biblio'] = array(
'label' => t('Biblio'),
'entity class' => 'Biblio',
'controller class' => 'BiblioController',
'views controller class' => 'EntityDefaultViewsController',
'base table' => 'biblio',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'bundle' => biblio_bundle_name('biblio'),
),
'bundle keys' => array(
'bundle' => biblio_bundle_name('biblio'),
),
'static cache' => TRUE,
'bundles' => array(),
'load hook' => 'biblio_load',
'view modes' => array(
'full' => array(
'label' => t('Full Content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'biblio',
'access callback' => 'biblio_entity_access',
);
$publication_types = biblio_types('biblio');
if (!empty($publication_types)) {
foreach ($publication_types as $type => $info) {
$return['biblio']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/biblio/publication-types/%biblio_bundle',
'real path' => 'admin/structure/biblio/publication-types/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array(
'manage biblio structure',
),
),
);
}
}
$return['biblio_contributor'] = array(
'label' => t('Contributor'),
'entity class' => 'BiblioContributor',
'controller class' => 'BiblioContributorController',
'base table' => 'biblio_contributor',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'cid',
'bundle' => biblio_bundle_name('biblio_contributor'),
'label' => 'title',
),
'bundle keys' => array(
'bundle' => biblio_bundle_name('biblio_contributor'),
),
'bundles' => array(),
'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',
);
$contributor_categories = biblio_types('biblio_contributor');
if (!empty($contributor_categories)) {
foreach ($contributor_categories as $type => $info) {
$return['biblio_contributor']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/biblio/contributors/%contributor_bundle',
'real path' => 'admin/structure/biblio/contributors/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array(
'manage biblio structure',
),
),
);
}
}
return $return;
}
function biblio_types($entity_type = FALSE) {
$query = db_select('biblio_types', 'bt')
->fields('bt', array(
'name',
'description',
'entity',
));
if ($entity_type) {
$query
->condition('entity', $entity_type);
}
$result = $query
->execute();
$entity_type_passed_in = $entity_type;
while ($row = $result
->fetchAssoc()) {
if ($entity_type_passed_in === FALSE) {
$entity_type = $row['entity'];
}
$bundle_name = biblio_bundle_name($entity_type);
$machine_name = str_replace(' ', '_', strtolower($row['name']));
$types[$machine_name] = (object) array(
$bundle_name => $machine_name,
'entity' => $row['entity'],
'name' => t($row['name']),
'description' => t($row['description']),
);
}
return $types;
}
function biblio_bundle_name($entity_type) {
switch ($entity_type) {
case 'biblio':
return 'publication_type';
break;
default:
return 'type';
break;
}
}
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');
}
}
function biblio_bundle_load($type) {
return biblio_combined_bundle_load($type);
}
function contributor_bundle_load($type) {
return biblio_combined_bundle_load($type);
}
function biblio_combined_bundle_load($type) {
foreach (biblio_entities() as $entity) {
foreach (biblio_types($entity) as $type_name => $info) {
$types[$type_name] = $info;
}
}
$type = str_replace('-', '_', $type);
return isset($types[$type]) ? $types[$type] : FALSE;
}
function biblio_entities() {
return array(
'biblio',
'biblio_contributor',
);
}
function biblio_wrapper($entity, $entity_type = 'biblio') {
$entity_types = biblio_entities();
if (!in_array($entity_type, $entity_types)) {
$info = entity_get_info($entity_type);
if (empty($info)) {
throw new Exception('Invalid entity type: ' . $entity_type);
}
}
return entity_metadata_wrapper($entity_type, $entity);
}
function biblio_create($publication_type, $values = array()) {
module_load_include('inc', 'biblio', 'includes/biblio.fields');
biblio_check_instances($publication_type);
$values['publication_type'] = $publication_type;
$field_values = array();
foreach ($values as $property => $value) {
if (biblio_is_field_instance($property, 'biblio', $publication_type)) {
$field_values[$property] = $value;
}
else {
$initial_values[$property] = $value;
}
}
$biblio = entity_create('biblio', $initial_values);
$wrapper = biblio_wrapper($biblio);
$property_info = $wrapper
->getPropertyInfo();
foreach ($field_values as $property => $value) {
$value_prop = isset($property_info[$property]['property info']['value']);
if ($value_prop) {
$wrapper->{$property} = array(
'value' => $value,
);
}
else {
$wrapper->{$property}
->set($value);
}
}
return $biblio;
}
function biblio_load($bid, $reset = FALSE) {
$biblios = biblio_load_multiple(array(
$bid,
), array(), $reset);
$biblio = reset($biblios);
return $biblio;
}
function biblio_load_multiple($bids = array(), $conditions = array(), $reset = FALSE) {
$biblios = entity_load('biblio', $bids, $conditions, $reset);
return $biblios;
}
function biblio_save($biblio) {
module_load_include('inc', 'biblio', 'includes/biblio.keywords');
_biblio_prepare_submit($biblio);
$biblio->changed = REQUEST_TIME;
entity_save('biblio', $biblio);
}
function biblio_delete($biblio) {
entity_delete_multiple('biblio', array(
$biblio->bid,
));
}
function biblio_delete_multiple($bids) {
if (!empty($bids)) {
$biblios = biblio_load_multiple($bids);
}
foreach ($biblios as $bid => $biblio) {
module_invoke_all('entity_delete', $biblio, 'biblio');
}
entity_delete_multiple('biblio', $bids);
}
function biblio_form_validate($form, &$form_state) {
if ($form_state['triggering_element']['#value'] == t('Next') || $form_state['triggering_element']['#value'] == t('Change Publication Type')) {
$form_state['rebuild'] = TRUE;
$form_state['biblio_type'] = $form_state['values']['biblio_type'];
if ($form_state['values']['biblio_type'] == '') {
form_set_error('biblio_type', t('Please select a publication type.'));
}
return;
}
if (isset($form_state['biblio']) && !isset($biblio)) {
$biblio = $form_state['biblio'];
}
if (isset($biblio)) {
$i = 0;
field_attach_form_validate('biblio', $biblio, $form, $form_state);
}
}
function biblio_form_submit($form, &$form_state) {
global $user;
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
$biblio =& $form_state['biblio'];
if (empty($biblio->uid)) {
$biblio->uid = $user->uid;
}
biblio_create_contributor_refs($form_state);
field_attach_submit('biblio', $biblio, $form, $form_state);
biblio_save($biblio);
drupal_set_message(t('Biblio saved.'));
$form_state['redirect'] = 'biblio/' . $biblio->bid;
}
function biblio_create_contributor_refs(&$form_state) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
$categories = biblio_contributor_categories();
foreach ($categories as $info) {
$form_state['values'][$info['field']]['und'] = array();
}
$delta = 0;
foreach ($form_state['values']['biblio_contributors'] as $contrib) {
if ($contrib['rank'] == '') {
$contrib['rank'] = $delta;
}
$delta++;
if (empty($contrib['name']) || empty($contrib['category'])) {
continue;
}
$contributor = biblio_get_contributor_by_name($contrib['name']);
if (!$contributor) {
$contributor = biblio_contributor_create($contrib['name']);
$wrapper = biblio_wrapper($contributor, 'biblio_contributor');
$wrapper->biblio_contributor_name
->set($contrib['name']);
biblio_contributor_save($contributor);
}
$contrib['cid'] = $contributor->cid;
$field = $categories[$contrib['category']]['field'];
$id_exists = FALSE;
foreach ($form_state['values'][$field]['und'] as $info) {
if ($info['target_id'] == $contributor->cid) {
$id_exists = TRUE;
}
}
if (!$id_exists) {
$form_state['values'][$field]['und'][$contrib['rank']] = array(
'target_id' => (string) $contrib['cid'],
'_weight' => $contrib['rank'],
);
}
}
}
function biblio_get_contributor_by_name($name) {
$contributors =& drupal_static(__FUNCTION__);
if (isset($contributors[$name])) {
return $contributors[$name];
}
$contributor = biblio_contributor_create($name);
$result = db_select('biblio_contributor', 'c')
->fields('c', array(
'cid',
'md5',
))
->condition('md5', $contributor->md5)
->execute()
->fetchObject();
if (isset($result->cid)) {
$contributors[$name] = biblio_contributor_load($result->cid);
return $contributors[$name];
}
else {
return FALSE;
}
}
function biblio_form_get_contributors($form) {
$i = 0;
foreach ($form['biblio_tabs']['contributors']['biblio_contributors'] as $key => $value) {
if (is_numeric($key)) {
$name_value = $value['name']['#value'];
$rank_value = $value['rank']['#value'] == '' ? $i : $value['rank']['#value'];
if (isset($name_value) && $name_value != '') {
$contributors[$rank_value] = $name_value;
$i++;
}
}
}
ksort($contributors);
return $contributors;
}
function biblio_page_view($biblio, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
global $user;
$wrapper = biblio_wrapper($biblio);
$title = $wrapper->biblio_title
->value();
$links = array();
$style = biblio_get_style();
$base = variable_get('biblio_base', 'biblio');
$base_title = check_plain(variable_get('biblio_base_title', 'Biblio'));
$biblio->content = array();
switch ($view_mode) {
case 'full':
if (variable_get('biblio_hide_bibtex_braces', 0) && !isset($biblio->view)) {
$title = biblio_remove_brace($title);
}
$title = filter_xss($title, biblio_get_allowed_tags());
case 'search_index':
switch (variable_get('biblio_layout', 'standard')) {
case 'standard':
field_attach_prepare_view('biblio', array(
$biblio->bid => $biblio,
), $view_mode);
entity_prepare_view('biblio', array(
$biblio->bid => $biblio,
));
$biblio->content = field_attach_view('biblio', $biblio, $view_mode);
if (isset($biblio->content['biblio_keywords'])) {
$keys = element_children($biblio->content['biblio_keywords']);
foreach ($keys as $key) {
$biblio->content['biblio_keywords'][$key]['#href'] = 'biblio/keywords/' . $biblio->content['biblio_keywords'][$key]['#options']['entity']->tid;
}
}
$coins = filter_xss($wrapper->biblio_coins
->value(), array(
'span',
));
$biblio->content['biblio_coins'] = array(
'#markup' => $coins,
);
break;
case 'orig':
case 'ft':
$biblio->content['body']['#markup'] = theme('biblio_long', array(
'biblio' => $biblio,
'base' => $base,
'style' => $style,
));
break;
case 'tabular':
default:
$biblio->content['body']['#markup'] = theme('biblio_tabular', array(
'biblio' => $biblio,
'base' => $base,
));
break;
}
break;
case 'teaser':
$biblio->content['teaser']['#markup'] = theme('biblio_style', array(
'biblio' => $biblio,
'base' => $base,
'style_name' => $style,
));
break;
}
drupal_set_title($title);
module_invoke_all('entity_view', $biblio, 'biblio', $view_mode, $langcode);
return $biblio->content;
}
function biblio_page_title($biblio) {
$wrapper = biblio_wrapper($biblio);
return $wrapper->biblio_title
->value();
}
function biblio_field_language($field_name, $biblio) {
$field_languages = field_language('biblio', $biblio);
return $field_languages[$field_name];
}
function biblio_delete_confirm($form, &$form_state, $biblio) {
$wrapper = biblio_wrapper($biblio);
$form['#biblio'] = $biblio;
$form['bid'] = array(
'#type' => 'value',
'#value' => $biblio->bid,
);
return confirm_form($form, t('Are you sure you want to delete %title?', array(
'%title' => $wrapper->biblio_title
->value(),
)), 'biblio/' . $biblio->bid, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function biblio_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$wrapper = biblio_wrapper($form['#biblio']);
biblio_delete($form['#biblio']);
watchdog('biblio', '@type: deleted %title.', array(
'@type' => $wrapper->publication_type,
'%title' => $wrapper->biblio_title
->value(),
));
$types = biblio_types('biblio');
drupal_set_message(t('@type %title has been deleted.', array(
'@type' => $types[$wrapper->publication_type
->value()]->name,
'%title' => $wrapper->biblio_title
->value(),
)));
}
$form_state['redirect'] = '<front>';
}
function biblio_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$biblio = $form_state['biblio'];
$form_state['redirect'] = array(
'biblio/' . $biblio->bid . '/delete',
array(
'query' => $destination,
),
);
}
function biblio_contributor_create($name = NULL, $type = NULL, $values = array()) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
if (variable_get('biblio_contributor_parser', 1) && isset($name)) {
$parsed_contributor = biblio_contributor_initial_parse($name);
if (isset($parsed_contributor->organization)) {
$values['type'] = 'organization';
}
else {
$values['type'] = 'person';
}
$contributor = entity_create('biblio_contributor', $values);
$contributor->orig_name = $name;
biblio_contributor_set_parsed_values($contributor, $parsed_contributor);
}
else {
$values['type'] = $type;
$contributor = entity_create('biblio_contributor', $values);
}
$contributor->md5 = _md5sum($contributor);
return $contributor;
}
function biblio_contributor_load($cid, $reset = FALSE) {
$contributors = biblio_contributor_load_multiple(array(
$cid,
), array(), $reset);
return reset($contributors);
}
function biblio_contributor_load_multiple($cids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('biblio_contributor', $cids, $conditions, $reset);
}
function biblio_contributor_save($contributor) {
module_load_include('inc', 'biblio', 'includes/biblio.contributors');
biblio_contributor_pre_save($contributor);
entity_save('biblio_contributor', $contributor);
}
function biblio_contributor_delete($contributor) {
biblio_contributor_delete_multiple(array(
$contributor->cid,
));
}
function biblio_contributor_delete_multiple($contributor_ids) {
$deleted = entity_delete_multiple('biblio_contributor', $contributor_ids);
}
function biblio_contributor_page_view($contributor, $view_mode = 'full') {
$contributor->content = array();
$wrapper = biblio_wrapper($contributor, 'biblio_contributor');
field_attach_prepare_view('biblio_contributor', array(
$contributor->cid => $contributor,
), $view_mode);
entity_prepare_view('biblio_contributor', array(
$contributor->cid => $contributor,
));
$contributor->content += field_attach_view('biblio_contributor', $contributor, $view_mode);
$extra_fields = field_extra_fields_get_display('biblio_contributor', 'contributor', $view_mode);
biblio_append_extra_fields($contributor->content, $extra_fields);
if (isset($contributor->content['view_all_by_contributor'])) {
$contributor->content['view_all_by_contributor']['#markup'] = l(t('View all publications by ' . $wrapper->biblio_contributor_name
->value()), 'biblio/contributor/' . $contributor->cid . '/publications', array(
'attributes' => array(
'class' => array(
'biblio-view-all-by-contrib',
),
),
));
}
return $contributor->content;
}
function biblio_append_extra_fields(&$content, $extra_fields) {
foreach ($extra_fields as $field => $info) {
if (isset($info['visible']) && $info['visible']) {
$content[$field] = array();
foreach ($info as $attribute => $value) {
$content[$field]['#' . $attribute] = $value;
}
}
}
}
function biblio_add() {
return drupal_get_form('biblio_form');
}
function biblio_page_edit($biblio) {
$wrapper = biblio_wrapper($biblio);
$lang = biblio_field_language('biblio_title', $biblio);
$types = biblio_types('biblio');
drupal_set_title(t('<em>Edit @type</em> @title', array(
'@type' => $types[$biblio->publication_type]->name,
'@title' => $wrapper->biblio_title
->value(),
)), PASS_THROUGH);
return drupal_get_form('biblio_form', $biblio);
}
function menu_get_children($menu_item) {
$leading_path = db_like($menu_item['path']);
$result = db_query('SELECT * FROM menu_router WHERE path LIKE :leading_path', array(
':leading_path' => db_like($leading_path) . '/%',
));
$children = array();
foreach ($result as $row) {
$children[$row->title] = $row->path;
}
return $children;
}
function biblio_contributor_add() {
$contributor = entity_create('biblio_contributor', array());
drupal_set_title(t('Create Contributor'), PASS_THROUGH);
return drupal_get_form('biblio_contributor_add_form', $contributor);
}
function biblio_contributor_add_form($form, &$form_state, $contributor) {
$form['#contributor'] = $contributor;
$form_state['contributor'] = $contributor;
field_attach_form('biblio_contributor', $contributor, $form, $form_state);
foreach ($form['field_author_name']['und'] as $key => $field_data) {
if (is_numeric($key)) {
$form['field_author_name']['und'][$key]['value']['#autocomplete_path'] = 'biblio/autocomplete/contributor';
}
}
$form['buttons'] = array();
$form['buttons']['#weight'] = 100;
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array(
'biblio_contributor_form_submit',
),
);
if (!empty($contributor->cid)) {
$form['buttons']['delete'] = array(
'#access' => user_access('delete biblios'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array(
'biblio_contributor_form_delete_submit',
),
);
}
return $form;
}
function biblio_contributor_form_submit($form, &$form_state) {
global $user;
$contributor =& $form_state['contributor'];
if (empty($contributor->uid)) {
$contributor->uid = $user->uid;
}
field_attach_submit('biblio_contributor', $contributor, $form, $form_state);
biblio_contributor_save($contributor);
drupal_set_message(t('Contributor saved.'));
$form_state['redirect'] = 'biblio/contributor/' . $contributor->cid;
}
function biblio_contributor_form($form, $form_state, $contributor = NULL) {
field_attach_form('biblio_contributor', $contributor, $form, $form_state);
$form_state['contributor'] = $contributor;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 50,
'#submit' => array(
'biblio_contributor_form_submit',
),
);
if (!empty($contributor->cid)) {
$form['delete'] = array(
'#access' => user_access('delete biblios'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 51,
'#submit' => array(
'biblio_contributor_form_delete_submit',
),
);
}
return $form;
}
function biblio_contributor_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$contributor = $form_state['contributor'];
$form_state['redirect'] = array(
'biblio/contributor/' . $contributor->cid . '/delete',
array(
'query' => $destination,
),
);
}
function biblio_contributor_delete_confirm($form, &$form_state, $contributor) {
$form['#contributor'] = $contributor;
$form['cid'] = array(
'#type' => 'value',
'#value' => $contributor->cid,
);
return confirm_form($form, t('Are you sure you want to delete %title?', array(
'%title' => $contributor->title,
)), 'biblio/contributor/' . $contributor->cid, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function biblio_contributor_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$contributor = $form['#contributor'];
biblio_contributor_delete($contributor);
watchdog('biblio', 'Deleted biblio contributor %title.', array(
'%title' => $contributor->title,
));
drupal_set_message(t('Contributor %title has been deleted.', array(
'%title' => $contributor->title,
)));
}
$form_state['redirect'] = '<front>';
}
function biblio_admin_paths() {
$base = variable_get('biblio_base', 'biblio');
$paths = array(
"{$base}/*/edit" => TRUE,
"{$base}/*/delete" => TRUE,
);
return $paths;
}
function biblio_admin_view($view_name, $view_display) {
if (!module_exists('views')) {
drupal_set_message('This page requires the Views module be enabled.', 'error');
return;
}
$view_obj = views_get_view($view_name);
if ($view_obj) {
return views_page($view_name, $view_display);
}
else {
$message = "Could not locate the <i>" . $view_name . "</i> View using the display <i>" . $view_display . "</i>. Please ensure that the Views module is enabled, and that the <i>" . $view_name . "</i> View is enabled.";
drupal_set_message($message, 'error');
}
return '';
}
function biblio_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#field']['bundles']['biblio'])) {
switch ($form_id) {
case 'field_ui_field_settings_form':
case 'field_ui_field_edit_form':
$field_name = $form['#instance']['field_name'];
$entity_type = $form['#instance']['entity_type'];
$bundle = $form['#instance']['bundle'];
$field_info = field_info_instance($entity_type, $field_name, $bundle);
if (isset($field_info['settings']['vtab'])) {
$vtab_default_value = $field_info['settings']['vtab'];
}
else {
$vtab_default_value = 'none';
}
$vtabs = biblio_form_vtab_info();
foreach ($vtabs as $vtab_info) {
$options[$vtab_info['tab_id']] = t($vtab_info['title']);
}
$options['none'] = t('None');
$form['instance']['settings']['vtab'] = array(
'#type' => 'select',
'#title' => t('Biblio Form Vertical Tab'),
'#options' => $options,
'#default_value' => $vtab_default_value,
'#required' => TRUE,
);
break;
}
}
}
function biblio_entity_property_info_alter(&$info) {
$properties =& $info['biblio']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the biblio was added."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'description' => t("The date the biblio was most recently changed/edited."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'changed',
);
$properties['uid'] = array(
'label' => t("Author"),
'type' => 'user',
'description' => t("The creator of the biblio entity."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'required' => TRUE,
'schema field' => 'uid',
);
$properties['biblio_sort_title'] = array(
'label' => t("Sort Title"),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'biblio_sort_title',
);
$properties =& $info['biblio_contributor']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the contribtuor was added."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'description' => t("The date the contributor was most recently changed/edited."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'changed',
);
$properties['md5'] = array(
'label' => t("MD5"),
'description' => t("MD5 of the contributor. Used for resolving duplicates"),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'edit all biblio entries',
'schema field' => 'md5',
);
}
function biblio_get_fielded_pubtypes() {
$pubtypes = array();
foreach (field_info_instances('biblio') as $bundle => $instances) {
if (!empty($instances)) {
$pubtypes[] = $bundle;
}
}
return $pubtypes;
}
function biblio_entity_presave($entity, $type) {
_biblio_truncate_long_fields($entity, $type);
}
function _biblio_truncate_long_fields($entity, $type) {
$valid_entity_types = array(
'biblio',
'biblio_contributor',
);
if (!in_array($type, $valid_entity_types)) {
return;
}
$wrapper = biblio_wrapper($entity, $type);
$entity_info = entity_get_info($type);
$fields_info = field_info_fields();
foreach ($entity as $field => $field_structure) {
if (isset($fields_info[$field]) && ($value = $wrapper->{$field}
->value())) {
$field_settings = $fields_info[$field]['settings'];
if (is_string($value) && isset($field_settings['max_length'])) {
$max_length = $field_settings['max_length'];
$length = strlen($value);
if ($length > $max_length) {
$wrapper->{$field} = substr($value, 0, $max_length);
}
}
}
}
}