<?php

/**
 * @file
 * Functions pertaining to the install/uninstall of this module
 */

/**
 * Implementation of hook_install().
 *
 * @ingroup tripal_organism
 */
function tripal_organism_install() {

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

  // create the directory where image files will be stored.  We create this
  // here otherwise it will get created when the first organism is synced.
  // The user that performs the syncing will receive ownership of the
  // images directory which may not allow for write access by the web server
  // user.  So, we create it here
  $dest = file_directory_path() . "/tripal/tripal_organism/images";
  file_check_directory($dest, FILE_CREATE_DIRECTORY);

  // create the tables that correlate drupal nodes with chado
  // features, organisms, etc....
  drupal_install_schema('tripal_organism');

}

/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_organism
 */
function tripal_organism_schema() {
  $schema = tripal_organism_get_schemas();
  return $schema;
}

/**
 * Implementation of hook_uninstall().
 *
 * @ingroup tripal_organism
 */
function tripal_organism_uninstall() {
  drupal_uninstall_schema('tripal_organism');

  // Get the list of nodes to remove
  $sql_lib_id = "SELECT nid, vid ".
               "FROM {node} ".
               "WHERE type='chado_organism'";
  $result = db_query($sql_lib_id);
  while ($node = db_fetch_object($result)) {
    node_delete($node->nid);
  }

  // remove the materialized views

  // Remove the custom view if exists
  if (db_table_exists('tripal_organism_views_common_name')) {
    $sql = "DROP TABLE {tripal_organism_views_common_name}";
    db_query($sql);
  }
}

/**
 * This function simply defines all tables needed for the module to work
 * correctly.  By putting the table definitions in a separate function we
 * can easily provide the entire list for hook_install or individual
 * tables for an update.
 *
 * @ingroup tripal_organism
 */
function tripal_organism_get_schemas() {
  $schema = array();

  $schema['chado_organism'] = 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
          ),
         'organism_id' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0
          )
      ),
      'indexes' => array(
         'organism_id' => array('organism_id')
      ),
      'unique keys' => array(
         'nid_vid' => array('nid', 'vid'),
         'vid' => array('vid')
      ),
      'primary key' => array('nid'),
  );
  return $schema;
}

/**
 * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
 * before installation
 *
 * @ingroup tripal_organism
 */
function tripal_organism_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    if (!function_exists('tripal_create_moddir')) {
      $requirements ['tripal_organism'] = array(
        'title' => "tripal_organism",
        'value' => "error. Some required modules are just being installed. Please try again.",
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}