t('Stocks'),
'description' => t('Basic Description of Tripal Stock Module Functionality'),
'page callback' => 'tripal_stock_module_description_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM
);
$items['admin/tripal/tripal_stock/configuration'] = array(
'title' => t('Configuration'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'drupal_get_form',
'page arguments' => array('tripal_stock_admin'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM
);
//Displaying stocks----------------------------
$items['stocks'] = array(
'menu_name' => ('primary-links'),
'title' => t('Stocks'),
'page callback' => 'tripal_stock_show_stocks',
'access arguments' => array('access chado_stock content'),
'type' => MENU_NORMAL_ITEM,
);
// Adding Secondary Properties-----------------
$items['node/%cs_node/properties'] = array(
'title' => t('Add Properties & Synonyms'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_add_ALL_property_page',
'page arguments' => array(1),
'access arguments' => array('create chado_stock content'),
'type' => MENU_CALLBACK
);
$items['node/%cs_node/db_references'] = array(
'title' => t('Add Database References'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
'page arguments' => array(1),
'access arguments' => array('create chado_stock content'),
'type' => MENU_CALLBACK
);
$items['node/%cs_node/relationships'] = array(
'title' => t('Add Relationships'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_add_ALL_relationships_page',
'page arguments' => array(1),
'access arguments' => array('create chado_stock content'),
'type' => MENU_CALLBACK
);
//Edit/Deleting Secondary Properties-------------
$items['node/%cs_node/edit_properties'] = array(
'title' => t('Edit Properties'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_edit_ALL_properties_page',
'page arguments' => array(1),
'access arguments' => array('edit chado_stock content'),
'type' => MENU_LOCAL_TASK,
'weight' => 8,
);
$items['node/%cs_node/edit_relationships'] = array(
'title' => t('Edit Relationships'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_edit_ALL_relationships_page',
'page arguments' => array(1),
'access arguments' => array('edit chado_stock content'),
'type' => MENU_LOCAL_TASK,
'weight' => 9,
);
$items['node/%cs_node/edit_db_references'] = array(
'title' => t('Edit DB References'),
'description' => t('Settings for Chado Stocks'),
'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
'page arguments' => array(1),
'access arguments' => array('edit chado_stock content'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
/*************************************************************************
* Implements Menu wildcard_load hook
* Purpose: Allows the node ID of a chado stock to be dynamically
* pulled from the path. The node is loaded from this node ID
* and supplied to the page as an arguement
*/
function cs_node_load($nid) {
if (is_numeric($nid)) {
$node = node_load($nid);
if ($node->type == 'chado_stock') {
return $node;
}
}
return FALSE;
}
/*************************************************************************
* Implementation of hook_perm()
* Purpose: Set the permission types that the chado stock module uses
*
* @return an array listing the possible permission catagories
*/
function tripal_stock_perm() {
return array(
'access chado_stock content',
'create chado_stock content',
'edit chado_stock content',
'delete chado_stock content'
);
}
/*************************************************************************
* Implements hook_access()
* Purpose: Maps permission catagories to actions;
* TRUE grants access; FALSE denies it
*/
function chado_stock_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create chado_stock content', $account);
}
if ($op == 'update') {
if (user_access('edit chado_stock content', $account)) {
return TRUE;
}
}
if ($op == 'delete') {
if (user_access('delete chado_stock content', $account)) {
return TRUE;
}
}
if ($op == 'view') {
if (user_access('access chado_stock content', $account)) {
return TRUE;
}
}
return FALSE;
}
/*************************************************************************
* Implements hook_views_api()
* Purpose: Essentially this hook tells drupal that there is views support for
* for this module which then includes tripal_stock.views.inc where all the
* views integration code is
*/
function tripal_stock_views_api() {
return array(
'api' => 2.0,
);
}
/*************************************************************************
* Implements hook_theme()
* Purpose: Register themeing functions for this module
*
* @return an array of themeing functions to register
*/
function tripal_stock_theme() {
return array(
'tripal_stock_stock_table' => array (
'arguments' => array('stocks'),
),
// Property edit forms--------------------------
'tripal_stock_edit_ALL_properties_form' => array(
'arguments' => array('form'),
'function' => 'theme_tripal_stock_edit_ALL_properties_form',
),
'tripal_stock_edit_ALL_db_references_form' => array(
'arguments' => array('form'),
'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
),
'tripal_stock_edit_ALL_relationships_form' => array(
'arguments' => array('form'),
'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
),
// Block Templates------------------------------
'tripal_stock_base' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_base',
),
'tripal_stock_properties' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_properties',
),
'tripal_stock_references' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_references',
),
'tripal_stock_relationships_as_object' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_relationships_as_object',
),
'tripal_stock_relationships_as_subject' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_relationships_as_subject',
),
'tripal_stock_synonyms' => array (
'arguments' => array('node'=> null),
'template' => 'tripal_stock_synonyms',
),
);
}
/*************************************************************************
* Purpose: show stocks stored in drupals chado_stock table
*/
function tripal_stock_show_stocks () {
$sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
$no_stocks = db_result(db_query($sql));
if($no_stocks != 0) {
$stocks = tripal_stock_get_all_stocks();
if($no_stocks != count($stocks)) {
drupal_set_message("Synchronization needed.");
}
return theme('tripal_stock_stock_table',&$stocks);
} else {
return t("No Stocks exists. Please contact administrators to ".
"synchronize stocks.");
}
}
function theme_tripal_stock_stock_table (&$stocks) {
// cycle through the stocks and build the stocks page
$output = "
";
$output .= '
';
$output .= "";
$output .= "Name | ";
$output .= "Type | ";
$output .= "Organism | ";
$output .= "Description | ";
$output .= "
";
foreach($stocks as $stock){
$output .= "";
$output .= "".l($stock->stock_name, "node/".$stock->nid)." | ";
$output .= "".$stock->stock_type." | ";
$output .= "".$stock->organism->common_name." | ";
$output .= "".$stock->description." | ";
$output .= "
";
}
$output .= "
";
$output .= "
";
return $output;
}
/*************************************************************************
* @section Implementation of stock Node
* including node_load, form, insert, update, delete(by nid and vid)
*************************************************************************/
/*************************************************************************
* Implements hook_node_info()
*/
function tripal_stock_node_info() {
return array(
'chado_stock' => array(
'name' => t('Stock'),
'module' => 'chado_stock',
'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
'has_title' => TRUE,
'has_body' => FALSE,
),
);
}
/*************************************************************************
* Implements hook_load()
* Everytime a chado_stock node is viewed or manipulated this hook is called first to prepare the data
*/
function chado_stock_load($node) {
// Gets the stock_id from the drupal chado_stock table---------------------------------------
$map = db_fetch_object(db_query(
"SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
$node->vid
));
$node->stock_id = $map->stock_id;
//Get the main stock information from the chado stock table-----------------------------------
$columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
$values = array('stock_id' => $node->stock_id);
$results = tripal_core_chado_select('stock', $columns, $values);
$node->stock_name = $results[0]->name;
$node->uniquename = $results[0]->uniquename;
$node->description = $results[0]->description;
$node->stock_type_id = $results[0]->type_id;
$node->organism->organism_id = $results[0]->organism_id;
$node->main_db_reference->dbxref_id = $results[0]->dbxref_id;
if (preg_match('/t/', $results[0]->is_obsolete)) {
$node->is_obsolete = TRUE;
} else {
$node->is_obsolete = FALSE;
}
// Get type for current stock------------------------------------------------------------------
$columns = array('name');
$values = array('cvterm_id' => $node->stock_type_id);
$results = tripal_core_chado_select('cvterm', $columns, $values);
$node->stock_type = $results[0]->name;
// Get organism details from chado & add to node-----------------------------------------------
// get organism if for current stock
// pull all data from chado for the organism and assign it to the current node
$node->organism = tripal_organism_get_organism_by_organism_id($node->organism->organism_id);
// Add Synonyms for stock----------------------------------------------------------------------
// These are a special case of property (stockprop table) where cvterm='synonym'
$columns = array('stockprop_id', 'type_id', 'value', 'rank');
$values = array(
'stock_id' => $node->stock_id,
'type_id' => array(
'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
'name' => 'synonym'
)
);
$results = tripal_core_chado_select('stockprop', $columns, $values);
if (!empty($results)) {
foreach ($results as $r) {
$r->type = 'synonym';
$node->synonyms[] = $r;
}
}
// Add properties for stock (not including synonyms)-------------------------------------------
// $node->properties is an array of objects where each object describes a single property and has a type and value
$columns = array('stockprop_id', 'type_id', 'value', 'rank');
$values = array(
'stock_id' => $node->stock_id,
);
$results = tripal_core_chado_select('stockprop', $columns, $values);
foreach ($results as $r) {
$columns = array('name');
$values = array('cvterm_id' => $r->type_id);
$type_results = tripal_core_chado_select('cvterm', $columns, $values);
$r->type = $type_results[0]->name;
$node->properties[] = $r;
}
// Add in main db reference-----------------------------------------------------------------------
// this is the dbxref_id in the stock table
if (!empty($node->main_db_reference->dbxref_id)) {
$columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
$values = array('dbxref_id' => $node->main_db_reference->dbxref_id);
$results = tripal_core_chado_select('dbxref', $columns, $values);
$node->main_db_reference = $results[0];
//get db info
$columns = array('name', 'description', 'url', 'urlprefix');
$values = array('db_id' => $node->main_db_reference->db_id);
$results = tripal_core_chado_select('db', $columns, $values);
$node->main_db_reference->db_name = $results[0]->name;
$node->main_db_reference->db_description = $results[0]->description;
$node->main_db_reference->db_url = $results[0]->url;
$node->main_db_reference->db_urlprefix = $results[0]->urlprefix;
}
// Add in extra references to external databases--------------------------------------------------
// this includes all the dbxref entries in stock_dbxref
$columns = array('dbxref_id');
$values = array('stock_id' => $node->stock_id);
$results = tripal_core_chado_select('stock_dbxref',$columns,$values);
$node->db_references = array();
foreach ($results as $r) {
$columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
$values = array('dbxref_id' => $r->dbxref_id);
$sub_results = tripal_core_chado_select('dbxref', $columns, $values);
$dbxref = $sub_results[0];
//get db info
$columns = array('name', 'description', 'url', 'urlprefix');
$values = array('db_id' => $dbxref->db_id);
$results = tripal_core_chado_select('db', $columns, $values);
$dbxref->db_name = $results[0]->name;
$dbxref->db_description = $results[0]->description;
$dbxref->db_url = $results[0]->url;
$dbxref->db_urlprefix = $results[0]->urlprefix;
$node->db_references[] = $dbxref;
}
// Add relationships for stock--------------------------------------------------------------------
// Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
// and those where the current stock is the object (other details stored in $node->subject_relationships)
// fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
// where current is subject.............................
$columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
$values = array('subject_id' => $node->stock_id);
$results = tripal_core_chado_select('stock_relationship', $columns, $values);
$node->object_relationships = array();
foreach ($results as $r) {
$columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
$values = array('stock_id' => $r->object_id);
$results = tripal_core_chado_select('stock', $columns, $values);
// Type
$type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
$r->type = $type_results[0]->name;
// Object
$r->object->stock_id = $r->object_id;
unset($r->object_id);
$r->object->stock_name = $results[0]->name;
$r->object->uniquename = $results[0]->uniquename;
$r->object->description = $results[0]->description;
$r->object->stock_type_id = $results[0]->type_id;
$r->object->organism->organism_id = $results[0]->organism_id;
$r->object->main_db_reference->dbxref_id = $results[0]->dbxref_id;
$sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
$object_node = db_fetch_object(db_query($sql, $r->object->stock_id));
$r->object->nid = $object_node->nid;
$node->object_relationships[] = $r;
}
// where current is object.............................
$columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
$values = array('object_id' => $node->stock_id);
$results = tripal_core_chado_select('stock_relationship', $columns, $values);
$node->subject_relationships = array();
foreach ($results as $r) {
$columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
$values = array('stock_id' => $r->subject_id);
$results = tripal_core_chado_select('stock', $columns, $values);
// Type
$type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
$r->type = $type_results[0]->name;
// Subject
$r->subject->stock_id = $r->subject_id;
unset($r->subject_id);
$r->subject->stock_name = $results[0]->name;
$r->subject->uniquename = $results[0]->uniquename;
$r->subject->description = $results[0]->description;
$r->subject->stock_type_id = $results[0]->type_id;
$r->subject->organism->organism_id = $results[0]->organism_id;
$r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
$sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
$subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
$r->subject->nid = $subject_node->nid;
$node->subject_relationships[] = $r;
}
return $node;
}
/*************************************************************************
* Implements hook_form()
* Creates the main Add/Edit/Delete Form for chado stocks
*
* Parts to be added by this form
* name,
* uniquename,
* description,
* type => select from cvterm with key cvterm_id,
* organism => select from available with key organism_id
* main_db_reference => accession, version, description, db_name(select from dropdown)
*/
function chado_stock_form($node, $form_state) {
$type = node_get_types('type', $node);
// This defines the path for the next step in a simulated multipart form
// NOTE: The %node gets replaced with the nid in insert
$form['next_step_path'] = array(
'#type' => 'hidden',
'#value' => 'node/%node/properties'
);
// If you don't want a multipart form set this to false
// Will then do default redirect (to new node) on submit
$form['simulate_multipart'] = array(
'#type' => 'textfield',
'#attributes'=>array('style'=>"display:none"),
'#default_value' => TRUE
);
if (!isset($node->uniquename)) {
$form['progress'] = array(
'#type' => 'item',
'#value' => tripal_stock_add_chado_properties_progress('main')
);
}
$form['names'] = array(
'#type' => 'fieldset',
'#title' => t('Stock Name')
);
$form['names']['title'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $node->title,
'#required' => TRUE
);
$form['names']['uniquename'] = array(
'#type' => 'textfield',
'#title' => t('Unique Name'),
'#default_value' => $node->uniquename,
'#required' => TRUE
);
$form['names']['stock_id'] = array(
'#type' => 'hidden',
'#value' => $node->stock_id
);
$form['details'] = array(
'#type' => 'fieldset',
'#title' => t('Stock Details')
);
$type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
$type_options[0] = 'Select a Type';
if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
$form['details']['type_id'] = array(
'#type' => 'select',
'#title' => t('Type of Stock'),
'#options' => $type_options,
'#default_value' => $type_default,
'#required' => TRUE,
);
$stock_oganism_options = tripal_organism_get_organism_options();
$stock_oganism_options[0] = 'Select An Organism';
if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
$form['details']['organism_id'] = array(
'#type' => 'select',
'#title' => t('Source Organism for stock'),
'#default_value' => $organism_default,
'#options' => $stock_oganism_options,
'#required' => TRUE
);
$form['details']['stock_description'] = array(
'#type' => 'textarea',
'#title' => t('Notes'),
'#default_value' => $node->description,
'#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
);
$form['database_reference'] = array(
'#type' => 'fieldset',
'#title' => t('Stock Database Reference')
);
$form['database_reference']['accession'] = array(
'#type' => 'textfield',
'#title' => t('Accession'),
'#default_value' => $node->main_db_reference->accession
);
$form['database_reference']['db_description'] = array(
'#type' => 'textarea',
'#title' => t('Description of Database Reference'),
'#default_value' => $node->main_db_reference->db_description,
'#description' => t('Optionally enter a description about the database accession.')
);
$db_options = tripal_db_get_db_options();
$db_options[0] = 'Select a Database';
if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
$form['database_reference']['database'] = array(
'#type' => 'select',
'#title' => t('Database'),
'#options' => $db_options,
'#default_value' => $db_default
);
return $form;
}
/*************************************************************************
* Implements hook_validate()
*/
function chado_stock_validate($node, &$form) {
$int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
$string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
// Validate Uniquename only if add
if (empty($node->stock_id)) {
$previous_db = tripal_db_set_active('chado');
$chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
tripal_db_set_active($previous_db);
if(!empty($chado_row->stock_id)) {
$drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
if (!empty($drupal_row->nid)) {
$link = l('node/'.$drupal_row->nid, $node->uniquename);
form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
} else {
form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
}
}
}
// Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
if ( $node->type_id == 0) {
form_set_error('type_id', 'Please select a type of stock.');
} else {
$previous_db = tripal_db_set_active('chado');
$num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
tripal_db_set_active($previous_db);
if ( $num_rows->count != 1) {
form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
}
// Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
if ( $node->organism_id == 0) {
form_set_error('organism_id', 'Please select a source organism for this stock');
} else {
$previous_db = tripal_db_set_active('chado');
$num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
tripal_db_set_active($previous_db);
if ( $num_rows->count != 1 ) {
form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
}
// Check if Accession also database
if ($node->accession != '') {
if ($node->database == 0) {
// there is an accession but no database selected
form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
}
} else {
if ($node->database > 0) {
// there is a database selected but no accession
form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
}
}
// Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
if ( $node->database > 0) {
$previous_db = tripal_db_set_active('chado');
$num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
tripal_db_set_active($previous_db);
if ($num_rows->count != 1) {
form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
}
// Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
if ( $node->accession != '') {
$previous_db = tripal_db_set_active('chado');
$num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
tripal_db_set_active($previous_db);
if ($num_rows->count > 0) {
form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
}
}
/*************************************************************************
* Implements hook_insert()
* Inserts data from chado_stock_form() into drupal and chado
*/
function chado_stock_insert($node) {
// create dbxref
if ( !empty($node->accession) ) {
if ( !empty($node->database) ) {
$values = array(
'db_id' => $node->database,
'accession' => $node->accession,
);
if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
$values['description'] = $node->db_description;
$values['version'] = '1';
$dbxref_status = tripal_core_chado_insert('dbxref', $values);
if (!$dbxref_status) {
drupal_set_message('Unable to add database reference to this stock.', 'warning');
watchdog('tripal_stock',
'Insert Stock: Unable to create dbxref where values:%values',
array('%values' => print_r($values, TRUE)),
WATCHDOG_WARNING
);
}
} else { $dbxref_status = 1; }
}
}
// create stock
if($dbxref_status) {
$values = array(
'dbxref_id' => array(
'db_id' => $node->database,
'accession' => $node->accession
),
'organism_id' => $node->organism_id,
'name' => $node->title,
'uniquename' => $node->uniquename,
'description' => $node->description,
'type_id' => $node->type_id
);
$stock_status = tripal_core_chado_insert('stock', $values);
} else {
$values = array(
'organism_id' => $node->organism_id,
'name' => $node->title,
'uniquename' => $node->uniquename,
'description' => $node->description,
'type_id' => $node->type_id
);
$stock_status = tripal_core_chado_insert('stock', $values);
}
// create drupal chado_stock entry
if ($stock_status) {
$values = array(
'organism_id' => $node->organism_id,
'uniquename' => $node->uniquename,
'type_id' => $node->type_id
);
$chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
if (!empty($chado_stock[0]->stock_id)) {
db_query(
"INSERT INTO {chado_stock} (nid, vid, stock_id) "
."VALUES (%d, %d, %d)",
$node->nid,
$node->vid,
$chado_stock[0]->stock_id
);
//Move on to next stage of Stock Creation based on next_stage_path field
if ($node->simulate_multipart) {
$next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
$_REQUEST['destination'] = $next_stage_path;
}
} else {
drupal_set_message('Error during stock creation.', 'error');
watchdog('tripal_stock',
'Insert Stock: Unable to find newly created stock where values:%values',
array('%values' => print_r($values, TRUE)),
WATCHDOG_ERROR
);
return FALSE;
}
} else {
drupal_set_message('Error during stock creation.', 'error');
watchdog('tripal_stock',
'Insert Stock: Unable to create stock where values:%values',
array('%values' => print_r($values, TRUE)),
WATCHDOG_WARNING
);
return FALSE;
}
}
/*************************************************************************
* Implements hook_update()
* Handles Editing/Updating of main stock info (that in the chado stock table)
* Currently just writes over all old data
*/
function chado_stock_update($node) {
if ($node->revision) {
chado_stock_insert($node);
} else {
//update dbxref
if ($node->database) {
if($node->accession) {
$dbxref_mode = '';
$stock = tripal_core_chado_select(
'stock',
array('dbxref_id', 'type_id'),
array('stock_id' => $node->stock_id)
);
if ($stock[0]->dbxref_id) {
$values = array(
'db_id' => $node->database,
'accession' => $node->accession,
'description' => $node->db_description
);
$dbxref_status = tripal_core_chado_update(
'dbxref',
array('dbxref_id' => $stock[0]->dbxref_id),
$values
);
$dbxref_mode = 'Update';
} else {
if ($stock[0]->type_id) {
//create the dbxref
//used the type_id as a control to check we have a stock but not a dbxref
$values = array(
'db_id' => $node->database,
'accession' => $node->accession,
'description' => $node->db_description,
'version' => '1',
);
$dbxref_status = tripal_core_chado_insert(
'dbxref',
$values
);
$dbxref_mode = 'Create';
} else {
drupal_set_message('Unable to find stock to Update', 'error');
watchdog(
'tripal_stock',
'Stock Update: Unable to find stock to update using values: %values',
array('%values', print_r($values, TRUE)),
WATCHDOG_ERROR
);
return FALSE;
}
}
}
}
if (!$dbxref_status) {
watchdog(
'tripal_stock',
'Stock Update: Unable to %mode main stock dbxref with values: %values',
array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
WATCHDOG_WARNING
);
}
//can't change stock id which is all thats stored in drupal thus only update chado
$update_values = array(
'organism_id' => $node->organism_id,
'name' => $node->title,
'uniquename' => $node->uniquename,
'description' => $node->description,
'type_id' => $node->type_id,
);
if ($dbxref_status) {
$update_values['dbxref_id'] = array(
'db_id' => $node->database,
'accession' => $node->accession
);
}
$status = tripal_core_chado_update(
'stock',
array('stock_id' => $node->stock_id),
$update_values
);
if (!$status) {
drupal_set_message('Unable to update stock', 'error');
watchdog(
'tripal_stock',
'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
WATCHDOG_ERROR
);
}
}
}
/*************************************************************************
* Implements hook_delete()
* Handles deleting of chado_stocks
*/
function chado_stock_delete($node) {
//remove drupal node and all revisions
db_query(
"DELETE FROM {chado_stock} WHERE nid=%d",
$node->nid
);
// Set stock in chado: is_obsolete = TRUE
$previous_db = tripal_db_set_active('chado');
db_query(
"DELETE FROM stock WHERE stock_id=%d",
$node->stock_id
);
tripal_db_set_active($previous_db);
}
/**************************************************************************
* @section Tripal Stock Blocks and Template Preprocessor Functions
**************************************************************************/
/*************************************************************************
* Purpose: Implement Blocks relating to stock content
*/
function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
switch($op) {
case 'list':
$blocks['base']['info'] = t('Tripal Stock Details');
$blocks['base']['cache'] = BLOCK_NO_CACHE;
$blocks['properties']['info'] = t('Tripal Stock Properties');
$blocks['properties']['cache'] = BLOCK_NO_CACHE;
$blocks['references']['info'] = t('Tripal Stock References');
$blocks['references']['cache'] = BLOCK_NO_CACHE;
$blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
$blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
$blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
$blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
$blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
$blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
if(user_access('access chado_stock 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('Stock Details');
$block['content'] = theme('tripal_stock_base',$node);
break;
case 'properties':
$block['subject'] = t('Properties');
$block['content'] = theme('tripal_stock_properties',$node);
break;
case 'references':
$block['subject'] = t('References');
$block['content'] = theme('tripal_stock_references',$node);
break;
case 'relationships_as_object':
$block['subject'] = t('Object Relationships');
$block['content'] = theme('tripal_stock_relationships_as_object',$node);
break;
case 'relationships_as_subject':
$block['subject'] = t('Subject Relationships');
$block['content'] = theme('tripal_stock_relationships_as_subject',$node);
break;
case 'synonyms':
$block['subject'] = t('Synonyms');
$block['content'] = theme('tripal_stock_synonyms',$node);
break;
}
return $block;
}
}
}
function tripal_stock_preprocess_node_chado_stock (&$variables) {
$variables['testing1'] = 'test';
drupal_set_message('here');
}