<?php
//$Id:

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

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

	// Use schema API to create database table.
	drupal_install_schema('tripal_analysis');
	
	// Create analysisfeatureprop table in chado. This cannot be accomplished
	// by calling drupal_install_schema because it's not in the drupal db. This
	// table is used to store Blast xml and Interpro html/goterms
	$previous_db = tripal_db_set_active('chado');
	if (!db_table_exists('analysisfeatureprop')) {
	   $sql = "CREATE TABLE {analysisfeatureprop} (".
	          "  analysisfeatureprop_id SERIAL PRIMARY KEY, ".
	          "  analysisfeature_id INTEGER NOT NULL REFERENCES analysisfeature(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
	          "  type_id INTEGER NOT NULL REFERENCES cvterm(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
	          "  value TEXT, ".
	          "  rank INTEGER NOT NULL, ".
	          "  CONSTRAINT analysisfeature_id_type_id_rank UNIQUE(analysisfeature_id, type_id, rank)".
	          ")";
	   db_query($sql);
	}
	tripal_db_set_active($previous_db);

   tripal_add_cvterms('analysis_type','The type of analysis was performed. This value is automatically set by each Tripal Analysis module and should be equal to the module name (e.g. tripal_analysis_blast, tripal_analysis_go).');
	tripal_add_cvterms('analysis_date','The date that an analysis was performed.');
	tripal_add_cvterms('analysis_short_name','A computer legible (no spaces '.
      'or special characters) abbreviation for the analysis.');
}

/*******************************************************************************
 * Implementation of hook_uninstall().
 */
function tripal_analysis_uninstall() {
	// Use schema API to delete database table.
	drupal_uninstall_schema('tripal_analysis');
	// Remove analysis nodes from drupal.
	$sql_ana_id = "SELECT nid, vid ".
                 "FROM {node} ".
                 "WHERE type like 'chado_analysi%'";
	$result = db_query($sql_ana_id);
	while ($ana = db_fetch_object($result)) {
		node_delete($ana->nid);
	}
}

/*******************************************************************************
 * Implementation of hook_schema() creates two tables. chado_analysis table
 * stores nodes that are also saved in the analysis table of chado database.
 * tripal_analysis table stores the sub-module names, such as
 * tripal_analysis_blast, that are registered with this module.
 */
function tripal_analysis_schema() {
	// chado_analysis table
	$schema['chado_analysis'] = 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
	),
         'analysis_id' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0
	)
	),
      'indexes' => array(
         'analysis_id' => array('analysis_id')
	),
      'unique keys' => array(
         'nid_vid' => array('nid','vid'),
         'vid' => array('vid')
	),
      'primary key' => array('nid'),
	);

	// tripal_analysis table
	$schema['tripal_analysis'] = array(
      'description' => t('Table to store analysis sub-modules'),
      'fields' => array(
         'modulename' => array(
            'type' => 'text',
            'size' => 'small',
            'not null' => TRUE,
            'description' => t('The module name. Tripal Analysis will use the '.
                               'module name to call module_setting_form()')
	)
	),
      'unique keys' => array(
         'modulename' => array('modulename')
	)
	);
	return $schema;
}

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