<?php
/**
 * @file
 * Installation of the library module
 */

/**
 * Implements hook_disable().
 * Disable default views when module is disabled
 *
 * @ingroup tripal_library
 */
function tripal_library_disable() {

  // Disable all default views provided by this module
  require_once("tripal_library.views_default.inc");
  $views = tripal_library_views_default_views();
  foreach (array_keys($views) as $view_name) {
    tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  }

}

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

/**
 * Implementation of hook_install().
 *
 * @ingroup tripal_library
 */
function tripal_library_install() {

  // add cvterms
  tripal_library_add_cvs();
  tripal_library_add_cvterms();

  // set the default vocabularies
  tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  tripal_set_default_cv('library', 'type_id', 'library_type');
}

/**
 * Implementation of hook_uninstall().
 *
 * @ingroup tripal_library
 */
function tripal_library_uninstall() {

}

/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_library
 */
function tripal_library_schema() {
  $schema['chado_library'] = 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
      ),
      'library_id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      )
    ),
    'indexes' => array(
      'chado_library_idx1' => array('library_id')
    ),
    'unique keys' => array(
      'chado_library_uq1' => array('nid', 'vid'),
      'chado_library_uq2' => array('vid')
    ),
    'primary key' => array('nid'),
  );
  return $schema;
}

/**
 * Adds new CV's used by this module
 *
 * @ingroup tripal_library
 */
function tripal_library_add_cvs(){
  tripal_insert_cv(
    'library_property',
    'Contains properties for libraries.'
  );
  tripal_insert_cv(
    'library_type',
    'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  );
}

/**
 * Adds cvterms needed for the library module
 *
 * @ingroup tripal_library
 */
function tripal_library_add_cvterms() {

  // Insert cvterm 'library_description' into cvterm table of chado
  // database. This CV term is used to keep track of the library
  // description in the libraryprop table.
  tripal_insert_cvterm(
     array(
       'name' => 'Library Description',
       'definition' => 'Description of a library',
       'cv_name' => 'library_property',
       'is_relationship' => 0,
       'db_name' => 'tripal'
      ),
      array('update_existing' => TRUE)
   );

  // add cvterms for the map unit types
  tripal_insert_cvterm(
    array(
      'name' => 'cdna_library',
      'definition' => 'cDNA library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
  tripal_insert_cvterm(
    array(
      'name' => 'bac_library',
      'definition' => 'Bacterial Artifical Chromsome (BAC) library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
  tripal_insert_cvterm(
    array(
      'name' => 'fosmid_library',
      'definition' => 'Fosmid library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
  tripal_insert_cvterm(
    array(
      'name' => 'cosmid_library',
      'definition' => 'Cosmid library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
  tripal_insert_cvterm(
    array(
      'name' => 'yac_library',
      'definition' => 'Yeast Artificial Chromosome (YAC) library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
  tripal_insert_cvterm(
    array(
      'name' => 'genomic_library',
      'definition' => 'Genomic Library',
      'cv_name' => 'library_type',
      'is_relationship' => 0,
      'db_name' => 'tripal'
    ),
    array('update_existing' => TRUE)
  );
}