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/includes/tripal_stock.chado_node.inc, line 451
Stock Node Functionality

Code

function chado_stock_insert($node) {


  $stock_id = '';

  // if there is an stock_id in the $node object then this must be a sync so
  // we can skip adding the stock to chado as it is already there, although
  // we do need to proceed with insertion into the chado/drupal linking table.
  if (!property_exists($node, 'stock_id')) {

    $node->uniquename = trim($node->uniquename);
    $node->sname = trim($node->sname);
    $node->accession = trim($node->accession);
    $node->stock_description = trim($node->stock_description['value']);

    // before we can add the stock, we must add the dbxref if one has been
    // provided by the user.
    $dbxref = NULL;
    if (!empty($node->accession) and !empty($node->database)) {
      $values = array(
        'db_id' => $node->database,
        'accession' => $node->accession,
      );
      if (!chado_select_record('dbxref', array('dbxref_id'), $values)) {
        $values['description'] = $node->db_description;
        $values['version'] = '1';
        $dbxref = chado_insert_record('dbxref', $values);
        if (!$dbxref) {
          drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
          tripal_report_error('tripal_stock', TRIPAL_WARNING, 
          'Insert Stock: Unable to create dbxref where values:%values', 
          array('%values' => print_r($values, TRUE)));
        }
      }
    }

    // create stock including the dbxref
    $stock = '';
    $values = array(
      'organism_id' => $node->organism_id,
      'name' => $node->sname,
      'uniquename' => $node->uniquename,
      'description' => $node->stock_description,
      'type_id' => $node->type_id
    );
    if ($dbxref) {
      $values['dbxref_id'] = array(
        'db_id' => $node->database,
        'accession' => $node->accession
      );
    }
    $stock = chado_insert_record('stock', $values);
    if (!$stock) {
      drupal_set_message(t('Unable to add stock.'), 'warning');
      tripal_report_error('tripal_stock', TRIPAL_WARNING, 'Insert stock: Unable to create stock where values: %values', 
      array('%values' => print_r($values, TRUE)));
      return;
    }
    $stock_id = $stock['stock_id'];

    // Now add properties
    $details = array(
      'property_table' => 'stockprop',
      'base_table' => 'stock',
      'foreignkey_name' => 'stock_id',
      'foreignkey_value' => $stock_id
    );
    chado_update_node_form_properties($node, $details);

    // Now add the additional references
    $details = array(
      'linking_table' => 'stock_dbxref',
      'foreignkey_name' => 'stock_id',
      'foreignkey_value' => $stock_id
    );
    chado_update_node_form_dbxrefs($node, $details);

    // Now add in relationships
    $details = array(
      'relationship_table' => 'stock_relationship',
      'foreignkey_value' => $stock_id
    );
    chado_update_node_form_relationships($node, $details);
  }
  else {
    $stock_id = $node->stock_id;
  }

  // Make sure the entry for this stock doesn't already exist in the
  // chado_stock table if it doesn't exist then we want to add it.
  $check_org_id = chado_get_id_from_nid('stock', $node->nid);
  if (!$check_org_id) {
    $record = new stdClass();
    $record->nid = $node->nid;
    $record->vid = $node->vid;
    $record->stock_id = $stock_id;
    drupal_write_record('chado_stock', $record);
  }
}