<?php
/**
 * @file
 * Basic functionality for the genetic
 */

/**
 * @defgroup tripal_genetic Genetic Module
 * @ingroup tripal_modules
 * @{
 * Provides functions for managing chado genetic data
 * @}
 */

require_once 'theme/tripal_genetic.theme.inc';

require_once 'includes/tripal_genetic.schema.inc';
require_once 'includes/tripal_genetic.admin.inc';

/**
 * Implements hook_permission().
 *
 * Set the permission types that the chado module uses.  Essentially we
 *  want permissionis
 *
 * @ingroup tripal_genetic
 */
function tripal_genetic_permission() {
  return array(
    'administer tripal genetic' => array(
      'title' => t('Administer Genetic Module'),
      'description' => t('Allow users to administer the genetic 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_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('administer 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('administer tripal genetic'),
   'type' => MENU_LOCAL_TASK,
  );

  $items['admin/tripal/chado/tripal_genetic/views/genetics/enable'] = array(
    'title' => 'Enable genetic Administrative View',
    'page callback' => 'tripal_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_search_biological_data_views().
 *
 * Adds the described views to the "Search Data" Page created by Tripal Views
 */
function tripal_genetic_search_biological_data_views() {
  return array(
    'tripal_genetic_user_genotypes' => array(
      'machine_name' => 'tripal_genetic_user_genotypes',
      'human_name' => 'Genotypes',
      'description' => 'Genetic variations such as SNPs, MNPs and indels.',
      'link' => 'chado/genotype'
    ),
  );
}

/**
 * Implements hook_views_api().
 *
 * 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/templates",
    ),
    'tripal_stock_genotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_stock_genotypes',
      'path' => "$path/theme/templates",
    ),
    'tripal_genetic_help' => array(
      'template' => 'tripal_genetic_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme/templates",
    ),
  );
  return $items;
}

/**
 * Implements hook_node_view(). Acts on all content types
 *
 * @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(
          '#theme' => 'tripal_feature_genotypes',
          '#node' => $node,
          '#tripal_toc_id'    => 'genotypes',
          '#tripal_toc_title' => 'Genotypes',
        );
      }
    }
  }
  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(
          '#theme' => 'tripal_stock_genotypes',
          '#node' => $node,
          '#tripal_toc_id'    => 'genotypes',
          '#tripal_toc_title' => 'Genotypes',
        );
      }
    }
  }
}