<?php
/**
 * @file
 * Installation of the feature module
 */

/**
 * Implements hook_disable().
 *
 * Disable default views when module is disabled
 *
 * @ingroup tripal_legacy_feature
 */
function tripal_feature_disable() {

  // Disable all default views provided by this module
  require_once("tripal_feature.views_default.inc");
  $views = tripal_feature_views_default_views();
  foreach (array_keys($views) as $view_name) {
    tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  }

}

/**
 * Implements hook_requirements().
 *
 * @ingroup tripal_legacy_feature
 */
function tripal_feature_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    // make sure chado is installed
    if (!$GLOBALS["chado_is_installed"]) {
      $requirements ['tripal_feature'] = array(
          'title' => "t ripal_feature",
          'value' => "ERROR: Chado must be installed before this module can be enabled",
          'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 *
 * @ingroup tripal_legacy_feature
 */
function tripal_feature_install() {

  // Note: the feature_property OBO that came with Chado v1.2 should not
  // be automatically installed.  Some of the terms are duplicates of
  // others in better maintained vocabularies.  New Tripal sites should
  // use those.
  // $obo_path = '{tripal_feature}/files/feature_property.obo';
  // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  // tripal_submit_obo_job(array('obo_id' => $obo_id));

  // Add the vocabularies used by the feature module.
  tripal_feature_add_cvs();

  // Set the default vocabularies.
  tripal_set_default_cv('feature', 'type_id', 'sequence');
  tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
}

/**
 * Implements hook_uninstall().
 *
 * @ingroup tripal_legacy_feature
 */
function tripal_feature_uninstall() {

}
/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_legacy_feature
 */
function tripal_feature_schema() {

  $schema['chado_feature'] = 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
       ),
      'feature_id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ),
      'sync_date' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'UNIX integer sync date/time'
      ),
    ),
    'indexes' => array(
      'chado_feature_idx1' => array('feature_id')
    ),
    'unique keys' => array(
      'chado_feature_uq1' => array('nid', 'vid'),
      'chado_feature_uq2' => array('vid')
    ),
    'primary key' => array('nid'),
  );

  return $schema;
};

/**
 * Add cvs related to publications
 *
 * @ingroup tripal_pub
 */
function tripal_feature_add_cvs() {

  // Add cv for relationship types
  tripal_insert_cv(
    'feature_relationship',
    'Contains types of relationships between features.'
  );

  // The feature_property CV may already exists. It comes with Chado, but
  // we need to  add it just in case it doesn't get added before the feature
  // module is installed. But as of Tripal v3.0 the Chado version of this
  // vocabulary is no longer loaded by default.
  tripal_insert_cv(
    'feature_property',
    'Stores properties about features'
  );

  // the feature type vocabulary should be the sequence ontology, and even though
  // this ontology should get loaded we will create it here just so that we can
  // set the default vocabulary for the feature.type_id field
  tripal_insert_cv(
    'sequence',
    'The Sequence Ontology'
  );
}
