<?php

/**
*  Implementation of hook_install();
*
* @ingroup tripal_cv
*/
function tripal_cv_install(){

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

   // Add the materialized view needed to keep track of the 
   // 
   $previous_db = tripal_db_set_active('chado');
   if (db_table_exists('cv_root_mview')) {
      $sql = "DROP TABLE cv_root_mview";
      db_query($sql);
   }
   tripal_db_set_active($previous_db);

   // Create the MView
   tripal_add_mview(
      // view name
      'cv_root_mview',
      // module name  
      'tripal_cv',
      // table name
      'cv_root_mview',
      // table schema
      'name character varying(1024), cvterm_id integer, cv_id integer, 
       cv_name character varying(255)',
      // indexed columns
      'cvterm_id, cv_id',
      // SQL statement that populates the view
      'SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
       FROM {cvterm_relationship} CVTR
         INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
         INNER JOIN CV on CV.cv_id = CVT.cv_id
       WHERE CVTR.object_id not in
         (SELECT subject_id FROM {cvterm_relationship}) ',
      // special index
      ''
   );

   // create the tables that correlate OBO files/references with a chado CV
   drupal_install_schema('tripal_cv');
   tripal_cv_add_obo_defaults();
}
/**
*  This update adds the new tripal_obo table.  This is an upgrade from
*  Tripal version 0.2
*
* @ingroup tripal_cv
*/

function tripal_cv_update_6000(){
   drupal_install_schema('tripal_cv');
   tripal_cv_add_obo_defaults();
   $ret = array(
      '#finished' => 1,
   );
   
   return $ret;
}
/**
* Implementation of hook_uninstall()
*
* @ingroup tripal_cv
*/
function tripal_cv_uninstall(){

	// remove the materialized view
   $mview = tripal_mviews_get_mview_id('cv_root_mview');
   if($mview){
	   tripal_mviews_action('delete',$mview);
	}

   drupal_uninstall_schema('tripal_cv');
}

/**
 * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
 * before installation
 *
 * @ingroup tripal_cv
 */
function tripal_cv_requirements($phase) {
   $requirements = array();
   if ($phase == 'install') {
      if (!function_exists('tripal_create_moddir')) {
         $requirements ['tripal_cv'] = array(
            'title' => "tripal_cv",
            'value' => "Required modules must be installed first before Tripal CV module can be installed",
            'severity' => REQUIREMENT_ERROR,
         );
      }
   }
   return $requirements;
}
/**
* Implementation of hook_schema().
*
* @ingroup tripal_cv
*/
function tripal_cv_schema() {
   $schema = tripal_cv_get_schemas();
   return $schema;
}
/**
* 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_cv
*/
function tripal_cv_get_schemas (){  
  $schema = array();

  $schema['tripal_cv_obo'] = array(
      'fields' => array(
         'obo_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
         'name'  => array('type' => 'varchar','length' => 255),
         'path'   => array('type' => 'varchar','length' => 1024),
      ),
      'indexes' => array(
         'obo_id' => array('obo_id'),
       ),
      'primary key' => array('obo_id'),
  );
  return $schema;
}
/**
* Add's defaults to the tripal_cv_obo table
*
* @ingroup tripal_cv
*/
function tripal_cv_add_obo_defaults(){
   // insert commonly used ontologies into the tables
   $isql = "INSERT INTO tripal_cv_obo (name,path) VALUES ('%s','%s')";
   db_query($isql,'Chado Feature Properties',drupal_get_path('module','tripal_cv').'/feature_property.obo');
   db_query($isql,'Relationship Ontology','http://www.obofoundry.org/ro/ro.obo');
   db_query($isql,'Sequence Ontology','http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo');
   db_query($isql,'Gene Ontology','http://www.geneontology.org/ontology/gene_ontology.obo');
   db_query($isql,'Cell Ontology','http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD');
   db_query($isql,'Plant Structure Ontology','http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co');  
   db_query($isql,'Plant Growth and Development Stages Ontology','http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co');

}