<?php

require('tripal_project.admin.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
 */

//-----------------------------------------------------------------------------
//  SECTION: Main Outline for Tripal Project Module
//-----------------------------------------------------------------------------


/**
 * 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
 *
 */
function tripal_project_views_api() {
  return array(
      'api' => 2.0,
  );
}

/**
 * Implements hook_menu
 */
function tripal_project_menu() {
  $items[ 'admin/tripal/tripal_project' ]= array(
    'title' => 'Projects',
    'page callback' => 'tripal_project_administration_description_page',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM
  );

  $items[ 'admin/tripal/tripal_project/configuration' ]= array(
    'title' => 'Configuration',
    'page callback' => 'tripal_project_configuration_page',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
}

/**
 * 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
 *
 */
function tripal_project_perm() {

  return array(
    'access chado_projects',
    'create chado_projects',
    'edit own chado_projects'
  );
}

/**
 * Implements hook_access()
 *
 *  This function sets the access permission for operations on the database.
 *
 *  @parm $op
 *    The operation that is to be performed
 *
 *  @parm $node
 *    The specific node that is to have the operation performed
 *
 *  @parm $account
 *    The account of the user that is performing the operations
 *
 *  @return
 *    True if a operation was performed
 *
 */
function chado_project_access($op, $node, $account) {

  if ($op == 'create') {

    // Only users with permission to do so may create this node type.
    if (!user_access('create chado_projects', $account)) {
      return FALSE;
    }
  }

  // Users who create a node may edit or delete it later, assuming they have the necessary permissions.
  if ($op == 'update' || $op == 'delete') {

    if (!user_access('edit own chado_projects', $account)) {
      return FALSE;
    }
    if (user_access('edit own chado_projects', $account) &&
      $account->uid != $node->uid) {
      return FALSE;
    }
  }

  if ($op == 'view') {
    if (!user_access('access chado_projects', $account)) {
      return FALSE;
    }
  }
  return NULL;
}

//-----------------------------------------------------------------------------
//  SECTION: Node Functionality
//-----------------------------------------------------------------------------

/**
 * Implementation of hook_node_info().
 *
 * This node_info, is a simple node that describes the functionallity of the module. It specifies
 * that the title(Project Name) and body(Description) set to true so that they information can be
 * entered
 *
 */
function tripal_project_node_info() {
  return array(
    'chado_project' => array(
      'name' => t('Project'),
      'module' => 'chado_project',
      'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
      'has_title' => TRUE,
      'title_label' => t('Project Name'),
      'had_body' => TRUE,
      'body_label' => t('Full Description'),
    )
  );
}

/**
 * Implementation of hook_form().
 *
 *  This form takes the Project Title information and description from the user.
 *
 *  @parm &$node
 *    The initialized node
 *
 *  @parm $form_state
 *    The state of the form, that has the user entered information that is neccessary for adding
 *    information to the project
 *
 *  @return $form
 *    An array as described by the Drupal Form API
 *
 */
function chado_project_form(&$node, $form_state) {

  $type = node_get_types('type', $node);

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5
  );

  $form['description'] = array(
    '#type' => 'textfield',
    '#maxlength' => 255,
    '#title' => 'Short Description',
    '#default_value' => $node->project->description,
  );

  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->body_label),
    '#default_value' => $node->body,
  );
  $form['body_filter']['filter'] = filter_form($node->format);

  // whether or not the project exists in chado
  $form['project_id'] = array(
    '#type' => 'value',
    '#value' => ($node->project->project_id) ? $node->project->project_id : FALSE,
  );

  return $form;
}

/**
 * Implementation of hook_insert().
 *
 *  @parm $node
 *    Then node that has the information stored within, accessed given the nid
 *
 */
function chado_project_insert($node) {

  $values =  array(
     'name' => $node->title,
     'description' => $node->description,
  );

  if (!$node->project_id) {
     //inserts info into chado table.
  $result = tripal_core_chado_insert('project', $values);
  $node->project_id = $result['project_id'];
  }

  //inserts the row of vid,nid,project_id into the chado_project table
  db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->project_id);

}

/**
 *
 * Implementation of hook_delete().
 *
 * @param $node
 * The node which is to be deleted, only chado project and chado_project need to be dealt with
 * since the drupal node is deleted automagically
 *
 */
function chado_project_delete($node) {
  // Notice that we're matching all revision, by using the node's nid.

  // Find the project to delete
  $values =  array(
     'project_id' => $node->project->project_id,
  );
  tripal_core_chado_delete('project', $values);

  //deleteing in drupal chado_project table
  db_query('DELETE FROM {chado_project} WHERE nid = %d', $node->nid);

}

/**
 * Implements hook_update().
 *
 * @param $node
 *  The node which is to have its containing information updated when the user modifies information
 *  pertaining to the specific project
 *
 */
function chado_project_update($node) {

  // Find the project to update
  $match= array(
    'project_id' => $node->project_id,
  );

  // New values
  $values =  array(
    'name' => $node->title,
    'description' => $node->description,
  );

  $result = tripal_core_chado_update('project', $match, $values);

}

/**
 * Implementation of node_load().
 *
 * @param $node
 *   The node that is to have its containing information loaded
 *
 * @return $node
 *   The node, containing the loaded project with the current nid
 *
 */
function chado_project_load($node) {

  //selecting the coresponding table information
  $result = db_fetch_object(db_query('SELECT * FROM {chado_project} WHERE nid=%d AND vid=%d', $node->nid, $node->vid));

  //assigning the project-Id to a variable
  $values = array(
    'project_id' => $result->project_id,
  );

  //the current project set to the 'project' with the $values(project-Id)
  $node->project = tripal_core_generate_chado_var('project', $values);

  return $node;

}


//-----------------------------------------------------------------------------
//  END OF SOFTWARE
//-----------------------------------------------------------------------------