<?php
/**
 * @file
 * Basic function for the natural diversity module
 */

require_once 'theme/tripal_natural_diversity.theme.inc';

require_once 'includes/tripal_natural_diversity.admin.inc';

/**
 * @defgroup tripal_natural_diversity Natural Diversity Module
 * @ingroup tripal_modules
 * @{
 * Provides functions for managing chado natural diversity data
 * @}
 */

/**
 * Implements hook_permission().
 *
 * Set the permission types that the chado module uses.  Essentially we
 *  want permissionis
 *
 * @ingroup tripal_natural_diversity
 */
function tripal_natural_diversity_permission() {
  return array(
    /*
    'administer tripal nd' => array(
      'title' => t('Administer Natural Diversity Module'),
      'description' => t('Allow users to administer the natural diversity module.'),
    ),
    */
  );
}

/**
 *  Implements hook_menu().
 *
 *  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_natural_diversity
 */
function tripal_natural_diversity_menu() {
  $items = array();

    // the administative settings menu

  $items['admin/tripal/legacy/tripal_natdiv'] = array(
   'title' => 'Natural Diversity Experiments',
   'description' => 'Experiments relating to natural diversity such as genotype and phenotype experiments.',
   'page callback' => 'tripal_natural_diversity_admin_natdiv_view',
   'access arguments' => array('administer tripal nd'),
   'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/tripal/legacy/tripal_natdiv/help'] = array(
    'title' => 'Help',
    'description' => ('Help for the Tripal natural diversity module.'),
    'page callback' => 'theme',
    'page arguments' => array('tripal_natural_diversity_help'),
    'access arguments' => array('administer tripal nd'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10
  );

  $items['admin/tripal/legacy/tripal_natdiv/views/natdiv_exp/enable'] = array(
    'title' => 'Enable Natural Diversity Administrative View',
    'page callback' => 'tripal_enable_view',
    'page arguments' => array('tripal_natural_diversity_admin_natdiv_exp', 'admin/tripal/legacy/tripal_natdiv'),
    'access arguments' => array('administer tripal nd'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

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

/**
 * Implements hook_theme().
 *
 * @ingroup tripal_natural_diversity
 */
function tripal_natural_diversity_theme($existing, $type, $theme, $path) {
  $core_path = drupal_get_path('module', 'tripal_core');

  $items = array(
    // tripal_feature templates
    'tripal_feature_nd_genotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_feature_nd_genotypes',
      'path' => "$path/theme/templates",
    ),
    // tripal_stock templates
    'tripal_stock_nd_genotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_stock_nd_genotypes',
      'path' => "$path/theme/templates",
    ),
    'tripal_stock_nd_phenotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_stock_nd_phenotypes',
      'path' => "$path/theme/templates",
    ),
    'tripal_natural_diversity_help' => array(
      'template' => 'tripal_natural_diversity_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme/templates",
    ),
  );
  return $items;
}

/**
 * Implements hook_node_view(). Acts on all content types.
 *
 * @ingroup tripal_natural_diversity
 */
function tripal_natural_diversity_node_view($node, $view_mode, $langcode) {
  switch ($node->type) {
    case 'chado_feature':
      if ($view_mode == 'full') {
        // the tripal_genetic module provides a tripal_feature_genotype
        // template. if the tripal_genetic content is present get rid of it as this
        // module superceeds it.
        if (array_key_exists('tripal_feature_genotypes', $node->content)) {
          unset($node->content['tripal_feature_genotypes']);
        }
        $node->content['tripal_feature_nd_genotypes'] = array(
          '#theme' => 'tripal_feature_nd_genotypes',
          '#node' => $node,
          '#tripal_toc_id'    => 'genotypes',
          '#tripal_toc_title' => 'Genotypes',
        );
      }
      break;
    case 'chado_stock':
      if ($view_mode == 'full') {
        // the tripal_genetic module provides a tripal_stock_genotype
        // template. if the tripal_genetic content is present get rid of it as this
        // module superceeds it.
        if (array_key_exists('tripal_stock_genotypes', $node->content)) {
          unset($node->content['tripal_stock_genotypes']);
        }
        $node->content['tripal_stock_nd_genotypes'] = array(
          '#theme' => 'tripal_stock_nd_genotypes',
          '#node' => $node,
          '#tripal_toc_id'    => 'genotypes',
          '#tripal_toc_title' => 'Genotypes',
        );
        $node->content['tripal_stock_nd_phenotypes'] = array(
          '#theme' => 'tripal_stock_nd_phenotypes',
          '#node' => $node,
          '#tripal_toc_id'    => 'phenotypes',
          '#tripal_toc_title' => 'Phenotypes',
        );
      }
      break;
  }
}
