<?php

/**
 * @file
 * This file contains all the functions which describe and implement drupal database tables
 * needed by this module. This module was developed by Chad N.A. Krilow and Lacey-Anne Sanderson,
 * University of Saskatchewan.
 *
 * The project manamgenet module allows you to sync data in a chado/Tripal instance with
 * multiple project/mysql instances as well as manage and create such project instances
 */

/**
 * Disable default views when module is disabled
 */
function tripal_project_disable() {

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

}

/**
 * Implementation of hook_requirements().
 *
 */
function tripal_project_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    // make sure chado is installed
    if (!$GLOBALS["chado_is_installed"]) {
      $requirements ['tripal_project'] = array(
        'title' => "tripal_project",
        'value' => "ERROR: Chado must be installed before this module can be enabled",
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}
/**
 * Implementation of hook_install().
 */
function tripal_project_install() {

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

  tripal_project_add_cvs();
  tripal_project_add_cvterms();
}

/**
 * Implementation of hook_uninstall().
 */
function tripal_project_uninstall() {

}

/**
 * Implementation of hook_schema().
 */
function tripal_project_schema() {
  $schema['chado_project'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
          'type' => 'int',
          'not null' => TRUE,
      ),
      'project_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('nid', 'vid', 'project_id'),
  );
  return $schema;
}

/**
 *
 */
function tripal_project_add_cvs() {
  tripal_cv_add_cv('project_property', 'Contains properties for projects');

  // Add cv for relationship types
  tripal_cv_add_cv(
    'project_relationship_types',
    'Contains Types of relationships between projects.'
  );
}
/**
 *
 */
function tripal_project_add_cvterms() {

  // Insert cvterm 'Project Description' into cvterm table of chado
  // database. This CV term is used to keep track of the project
  // description in the projectprop table.
  tripal_cv_add_cvterm(
    array(
      'name' => 'Project Description',
      'def'  => 'Description of a project'
    ),
    'project_property', 0, 1, 'tripal'
  );

  tripal_cv_add_cvterm(
    array(
      'name' => 'Project Type',
      'def'  => 'The short few word description of the type of project.'
    ),
   'project_property', 0, 1, 'tripal'
  );
  tripal_cv_add_cvterm(
    array(
      'name' => 'Funding Source',
      'def'  => 'The funding source for this project.'
    ),
    'project_property', 0, 1, 'tripal'
  );
}

/**
 * This is the required update for tripal_project when upgrading from Drupal core API 6.x.
 */
function tripal_project_update_7000() {

  // For Tripal in Drupal 6 the project_description cvterm was stored in the
  // 'tripal' CV.  It should be stored in the new project_property CV that
  // is added by this module for Tripal 2.0 and Drupal 7.  So, we need to
  // reset the CV ID for that term and rename the term to 'Project Description'
  tripal_project_add_cvs();
  $cv = tripal_cv_get_cv_by_name('project_property');
  $cvterm = tripal_cv_get_cvterm_by_name('project_description', $cv->cv_id);
  $match = array(
    'cvterm_id' => $cvterm->cvterm_id,
  );
  $values = array(
    'name'  => 'Project Description',
  );
  tripal_core_chado_update('cvterm', $match, $values);
  if (!$success) {
    throw new DrupalUpdateException('Failed to move project properties to new project_property CV.');
  }

  // add in new CVterms
  tripal_project_add_cvterms();
}