<?php

/**
 * @file
 * Contains basic functionality for the analysis module.
 *
 * @ingroup tripal_legacy_analysis
 */

/**
 * @defgroup tripal_analysis Legacy Analysis Module
 * @ingroup tripal_modules
 * @{
 * Integrates the Chado Analysis module with Drupal Nodes & Views
 * @}
 */

require_once 'api/tripal_analysis.DEPRECATED.inc';
require_once 'includes/tripal_analysis.delete.inc';
require_once 'includes/tripal_analysis_privacy.inc';
require_once 'includes/tripal_analysis.admin.inc';
require_once 'includes/tripal_analysis.chado_node.inc';


/**
 * Implements hook_init().
 * Add tripal javascript to page headers
 *
 * @ingroup tripal_legacy_analysis
 */
function tripal_analysis_init() {

}


/**
 * Implementation of hook_menu().
 * Entry points and paths of the module
 *
 * @ingroup tripal_legacy_analysis
 */
function tripal_analysis_menu() {
  $items = array();
  // Tripal Analysis administrative settings

  $items['admin/tripal/legacy/tripal_analysis'] = array(
    'title' => 'Analyses',
    'description' => 'A bioinformatics analysis producing features.',
    'page callback' => 'tripal_analysis_admin_analysis_view',
    'access arguments' => array('administer tripal analysis'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/tripal/legacy/tripal_analysis/help'] = array(
    'title' => 'Help',
    'description' => "A description of the Tripal Analysis module including a short description of it's usage.",
    'page callback' => 'theme',
    'page arguments' => array('tripal_analysis_help'),
    'access arguments' => array('administer tripal analysis'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );

  $items['admin/tripal/legacy/tripal_analysis/configuration'] = array(
    'title' => 'Settings',
    'description' => 'Settings for the displays of analysis results.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_analysis_admin'),
    'access arguments' => array('administer tripal analysis'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5
  );

  $items['admin/tripal/legacy/tripal_analysis/sync'] = array(
    'title' => ' Sync',
    'description' => 'Create pages on this site for analyses stored in Chado',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('chado_node_sync_form', 'tripal_analysis', 'chado_analysis'),
    'access arguments' => array('administer tripal analysis'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1
  );
  $items['admin/tripal/legacy/tripal_analysis/delete'] = array(
      'title' => ' Delete',
      'description' => 'Delete multiple analyses from Chado',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('tripal_analysis_delete_form'),
      'access arguments' => array('administer tripal feature'),
      'type' => MENU_LOCAL_TASK,
      'file path' => drupal_get_path('module', 'tripal_analysis'),
      'file' =>  'includes/tripal_analysis.delete.inc',
      'weight' => 2
  );
  $items['admin/tripal/legacy/tripal_analysis/chado_analysis_toc'] = array(
    'title' => ' TOC',
    'description' => 'Manage the table of contents for analysis nodes.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_core_content_type_toc_form', 'chado_analysis'),
    'access arguments' => array('administer tripal analysis'),
    'type' => MENU_LOCAL_TASK,
    'file' =>  'includes/tripal_core.toc.inc',
    'file path' => drupal_get_path('module', 'tripal_core'),
    'weight' => 3
  );

  return $items;
}

/**
 * Implements hook_search_biological_data_views().
 *
 * Adds the described views to the "Search Data" Page created by Tripal Views
 */
function tripal_analysis_search_biological_data_views() {
  return array(
    'tripal_analysis_user_analyses' => array(
      'machine_name' => 'tripal_analysis_user_analyses',
      'human_name' => 'Analyses',
      'description' => 'Bioinformatics analyses which often produces features.',
      'link' => 'chado/analysis'
    ),
  );
}

/**
 * Implements hook_help().
 * Purpose: Adds a help page to the module list
 *
 * @ingroup tripal_legacy_analysis
 */
function tripal_analysis_help ($path, $arg) {
  if ($path == 'admin/help#tripal_analysis') {
    return theme('tripal_analysis_help', array());
  }
}

/**
 *  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_legacy_analysis
 */
function tripal_analysis_permission() {
  return array(
    /*
    'access chado_analysis content' => array(
      'title' => t('View Analyses'),
      'description' => t('Allow users to view analysis pages.'),
    ),
    'create chado_analysis content' => array(
      'title' => t('Create Analyses'),
      'description' => t('Allow users to create new analysis pages.'),
    ),
    'delete chado_analysis content' => array(
      'title' => t('Delete Analyses'),
      'description' => t('Allow users to delete analysis pages.'),
    ),
    'edit chado_analysis content' => array(
      'title' => t('Edit Analyses'),
      'description' => t('Allow users to edit analysis pages.'),
    ),
    'administer tripal analysis' => array(
      'title' => t('Administer Analyses'),
      'description' => t('Allow users to administer all analyses.'),
    ),
    */
  );
}

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

  $items = array(
    'node__chado_analysis' => array(
      'template' => 'node--chado-generic',
      'render element' => 'node',
      'base hook' => 'node',
      'path' => "$core_path/theme/templates",
    ),
    'tripal_analysis_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_base',
      'path' => "$path/theme/templates",
    ),
    'tripal_analysis_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_properties',
      'path' => "$path/theme/templates",
    ),
    'tripal_analysis_teaser' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_teaser',
      'path' => "$path/theme/templates",
    ),
    'tripal_analysis_help' => array(
      'template' => 'tripal_analysis_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme/templates",
    ),

    // tripal_feature theme
    'tripal_feature_analyses' => array(
      'template' => 'tripal_feature_analyses',
      'variables' =>  array('node' => NULL),
      'path' => "$path/theme/templates",
    ),

  );

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

/**
 * Implementation of hook_form_alter().
 *
 * @ingroup tripal_legacy_analysis
 */
function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
  // turn of preview button for insert/updates
  if ($form_id == "chado_analysis_node_form") {
    $form['actions']['preview']['#access'] = FALSE;

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


/**
 * Register tripal_analysis_api sub-modules
 *
 * @param $modulename
 *  The name of the module to be registered as a tripal analysis submodule
 *
 * @ingroup tripal_legacy_analysis_api
 */
function tripal_register_analysis_child($modulename) {
  $sql = "SELECT * FROM {tripal_analysis} WHERE modulename = :modname";
  if (!db_query($sql, array(':modname' => $modulename))->fetchField()) {
    $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES (:modname)";
    db_query($sql, array(':modname' => $modulename));
  }
}

/**
 * Un-register a tripal analysis sub-module
 *
 * @param $modulename
 *  The name of the module to un-register
 *
 * @ingroup tripal_legacy_analysis_api
 */
function tripal_unregister_analysis_child($modulename) {
  if (db_table_exists('tripal_analysis')) {
    $sql = "DELETE FROM {tripal_analysis} WHERE modulename = :modname";
    db_query($sql, array(':modname' => $modulename));
  }
}


