function chado_library_insert

2.x tripal_library.chado_node.inc chado_library_insert($node)
3.x tripal_library.chado_node.inc chado_library_insert($node)
1.x tripal_library.module chado_library_insert($node)

Implements hook_insert().

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

Related topics

File

tripal_library/includes/tripal_library.chado_node.inc, line 280
Implements the library node content type

Code

function chado_library_insert($node) {

  $library_id = '';

  // if there is an library_id in the $node object then this must be a sync so
  // we can skip adding the library as it is already there, although
  // we do need to proceed with insertion into the chado/drupal linking table.
  if (!property_exists($node, 'library_id')) {
    $node->libraryname = trim($node->libraryname);
    $node->uniquename = trim($node->uniquename);
    $node->description = trim($node->description['value']);

    $values = array(
      'name' => $node->libraryname,
      'uniquename' => $node->uniquename,
      'organism_id' => $node->organism_id,
      'type_id' => $node->library_type,
    );
    $library = chado_insert_record('library', $values);
    if (!$library) {
      drupal_set_message(t('Unable to add library.', 'warning'));
      watchdog('tripal_library', 'Insert library: Unable to create library where values: %values', 
      array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
      return;
    }
    $library_id = $library['library_id'];

    // * Properties Form *
    // add the description property
    $properties = chado_retrieve_node_form_properties($node);
    $descrip_id = tripal_get_cvterm(array(
      'name' => 'Library Description',
      'cv_id' => array('name' => 'library_property')
    ));
    $properties[$descrip_id->cvterm_id][0] = $node->description;

    $details = array(
      'property_table' => 'libraryprop', // the name of the prop table
      'base_table' => 'library', // the name of your chado base table
      'foreignkey_name' => 'library_id', // the name of the key in your base table
      'foreignkey_value' => $library_id // the value of the library_id key
    );
    chado_update_node_form_properties($node, $details, $properties);

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

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

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