<?php
/**
 * @file
 * Contains functions used to install/uninstall tripal_core.
 */

/**
 * Implementation of hook_install().
 *
 * @ingroup tripal_legacy_core
 */
function tripal_core_install() {


}
/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_legacy_core
 */
function tripal_core_schema() {

  // Get the schemas defined by this install file.
  $schema = tripal_core_get_schemas();

  // if this module is already installed and enabled, then we want to provide
  // the schemas for all of the custom tables.  This will allow Views to
  // see the schemas.  We check if the module is installed because during
  // installation we don't want to make these custom tables available as we
  // don't want them created in the Drupal database.  The custom tables go in
  // the Chado database.
  if (db_table_exists('tripal_custom_tables')) {
    $sql = 'SELECT * FROM {tripal_custom_tables}';
    $results = db_query($sql);
    foreach ($results as $custom) {
      $schema[$custom->table_name] = unserialize($custom->schema);
    }
  }

  return $schema;
}

/**
 * Implementation of hook_uninstall().
 *
 * @ingroup tripal_legacy_core
 */
function tripal_core_uninstall() {

  // drop the foreign key between tripal_custom_tables and tripal_mviews
  // so that Drupal can then drop the tables
//   db_query('
//     ALTER TABLE {tripal_custom_tables}
//     DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
//   ');
}

/**
 * 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_legacy_core
 */
function tripal_core_get_schemas() {
  $schema = array();

  // Get all the various schema parts and join them together
  $temp = tripal_core_get_tripal_toc_schema();
  foreach ($temp as $table => $arr) {
    $schema[$table] = $arr;
  }
  $temp = tripal_core_get_tripal_vars_schema();
  foreach ($temp as $table => $arr) {
    $schema[$table] = $arr;
  }

  return $schema;
}

/**
 *
 */
function tripal_core_get_tripal_toc_schema() {
  $schema = array();
  $schema['tripal_toc'] = array(
    'fields' => array(
      'toc_item_id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'node_type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE
      ),
      'key' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => FALSE
      ),
      'hide' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => FALSE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => FALSE,
      )
    ),
    'indexes' => array(
      'tripal_toc_idx1' => array('node_type', 'key'),
      'tripal_toc_idx2' => array('node_type', 'key', 'nid'),
    ),
    'unique keys' => array(
      'tripal_toc_uq1' => array('node_type', 'key', 'nid'),
    ),
    'primary key' => array('toc_item_id'),
  );

  return $schema;
}

/**
 *
 */
function tripal_core_get_tripal_vars_schema() {
  $schema = array();

  $schema['tripal_node_variables'] = array(
    'description' => 'This table is used for storing any type of variable such as ' .
      'a property or setting that should be associated with a Tripal managed Drupal node.  This table is '.
      'meant to store non-biological information only. All biological data should be housed ' .
      'in the Chado tables. Be aware that any data stored here will not be made visible ' .
      'through services such as Tripal Web Services and therefore can be a good place to ' .
      'hide application specific settings.',
    'fields' => array (
      'node_variable_id' => array (
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'nid' => array (
        'type' => 'int',
        'not null' => TRUE,
      ),
      'variable_id' => array (
        'type' => 'int',
        'not null' => TRUE,
      ),
      'value' => array (
        'type' => 'text',
        'not null' => FALSE,
      ),
      'rank' => array (
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array (
      0 => 'node_variable_id',
    ),
    'unique keys' => array (
      'tripal_node_variables_c1' => array (
        0 => 'nid',
        1 => 'variable_id',
        2 => 'rank',
      ),
    ),
    'indexes' => array (
      'tripal_node_variables_idx1' => array (
        0 => 'variable_id',
      ),
    ),
    'foreign keys' => array (
      'tripal_variables' => array (
        'table' => 'tripal_variables',
        'columns' => array (
          'variable_id' => 'variable_id',
        ),
      ),
    ),
  );

  return $schema;
}