<?php
/**
 * @file
 * Integrates the Chado Map module with Drupal Nodes & Views
 */

/**
 * @defgroup tripal_featuremap Feature Map Module
 * @ingroup tripal_modules
 * @{
 * Integrates the Chado Map module with Drupal Nodes & Views
 * @}
 */

require_once 'api/tripal_featuremap.DEPRECATED.inc';

require_once 'theme/tripal_featuremap.theme.inc';

require_once 'includes/tripal_featuremap.admin.inc';
require_once 'includes/tripal_featuremap.chado_node.inc';

/**
 * Implements hook_help().
 * Display help and module information
 *
 * @param path
 *   which path of the site we're displaying help
 * @param arg
 *   array that holds the current path as would be returned from arg() function
 * @return
 *  help text for the path
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#tripal_featuremap":
      $output = '<p>'.
      t("Displays links to nodes created on this date") .
               '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_permission().
 *
 * Set the permission types that the chado module uses.  Essentially we
 * want permissionis that protect creation, editing and deleting of chado
 * data objects
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_permission() {
  return array(
    'access chado_featuremap content' => array(
      'title' => t('View Maps'),
      'description' => t('Allow users to view map pages.'),
    ),
    'create chado_featuremap content' => array(
      'title' => t('Create Maps'),
      'description' => t('Allow users to create new map pages.'),
    ),
    'delete chado_featuremap content' => array(
      'title' => t('Delete Maps'),
      'description' => t('Allow users to delete map pages.'),
    ),
    'edit chado_featuremap content' => array(
      'title' => t('Edit Maps'),
      'description' => t('Allow users to edit map pages.'),
    ),
    'administer tripal featuremap' => array(
      'title' => t('Administer Maps'),
      'description' => t('Allow users to administer all maps.'),
    ),
  );
}

/**
 * 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_featuremap
 */
function tripal_featuremap_menu() {
  $items = array();

  // The administative settings menu
  $items['admin/tripal/chado/tripal_featuremap'] = array(
    'title' => 'Feature Maps',
    'description' => 'A map of features from the chado database (e.g. genetic map)',
    'page callback' => 'tripal_featuremap_admin_featuremaps_listing',
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/tripal/chado/tripal_featuremap/help'] = array(
    'title' => 'Help',
    'description' => 'Basic Description of Tripal Map Module Functionality',
    'page callback' => 'theme',
    'page arguments' => array('tripal_featuremap_help'),
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10
  );

  $items['admin/tripal/chado/tripal_featuremap/configuration'] = array(
    'title' => 'Settings',
    'description' => 'Manage integration of Chado maps including associated features.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_featuremap_admin'),
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2
  );

  $items['admin/tripal/chado/tripal_featuremap/sync'] = array(
    'title' => ' Sync',
    'description' => 'Sync featuremaps from Chado with Drupal',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('chado_node_sync_form', 'tripal_featuremap', 'chado_featuremap'),
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0
  );

  $items['admin/tripal/chado/tripal_featuremap/chado_featuremap_toc'] = array(
    'title' => ' TOC',
    'description' => 'Manage the table of contents for feature map nodes.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_core_content_type_toc_form', 'chado_featuremap'),
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_LOCAL_TASK,
    'file' =>  'includes/tripal_core.toc.inc',
    'file path' => drupal_get_path('module', 'tripal_core'),
    'weight' => 3
  );

  $items['admin/tripal/chado/tripal_featuremap/views/featuremaps/enable'] = array(
    'title' => 'Enable featuremap Administrative View',
    'page callback' => 'tripal_enable_view',
    'page arguments' => array('tripal_featuremap_admin_featuremaps', 'admin/tripal/chado/tripal_featuremap'),
    'access arguments' => array('administer tripal featuremap'),
    '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_featuremap_search_biological_data_views() {
  return array(
    'tripal_featuremap_user_featuremaps' => array(
      'machine_name' => 'tripal_featuremap_user_featuremaps',
      'human_name' => 'Maps',
      'description' => 'A map of features from the chado database (e.g. genetic map)',
      'link' => 'chado/featuremap'
    ),
  );
}

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

/**
 * 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_featuremap
 */
function tripal_featuremap_theme($existing, $type, $theme, $path) {
  $core_path = drupal_get_path('module', 'tripal_core');

  $items = array(
    'node__chado_featuremap' => array(
      'template' => 'node--chado-generic',
      'render element' => 'node',
      'base hook' => 'node',
      'path' => "$core_path/theme/templates",
    ),
    'tripal_featuremap_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_base',
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_properties',
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_featurepos' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_featurepos',
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_publication' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_publication',
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_references' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_references',
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_help' => array(
      'template' => 'tripal_featuremap_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme/templates",
    ),
    'tripal_featuremap_teaser' => array(
      'template' => 'tripal_featuremap_teaser',
      'variables' =>  array(NULL),
      'path' => "$path/theme/templates",
    ),

    // templates for the chado_feature nodes:
    'tripal_feature_featurepos' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_feature_featurepos',
      'path' => "$path/theme/templates",
    ),
  );
  return $items;
}

/**
 * Implementation of hook_form_alter()
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == "chado_featuremap_node_form") {
    // turn of preview button for insert/updates
    $form['actions']['preview']['#access'] = FALSE;

    //remove the body field
    unset($form['body']);
  }
}