<?php

/**
 * @file
 * Contains all the main hook implementations for the tripal_analysis module
 *
 * @defgroup tripal_analysis Analysis Module
 * @ingroup tripal_modules
 * @{
 * Provides functions for managing chado analysis' including creating details pages for each one
 *
 * @}
 *
 *
 */

require_once 'api/tripal_analysis.api.inc';
require_once 'includes/tripal_analysis_privacy.inc';
require_once 'includes/tripal_analysis.admin.inc';
require_once 'includes/tripal_analysis.chado_node.inc';
require_once 'includes/tripal_analysis.sync.inc';
require_once "api/tripal_analysis.schema.api.inc";


/**
 * Add tripal javascript to page headers
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_init() {
  drupal_add_js(drupal_get_path('module', 'tripal_analysis') . '/theme/js/tripal_analysis.js');
  drupal_add_css(drupal_get_path('module', 'tripal_analysis') . '/theme/css/tripal_analysis.css');
}

/**
 * Provide information to drupal about the node types that we're creating
 * in this module
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_node_info() {
  $nodes = array();
  $nodes['chado_analysis'] = array(
      'name' => t('Analysis'),
      'base' => 'chado_analysis',
      'description' => t('An analysis'),
      'has_title' => FALSE,
      'title_label' => t('Analysis'),
      'locked' => TRUE
  );
  return $nodes;
}
/**
 * Implementation of hook_menu().
 * Entry points and paths of the module
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_menu() {

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

  $items['admin/tripal/chado/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 analyses'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );

  $items['admin/tripal/chado/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 analyses'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5
  );

  $items['admin/tripal/chado/tripal_analysis/sync'] = array(
    'title' => 'Sync',
    'description' => 'Sync Chado analyses with Drupal.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_analysis_sync_form'),
    'access arguments' => array('administer tripal analyses'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0
  );

  return $items;
}

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

/**
 *  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_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.'),
    ),
    'adminster 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_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",
    ),
    'tripal_analysis_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_base',
      'path' => "$path/theme/tripal_analysis",
    ),
    'tripal_analysis_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_properties',
      'path' => "$path/theme/tripal_analysis",
    ),
    'tripal_analysis_teaser' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_analysis_teaser',
      'path' => "$path/theme/tripal_analysis",
    ),
    'tripal_analysis_help' => array(
      'template' => 'tripal_analysis_help',
      'variables' =>  array(NULL),
      'path' => "$path/theme",
    ),
    
    // tripal_feature theme
    'tripal_feature_analyses' => array(
      'template' => 'tripal_feature_analyses',
      'variables' =>  array('node' => NULL),
      'path' => "$path/theme/tripal_analysis",
    ),

  );

  return $items;
}
/**
 *
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_block_info() {
  $blocks['base']['info'] = t('Tripal Analysis Details');
  $blocks['base']['cache'] = DRUPAL_NO_CACHE;

  $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  $blocks['featureblast']['cache'] = DRUPAL_NO_CACHE;

  return $blocks;
}

/**
 *
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_block_view($delta = '') {

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

    $block = array();
    switch ($delta) {
      case 'base':
        $block['subject'] = t('Analysis Details');
        $block['content'] = theme('tripal_analysis_base', $node);
        break;
      case 'tripal_feature_analyses':
        $block['subject'] = t('Feature Analyses');
        $block['content'] = theme('tripal_feature_analyses', $node);
        break;
      default :
    }
    return $block;
  }
}

/**
 *
 * @ingroup tripal_analysis
 */
function tripal_analysis_node_view($node, $view_mode, $langcode) {
  switch ($node->type) {
    case 'chado_analysis':
      // Show feature browser and counts
      if ($view_mode == 'full') {
        $node->content['tripal_analysis_base'] = array(
          '#value' => theme('tripal_analysis_base', array('node' => $node)),
        );
        $node->content['tripal_analysis_properties'] = array(
          '#value' => theme('tripal_analysis_properties', array('node' => $node)),
        );
      }
      if ($view_mode == 'teaser') {
        $node->content['tripal_analysis_teaser'] = array(
          '#value' => theme('tripal_analysis_teaser', array('node' => $node)),
        );
      }
      break;
  }
}
/**
 * 
 * @param $node
 */
function tripal_analysis_node_presave($node) {
  // If this is an analysis of some type it will should have thre three required
  // fields for the Chado analysis table: program, programversion and sourcename.
  // So we will set the title for any node that has these three fields
  if (property_exists($node, 'program') and 
      property_exists($node, 'programversion') and
      property_exists($node, 'sourcename')) {
    if ($node->analysisname) {
      $node->title = $node->analysisname;
    }
    else {
      $node->title = "$node->program ($node->programversion) $node->sourcename";
    }
  }
}
/**
 * Implements hook_views_api()
 * Purpose: 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_analysis
 */
function tripal_analysis_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Implementation of hook_form_alter()
 * 
 * @param $form
 * @param $form_state
 * @param $form_id
 */
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;
  }
}