<?php

/**
 * @file
 * This file contains all the functions which describe and implement drupal database tables
 * needed by this module. This module was developed by Chad N.A. Krilow and Lacey-Anne Sanderson,
 * University of Saskatchewan.
 *
 * The project manamgenet module allows you to sync data in a chado/Tripal instance with
 * multiple project/mysql instances as well as manage and create such project instances
 */


/**
 * Implementation of hook_install().
 */
function tripal_pub_install() {

  // create the module's data directory
  tripal_create_moddir('tripal_pub');

  // install the tripal_oub
  drupal_install_schema('tripal_pub');

  // add loading of the the tripal pub ontology to the job queue
  $obo_path =  realpath('./') . '/' . drupal_get_path('module', 'tripal_pub') . '/files/tpub.obo';
  $obo_id = tripal_cv_add_obo_ref('Tripal Publication', $obo_path);
  tripal_cv_submit_obo_job($obo_id);

  // add the custom tables
  tripal_pub_add_custom_tables();

}

function tripal_pub_enable() {
  // make sure we have our supported databases
  tripal_db_add_db('PMID', 'PubMed', 'http://www.ncbi.nlm.nih.gov/pubmed', 'http://www.ncbi.nlm.nih.gov/pubmed/', TRUE);
  tripal_db_add_db('AGL', 'USDA National Agricultural Library', 'http://agricola.nal.usda.gov/', '', TRUE);

  variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
}
/**
 * Implementation of hook_uninstall().
 */
function tripal_pub_uninstall() {
  //Remove tables
  drupal_uninstall_schema('tripal_pub');
}


/**
 * Implementation of hook_schema().
 */
function tripal_pub_schema() {

  $schema['chado_pub'] = array(
    'fields' => array(
      'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'pub_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'sync_date' => array('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
    ),
    'indexes' => array(
      'pub_id' => array('pub_id')
    ),
    'unique keys' => array(
      'nid_vid' => array('nid', 'vid'),
      'vid' => array('vid')
    ),
    'primary key' => array('nid'),
  );
  
  $schema['tripal_pub_import'] = array(
    'fields' => array(
      'pub_import_id' => array('type' => 'serial', 'not null' => TRUE),
      'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
      'criteria' => array('type' => 'text', 'size' => 'normal', 'not null' => TRUE, 'description' => 'Contains a serialized PHP array containing the search criteria'),
      'disabled'  => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'default' => 0),      
      'do_contact'  => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'default' => 0),
    ),
    'primary key' => array('pub_import_id'),
    'indexes' => array(
      'name' => array('name')
    ),
  );
   
  return $schema;
}



/**
 * Implementation of hook_requirements().
 */
function tripal_pub_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    // make sure chado is installed
    if (!tripal_core_is_chado_installed()) {
      $requirements ['tripal_pub'] = array(
        'title' => "tripal_pub",
        'value' => "ERROR: Chado most be installed before this module can be enabled",
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/*
 *
 */
function tripal_pub_add_custom_tables() {
  $schema = array (
    'table' => 'pubauthor_contact',
    'fields' => array (
      'pubauthor_contact_id' => array (
        'type' => 'serial',
        'not null' => true,
      ),
      'contact_id' => array (
        'type' => 'int',
        'not null' => true,
      ),
      'pubauthor_id' => array (
        'type' => 'int',
        'not null' => true,
      ),
    ),
    'primary key' => array (
      0 => 'pubauthor_contact_id',
    ),
    'unique keys' => array (
      'pubauthor_contact_c1' => array (
        0 => 'contact_id',
        1 => 'pubauthor_id',
      ),
    ),
    'foreign keys' => array (
      'contact' => array (
        'table' => 'contact',
        'columns' => array (
          'contact_id' => 'contact_id',
        ),
      ),
      'pubauthor' => array (
        'table' => 'pubauthor',
        'columns' => array (
          'pubauthor_id' => 'pubauthor_id',
        ),
      ),
    ),
  );
  tripal_core_create_custom_table($ret, 'pubauthor_contact', $schema, TRUE);
}