<?php

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

require('api/tripal_featuremap.api.inc');
require('theme/tripal_featuremap.theme.inc');
require('includes/tripal_featuremap.admin.inc');
require('includes/tripal_featuremap.chado_node.inc');

/**
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_init() {

}


/**
 * 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;
}



/**
 * 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_permissions() {
  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.'),
    ),
    'adminster tripal featuremap' => array(
      'title' => t('Administer Maps'),
      'description' => t('Allow users to administer all maps.'),
    ),
  );
}

/**
 * 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('tripal_core_chado_node_sync_form', 'tripal_featuremap', 'chado_featuremap'),
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0
  );

  // Synchronizing maps from Chado to Drupal
  $items['chado_sync_featuremaps'] = array(
    'title' => 'Sync Data',
    'page callback' => 'tripal_featuremap_sync_featuremaps',
    'access arguments' => array('administer tripal featuremap'),
    'type' => MENU_CALLBACK
  );

  $items['admin/tripal/chado/tripal_featuremap/views/featuremaps/enable'] = array(
    'title' => 'Enable featuremap Administrative View',
    'page callback' => 'tripal_views_admin_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_views_api()
 * Purpose: Essentially this hook tells drupal that there is views support for
 *  for this module which then includes tripal_db.views.inc where all the
 *  views integration code is
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 *  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",
    ),
    'tripal_featuremap_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_base',
      'path' => "$path/theme/tripal_featuremap",
    ),
    'tripal_featuremap_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_properties',
      'path' => "$path/theme/tripal_featuremap",
    ),
    'tripal_featuremap_featurepos' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_featurepos',
      'path' => "$path/theme/tripal_featuremap",
    ),
    'tripal_featuremap_publication' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_publication',
      'path' => "$path/theme/tripal_featuremap",
    ),
    'tripal_featuremap_references' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_featuremap_references',
      'path' => "$path/theme/tripal_featuremap",
    ),
    'tripal_featuremap_help' => array(
      'template' => 'tripal_featuremap_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme",
    ),
    'tripal_featuremap_teaser' => array(
      'template' => 'tripal_featuremap_teaser',
      'variables' =>  array(NULL),
      'path' => "$path/theme/tripal_featuremap",
    ),
  );
  return $items;
}

/**
 * @ingroup tripal_library
 */
function tripal_featuremap_block_info() {

  $blocks['mapbase']['info'] = t('Tripal Map Details');
  $blocks['mapbase']['cache'] = 'BLOCK_NO_CACHE';

  $blocks['mapprops']['info'] = t('Tripal Map Properties');
  $blocks['mapprops']['cache'] = 'BLOCK_NO_CACHE';

  $blocks['mappos']['info'] = t('Tripal Map Features');
  $blocks['mappos']['cache'] = 'BLOCK_NO_CACHE';

  $blocks['mappubs']['info'] = t('Tripal Map Publications');
  $blocks['mappubs']['cache'] = 'BLOCK_NO_CACHE';

  $blocks['maprefs']['info'] = t('Tripal Map References');
  $blocks['maprefs']['cache'] = 'BLOCK_NO_CACHE';

  return $blocks;
}
/**
 * @ingroup tripal_library
 */
function tripal_featuremap_block_view($delta = '') {

  if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);

    $block = array();
    switch ($delta) {
      case 'mapbase':
        $block['subject'] = t('Library Details');
        $block['content'] = theme('tripal_featuremap_base', $node);
        break;
      case 'mapprops':
        $block['subject'] = t('Properties');
        $block['content'] = theme('tripal_featuremap_properties', $node);
        break;
      case 'mappos':
        $block['subject'] = t('Features');
        $block['content'] = theme('tripal_featuremap_featurepos', $node);
        break;
      case 'mappubs':
        $block['subject'] = t('Publications');
        $block['content'] = theme('tripal_featuremap_publication', $node);
        break;
      case 'maprefs':
        $block['subject'] = t('References');
        $block['content'] = theme('tripal_featuremap_references', $node);
        break;
      default :
    }
    return $block;
  }
}

/**
 *
 * @ingroup tripal_featuremap
 */
function tripal_featuremap_cron() {

}


/**
 * Implementation of hook_form_alter()
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function tripal_featuremap_form_alter(&$form, &$form_state, $form_id) {
  // turn of preview button for insert/updates
  if ($form_id == "chado_featuremap_node_form") {
    $form['actions']['preview']['#access'] = FALSE;
  }
}