Tripal 0.3b
tripal_stock/tripal_stock.module
Go to the documentation of this file.
00001 <?php
00002 // $Id$
00003 
00004 /**
00005  * @file
00006  * Implements Tripal Stock Module hooks
00007  */
00008 
00009 /**
00010  * @defgroup tripal_stock Stock Module
00011  * @{
00012  * Provides functions for managing chado stocks including creating details pages for each stock
00013  *
00014  * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado 
00015  * stocks. The stock module was designed to store information about stock collections in a 
00016  * laboratory. What is called a stock could also be called a strain or an accession. There is a lot 
00017  * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all 
00018  * come from some taxon, have genotypes, physical locations in the lab, some conceivable 
00019  * relationship with a publication, some conceivable relationship with a sequence feature (such as a 
00020  * transgene), and could be described by some ontology term. For more information about the chado 
00021  * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
00022  * @}
00023  * @ingroup tripal_modules
00024  */
00025 require("tripal_stock-administration.inc");
00026 
00027 require("tripal_stock.api.inc");
00028 require("other_module_api_functions.inc");
00029 
00030 require("tripal_stock-secondary_tables.inc");
00031 require("tripal_stock-properties.inc");
00032 require("tripal_stock-relationships.inc");
00033 require("tripal_stock-db_references.inc");
00034 
00035 /**
00036  * Implements hook_menu(): Adds menu items for the tripal_stock
00037  *
00038  * @return
00039  *   Menu definitions for the tripal_stock
00040  *
00041  * @ingroup tripal_stock
00042  */
00043 function tripal_stock_menu() {
00044   $items = array();
00045 
00046   //Administrative settings menu-----------------
00047   $items['admin/tripal/tripal_stock'] = array(
00048     'title' => t('Stocks'),
00049     'description' => t('Basic Description of Tripal Stock Module Functionality'),
00050     'page callback' => 'tripal_stock_module_description_page',
00051     'access arguments' => array('administer site configuration'),
00052     'type' => MENU_NORMAL_ITEM
00053   );
00054   
00055   $items['admin/tripal/tripal_stock/configuration'] = array(
00056     'title' => t('Configuration'),
00057     'description' => t('Settings for Chado Stocks'),
00058     'page callback' => 'drupal_get_form',
00059     'page arguments' => array('tripal_stock_admin'),
00060     'access arguments' => array('administer site configuration'),
00061     'type' => MENU_NORMAL_ITEM
00062   );
00063   
00064   //Displaying stocks----------------------------
00065   $items['stocks'] = array(
00066     'menu_name' => ('primary-links'),
00067     'title' => t('Stocks'),
00068     'page callback' => 'tripal_stock_show_stocks',
00069     'access arguments' => array('access chado_stock content'),
00070     'type' => MENU_NORMAL_ITEM,  
00071   );
00072 
00073   // Adding Secondary Properties-----------------
00074   $items['node/%cs_node/properties'] = array(       
00075     'title' => t('Add Properties & Synonyms'),                         
00076     'description' => t('Settings for Chado Stocks'),
00077     'page callback' => 'tripal_stock_add_ALL_property_page',           
00078     'page arguments' => array(1), 
00079     'access arguments' => array('create chado_stock content'),
00080     'type' => MENU_CALLBACK
00081   ); 
00082 
00083   $items['node/%cs_node/db_references'] = array(                        
00084     'title' => t('Add Database References'),                   
00085     'description' => t('Settings for Chado Stocks'),              
00086     'page callback' => 'tripal_stock_add_ALL_dbreferences_page',                         
00087     'page arguments' => array(1),
00088     'access arguments' => array('create chado_stock content'),
00089     'type' => MENU_CALLBACK
00090   ); 
00091 
00092   $items['node/%cs_node/relationships'] = array(                      
00093     'title' => t('Add Relationships'),                      
00094     'description' => t('Settings for Chado Stocks'),               
00095     'page callback' => 'tripal_stock_add_ALL_relationships_page',                          
00096     'page arguments' => array(1),
00097     'access arguments' => array('create chado_stock content'),
00098     'type' => MENU_CALLBACK
00099   );
00100 
00101   //Edit/Deleting Secondary Properties-------------
00102   $items['node/%cs_node/edit_properties'] = array(
00103     'title' => t('Edit Properties'),
00104     'description' => t('Settings for Chado Stocks'),
00105     'page callback' => 'tripal_stock_edit_ALL_properties_page',
00106     'page arguments' => array(1),
00107     'access arguments' => array('edit chado_stock content'),
00108     'type' => MENU_LOCAL_TASK,
00109     'weight' => 8,
00110   );
00111 
00112     $items['node/%cs_node/edit_relationships'] = array(
00113     'title' => t('Edit Relationships'),
00114     'description' => t('Settings for Chado Stocks'), 
00115     'page callback' => 'tripal_stock_edit_ALL_relationships_page',
00116     'page arguments' => array(1),
00117     'access arguments' => array('edit chado_stock content'),
00118     'type' => MENU_LOCAL_TASK,
00119     'weight' => 9,
00120   );
00121 
00122   $items['node/%cs_node/edit_db_references'] = array(
00123     'title' => t('Edit DB References'),
00124     'description' => t('Settings for Chado Stocks'),
00125     'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
00126     'page arguments' => array(1),
00127     'access arguments' => array('edit chado_stock content'),
00128     'type' => MENU_LOCAL_TASK,
00129     'weight' => 10,
00130   );
00131   return $items;
00132 }
00133 
00134 /**
00135  * Implements Menu wildcard_load hook
00136  *
00137  * Purpose: Allows the node ID of a chado stock to be dynamically 
00138  *   pulled from the path. The node is loaded from this node ID
00139  *   and supplied to the page as an arguement. This is an example 
00140  *   of dynamic argument replacement using wildcards in the path.
00141  *
00142  * @param $nid
00143  *   The node ID passed in from the path
00144  *
00145  * @return
00146  *   The node object with the passed in nid
00147  *
00148  * @ingroup tripal_stock
00149  */
00150 function cs_node_load($nid) {
00151   if (is_numeric($nid)) {
00152     $node = node_load($nid);
00153     if ($node->type == 'chado_stock') {
00154       return $node;
00155     }
00156   }
00157 
00158   return FALSE;
00159 }
00160 
00161 /**
00162  * Implementation of hook_perm()
00163  *
00164  * Purpose: Set the permission types that the chado stock module uses
00165  *
00166  * @return
00167  *   Listing of the possible permission catagories
00168  *
00169  * @ingroup tripal_stock
00170  */
00171 function tripal_stock_perm() {
00172   return array(
00173     'access chado_stock content',
00174     'create chado_stock content',
00175     'edit chado_stock content',
00176     'delete chado_stock content'
00177   );
00178 }
00179 
00180 /**
00181  * Implements hook_access(): Maps permission catagories to actions
00182  *
00183  * @param $op
00184  *   The operation current operation: one of create, update, delete
00185  * @param $node
00186  *   The node object the current operation is being performed on
00187  * @param $account
00188  *   A user object representing the user for whom the operation is to be performed
00189  *
00190  * @return
00191  *   TRUE grants access; FALSE denies it
00192  *
00193  * @ingroup tripal_stock
00194  */
00195 function chado_stock_access($op, $node, $account) {
00196    if ($op == 'create') {
00197       return user_access('create chado_stock content', $account);
00198    }
00199 
00200    if ($op == 'update') {
00201       if (user_access('edit chado_stock content', $account)) {
00202          return TRUE;
00203       }
00204    }
00205    if ($op == 'delete') {
00206       if (user_access('delete chado_stock content', $account)) {
00207          return TRUE;
00208       }
00209    }
00210    if ($op == 'view') {
00211       if (user_access('access chado_stock content', $account)) {
00212          return TRUE;
00213       }
00214    }
00215    return FALSE;
00216 }
00217 
00218 /**
00219  * Implements hook_views_api()
00220  *
00221  * Purpose: Essentially this hook tells drupal that there is views support for
00222  *  for this module which then includes tripal_stock.views.inc where all the
00223  *  views integration code is
00224  *
00225  * @return
00226  *   An array with fields important for views integration
00227  *
00228  * @ingroup tripal_stock
00229  */ 
00230 function tripal_stock_views_api() {
00231    return array(
00232       'api' => 2.0,
00233    );
00234 }
00235 
00236 /**
00237  * Implements hook_theme(): Register themeing functions for this module
00238  *
00239  * @return
00240  *   An array of themeing functions to register
00241  *
00242  * @ingroup tripal_stock
00243  */
00244 function tripal_stock_theme() {
00245   return array(
00246     'tripal_stock_stock_table' => array (
00247       'arguments' => array('stocks'),
00248     ),
00249     // Property edit forms--------------------------
00250     'tripal_stock_edit_ALL_properties_form' => array(
00251       'arguments' => array('form'),
00252       'function' => 'theme_tripal_stock_edit_ALL_properties_form',
00253     ),
00254     'tripal_stock_edit_ALL_db_references_form' => array(
00255       'arguments' => array('form'),
00256       'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
00257     ),
00258     'tripal_stock_edit_ALL_relationships_form' => array(
00259       'arguments' => array('form'),
00260       'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
00261     ),
00262     // Block Templates------------------------------
00263     'tripal_stock_base' => array (
00264       'arguments' => array('node'=> null),
00265       'template' => 'tripal_stock_base',
00266     ),
00267     'tripal_stock_properties' => array (
00268       'arguments' => array('node'=> null),
00269       'template' => 'tripal_stock_properties',
00270     ),
00271     'tripal_stock_references' => array (
00272       'arguments' => array('node'=> null),
00273       'template' => 'tripal_stock_references',
00274     ),
00275     'tripal_stock_relationships_as_object' => array (
00276       'arguments' => array('node'=> null),
00277       'template' => 'tripal_stock_relationships_as_object',
00278     ),    
00279     'tripal_stock_relationships_as_subject' => array (
00280       'arguments' => array('node'=> null),
00281       'template' => 'tripal_stock_relationships_as_subject',
00282     ),
00283     'tripal_stock_synonyms' => array (
00284       'arguments' => array('node'=> null),
00285       'template' => 'tripal_stock_synonyms',
00286     ),
00287   );
00288 }
00289 
00290 /**
00291  * Purpose: show stocks stored in drupals chado_stock table
00292  *
00293  * This function provides the default html representation of the stock table. This
00294  * representation can be overridden using Drupal Views2 to create more flexible tables
00295  * listing stocks.
00296  *
00297  * @return
00298  *   HTML representation of a table of stocks
00299  *
00300  * @ingroup tripal_stock
00301  */
00302 function tripal_stock_show_stocks () {
00303   $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
00304    $no_stocks = db_result(db_query($sql));
00305    if($no_stocks != 0) {
00306       $stocks = tripal_stock_get_all_stocks();
00307       if($no_stocks != count($stocks)) {
00308          drupal_set_message("Synchronization needed.");
00309       }
00310       return theme('tripal_stock_stock_table',&$stocks);
00311    } else {
00312       return t("No Stocks exists. Please contact administrators to ".
00313                "synchronize stocks.");
00314    }
00315 }
00316 
00317 /**
00318  * A themeing funtion for the default tripal stock table
00319  *
00320  * @param $stocks
00321  *   An array of all stock nodes
00322  *
00323  * @return
00324  *   HTML representation of a table of stocks
00325  *
00326  * @ingroup tripal_stock
00327  */
00328 function theme_tripal_stock_stock_table (&$stocks) {
00329 
00330   // cycle through the stocks and build the stocks page
00331   $output = "<div id=\"stocks\">";
00332   $output .= '<table>';
00333   $output .= "<tr>";
00334   $output .= "<th>Name</th>";
00335   $output .= "<th>Type</th>";
00336   $output .= "<th>Organism</th>";
00337   $output .= "<th>Description</th>";
00338   $output .= "</tr>";
00339 
00340   foreach($stocks as $node){
00341     $output .= "<tr>";
00342     $output .= "<td>".l($node->stock->name, "node/".$node->nid)."</td>";
00343     $output .= "<td>".$node->stock->type_id->name."</td>";
00344     $output .= "<td nowrap>".$node->stock->organism_id->common_name."</td>";
00345     $output .= "<td>".$node->stock->description."</td>";
00346     $output .= "</tr>";
00347   }
00348   $output .= "</table>";
00349   $output .= "</div>";
00350 
00351    return $output;
00352 }
00353 
00354 /**
00355  * Implements hook_node_info(): registers a stock node type
00356  *
00357  * @return
00358  *   An array describing various details of the node
00359  *
00360  * @ingroup tripal_stock
00361  */
00362 function tripal_stock_node_info() {
00363   return array(
00364     'chado_stock' => array(
00365       'name' => t('Stock'),
00366       'module' => 'chado_stock',
00367       'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
00368       'has_title' => TRUE,
00369       'has_body' => FALSE,
00370     ),
00371   );
00372 }
00373 
00374 /**
00375  * Implements hook_load(): Prepares the chado_stock node
00376  *
00377  * @param $node
00378  *   The basic node containing all variables common to all nodes
00379  *
00380  * @return
00381  *   A stock node containing all the variables from the basic node and all stock specific variables
00382  *
00383  * @ingroup tripal_stock
00384  */
00385 function chado_stock_load($node) {
00386 
00387   // Get stock_id from chado_stock linking table
00388   $map = db_fetch_object(db_query(
00389     "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
00390     $node->vid
00391   ));
00392   
00393   // Get stock content and add to node
00394   $stock = tripal_core_generate_chado_var('stock', array('stock_id'=>$map->stock_id));
00395   //
00396   // move expandable fields downwards
00397   $node->expandable_fields = $stock->expandable_fields;
00398   unset($stock->expandable_fields);
00399   $node->expandable_tables = $stock->expandable_tables;
00400   unset($stock->expandable_tables);
00401   $node->expandable_nodes = $stock->expandable_nodes;
00402   unset($stock->expandable_nodes);
00403   $node->stock = $stock;
00404   
00405   return $node;
00406 }
00407 
00408 
00409 /**
00410  * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
00411  *
00412  * Parts to be added by this form
00413  *     name, 
00414  *     uniquename, 
00415  *     description, 
00416  *     type => select from cvterm with key cvterm_id,
00417  *     organism => select from available with key organism_id
00418  *     main_db_reference => accession, version, description, db_name(select from dropdown) 
00419  *
00420  * @param $node
00421  *   An empty node object on insert OR the current stock node object on update
00422  * @param $form_state
00423  *   The current state of the form
00424  *
00425  * @return
00426  *   A description of the form to be rendered by drupal_get_form()
00427  *
00428  * @ingroup tripal_stock
00429  */
00430 function chado_stock_form($node, $form_state) {
00431 
00432   // Expand all fields needed
00433   $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
00434   foreach ($fields_needed as $field_name) {
00435     // Check to see if it's excluded and expand it if so
00436     if ($node->expandable_fields) {
00437       if (in_array($field_name, $node->expandable_fields)) {
00438         $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
00439       }
00440     }
00441   }
00442 
00443   // This defines the path for the next step in a simulated multipart form
00444   // NOTE: The %node gets replaced with the nid in insert
00445   $form['next_step_path'] = array(
00446     '#type' => 'hidden',
00447     '#value' => 'node/%node/properties'
00448   );
00449 
00450   // If you don't want a multipart form set this to false
00451   // Will then do default redirect (to new node) on submit
00452   $form['simulate_multipart'] = array(
00453     '#type' => 'textfield',
00454     '#attributes'=>array('style'=>"display:none"),
00455     '#default_value' => TRUE
00456   );
00457 
00458   if (!isset($node->stock->uniquename)) {
00459     $form['progress'] = array( 
00460       '#type' => 'item', 
00461       '#value' => tripal_stock_add_chado_properties_progress('main')
00462     );
00463   }
00464 
00465   $form['names'] = array(
00466     '#type' => 'fieldset',
00467     '#title' => t('Stock Name')
00468   );
00469 
00470   $form['names']['title'] = array(
00471     '#type' => 'textfield',
00472     '#title' => t('Name'),
00473     '#default_value' => $node->stock->name,
00474     '#required'      => TRUE
00475   );
00476 
00477   $form['names']['uniquename'] = array(
00478     '#type' => 'textfield',
00479     '#title' => t('Unique Name'),
00480     '#default_value' => $node->stock->uniquename,
00481     '#required'      => TRUE
00482   );
00483 
00484   $form['names']['stock_id'] = array(
00485     '#type' => 'hidden',
00486     '#value' => $node->stock->stock_id
00487   );
00488 
00489   $form['details'] = array(
00490     '#type' => 'fieldset',
00491     '#title' => t('Stock Details')
00492   );
00493 
00494   $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
00495   $type_options[0] = 'Select a Type';
00496   if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock->type_id->cvterm_id; }
00497   $form['details']['type_id'] = array(
00498     '#type' => 'select',
00499     '#title' => t('Type of Stock'),
00500     '#options' => $type_options,
00501     '#default_value' => $type_default,
00502     '#required'   => TRUE,
00503   );
00504 
00505   $stock_oganism_options = tripal_organism_get_organism_options();
00506   $stock_oganism_options[0] = 'Select An Organism';
00507   if ($node->nid == '') { $organism_default = 0; } else {  $organism_default = $node->stock->organism_id->organism_id; }
00508   $form['details']['organism_id'] = array(
00509     '#type' => 'select',
00510     '#title' => t('Source Organism for stock'),
00511     '#default_value' => $organism_default,
00512     '#options' => $stock_oganism_options,
00513     '#required'   => TRUE
00514   );
00515 
00516 
00517   $form['details']['stock_description'] = array(
00518     '#type' => 'textarea',
00519     '#title' => t('Notes'),
00520     '#default_value' => $node->stock->description,
00521     '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
00522   );
00523 
00524   $form['database_reference'] = array(
00525     '#type' => 'fieldset',
00526     '#title' => t('Stock Database Reference')
00527   );
00528 
00529   $form['database_reference']['accession'] = array(
00530     '#type' => 'textfield',
00531     '#title' => t('Accession'),
00532     '#default_value' => $node->stock->dbxref_id->accession
00533   );
00534 
00535   $form['database_reference']['db_description'] = array(
00536     '#type' => 'textarea',
00537     '#title' => t('Description of Database Reference'),
00538     '#default_value' => $node->stock->dbxref_id->description,
00539     '#description' => t('Optionally enter a description about the database accession.')
00540   );
00541 
00542   $db_options = tripal_db_get_db_options();
00543   $db_options[0] = 'Select a Database';
00544   if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
00545   $form['database_reference']['database'] = array(
00546     '#type' => 'select',
00547     '#title' => t('Database'),
00548     '#options' => $db_options,
00549     '#default_value' => $db_default
00550   );
00551 
00552   return $form;
00553 }
00554 
00555 /**
00556  * Implements hook_validate(): Validate the input from the chado_stock node form
00557  *
00558  * @param $node
00559  *   The current node including fields with the form element names and submitted values
00560  * @param $form
00561  *   A description of the form to be rendered by drupal_get_form()
00562  *
00563  * @ingroup tripal_stock
00564  */
00565 function chado_stock_validate($node, &$form) {
00566 
00567   $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
00568   $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
00569   
00570   // Validate Uniquename only if add
00571   if (empty($node->stock_id)) {
00572     $previous_db = tripal_db_set_active('chado');
00573     $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
00574     tripal_db_set_active($previous_db);
00575     if(!empty($chado_row->stock_id)) {
00576       $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
00577       if (!empty($drupal_row->nid)) {
00578         $link = l('node/'.$drupal_row->nid, $node->uniquename);
00579         form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
00580       } else {
00581         form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
00582       }
00583     }
00584   }
00585 
00586 
00587   // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
00588   if ( $node->type_id == 0) {
00589     form_set_error('type_id', 'Please select a type of stock.');
00590   } else {
00591     $previous_db = tripal_db_set_active('chado');
00592     $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
00593     tripal_db_set_active($previous_db);
00594     if ( $num_rows->count != 1) {
00595       form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
00596   }
00597 
00598   // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
00599   if ( $node->organism_id == 0) {
00600     form_set_error('organism_id', 'Please select a source organism for this stock');
00601   } else {
00602     $previous_db = tripal_db_set_active('chado');
00603     $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
00604     tripal_db_set_active($previous_db);
00605     if ( $num_rows->count != 1 ) {
00606       form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
00607   }
00608 
00609   // Check if Accession also database
00610   if ($node->accession != '') { 
00611     if ($node->database == 0) {
00612       // there is an accession but no database selected
00613       form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
00614     }
00615   } else {
00616     if ($node->database > 0) {
00617       // there is a database selected but no accession
00618       form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
00619     }
00620   }
00621 
00622   // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
00623   if ( $node->database > 0) {
00624     $previous_db = tripal_db_set_active('chado');
00625     $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
00626     tripal_db_set_active($previous_db);
00627     if ($num_rows->count != 1) {
00628       form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
00629   }
00630 
00631 }
00632 
00633 /**
00634  * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
00635  *
00636  * @param $node
00637  *   The current node including fields with the form element names and submitted values
00638  *
00639  * @return
00640  *   TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
00641  *
00642  * @ingroup tripal_stock
00643  */
00644 function chado_stock_insert($node) {
00645 
00646   // create dbxref
00647   if ( !empty($node->accession) ) {
00648     if ( !empty($node->database) ) { 
00649       $values = array(
00650         'db_id' => $node->database,
00651         'accession' => $node->accession,
00652       );
00653       if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
00654         $values['description'] = $node->db_description;
00655         $values['version'] = '1';
00656         $dbxref_status = tripal_core_chado_insert('dbxref', $values);
00657         if (!$dbxref_status) {
00658           drupal_set_message('Unable to add database reference to this stock.', 'warning');
00659           watchdog('tripal_stock', 
00660             'Insert Stock: Unable to create dbxref where values:%values', 
00661             array('%values' => print_r($values, TRUE)),
00662             WATCHDOG_WARNING
00663           );
00664         }
00665       } else { $dbxref_status = 1; }
00666     }
00667   }
00668   
00669   // create stock
00670   if($dbxref_status) {
00671     $values = array(
00672       'dbxref_id' => array(
00673         'db_id' => $node->database, 
00674         'accession' => $node->accession
00675       ),
00676       'organism_id' => $node->organism_id,
00677       'name' => $node->title,
00678       'uniquename' => $node->uniquename,
00679       'description' => $node->stock_description,
00680       'type_id' => $node->type_id
00681     );
00682     $stock_status = tripal_core_chado_insert('stock', $values);
00683   } else {
00684     $values = array(
00685       'organism_id' => $node->organism_id,
00686       'name' => $node->title,
00687       'uniquename' => $node->uniquename,
00688       'description' => $node->stock_description,
00689       'type_id' => $node->type_id
00690     );
00691     $stock_status = tripal_core_chado_insert('stock', $values);
00692   }
00693 
00694   // create drupal chado_stock entry
00695   if ($stock_status) {
00696     $values = array(
00697       'organism_id' => $node->organism_id,
00698       'uniquename' => $node->uniquename,
00699       'type_id' => $node->type_id
00700     );
00701     $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
00702     if (!empty($chado_stock[0]->stock_id)) {
00703       db_query(
00704         "INSERT INTO {chado_stock} (nid, vid, stock_id) "
00705         ."VALUES (%d, %d, %d)",
00706         $node->nid,
00707         $node->vid,
00708         $chado_stock[0]->stock_id
00709       );
00710 
00711       //Move on to next stage of Stock Creation based on next_stage_path field
00712       if ($node->simulate_multipart) {
00713         $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);  
00714         $_REQUEST['destination'] = $next_stage_path;
00715       }
00716     } else {
00717       drupal_set_message('Error during stock creation.', 'error');
00718       watchdog('tripal_stock', 
00719         'Insert Stock: Unable to find newly created stock where values:%values', 
00720         array('%values' => print_r($values, TRUE)),
00721         WATCHDOG_ERROR
00722       );
00723       return FALSE;
00724     }
00725   } else {
00726     drupal_set_message('Error during stock creation.', 'error');
00727     watchdog('tripal_stock', 
00728       'Insert Stock: Unable to create stock where values:%values', 
00729       array('%values' => print_r($values, TRUE)),
00730       WATCHDOG_WARNING
00731     );
00732     return FALSE;
00733   }
00734 
00735 }
00736 
00737 /**
00738  * Implements hook_update(): Handles Editing/Updating of main stock info
00739  *
00740  * NOTE: Currently just writes over all old data
00741  *
00742  * @param $node
00743  *   The current node including fields with the form element names and submitted values
00744  *
00745  * @return
00746  *   TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
00747  *
00748  * @ingroup tripal_stock
00749  */
00750 function chado_stock_update($node) {
00751 
00752   if ($node->revision) {
00753     chado_stock_insert($node);
00754   } else {
00755 
00756     //update dbxref
00757     if ($node->database) {
00758       if($node->accession) {
00759         $dbxref_mode = '';
00760         $stock = tripal_core_chado_select(
00761           'stock', 
00762           array('dbxref_id', 'type_id'), 
00763           array('stock_id' => $node->stock_id)
00764         );
00765       
00766         if ($stock[0]->dbxref_id) {
00767           $values = array(
00768             'db_id' => $node->database,
00769             'accession' => $node->accession,
00770             'description' => $node->db_description
00771           );
00772           $dbxref_status = tripal_core_chado_update(
00773             'dbxref', 
00774             array('dbxref_id' => $stock[0]->dbxref_id), 
00775             $values
00776           );
00777           $dbxref_mode = 'Update';
00778         } else {
00779           if ($stock[0]->type_id) {
00780             //create the dbxref
00781             //used the type_id as a control to check we have a stock but not a dbxref
00782             $values = array(
00783               'db_id' => $node->database,
00784               'accession' => $node->accession,
00785               'description' => $node->db_description,
00786               'version' => '1',
00787             );            
00788             $dbxref_status = tripal_core_chado_insert(
00789               'dbxref',
00790               $values
00791             );
00792             $dbxref_mode = 'Create';
00793           } else {
00794             drupal_set_message('Unable to find stock to Update', 'error');
00795             watchdog(
00796               'tripal_stock',
00797               'Stock Update: Unable to find stock to update using values: %values',
00798               array('%values', print_r($values, TRUE)),
00799               WATCHDOG_ERROR
00800             );
00801             return FALSE;
00802           }
00803         }
00804       }
00805     }
00806     
00807     if (!$dbxref_status) {
00808       watchdog(
00809         'tripal_stock',
00810         'Stock Update: Unable to %mode main stock dbxref with values: %values',
00811         array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
00812         WATCHDOG_WARNING
00813       );
00814     }
00815 
00816     //can't change stock id which is all thats stored in drupal thus only update chado
00817     $update_values = array(
00818       'organism_id' => $node->organism_id,
00819       'name' => $node->title,
00820       'uniquename' => $node->uniquename,
00821       'description' => $node->stock_description,
00822       'type_id' => $node->type_id,
00823     );
00824     if ($dbxref_status) {
00825       $update_values['dbxref_id'] = array(
00826         'db_id' => $node->database, 
00827         'accession' => $node->accession
00828       );
00829     }
00830     $status = tripal_core_chado_update(
00831       'stock',
00832       array('stock_id' => $node->stock_id), 
00833       $update_values
00834     );
00835     
00836     if (!$status) {
00837       drupal_set_message('Unable to update stock', 'error');
00838       watchdog(
00839         'tripal_stock',
00840         'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
00841         array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
00842         WATCHDOG_ERROR
00843       );
00844     }
00845   }
00846 
00847 }
00848 
00849 /**
00850  * Implements hook_delete(): Handles deleting of chado_stocks
00851  *
00852  * NOTE: Currently deletes data -no undo or record-keeping functionality
00853  *
00854  * @param $node
00855  *   The current node including fields with the form element names and submitted values
00856  *
00857  * @return
00858  *   TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
00859  *
00860  * @ingroup tripal_stock
00861  */
00862 function chado_stock_delete($node) {
00863 
00864   //remove drupal node and all revisions
00865   db_query(
00866     "DELETE FROM {chado_stock} WHERE nid=%d",
00867     $node->nid
00868   );
00869   
00870   // Set stock in chado: is_obsolete = TRUE
00871   $previous_db = tripal_db_set_active('chado');
00872   db_query(
00873     "DELETE FROM stock WHERE stock_id=%d",
00874     $node->stock_id
00875   );
00876   tripal_db_set_active($previous_db);
00877 }
00878 
00879 /**
00880  * Purpose: Implement Blocks relating to stock content
00881  *
00882  * @param $op
00883  *   What kind of information to retrieve about the block or blocks. 
00884  *   Possible values include list, configure, save, view.
00885  * @param $delta
00886  *   Which block to return (not applicable if $op is 'list').
00887  * @param $edit
00888  *   If $op is 'save', the submitted form data from the configuration form.
00889  *
00890  * @return
00891  *   One of the following depending on $op: An array of block descriptions (list), the configuration 
00892  *   form (configure), nothing (save), an array defining subject and content for the block indexed 
00893  *   by $delta (view)
00894  *
00895  * @ingroup tripal_stock
00896  */
00897 function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
00898   switch($op) {
00899     case 'list':
00900       $blocks['base']['info'] = t('Tripal Stock Details');
00901       $blocks['base']['cache'] = BLOCK_NO_CACHE;
00902 
00903       $blocks['properties']['info'] = t('Tripal Stock Properties');
00904       $blocks['properties']['cache'] = BLOCK_NO_CACHE;
00905          
00906       $blocks['references']['info'] = t('Tripal Stock References');
00907       $blocks['references']['cache'] = BLOCK_NO_CACHE;
00908          
00909       $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
00910       $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
00911          
00912       $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
00913       $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
00914          
00915       $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
00916       $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
00917                   
00918       return $blocks;
00919 
00920     case 'view':
00921       if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
00922         $nid = arg(1);
00923         $node = node_load($nid);
00924  
00925         $block = array();
00926         switch($delta){
00927           case 'base':
00928             $block['subject'] = t('Stock Details');
00929             $block['content'] = theme('tripal_stock_base',$node);
00930             break;
00931             
00932           case 'properties':
00933             $block['subject'] = t('Properties');
00934             $block['content'] = theme('tripal_stock_properties',$node);
00935             break;
00936             
00937           case 'references':
00938             $block['subject'] = t('References');
00939             $block['content'] = theme('tripal_stock_references',$node);
00940             break;
00941             
00942           case 'relationships_as_object':
00943             $block['subject'] = t('Object Relationships');
00944             $block['content'] = theme('tripal_stock_relationships_as_object',$node);
00945             break;
00946             
00947           case 'relationships_as_subject':
00948             $block['subject'] = t('Subject Relationships');
00949             $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
00950             break;
00951             
00952           case 'synonyms':
00953             $block['subject'] = t('Synonyms');
00954             $block['content'] = theme('tripal_stock_synonyms',$node);
00955             break;
00956             
00957         }
00958         return $block;
00959       }
00960   }
00961 }
 All Classes Files Functions Variables