<?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' => t('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' => t('Configuration'),
    'page callback' => 'tripal_project_configuration_page',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM
  ); 
  
  return $items;
}

/**
 *  Implements Hook_help()
 *
 *  This function simply states, in HTML tags, the creator of the the module and the contact	
 *  for the programmer
 *
 *  @parm $path
 *    The absolute path of the module and help information
 *
 *  @parm $arg
 *    The argument
 */
function tripal_project_help($path, $arg) {

  switch ($path) {
  
    case 'admin/help#tripal_project':
      
      return '<p>'. t('Module created by:Chad Krilow (e-mail:cnk046@mail.usask.ca)') .'</p>';
			
			break;
  }
}

/**
 * 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(
    'create tripal_project', 
    'edit own tripal_project'
  );
}

/**
 * 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 tripal_project_access($op, $node, $account) {

  if ($op == 'create') {
  
    // Only users with permission to do so may create this node type.
    if(!user_access('create tripal_project', $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 tripal_project',$account)){
       return FALSE;
    }
    if(user_access('edit own tripal_project',$account) &&
       $account->uid != $node->uid){
      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(
    'tripal_project' => array(
      'name' => t('Project'),
      'module' => 'tripal_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('Description'),
    )
  );
}

/**
* Implementation of hook_form().
*
*  This form takes the Project Title infromation and the Project description from the user. It 
*  then puts the infromation into the Chado_project database table.
*
*  @parm &$node
*    The node that is created when the database is initialized
*
*  @parm $form_state
*    The state of the form, that has the user entered information that is neccessary for, setting
*    up the database of the project
*
*  @return $form
*    The information that was enterd allong with   
*
*/
function tripal_project_form(&$node, $form_state) {
  
  $type = node_get_types('type', $node);

  //define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5
  );
  
  // Putting body and filter elements to be adjacent them into a sub-array together
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->body_label),
    '#default_value' => $node->body,
    '#required' => FALSE
  );
  
  $form['body_filter']['filter'] = filter_form($node->format);
  
  // whether or not the project exists in chado
  $form['project_id'] = array(
    '#type' => 'value',
    '#value' => FALSE,
  );

  return $form;
}

/**
* Implementation of hook_insert()
*
*  @parm $node
*    Then node that has the information stored within, accessed given the node-Id
*
*/
function tripal_project_insert($node) {
		
	$values =  array(
     'name' => $node->title,
     'description' => $node->body,
   );
   
   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, automagically by Drupal Black Box
*
*/
function tripal_project_delete($node) {
  // Notice that we're matching all revision, by using the node's nid.
  
  $values =  array(
     'name' => $node->title,
     'description' => $node->body,
   );
  
  //deleting row in chado table
  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 modifys information
*  pertaining to the specific project
*
*/
function tripal_project_update($node){
	
	$result = db_fetch_object(db_query('SELECT * FROM {chado_project} WHERE nid=%d AND vid=%d',$node->nid, $node->vid));
	
	$match= array(
		'project_id'=>$result->project_id,
	);
	
	$values =  array(
    'name' => $node->title,
    'description' => $node->body,
  );
   
  //selects:$table, $match, $value when updating 
  $result = tripal_core_chado_update('project',$match,$values);

}

/**
* Implementation of tripal_project_load().
*
* @param $node
*   The node that is to have its containing infromation loaded
*
* @return $node
*   The node, containing the loaded project with the current node-Id
*
*/
function tripal_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
//-----------------------------------------------------------------------------