function chado_insert_contact

3.x tripal_chado.contact.api.inc chado_insert_contact($values)

Adds a contact to the Chado contact table.

Parameters

$values: An array of values to be inserted. Valid keys include:

  • name: The name of the contact.
  • description: Text describing the contact.
  • type_name: The type of contact. Must be a term in the tripal_contact vocabulary.
  • properties: An associative array containing a list of key value pairs for the properites. The key's must be valid terms in the tripal_contact vocabulary (e.g. Affiliation, Address, etc).

Return value

On success, an array is returned containing the fields of the contact record including the newly added contact_id. On failure, FALSE is returned.

Related topics

2 calls to chado_insert_contact()
tripal_insert_contact in tripal_chado/api/modules/tripal_chado.module.DEPRECATED.api.inc
Adds a contact to the Chado contact table.
tripal_pub_add_authors in tripal_chado/includes/loaders/tripal_chado.pub_importers.inc
Add one or more authors to a publication

File

tripal_chado/api/modules/tripal_chado.contact.api.inc, line 38
Provides API functions specificially for managing contact records in Chado.

Code

function chado_insert_contact($values) {

  $name = $values['name'];
  $description = $values['description'];
  $type = $values['type_name'];
  $properties = $values['properties'];

  // check to see if this contact name already exists.
  $values = array('name' => $name);
  $options = array('statement_name' => 'sel_contact_na');
  $contact = chado_select_record('contact', array('contact_id'), $values, $options);

  if (count($contact) == 0) {
    $cvterm = chado_get_cvterm(array(
      'name' => $type,
      'cv_id' => array('name' => 'tripal_contact')
    ));
    if (!$cvterm) {
      tripal_report_error('tripal_contact', TRIPAL_ERROR, "Cannot find contact type '%type'", 
      array('%type' => $type));
      return FALSE;
    }
    $values = array(
      'name' => $name,
      'description' => '',
      'type_id' => $cvterm->cvterm_id,
    );
    $options = array('statement_name' => 'ins_contact_nadety');
    $contact = chado_insert_record('contact', $values, $options);
    if (!$contact) {
      tripal_report_error('tripal_contact', TRIPAL_ERROR, 'Could not add the contact', array());
      return FALSE;
    }
  }
  else {
    $contact = (array) $contact[0];
  }

  // add the description property. We don't store this in the contact.description
  // field because it is only 255 characters long and may not be enough
  if ($description) {
    chado_insert_property(
    array(
      'table' => 'contact',
      'id' => $contact['contact_id'],
    ), 
    array(
      'type_name' => 'contact_description',
      'cv_name' => 'tripal_contact',
      'value' => $description,
    ), 
    array(
      'update_if_present' => TRUE,
    )
    );
  }

  // add in the other properties provided
  foreach ($properties as $key => $value) {
    $success = chado_insert_property(
    array('table' => 'contact', 'id' => $contact['contact_id']), 
    array(
      'type_name' => $key,
      'cv_name' => 'tripal_contact',
      'value' => $value,
    ), 
    array('update_if_present' => TRUE)
    );

    if (!$success) {
      tripal_report_error('tripal_contact', TRIPAL_ERROR, 
      "Could not add the contact property '%prop'", array('%prop' => $key));
      return FALSE;
    }
  }
  return $contact;
}