<?php

/**
 * Implementation of hook_install().
 */
function bio_ontologies_install () {
  // Set the weight of the bio_ontologies.module to 1 so it is loaded after
  // the taxonomy and taxonomy_bulk
  db_query("UPDATE {system} SET weight = 1 WHERE name = 'bio_ontologies'");
}

function bio_ontologies_enable() {
  _bio_ontologies_create_vocab(
    'bio_ontologies_biological_process_vocabulary',
    'Biological processes',
    "Terms from the OBO GO Ontology's 'biological_process' namespace."
  );
  _bio_ontologies_create_vocab(
    'bio_ontologies_cellular_component_vocabulary',
    'Cellular components',
    "Terms from the OBO GO Ontology's 'cellular_component' namespace."
  );
  _bio_ontologies_create_vocab(
    'bio_ontologies_molecular_function_vocabulary',
    'Molecular functions',
    "Terms from the OBO GO Ontology's 'molecular_function' namespace."
  );
}

function _bio_ontologies_create_vocab ($var, $name, $description) {
  static $wt = 20;
  $vid = variable_get($var, 0);
  $vocab = NULL;
  if ($vid > 0) {
    $vocab = taxonomy_vocabulary_load($vid);
  }
  if (!isset($vocab)) {
    // Create the vocabulary if it does not exist.
    $wt++;
    $vocab = array(
      'name' => t($name),
      'multiple' => 1,
      'required' => 0,
      'hierarchy' => 1,
      'relations' => 0,
      'tags' => 1,
      'module' => 'bio_ontologies',
      'description' => t($description),
      'weight' => $wt,
      'nodes' => array()
    );
    taxonomy_save_vocabulary($vocab);
  }
  if (!$vid) {
    $vid = $vocab['vid'];
    variable_set($var, $vid);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function bio_ontologies_uninstall() {
  // Load the required Taxonomy module, in case it has been disabled.
  drupal_load('module', 'taxonomy');
  // Delete the vocabularies
  foreach (array('biological_process', 'cellular_component', 'molecular_function') as $ns) {
    _bio_ontologies_delete_vocab('bio_ontologies_' . $ns . '_vocabulary');
  }
}

function _bio_ontologies_delete_vocab ($var) {
  $vid = variable_get($var, '');
  if (!empty($vid)) {
    @taxonomy_del_vocabulary($vid);
  }
  variable_del($var);
}