<?php
/**
 * @file
 * Basic functionality for the phenotype module
 */

/**
 * @defgroup tripal_phenotype Phenotype Module
 * @ingroup tripal_modules
 * @{
 * Provides functions for managing chado phenotype data
 * @}
 */

/**
 * Implements hook_permission().
 *
 * Set the permission types that the chado module uses.  Essentially we
 *  want permissionis
 *
 * @ingroup tripal_phenotype
 */
function tripal_phenotype_permission() {
  return array(
    'adminster tripal phenotype' => array(
      'title' => t('Administer Phenotype Module'),
      'description' => t('Allow users to administer the phenotype 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_phenotype
 */
function tripal_phenotype_menu() {
  $items = array();

    // the administative settings menu
  $items['admin/tripal/chado/tripal_phenotype'] = array(
   'title' => 'Phenotypes',
   'description' => 'A controlled sentence describing observable effects of non-wild type function.',
   'page callback' => 'tripal_phenotype_admin_phenotypes_listing',
   'access arguments' => array('adminster tripal phenotype'),
   'type' => MENU_NORMAL_ITEM,
  );

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

  $items['admin/tripal/chado/tripal_phenotype/views/phenotypes/enable'] = array(
    'title' => 'Enable Phenotype Administrative View',
    'page callback' => 'tripal_views_admin_enable_view',
    'page arguments' => array('tripal_phenotype_admin_phenotypes', 'admin/tripal/chado/tripal_phenotype'),
    'access arguments' => array('administer tripal phenotype'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 *  Implements hook_theme().
 *
 *  We need to let drupal know about our theme functions and their arguments.
 *  We create theme functions to allow users of the module to customize the
 *  look and feel of the output generated in this module
 *
 * @ingroup tripal_phenotype
 */
function tripal_phenotype_theme($existing, $type, $theme, $path) {
  $core_path = drupal_get_path('module', 'tripal_core');

  $items = array(
    'tripal_feature_phenotypes' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_feature_phenotypes',
      'path' => "$path/theme/tripal_feature",
    ),
    'tripal_phenotype_help' => array(
      'template' => 'tripal_phenotype_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme",
    ),
  );
  return $items;
}

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

/**
 * Admin Launchpad
 *
 * @ingroup tripal_phenotype
 */
function tripal_phenotype_admin_phenotypes_listing() {
  $output = '';

  // set the breadcrumb
  $breadcrumb = array();
  $breadcrumb[] = l('Home', '<front>');
  $breadcrumb[] = l('Administration', 'admin');
  $breadcrumb[] = l('Tripal', 'admin/tripal');
  $breadcrumb[] = l('Chado', 'admin/tripal/chado');
  $breadcrumb[] = l('Phenotypes', 'admin/tripal/chado/tripal_phenotype');
  drupal_set_breadcrumb($breadcrumb);

  // Add the view
  $view = views_embed_view('tripal_phenotype_admin_phenotypes','default');
  if (isset($view)) {
    $output .= $view;
  }
  else {
    $output .= '<p>The Tripal Phenotype Module uses primarily views to provide an '
      . 'administrative interface. Currently one or more views needed for this '
      . 'administrative interface are disabled. <strong>Click each of the following links to '
      . 'enable the pertinent views</strong>:</p>';
    $output .= '<ul>';
      $output .= '<li>'.l('Phenotype Admin', 'admin/tripal/chado/tripal_phenotype/views/phenotypes/enable').'</li>';
    $output .= '</ul>';
  }

  return $output;
}

/**
 * Implements hook_node_view().
 *
 * @ingroup tripal_phenotype
 */
function tripal_phenotype_node_view($node, $view_mode, $langcode) {
  switch ($node->type) {
    case 'chado_feature':
      // Show feature browser and counts
      if ($view_mode == 'full') {
        $node->content['tripal_feature_phenotypes'] = array(
          '#markup' => theme('tripal_feature_phenotypes', array('node' => $node)),
          '#tripal_toc_id'    => 'phenotypes',
          '#tripal_toc_title' => 'Phenotypes',
        );
      }
      break;
  }
}