function chado_organism_insert

2.x tripal_organism.chado_node.inc chado_organism_insert($node)
3.x tripal_organism.chado_node.inc chado_organism_insert($node)
1.x tripal_organism.module chado_organism_insert($node)

Implements hook_insert().

When a new chado_organism node is created we also need to add information to our chado_organism table. This function is called on insert of a new node of type 'chado_organism' and inserts the necessary information.

Related topics

File

legacy/tripal_organism/includes/tripal_organism.chado_node.inc, line 378
Implements the organims node content type

Code

function chado_organism_insert($node) {

  $chado_version = chado_get_version(TRUE);
  $organism_id = '';

  // if there is an organism_id in the $node object then this must be a sync so
  // we can skip adding the organism as it is already there, although
  // we do need to proceed with insertion into the chado/drupal linking table.
  if (!property_exists($node, 'organism_id')) {
    // remove any white space around values
    $node->genus = trim($node->genus);
    $node->species = trim($node->species);
    $node->abbreviation = trim($node->abbreviation);
    $node->common_name = trim($node->common_name);
    $node->description = trim($node->description['value']);
    if ($chado_version > 1.2) {
      $node->type_id = trim($node->type_id);
      $node->infraspecific_name = trim($node->infraspecific_name);
    }

    $values = array(
      'genus' => $node->genus,
      'species' => $node->species,
      'abbreviation' => $node->abbreviation,
      'common_name' => $node->common_name,
      'comment' => $node->description,
    );
    if ($chado_version > 1.2) {
      if ($node->type_id) {
        $values['type_id'] = $node->type_id;
      }
      if ($node->infraspecific_name) {
        $values['infraspecific_name'] = $node->infraspecific_name;
      }
    }

    $organism = chado_insert_record('organism', $values);
    if (!$organism) {
      drupal_set_message(t('Unable to add organism.', 'warning'));
      tripal_report_error('tripal_organism', TRIPAL_ERROR, 'Insert Organism: Unable to create organism where values:%values', 
      array('%values' => print_r($values, TRUE)));
      return;
    }
    $organism_id = $organism['organism_id'];

    if ($organism_id) {
      // * Properties Form *
      $details = array(
        'property_table' => 'organismprop', // the name of the prop table
        'base_table' => 'organism', // the name of your chado base table
        'foreignkey_name' => 'organism_id', // the name of the key in your base table
        'foreignkey_value' => $organism_id // the value of the example_id key
      );
      chado_update_node_form_properties($node, $details);

      // * Additional DBxrefs Form *
      $details = array(
        'linking_table' => 'organism_dbxref', // the name of your _dbxref table
        'foreignkey_name' => 'organism_id', // the name of the key in your base table
        'foreignkey_value' => $organism_id // the value of the organism_id key
      );
      chado_update_node_form_dbxrefs($node, $details);

    }
  }
  else {
    $organism_id = $node->organism_id;
  }

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

  // add the image
  if (property_exists($node, 'organism_image')) {
    chado_organism_add_image($node);
  }
}