<?php
/**
 * @file
 * Database table creation for biblio_crossref module.
 */

/**
 * Implementation of hook_install().
 */
function biblio_crossref_install() {
  _save_crossref_maps();
}

function biblio_crossref_uninstall() {
  if (db_table_exists('biblio_type_maps')) {
    db_delete('biblio_type_maps')
      ->condition('format', 'crossref')
      ->execute();
  }
}

function biblio_crossref_enable() {
  biblio_crossref_set_system_weight();
}

function biblio_crossref_set_system_weight() {
  db_update('system')
    ->fields(array('weight' => 20))
    ->condition('name', 'biblio_crossref')
    ->execute();
}
function _save_crossref_maps() {
  $typemap = _get_crossref_type_map();
  $typenames = _get_crossref_type_names();
  $fieldmap = _get_crossref_field_map();
  $maps = array_merge($typemap, $typenames, $fieldmap);
  biblio_save_map($maps);
}
function _reset_crossref_map($type) {
  $count = db_query("SELECT COUNT(*) FROM {biblio_type_maps} WHERE format='crossref'")->fetchField();
  if ($count && $type) { //update
    $function = '_get_crossref_' . $type;
    if (!function_exists($function)) return;
    $map = $function();
    db_update('biblio_type_maps')
      ->fields($map)
      ->condition('format', 'crossref')
      ->execute();
  }
  else { // install
//    db_query("DELETE FROM {biblio_type_maps} WHERE format='crossref'");
    db_delete('biblio_type_maps')
      ->condition('format', 'crossref')
      ->execute();
      _save_crossref_maps();
  }
}

function _get_crossref_type_map() {
  $map['type_map'] = serialize(
        array(
                'error'        => 0,
                'book'         => 100, // Book
                'journal'      => 102, // Journal Article
                'standard'     => 129, // Generic
                'conference'   => 103, // conference_paper
                'report-paper' => 109, // Report
                'dissertation' => 108, // Thesis
                'database'     => 125, // online database
                'sa_component' => 129
        )
  );
  $map['format'] = 'crossref';
  return $map;
}

function _get_crossref_type_names() {
  $map['type_names'] =  serialize(
        array(
                'error'        => 'Error',
                'book'         => 'Book',
                'journal'      => 'Journal Article',
                'standard'     => 'Generic',
                'conference'   => 'Conference Paper',
                'report-paper' => 'Report',
                'dissertation' => 'Thesis',
                'database'     => 'Online database',
                'sa_component' => 'SA Component',
        )
  );
  $map['format'] = 'crossref';
  return $map;
}

function _get_crossref_field_map() {
  $map['field_map'] =  serialize(
        array(
                'publisher_place'     => 'biblio_place_published',
                'publisher_name'      => 'biblio_publisher',
                'volume'              => 'biblio_volume',
                'number'              => 'biblio_number',
                'issue'               => 'biblio_issue',
                'edition_number'      => 'biblio_edition',
                'section'             => 'biblio_section',
                'doi'                 => 'biblio_doi',
                'title'               => 'title',
                'isbn'                => 'biblio_isbn',
                'issn'                => 'biblio_issn',
                'first_page'          => 'biblio_first_page',
                'last_page'           => 'biblio_last_page',
                // Journal metadata
                'full_title'          => 'biblio_secondary_title',
                'abbrev_title'        => 'biblio_short_title',
                // Conference metadata
                'conference_location' => 'biblio_place_published',
                'conference_name'     => 'biblio_secondary_title',
                'conference_acronym'  => 'biblio_short_title',
                // Proceedings metadata
                'proceedings_title'   => 'biblio_secondary_title',
                'year'                => 'year',
                'month'               => 'month',
                'day'                 => 'day',
                'degree'              => 'biblio_type_of_work',
                'error'               => 'error',
                'language'            => 'biblio_lang',
        )
  );

  $map['format'] = 'crossref';
  return $map;

}

/**
 * Implementation of hook_schema().
 *
 * Note:  Pro Drupal Development models use of t() to translate 'description'
 * for field definitions, but Drupal core does not use them.  We follow core.
 */
function biblio_crossref_schema() {
  $schema = array();
  $schema['biblio_crossref'] = array(
    'fields' => array(
      'nid'       => array('type' => 'int', 'not null' => TRUE),
      'biblio_crossref_md5'       => array('type' => 'char', 'length' => 32, 'not null' => TRUE),
      'biblio_crossref_id' => array('type' => 'char', 'length' => 255, 'not null' => TRUE),
      ),
  'primary key' => array('nid'),
  );
  return $schema;
}
