<?php
/**
 * @file
 * Integrates the Chado Project tables with Drupal Nodes & Views
 */

require_once 'api/tripal_project.DEPRECATED.inc';

require_once 'theme/tripal_project.theme.inc';

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

/**
 * @defgroup tripal_project Project Module
 * @ingroup tripal_modules
 * @{
 * Integrates the Chado Project tables with Drupal Nodes & Views
 * @}
 */

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

/**
 * Implements hook_menu().
 *
 * @ingroup tripal_project
 */
function tripal_project_menu() {
  $items = array();

  $items[ 'admin/tripal/legacy/tripal_project' ]= array(
    'title' => 'Projects',
    'description' => ('A project. Can be used for grouping data such as with the natural diversity module data.'),
    'page callback' => 'tripal_project_admin_project_view',
    'access arguments' => array('administer tripal project'),
    'type' => MENU_NORMAL_ITEM
  );

  $items['admin/tripal/legacy/tripal_project/help']= array(
    'title' => 'Help',
    'description' => ("Basic Description of Tripal Project Module Functionality."),
    'page callback' => 'theme',
    'page arguments' => array('tripal_project_help'),
    'access arguments' => array('administer tripal project'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 6
  );

  $items['admin/tripal/legacy/tripal_project/configuration']= array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_project_admin'),
    'access arguments' => array('administer tripal project'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 4
  );

  $items['admin/tripal/legacy/tripal_project/sync'] = array(
    'title' => ' Sync',
    'description' => 'Create pages on this site for projects stored in Chado',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('chado_node_sync_form', 'tripal_project', 'chado_project'),
    'access arguments' => array('administer tripal project'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0
  );

  $items['admin/tripal/legacy/tripal_project/chado_project_toc'] = array(
    'title' => ' TOC',
    'description' => 'Manage the table of contents for project nodes.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_core_content_type_toc_form', 'chado_project'),
    'access arguments' => array('administer tripal project'),
    'type' => MENU_LOCAL_TASK,
    'file' =>  'includes/tripal_core.toc.inc',
    'file path' => drupal_get_path('module', 'tripal_core'),
    'weight' => 3
  );

  $items['admin/tripal/legacy/tripal_project/views/projects/enable'] = array(
    'title' => 'Enable Project Administrative View',
    'page callback' => 'tripal_enable_view',
    'page arguments' => array('tripal_project_admin_projects', 'admin/tripal/legacy/tripal_project'),
    'access arguments' => array('administer tripal project'),
    '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_project_search_biological_data_views() {
  return array(
    'tripal_project_user_projects' => array(
      'machine_name' => 'tripal_project_user_projects',
      'human_name' => 'Projects',
      'description' => 'A project. Can be used for grouping data such as with the natural diversity module data.',
      'link' => 'chado/project'
    ),
  );
}

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

/**
 * Implements hook_permission()
 *
 *  This function sets the permission for the user to access the information in the database.
 *  This includes creating, inserting, deleting and updating of information in the database
 *
 * @ingroup tripal_project
 */
function tripal_project_permission() {
  return array(
    /*
    'access chado_project content' => array(
      'title' => t('View Projects'),
      'description' => t('Allow users to view project pages.'),
    ),
    'create chado_project content' => array(
      'title' => t('Create Projects'),
      'description' => t('Allow users to create new project pages.'),
    ),
    'delete chado_project content' => array(
      'title' => t('Delete Projects'),
      'description' => t('Allow users to delete project pages.'),
    ),
    'edit chado_project content' => array(
      'title' => t('Edit Projects'),
      'description' => t('Allow users to edit project pages.'),
    ),
    'administer tripal project' => array(
      'title' => t('Administer Projects'),
      'description' => t('Allow users to administer all projects.'),
    ),
    */
  );
}

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

  $items = array(
    'node__chado_project' => array(
      'template' => 'node--chado-generic',
      'render element' => 'node',
      'base hook' => 'node',
      'path' => "$core_path/theme/templates",
    ),
    'tripal_project_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_base',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_contact' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_contact',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_properties',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_publications' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_publications',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_relationships' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_relationships',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_teaser' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project_teaser',
      'path' => "$path/theme/templates",
    ),
    'tripal_project_help' => array(
      'variables' =>  array(NULL),
      'template' => 'tripal_project_help',
      'path' => "$path/theme/templates",
    ),
  );
  return $items;
}

/**
 * Implementation of hook_form_alter().
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 *
 * @ingroup tripal_project
 */
function tripal_project_form_alter(&$form, &$form_state, $form_id) {

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

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