function chado_featuremap_update

2.x tripal_featuremap.chado_node.inc chado_featuremap_update($node)
3.x tripal_featuremap.chado_node.inc chado_featuremap_update($node)
1.x tripal_featuremap.module chado_featuremap_update($node)

Update nodes

Related topics

File

tripal_featuremap/tripal_featuremap.module, line 458

Code

function chado_featuremap_update($node) {
  if ($node->revision) {
    // there is no way to handle revisions in Chado but leave
    // this here just to make not we've addressed it.
  }
  $featuremap_id = chado_get_id_for_node('featuremap', $node);

  // update the map record
  $match = array(
    'featuremap_id' => $featuremap_id,
  );
  $values = array(
    'name' => $node->title,
    'description' => $node->description,
    'unittype_id' => $node->unittype_id
  );
  $status = tripal_core_chado_update('featuremap', $match, $values);
  if (!$status) {
    drupal_set_message("Error updating map", "error");
    watchdog('t_featuremap', "Error updating map", array(), WATCHDOG_ERROR);
    return;
  }

  // now update the properties
  $properties = array(); // stores all of the properties we need to add
  $cross_refs = array(); // stores any cross references for this map

  // get the list of properties for easy lookup (without doing lots of database queries
  $properties_list = array();
  $sql = "
    SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
    FROM  {cvterm} CVT
      INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
    WHERE 
      CV.name = 'featuremap_property' AND 
      NOT CVT.is_obsolete = 1
    ORDER BY CVT.name ASC 
  ";
  $prop_types = chado_query($sql);
  while ($prop = db_fetch_object($prop_types)) {
    $properties_list[$prop->cvterm_id] = $prop->name;
  }

  // get the properties that should be added. Properties are in one of three forms:
  //  1) prop_value-[type id]-[index]
  //  2) new_value-[type id]-[index]
  //  3) new_id, new_value
  //  dpm($node);
  foreach ($node as $key => $value) {
    if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
      $type_id = $matches[1];
      $index = $matches[2];
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($value);
    }
    if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
      $type_id = $matches[1];
      $index = $matches[2];
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($value);
    }
  }
  if ($node->new_id and $node->new_value) {
    $type_id = $node->new_id;
    $name = $properties_list[$type_id];
    $index = count($properties[$name]);
    $properties[$name][$index] = trim($node->new_value);
  }

  // iterate through all of the properties to see if the Map dbxref is set, 
  // if so, add it to the $cross_refs array
  foreach ($properties as $name => $element) {
    foreach ($element as $index => $value) {
      if ($name == "Map Dbxref") {
        // we will add the cross-references to the featuremap_dbxref table
        // but we also want to keep the property in the featuremapprop table so don't unset it
        $cross_refs[] = $value;
      }
    }
  }

  // now add in the properties by first removing any the featuremap
  // already has and adding the ones we have
  tripal_core_chado_delete('featuremapprop', array('featuremap_id' => $featuremap_id));
  foreach ($properties as $property => $elements) {
    foreach ($elements as $rank => $value) {
      $status = tripal_featuremap_insert_property($featuremap_id, $property, $value, FALSE);
      if (!$status) {
        drupal_set_message("Error cannot add property: '$property'", "error");
        watchdog('t_featuremap', "Error cannot add property: '%prop'", 
        array('%prop' => $property), WATCHDOG_ERROR);
      }
    }
  }

  // add in any database cross-references after first removing
  tripal_core_chado_delete('featuremap_dbxref', array('featuremap_id' => $featuremap_id));
  foreach ($cross_refs as $index => $ref) {
    $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, trim($ref));
    if (!$featuremap_dbxref) {
      drupal_set_message("Error cannot add map cross reference: $ref", "error");
      watchdog('t_featuremap', "Error cannot add map cross reference: %ref", 
      array('%ref' => $ref), WATCHDOG_ERROR);
    }
  }
}