function chado_contact_update

2.x tripal_contact.chado_node.inc chado_contact_update($node)
3.x tripal_contact.chado_node.inc chado_contact_update($node)
1.x tripal_contact.module chado_contact_update($node)

File

tripal_contact/tripal_contact.module, line 361
This file contains the basic functions needed for this drupal module. The drupal tripal_contact module maps directly to the chado X module.

Code

function chado_contact_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.
  }

  $contact_id = chado_get_id_for_node('contact', $node);

  // check to see if this contact name doens't already exists.    
  $sql = "SELECT contact_id FROM {contact} WHERE NOT contact_id = %d AND name = '%s'";
  $contact = db_fetch_object(chado_query($sql, $contact_id, $node->contact_name));
  if ($contact) {
    drupal_set_message(t('A contact with this name already exists. Cannot perform update.'), 'warning');
    return;
  }

  // update the contact record
  $match = array(
    'contact_id' => $contact_id,
  );
  $values = array(
    'name' => $node->title,
    'description' => '',
    'type_id' => $node->type_id
  );
  $status = tripal_core_chado_update('contact', $match, $values);
  if (!$status) {
    drupal_set_message("Error updating contact", "error");
    watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
    return;
  }

  // now update the properties
  $properties = array(); // stores all of the properties we need to add

  // get the list of properties for easy lookup (without doing lots of database queries
  $properties_list = array();
  $sql = "
    SELECT CVTS.cvterm_id, CVTS.name
    FROM {cvtermpath} CVTP
      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
      INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
      INNER JOIN {cv} CV       ON CVTO.cv_id = CV.cv_id
    WHERE 
      CV.name = 'tripal_contact' AND 
      NOT CVTO.name = 'Contact Type' 
    ORDER BY CVTS.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);
  }

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

  tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
}