<?php

/**
 *  @file
 * This file contains the basic functions needed for this drupal module.
 * The drupal tripal_genetic module maps directly to the chado X module.
 *
 * For documentation regarding the Chado X module:
 * @see http://gmod.org/wiki/Chado_General_Module
 *
 * @defgroup tripal_genetic Genetic Module
 * @ingroup tripal_modules
 */

require('api/tripal_genetic.api.inc');
require('theme/tripal_genetic.theme.inc');
require('includes/tripal_genetic.schema.inc');
require('includes/tripal_genetic.admin.inc');

/**
 *  Menu items are automatically added for the new node types created
 *  by this module to the 'Create Content' Navigation menu item.  This function
 *  adds more menu items needed for this module.
 *
 * @ingroup tripal_genetic
 */
function tripal_genetic_menu() {
  $items = array();

    // the administative settings menu
  $items['admin/tripal/chado/tripal_genetic'] = array(
   'title' => 'Genetics',
   'description' => 'Genetic data including Genotypes.',
   'page callback' => 'tripal_genetic_admin_genetics_listing',
   'access arguments' => array('adminster tripal genetic'),
   'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/tripal/chado/tripal_genetic/help'] = array(
   'title' => 'Help',
   'description' => "A description of the Tripal genetic module including a short description of it's usage.",
   'page callback' => 'theme',
   'page arguments' => array('tripal_genetic_help'),
   'access arguments' => array('adminster tripal genetic'),
   'type' => MENU_LOCAL_TASK,
  );

  $items['admin/tripal/chado/tripal_genetic/views/genetics/enable'] = array(
    'title' => 'Enable genetic Administrative View',
    'page callback' => 'tripal_views_admin_enable_view',
    'page arguments' => array('tripal_genetic_admin_genetics', 'admin/tripal/chado/tripal_genetic'),
    'access arguments' => array('administer tripal genetic'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_views_api()
 * Purpose: Essentially this hook tells drupal that there is views support for
 *  for this module which then includes tripal_genetic.views.inc where all the
 *  views integration code is
 *
 * @ingroup tripal_genetic
 */
function tripal_genetic_views_api() {
  return array(
      'api' => 3.0,
  );
}

/**
 * Implements hook_theme().
 *
 * @ingroup tripal_genetic
 */
function tripal_genetic_theme($existing, $type, $theme, $path) {
  $core_path = drupal_get_path('module', 'tripal_core');
    
  $items = array(
    'tripal_feature_genotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_feature.genotypes',
      'path' => "$path/theme/tripal_feature",
    ),
    'tripal_stock_genotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_stock.genotypes',
      'path' => "$path/theme/tripal_stock",
    ),
    'tripal_genetic_help' => array(
      'template' => 'tripal_genetic.help',
      'variables' =>  array(NULL),
      'path' => "$path/theme/",
    ),
  );
  return $items;
}

/**
 * Implements hook_nodeapi().
 *
 * @ingroup tripal_genetic
 */
function tripal_genetic_node_view($node, $view_mode, $langcode) {
  
  if ($node->type == 'chado_feature') {
    if ($view_mode == 'full') {
      // the tripal_natural_diversity module provides a tripal_feature_nd_genotype
      // template.  The ND template superceeds this one. Therefore,
      // if the tripal_natural_diversity content is present then don't add the
      // template from this module as the ND module would superceed this.
      if (!array_key_exists('tripal_feature_nd_genotypes', $node->content)) {
        $node->content['tripal_feature_genotypes'] = array(
          '#value' => theme('tripal_feature_genotypes', array('node' => $node)),
        );
      }
    }
  }
  if ($node->type == 'chado_stock') {
    if ($view_mode == 'full') {
      // the tripal_natural_diversity module provides a tripal_stock_nd_genotype
      // template.  The ND template superceeds this one. Therefore,
      // if the tripal_natural_diversity content is present then don't add the
      // template from this module as the ND module would superceed this.
      if (!array_key_exists('tripal_stock_nd_genotypes', $node->content)) {
        $node->content['tripal_stock_genotypes'] = array(
          '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
        );
      }
    }
  }
}