You are here

biblio_citeproc.module in Bibliography Module 7.2

File

modules/CiteProc/biblio_citeproc.module
View source
<?php

function biblio_citeproc_menu() {
  global $user;
  $items = array();
  $items['admin/config/content/biblio/citeproc'] = array(
    'title' => 'CiteProc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'biblio_citeproc_style_manager_form',
    ),
    'access arguments' => array(
      'administer biblio',
    ),
    'file' => 'biblio_citeproc.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 12,
  );
  $items['admin/config/content/biblio/citeproc/styles'] = array(
    'title' => 'CiteProc Style Manager',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'biblio_citeproc_style_manager_form',
    ),
    'access arguments' => array(
      'administer biblio',
    ),
    'file' => 'biblio_citeproc.admin.inc',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 12,
  );
  $items['admin/config/content/biblio/citeproc/styles/%/edit'] = array(
    'title' => 'CiteProc Style Editor',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'biblio_citeproc_csl_editor',
      6,
    ),
    'access arguments' => array(
      'administer biblio',
    ),
    'file' => 'biblio_citeproc.admin.inc',
    'type' => MENU_CALLBACK,
    'weight' => 12,
  );
  $items['admin/config/content/biblio/citeproc/map'] = array(
    'title' => 'CSL Field Mapper',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'biblio_admin_io_mapper_form',
      'csl',
      FALSE,
    ),
    'access arguments' => array(
      'administer biblio',
    ),
    'file' => '../../includes/biblio.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 12,
  );
  return $items;
}
function biblio_citeproc_theme() {
  return array(
    'biblio_citeproc_style' => array(
      'function' => 'theme_biblio_citeproc_style',
      'file' => 'biblio_citeproc.module',
      'variables' => array(
        'node' => '',
        'style_name' => 'cse',
      ),
    ),
    'biblio_citeproc_style_manager_form' => array(
      'render element' => 'form',
    ),
  );
}
function biblio_citeproc_theme_registry_alter(&$theme_registry) {
  $theme_registry['biblio_style'] = $theme_registry['biblio_citeproc_style'];
}
function theme_biblio_citeproc_style($variables) {
  static $citeproc;
  global $language;
  $cached = NULL;
  $node = $variables['node'];
  $style = isset($variables['style_name']) ? $variables['style_name'] : NULL;
  module_load_include('inc', 'biblio_citeproc', 'CSL');
  if (!$citeproc) {
    $csl_id = $style ? $style : biblio_get_style();
    if (strpos($csl_id, '.csl') === FALSE) {

      // try to convert old style names to csl...
      if (in_array($csl_id, array(
        'ama',
        'apa',
        'cse',
        'ieee',
        'mla',
        'vancouver',
      ))) {
        $csl_id .= '.csl';
      }
      elseif ($csl_id == 'chicago') {
        $csl_id = 'chicago-fullnote-bibliography.csl';
      }
      else {
        $csl_id = '';
        $message = t('An invalid style "@style" was selected, please check your "CiteProc" style settings.', array(
          '@style' => $csl_id,
        ));
        drupal_set_message($message, 'error');
      }
    }
    if (!empty($csl_id)) {
      $csl = db_query('SELECT parent,csl FROM {biblio_citeproc_styles} WHERE filename = :id', array(
        ':id' => $csl_id,
      ))
        ->fetchObject();
      if (!isset($csl->csl)) {
        drupal_set_message(t('Biblio-CiteProc could not fetch the style file: !csl_id from the database. Check your CiteProc settings.', array(
          '!csl_id' => $csl_id,
        )), 'error');
        return;
      }
      if (!empty($csl->parent)) {
        $csl_file_contents = db_query("SELECT csl FROM biblio_citeproc_styles WHERE id = :id", array(
          ':id' => $csl->parent,
        ))
          ->fetchField();
      }
      else {
        $csl_file_contents = $csl->csl;
      }

      //    $cslid = $csl_file_name . '-' . $language->language;
      //    $cached = cache_get($cslid, 'cache_biblio_csl_object');
      if (!$cached) {
        $citeproc = new citeproc($csl_file_contents, $language->language);

        //      cache_set($cslid, $citeproc, 'cache_biblio_csl_object');
      }
      else {
        $citeproc = $cached->data;
      }
    }
  }
  $styled_node = $citeproc
    ->render($node);
  return $styled_node . filter_xss($node->biblio_coins, array(
    'span',
  ));
}
function biblio_citeproc_csl_map_reset($type = NULL) {
  module_load_include('install', 'biblio_citeproc', 'biblio_citeproc');
  _reset_csl_map($type);
}
function biblio_citeproc_install_style($name, $csl) {
  $xml = simplexml_load_string($csl);
  $parent = '';
  foreach ($xml->info->link as $link) {
    $attrs = $link
      ->attributes();
    if (isset($attrs['rel']) && $attrs['rel'] == 'independent-parent') {
      $parent = (string) $attrs['href'];
    }
  }
  $old_sha1 = NULL;
  $old_sha1 = db_query('SELECT sha1 FROM {biblio_citeproc_styles} WHERE id = :id', array(
    ':id' => (string) $xml->info->id,
  ))
    ->fetchField();
  $record = array(
    'filename' => $name,
    'parent' => $parent,
    'title' => (string) $xml->info->title,
    'summary' => (string) $xml->info->summary,
    'csl' => $csl,
    'sha1' => sha1($csl),
    'id' => (string) $xml->info->id,
  );
  if ($old_sha1 && $old_sha1 == sha1($csl)) {

    //style exists and has not changed
    return;
  }
  elseif ($old_sha1 && $old_sha1 != sha1($csl)) {

    // update an existing style
    $query = db_update('biblio_citeproc_styles')
      ->fields($record)
      ->condition('id', $record['id']);
  }
  elseif (!$old_sha1) {

    //install new style
    $query = db_insert('biblio_citeproc_styles')
      ->fields(array(
      'id',
      'title',
      'filename',
      'summary',
      'csl',
      'sha1',
    ));
    $query
      ->values($record);
  }
  $query
    ->execute();
}