function chado_contact_insert

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

Implementation of tripal_contact_insert().

This function inserts user entered information pertaining to the contact instance into the 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.

@parm $node Then node which contains the information stored within the node-ID

File

tripal_contact/tripal_contact.module, line 247
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_insert($node) {

  // if a contact_id already exists for this node then it already exists in Chado and 
  // we get here because we are syncing the node.  Therefore, we can skip the insert
  if ($node->contact_id) {
    $contact['contact_id'] = $node->contact_id;
  }
  else {
    // we don't want to store the description in the description field as it may be longer than
    // 255 characters, so we'll use a property to store this value.
    $values = array(
      'name' => $node->title,
      'description' => '',
      'type_id' => $node->type_id
    );
    $options = array('statement_name' => 'ins_contact_nadety');
    $contact = tripal_core_chado_insert('contact', $values, $options);
    if (!$contact) {
      drupal_set_message(t('Could not add the contact'), 'error');
      watchdog('tripal_contact', 'Could not add the contact', array(), WATCHDOG_ERROR);
      return FALSE;
    }

    // now add 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 two forms:
    //  1) prop_value-[type id]-[index]
    //  2) new_value-[type id]-[index]
    //  3) new_id, new_value
    foreach ($node as $name => $value) {
      if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $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;
      $index = count($properties[$name]);
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($node->new_value);
    }
    // now add in the properties
    foreach ($properties as $property => $elements) {
      foreach ($elements as $rank => $value) {

        $status = tripal_contact_insert_property($contact['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('%property' => $property), WATCHDOG_ERROR);
        }
      }
    }
  }

  // add the record to the chado_contact table in Drupal
  if ($contact) {

    // add the description property
    tripal_contact_insert_property($contact['contact_id'], 'contact_description', 
    $node->description, TRUE);

    // make sure the entry for this contact doesn't already exist in the chado_contact table
    // if it doesn't exist then we want to add it.
    $contact_id = chado_get_id_for_node('contact', $node);
    if (!$contact_id) {
      // next add the item to the drupal table
      $sql = "INSERT INTO {chado_contact} (nid, vid, contact_id) " .
        "VALUES (%d, %d, %d)";
      db_query($sql, $node->nid, $node->vid, $contact['contact_id']);
    }
  }
  else {
    drupal_set_message(t('Unable to add contact.', 'warning'));
    watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values', 
    array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  }
  return TRUE;
}