function chado_example_insert

2.x tripal_example.chado_node.inc chado_example_insert($node)

Implementation of hook_insert(). This function is called after the node is inserted into the database. We need it so that we can insert appropriate fields as provided by the user into the database. And so that we can link the new Drupal node to the data in Chado via the chado_example linking table. We can get to this function also during "syncing". With syncing, however, the data already exists in Chado and we do not want to try to re-add it. But we do need to add an entry to the chado_example table to link the Drupal node with the data in the 'example' table of Chado.

This function is not required if the hook_node_info() does not define any custom node types.

File

tripal_example/includes/tripal_example.chado_node.inc, line 376
This file should contain all Drupal hooks for interacting with nodes.

Code

function chado_example_insert($node) {

  $example_id = '';

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

    // be sure to always trim text fields
    $node->uniquename = trim($node->uniquename);
    $node->description = trim($node->description['value']);

    // get the example type record
    $type_cv = tripal_get_default_cv('example', 'type_id');
    $type = tripal_get_cvterm(array(
      'name' => $node->example_type,
      'cv_id' => $type_cv->cv_id,
    ));

    // perform the insert using the chado_insert_record function();
    $values = array(
      'uniquename' => $node->uniquename,
      'description' => $node->description,
      'type_id' => $type->cvterm_id,
      'organism_id' => $node->organism_id,
    );
    $example = chado_insert_record('example', $values);
    if (!$example) {
      drupal_set_message(t('Unable to add example.'), 'warning');
      tripal_report_error('tripal_example', TRIPAL_WARNING, 'Insert example: Unable to create example where values: %values', 
      array('%values' => print_r($values, TRUE)));
      return;
    }

    // get the example_id for linking Drupal node with Chado data
    $example_id = $example['example_id'];

    // Only add to other Chado tables if the base record was inserted properly
    if ($example_id > 0) {

      // If you implemented the properties form in chado_example_form then you
      // need to handle inserting these properties into your Chado prop table.
      $details = array(
        // the name of the prop table
        'property_table' => 'exampleprop',
        // the name of your Chado base table
        'base_table' => 'example',
        // the name of the key in your base table
        'foreignkey_name' => 'example_id',
        // the value of the example_id key
        'foreignkey_value' => $example_id
      );
      chado_update_node_form_properties($node, $details);

      // If you implemented the dbxrefs form in chado_example_form then you need
      // to handle inserting these database references into your Chado _dbxref
      // table.
      $details = array(
        // the name of your _dbxref table
        'linking_table' => 'example_dbxref',
        // the name of the key in your base table
        'foreignkey_name' => 'example_id',
        // the value of the example_id key
        'foreignkey_value' => $example_id
      );
      chado_update_node_form_dbxrefs($node, $details);

      // If you implemented the relationships form in chado_example_form then
      // you need to handle inserting these relationships into your Chado
      // _relationship table.
      $details = array(
        // name of the _relationship table
        'relationship_table' => 'example_relationship',
        // value of the example_id key
        'foreignkey_value' => $example_id
      );
      chado_update_node_form_relationships($node, $details);
    }
  }
  else {
    // the node has an example_id so get it for linking Drupal node with Chado
    // data
    $example_id = $node->example_id;
  }

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