function chado_phylotree_insert

2.x tripal_phylogeny.chado_node.inc chado_phylotree_insert($node)
3.x tripal_phylogeny.chado_node.inc chado_phylotree_insert($node)

Implements hook_insert().

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

Related topics

File

tripal_phylogeny/includes/tripal_phylogeny.chado_node.inc, line 512
Implements the phylotree node content type

Code

function chado_phylotree_insert($node) {
  global $user;

  $node->tree_name = trim($node->tree_name);
  $node->description = trim($node->description);
  $node->dbxref = trim($node->dbxref);

  // if there is a phylotree_id in the $node object then this must
  // be a sync (not an insert) so we can skip adding the phylotree as it is
  // already there, although we do need to proceed with the rest of the
  // insert.
  $phylotree_id = NULL;
  if (!property_exists($node, 'phylotree_id')) {
    $options = array(
      'name' => $node->tree_name,
      'description' => $node->description,
      'analysis_id' => $node->analysis_id,
      'leaf_type' => $node->leaf_type,
      'tree_file' => $node->tree_file,
      'format' => 'newick',
      'dbxref' => $node->dbxref,
      'match' => $node->match,
      'name_re' => $node->name_re,
    );
    $errors = array();
    $warnings = array();
    if (tripal_insert_phylotree($options, $errors, $warnings)) {
      $phylotree_id = $options['phylotree_id'];

      // Add the Tripal variables to this node.
      tripal_add_node_variable($node->nid, 'phylotree_name_re', $node->name_re);
      tripal_add_node_variable($node->nid, 'phylotree_use_uniquename', $node->match);
      tripal_add_node_variable($node->nid, 'phylotree_tree_file', $node->tree_file);
    }
    else {
      drupal_set_message(t('Unable to insert phylotree.'), 'error');
      tripal_report_error('tripal_phylogeny', TRIPAL_WARNING, 
      'Insert phylotree: Unable to insert phylotree where values: %values', 
      array('%values' => print_r($options, TRUE))
      );
    }
  }
  else {
    $phylotree_id = $node->phylotree_id;
  }

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