View source
<?php
function biblio_bibtex_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'biblio_bibtex') . '/views',
);
}
function biblio_bibtex_biblio_import_options() {
return array(
'biblio_bibtex' => t('BibTex'),
);
}
function biblio_bibtex_biblio_mapper_options() {
return array(
'bibtex' => array(
'title' => t('BibTex'),
'export' => TRUE,
),
);
}
function biblio_bibtex_form_biblio_form_alter(&$form, &$form_state) {
global $user;
if (!$form_state['submitted'] && !isset($form_state['values']) && !isset($form_state['biblio']->bid)) {
if (!$form_state['submitted']) {
$form['biblio_cut_paste'] = array(
'#type' => 'fieldset',
'#title' => t('Paste BibTex Record'),
'#weight' => -20,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['biblio_cut_paste']['paste_data_bibtex'] = array(
'#type' => 'textarea',
'#title' => t('BibTex'),
'#required' => FALSE,
'#default_value' => isset($form_state['values']['paste_data_bibtex']) ? $form_state['values']['paste_data_bibtex'] : '',
'#description' => t('Paste a BibTex entry here'),
'#size' => 60,
'#weight' => -4,
);
$form['biblio_cut_paste']['paste_submit'] = array(
'#type' => 'submit',
'#value' => t('Populate using BibTex'),
'#submit' => array(
'biblio_bibtex_form_biblio_form_submit',
),
);
}
}
$biblio_bibtex_id = isset($form_state['values']['biblio_bibtex_id']) ? $form_state['values']['biblio_bibtex_id'] : '';
$biblio_bibtex_md5 = isset($form_state['values']['biblio_bibtex_md5']) ? $form_state['values']['biblio_bibtex_md5'] : '';
$form['biblio_bibtex_id'] = array(
'#type' => 'value',
'#value' => $biblio_bibtex_id,
);
$form['biblio_bibtex_md5'] = array(
'#type' => 'value',
'#value' => $biblio_bibtex_md5,
);
}
function biblio_bibtex_form_biblio_form_submit($form, &$form_state) {
global $user;
$biblio_data = array();
$dups = array();
if (strlen($form_state['values']['paste_data_bibtex'])) {
list($biblio_data, $dups) = biblio_bibtex_biblio_import($form_state['values']['paste_data_bibtex'], array(), FALSE, NULL, FALSE, TRUE);
}
$biblio = reset($biblio_data);
if (!empty($biblio_data) && is_object($biblio)) {
$form_state['input']['publication_type'] = $form_state['publication_type'] = $biblio->publication_type;
$form_state['biblio_imported_contributors'] = $biblio->biblio_contributors;
$form_state['biblio_imported'] = $biblio;
}
elseif (!empty($dups)) {
$message = t('The bibtex entity that you are trying to paste into the form already exists in the database, see !url', array(
'!url' => l('biblio/' . $dups[0], 'biblio/' . $dups[0]),
));
form_set_error('paste_data_bibtex', $message);
}
$form_state['rebuild'] = TRUE;
return;
}
function biblio_bibtex_biblio_export_options() {
return array(
'bibtex' => t('BibTex'),
);
}
function biblio_bibtex_entity_view($entity, $type, $view_mode, $langcode) {
if ($type == 'biblio') {
$biblio = $entity;
switch ($view_mode) {
case 'full':
case 'teaser':
$links = biblio_bibtex_biblio_export_link($biblio->bid);
$biblio->content['links']['biblio_bibtex'] = array(
'#links' => $links,
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
);
}
}
}
function biblio_bibtex_biblio_export_link($bid = NULL, $filter = array()) {
$show_link = variable_get('biblio_export_links', array(
'bibtex' => TRUE,
));
if (!$show_link['bibtex'] || !biblio_access('export')) {
return array();
}
$base = variable_get('biblio_base', 'biblio');
if (module_exists('popups') && !empty($bid)) {
$link = array(
'attributes' => array(
'class' => 'popups',
'title' => t("Click to get the BibTEX output"),
),
);
}
else {
$link = array(
'attributes' => array(
'title' => t("Click to download the BibTEX formatted file"),
),
);
}
$link['href'] = "{$base}/export/bibtex";
if (!empty($bid)) {
$link['href'] .= '/' . $bid;
}
$link['title'] = t('BibTex');
if (empty($bid) && !empty($filter)) {
$link['query'] = $filter;
}
return array(
'biblio_bibtex' => $link,
);
}
function biblio_bibtex_entity_delete($entity, $type) {
if ($type != 'biblio') {
return;
}
db_delete('biblio_bibtex')
->condition('bid', $entity->bid)
->execute();
}
function biblio_bibtex_entity_insert($entity, $type) {
if ($type != 'biblio') {
return;
}
if (!isset($entity->biblio_bibtex_md5)) {
return;
}
drupal_write_record('biblio_bibtex', $entity);
}
function biblio_bibtex_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE, $string = FALSE) {
$bids = array();
$dups = array();
module_load_include('php', 'biblio_bibtex', 'PARSEENTRIES');
$bibtex = new PARSEENTRIES();
if ($string) {
$bibtex
->loadBibtexString($file);
}
else {
$bibtex
->openBib($file->uri);
}
$bibtex
->extractEntries();
if ($bibtex->count) {
$entries =& $bibtex
->getEntries();
list($bids, $dups) = _biblio_bibtex_import($entries, $terms, $batch, $session_id, $save);
}
return array(
$bids,
$dups,
);
}
function biblio_bibtex_biblio_export($bids) {
if (module_exists('popups') && $bid) {
$popup = TRUE;
}
else {
$popup = FALSE;
drupal_add_http_header('Content-type', 'application/text; charset=utf-8');
drupal_add_http_header('Content-Disposition', 'attachment; filename="Biblio-Bibtex.bib"');
}
$biblios = biblio_load_multiple($bids, array(), TRUE);
foreach ($biblios as $biblio) {
if (!$popup) {
print _biblio_bibtex_export($biblio);
}
else {
$popup_data .= _biblio_bibtex_export($biblio);
}
}
if ($popup && !empty($popup_data)) {
return '<pre>' . $popup_data . '</pre>';
}
}
function _biblio_bibtex_import($entries, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE) {
module_load_include('inc', 'biblio', 'includes/biblio.fields');
module_load_include('inc', 'biblio', 'includes/biblio.import.export');
global $user;
$bids = array();
$dups = array();
foreach ($entries as $entry) {
$type = _biblio_bibtex_type_map($entry['bibtexEntryType'], 'import');
$biblio = biblio_create($type);
$wrapper = biblio_wrapper($biblio, 'biblio');
$biblio->uid = $user->uid;
$biblio->biblio_contributors = array();
switch ($entry['bibtexEntryType']) {
case 'mastersthesis':
$biblio->biblio_type_of_work = 'masters';
break;
case 'phdthesis':
$biblio->biblio_type_of_work = 'phd';
break;
}
if (!empty($entry['author'])) {
$authorArray = preg_split("/\\s(and|&)\\s/i", trim($entry['author']));
foreach ($authorArray as $key => $author) {
$biblio->biblio_contributors[] = array(
'name' => $author,
'category' => 'primary',
);
}
}
$wrapper->biblio_citekey = !empty($entry['bibtexCitation']) ? $entry['bibtexCitation'] : NULL;
if (!empty($entry['editor'])) {
$authorArray = preg_split("/\\s(and|&)\\s/i", trim($entry['editor']));
foreach ($authorArray as $key => $author) {
$biblio->biblio_contributors[] = array(
'name' => $author,
'category' => 'secondary',
);
}
}
$wrapper->biblio_title_secondary = !empty($entry['journal']) ? $entry['journal'] : NULL;
if (!empty($entry['booktitle'])) {
$wrapper->biblio_title_secondary = $entry['booktitle'];
}
if (!empty($entry['series'])) {
if (!empty($entry['booktitle'])) {
$wrapper->biblio_title_tertirary = $entry['series'];
}
else {
$wrapper->biblio_title_secondary = $entry['series'];
}
}
$wrapper->biblio_volume = !empty($entry['volume']) ? $entry['volume'] : NULL;
$wrapper->biblio_number = !empty($entry['number']) ? $entry['number'] : NULL;
$wrapper->biblio_year = !empty($entry['year']) ? $entry['year'] : 0;
$wrapper->biblio_notes = !empty($entry['note']) ? $entry['note'] : NULL;
$wrapper->biblio_date = !empty($entry['month']) ? $entry['month'] : NULL;
$wrapper->biblio_pages = !empty($entry['pages']) ? $entry['pages'] : NULL;
$wrapper->biblio_publisher = !empty($entry['publisher']) ? $entry['publisher'] : NULL;
if (!empty($entry['organization'])) {
$wrapper->biblio_publisher = $entry['organization'];
}
if (!empty($entry['school'])) {
$wrapper->biblio_publisher = $entry['school'];
}
if (!empty($entry['institution'])) {
$wrapper->biblio_publisher = $entry['institution'];
}
$wrapper->biblio_title = !empty($entry['title']) ? $entry['title'] : NULL;
$type_of_work = !empty($entry['type']) ? $entry['type'] . $type_of_work : NULL;
$wrapper->biblio_type_of_work = !empty($type_of_work) ? $type_of_work : NULL;
$wrapper->biblio_edition = !empty($entry['edition']) ? $entry['edition'] : NULL;
$wrapper->biblio_section = !empty($entry['chapter']) ? $entry['chapter'] : NULL;
$wrapper->biblio_place_published = !empty($entry['address']) ? $entry['address'] : NULL;
$wrapper->biblio_abst_e = !empty($entry['abstract']) ? array(
'value' => $entry['abstract'],
) : NULL;
if (!empty($entry['keywords'])) {
if (strpos($entry['keywords'], ';')) {
$entry['keywords'] = str_replace(';', ',', $entry['keywords']);
}
$biblio->biblio_keywords = explode(',', $entry['keywords']);
}
$wrapper->biblio_isbn = !empty($entry['isbn']) ? $entry['isbn'] : NULL;
$wrapper->biblio_issn = !empty($entry['issn']) ? $entry['issn'] : NULL;
$wrapper->biblio_url = !empty($entry['url']) ? $entry['url'] : NULL;
$wrapper->biblio_doi = !empty($entry['doi']) ? $entry['doi'] : NULL;
$temp_properties = biblio_remove_variable_properties($biblio);
$biblio->biblio_bibtex_md5 = md5(serialize((array) $biblio));
biblio_retrieve_variable_properties($biblio, $temp_properties);
$biblio->created = $temp_properties['created'];
$biblio->changed = $temp_properties['changed'];
$biblio->biblio_import_type = 'bibtex';
if (!($dup = biblio_bibtex_check_md5($biblio->biblio_bibtex_md5))) {
if ($save) {
entity_get_controller('biblio');
biblio_save_node($biblio, $terms, $batch, $session_id, $save);
$bids[] = !empty($biblio->bid) ? $biblio->bid : NULL;
}
else {
$bids[] = $biblio;
}
}
else {
$dups[] = $dup;
}
}
return array(
$bids,
$dups,
);
}
function _biblio_bibtex_export($biblio) {
static $converter = NULL;
$bibtex = '';
$type = "article";
$journal = $series = $booktitle = $school = $organization = $institution = NULL;
$type = _biblio_bibtex_type_map($biblio->publication_type);
$wrapper = biblio_wrapper($biblio);
switch ($biblio->publication_type) {
case 'book':
$series = $wrapper->biblio_title_secondary
->value();
$organization = $wrapper->biblio_publisher
->value();
break;
case 'book_chapter':
case 'conference_paper':
$booktitle = $wrapper->biblio_title_secondary
->value();
$organization = $wrapper->biblio_publisher
->value();
$series = $wrapper->biblio_title_tertiary
->value();
break;
case 'thesis':
$school = $wrapper->biblio_publisher
->value();
$wrapper->biblio_publisher = NULL;
if (stripos($wrapper->biblio_type_of_work
->value(), 'masters')) {
$type = "mastersthesis";
}
break;
case 'report':
$institution = $wrapper->biblio_publisher
->value();
$wrapper->biblio_publisher = NULL;
break;
case 'journal_article':
default:
$journal = $wrapper->biblio_title_secondary
->value();
break;
}
$bibtex .= '@' . $type . ' {';
$bibtex .= $wrapper->biblio_citekey
->value() ? $wrapper->biblio_citekey
->value() : "";
$bibtex .= _biblio_bibtex_format_entry('title', $wrapper->biblio_title
->value());
$bibtex .= _biblio_bibtex_format_entry('journal', $journal);
$bibtex .= _biblio_bibtex_format_entry('booktitle', $booktitle);
$bibtex .= _biblio_bibtex_format_entry('series', $series);
$bibtex .= _biblio_bibtex_format_entry('volume', $wrapper->biblio_volume
->value());
$bibtex .= _biblio_bibtex_format_entry('number', $wrapper->biblio_number
->value());
$bibtex .= _biblio_bibtex_format_entry('year', $wrapper->biblio_year
->value());
$bibtex .= _biblio_bibtex_format_entry('note', $wrapper->biblio_notes
->value());
$bibtex .= _biblio_bibtex_format_entry('month', $wrapper->biblio_date
->value());
$bibtex .= _biblio_bibtex_format_entry('pages', $wrapper->biblio_pages
->value());
$bibtex .= _biblio_bibtex_format_entry('publisher', $wrapper->biblio_publisher
->value());
$bibtex .= _biblio_bibtex_format_entry('school', $school);
$bibtex .= _biblio_bibtex_format_entry('organization', $organization);
$bibtex .= _biblio_bibtex_format_entry('institution', $institution);
$bibtex .= _biblio_bibtex_format_entry('type', $wrapper->biblio_type_of_work
->value());
$bibtex .= _biblio_bibtex_format_entry('edition', $wrapper->biblio_edition
->value());
$bibtex .= _biblio_bibtex_format_entry('chapter', $wrapper->biblio_section
->value());
$bibtex .= _biblio_bibtex_format_entry('address', $wrapper->biblio_place_published
->value());
$bibtex .= _biblio_bibtex_format_entry('abstract', $wrapper->biblio_abst_e
->value());
$kw_array = array();
foreach ($wrapper->biblio_keywords as $term) {
$kw_array[] = $term->name
->value();
}
if (!empty($kw_array)) {
$kw_array = array_unique($kw_array);
$bibtex .= _biblio_bibtex_format_entry('keywords', implode(', ', $kw_array));
}
$bibtex .= _biblio_bibtex_format_entry('isbn', $wrapper->biblio_isbn
->value());
$bibtex .= _biblio_bibtex_format_entry('issn', $wrapper->biblio_issn
->value());
$bibtex .= _biblio_bibtex_format_entry('doi', $wrapper->biblio_doi
->value());
$bibtex .= _biblio_bibtex_format_entry('url', $wrapper->biblio_url
->value());
if (!empty($biblio->upload) && count($biblio->upload['und']) && user_access('view uploaded files')) {
foreach ($biblio->upload['und'] as $file) {
$attachments[] = file_create_url($file['uri']);
}
$bibtex .= _biblio_bibtex_format_entry('attachments', implode(' , ', $attachments));
}
$a = $e = $authors = array();
if ($authors = biblio_get_contributors($biblio)) {
foreach ($authors as $auth) {
$a[] = trim($auth['name']);
}
}
$a = implode(' and ', $a);
$e = implode(' and ', $e);
if (!empty($a)) {
$bibtex .= _biblio_bibtex_format_entry('author', $a);
}
if (!empty($e)) {
$bibtex .= _biblio_bibtex_format_entry('editor', $e);
}
$bibtex .= "\n}\n";
if (!isset($converter)) {
module_load_include('php', 'biblio_bibtex', 'PARSEENTRIES');
include_once drupal_get_path('module', 'biblio_bibtex') . '/transtab_unicode_bibtex.inc.php';
$converter = new PARSEENTRIES();
}
$bibtex = $converter
->searchReplaceText(_biblio_bibtex_get_transtab(), $bibtex, FALSE);
return $bibtex;
}
function _biblio_bibtex_format_entry($key, $value) {
return !empty($value) ? ",\n\t{$key} = {" . $value . "}" : '';
}
function _biblio_bibtex_type_map($type, $direction = 'export') {
static $map = array();
if (empty($map)) {
$map = biblio_get_map('type_map', 'bibtex');
}
if ($direction == 'export') {
return ($type = array_search($type, $map)) ? $type : 'article';
}
else {
return isset($map[$type]) ? $map[$type] : 129;
}
}
function biblio_bibtex_bibtex_map_reset($type = NULL) {
module_load_include('install', 'biblio_bibtex', 'biblio_bibtex');
_reset_bibtex_map($type);
}
function biblio_bibtex_check_md5($md5) {
static $bibtex_md5s = array();
if (empty($bibtex_md5s)) {
$result = db_query("SELECT * FROM {biblio_bibtex} ");
foreach ($result as $row) {
$bibtex_md5s[$row->biblio_bibtex_md5] = $row->bid;
}
}
if (isset($bibtex_md5s[$md5])) {
return $bibtex_md5s[$md5];
}
else {
$bibtex_md5s[$md5] = TRUE;
return;
}
}
function biblio_bibtex_entity_load($entities, $type) {
if ($type != 'biblio') {
return;
}
foreach ($entities as $entity) {
$wrapper = biblio_wrapper($entity, $type);
$title = $wrapper->biblio_title
->value();
$wrapper->biblio_title = biblio_remove_brace($title);
}
}