function chado_analysis_insert

2.x tripal_analysis.chado_node.inc chado_analysis_insert($node)
3.x tripal_analysis.chado_node.inc chado_analysis_insert($node)
1.x tripal_analysis.module chado_analysis_insert($node)

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

Related topics

File

tripal_analysis/includes/tripal_analysis.chado_node.inc, line 407
Implements Drupal Node hooks to create the chado_analysis node content type.

Code

function chado_analysis_insert($node) {

  $node->analysisname = trim($node->analysisname);
  $node->program = trim($node->program);
  $node->programversion = trim($node->programversion);
  $node->algorithm = trim($node->algorithm);
  $node->sourcename = trim($node->sourcename);
  $node->sourceversion = trim($node->sourceversion);
  $node->sourceuri = trim($node->sourceuri);
  $node->description = trim($node->description['value']);

  // if there is an analysis_id in the $node object then this must be a sync so
  // we can skip adding the analysis as it is already there, although
  // we do need to proceed with the rest of the insert
  if (!property_exists($node, 'analysis_id')) {

    // Create a timestamp so we can insert it into the chado database
    $time = $node->timeexecuted;
    $month = $time['month'];
    $day = $time['day'];
    $year = $time['year'];
    $timestamp = $month . '/' . $day . '/' . $year;

    // Insert and then get the newly inserted analysis record
    $values = array(
      'name' => $node->analysisname,
      'description' => $node->description,
      'program' => $node->program,
      'programversion' => $node->programversion,
      'algorithm' => $node->algorithm,
      'sourcename' => $node->sourcename,
      'sourceversion' => $node->sourceversion,
      'sourceuri' => $node->sourceuri,
      'timeexecuted' => $timestamp
    );
    $analysis = chado_insert_record('analysis', $values);
    if (!$analysis) {
      drupal_set_message(t('Unable to add analysis.'), 'warning');
      tripal_report_error('tripal_analysis', TRIPAL_ERROR, 'Insert analysis: Unable to create analysis where values:%values', 
      array('%values' => print_r($values, TRUE)));
      return;
    }
    $analysis_id = $analysis['analysis_id'];

    // now add in the properties
    $details = array(
      'property_table' => 'analysisprop',
      'base_table' => 'analysis',
      'foreignkey_name' => 'analysis_id',
      'foreignkey_value' => $analysis_id
    );
    chado_update_node_form_properties($node, $details);
  }
  else {
    $analysis_id = $node->analysis_id;
  }

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

  // add the analysis to the node object for
  // use by other analysis modules that may be using this function
  $node->analysis = $analysis;
  $node->analysis_id = $analysis_id; // we need to set this for children
}