<?php
require('api/tripal_project.api.inc');
require('theme/tripal_project.theme.inc');
require('includes/tripal_project.admin.inc');
require('includes/tripal_project.chado_node.inc');

/**
 *  @file
 * This file contains the basic functions needed for this drupal module.
 * The drupal tripal_project module maps directly to the chado general module.
 *
 * For documentation regarding the Chado General module:
 * @see http://gmod.org/wiki/Chado_General_Module
 *
 * @defgroup tripal_project Project Module
 * @ingroup tripal_modules
 */

/**
 * Implements hook_views_api()
 *
 * Purpose: 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' => 2.0,
  );
}

/**
 * Implements hook_menu
 *
 * @ingroup tripal_project
 */
function tripal_project_menu() {
  $items[ 'admin/tripal/chado/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('adminster tripal projects'),
    'type' => MENU_NORMAL_ITEM
  );

  $items['admin/tripal/chado/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('adminster tripal projects'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 6
  );

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

  $items['admin/tripal/chado/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('tripal_core_chado_node_sync_form', 'tripal_project', 'chado_project'),
    'access arguments' => array('administer tripal projects'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0
  );
  
  $items['admin/tripal/chado/tripal_project/views/projects/enable'] = array(
    'title' => 'Enable Project Administrative View',
    'page callback' => 'tripal_views_admin_enable_view',
    'page arguments' => array('tripal_project_admin_projects', 'admin/tripal/chado/tripal_project'),
    'access arguments' => array('administer tripal projects'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

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

/**
 * Implements hook_perm()
 *
 *  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.'),
    ),
    'adminster tripal project' => array(
      'title' => t('Administer Projects'),
      'description' => t('Allow users to administer all projects.'),
    ),
  );
}



/**
 *  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",
    ),
    'tripal_project_base' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.base',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_contact' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.contact',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_properties' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.properties',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_publications' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.publications',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_relationships' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.relationships',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_teaser' => array(
      'variables' => array('node' => NULL),
      'template' => 'tripal_project.teaser',
      'path' => "$path/theme/tripal_project",
    ),
    'tripal_project_help' => array(
      'variables' => 'tripal_project.help',
      'variables' =>  array(NULL),
      'path' => "$path/theme",
    ),
  );
  return $items;
}

/**
 *
 * @ingroup tripal_project
 */
function tripal_project_block_info() {

  $blocks['projectbase']['info'] = t('Tripal Project Details');
  $blocks['projectbase']['cache'] = DRUPAL_NO_CACHE;

  $blocks['projectprops']['info'] = t('Tripal Project Properties');
  $blocks['projectprops']['cache'] = DRUPAL_NO_CACHE;

  $blocks['projectpubs']['info'] = t('Tripal Project Publications');
  $blocks['projectpubs']['cache'] = DRUPAL_NO_CACHE;

  $blocks['projectcont']['info'] = t('Tripal Project Contact');
  $blocks['projectcont']['cache'] = DRUPAL_NO_CACHE;

  $blocks['projectrels']['info'] = t('Tripal Project Relationships');
  $blocks['projectrels']['cache'] = DRUPAL_NO_CACHE;

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

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

    $block = array();
    switch ($delta) {
      case 'projectbase':
        $block['subject'] = t('Project Details');
        $block['content'] = theme('tripal_project_base', $node);
        break;
      case 'projectprops':
        $block['subject'] = t('Properties');
        $block['content'] = theme('tripal_project_properties', $node);
        break;
      case 'projectpubs':
        $block['subject'] = t('Publications');
        $block['content'] = theme('tripal_project_publications', $node);
        break;
      case 'projectcont':
        $block['subject'] = t('Contact');
        $block['content'] = theme('tripal_project_contact', $node);
        break;
      case 'projectrels':
        $block['subject'] = t('Relationships');
        $block['content'] = theme('tripal_project_relationships', $node);
        break;
      default :
    }
    return $block;
  }
}

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