<?php

/*******************************************************************************
*  Implementation of hook_install();
*/
function tripal_analysis_kegg_install(){
   // create the module's data directory
   tripal_create_moddir('tripal_analysis_kegg');
   
   // We need to register to tripal_analysis module so it can provide a control
   // for our kegg result. Basically the registration is done by inserting
   // modulename into the drupal {tripal_analysis} table AND inserting required
   // information to the chado Analysis table. Also in tripal_analysis_interpro.module,
   // we need to define HOOK_get_settings() for the module to work properly.
   
   // Inert into drupal's {tripal_analysis}
   tripal_analysis_register_child('tripal_analysis_kegg');

   // Add cvterm 'analysis_interpro_settings' for inserting into analysisprop table
   tripal_add_cvterms('analysis_kegg_settings', 'Settings of a KEGG analysis, '.
      'Currently include only the heir.tar.gz file name & path.');
   tripal_add_cvterms('kegg_brite_data', 'This term is intended for use '.
      'in the analysisfeatureprop table to store the KAAS results data.');
   
   // add the db and the cv for the KEGG terms
   tripal_add_db('KEGG','KEGG: Kyoto Encyclopedia of Genes and Genomes.',
      'http://www.genome.jp',
      'http://www.genome.jp/kegg/');

  tripal_add_mview(
      // view name
      'kegg_by_organism',
      // tripal module name
      'kegg_by_organism',
      // table name
      'kegg_by_organism',
      // table schema definition
      'analysis_name character varying(255),
       analysis_id integer,
       organism_id integer',
      // columns for indexing
      'analysis_id,organism_id',
      // SQL statement to populate the view
      "SELECT DISTINCT A.name,A.analysis_id,F.organism_id
       FROM {analysisprop} AP
          INNER JOIN analysis A on A.analysis_id = AP.analysis_id
          INNER JOIN cvterm CVT on CVT.cvterm_id = AP.type_id
          INNER JOIN analysisfeature AF on AF.analysis_id = A.analysis_id
          INNER JOIN feature F on F.feature_id = AF.feature_id
       WHERE CVT.name = 'analysis_kegg_settings'",
      // special index
      ''
   );
}

/*******************************************************************************
* Implementation of hook_uninstall()
*/
function tripal_analysis_kegg_uninstall(){
//   tripal_delete_db('KEGG');
   tripal_analysis_unregister_child('tripal_analysis_kegg');

   $mview = tripal_mviews_get_mview_id('kegg_by_organism');
   if($mview){
	   tripal_mviews_action('delete',$mview);
	}
}

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