function chado_stock_insert

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

Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado

Parameters

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

Return value

TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise

Related topics

File

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

Code

function chado_stock_insert($node) {

  // If the chado stock exists (e.g. this is only a syncing operation)
  // then don't create but simply link to node
  if ($node->chado_stock_exists) {
    if (!empty($node->stock_id)) {
      db_query(
      "INSERT INTO {chado_stock} (nid, vid, stock_id) "
        . "VALUES (%d, %d, %d)", 
      $node->nid, 
      $node->vid, 
      $node->stock_id
      );
    }

    return $node;
  }

  // before we can add the stock, we must add the dbxref if one has been
  // provided by the user.
  $dbxref_status = 0;
  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(t('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 including the dbxref
  $stock = '';
  if ($dbxref_status) {
    $values = array(
      'dbxref_id' => array(
        'db_id' => $node->database,
        'accession' => $node->accession
      ),
      'organism_id' => $node->organism_id,
      'name' => $node->sname,
      'uniquename' => $node->uniquename,
      'description' => $node->stock_description,
      'type_id' => $node->type_id
    );
    $stock = tripal_core_chado_insert('stock', $values);
  }
  // create a stock without a dbxref
  else {
    $values = array(
      'organism_id' => $node->organism_id,
      'name' => $node->sname,
      'uniquename' => $node->uniquename,
      'description' => $node->stock_description,
      'type_id' => $node->type_id
    );
    $stock = tripal_core_chado_insert('stock', $values);
  }

  // if the stock creation was succesful then add the URL and the entry in the
  // chado_stock table
  if (is_array($stock)) {

    // convert the stock into an object
    $stock = (object) $stock;

    // add the entry to the chado_stock table
    $sql = "INSERT INTO {chado_stock} (nid, vid, stock_id) VALUES (%d, %d, %d)";
    db_query($sql, $node->nid, $node->vid, $stock->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(t('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;
  }
}