function chado_stock_update

2.x tripal_stock.chado_node.inc chado_stock_update($node)
3.x tripal_stock.chado_node.inc chado_stock_update($node)
1.x tripal_stock.module chado_stock_update($node)

Implements hook_update(): Handles Editing/Updating of main stock info

NOTE: Currently just writes over all old data

Parameters

$node: The current node including fields with the form element names and submitted values

Return value

TRUE if the node was successfully updated in drupal/chado; FALSE otherwise

Related topics

File

tripal_stock/tripal_stock.module, line 769
Implements Tripal Stock Module hooks

Code

function chado_stock_update($node) {

  if ($node->revision) {
    // there is no way to handle revisions in Chado but leave
    // this here just to make not we've addressed it.
  }

  //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(t('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->sname,
    'uniquename' => $node->uniquename,
    'description' => $node->stock_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(t('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
    );
  }
  else {
    // set the URL for this stock page
    $values = array('stock_id' => $node->stock_id);
    $stock = tripal_core_chado_select('stock', array('*'), $values);
  }
}