$name]; $options = ['statement_name' => 'sel_contact_na']; $contact = chado_select_record('contact', ['contact_id'], $values, $options); if (count($contact) == 0) { $cvterm = chado_get_cvterm([ 'name' => $type, 'cv_id' => ['name' => 'tripal_contact'], ]); if (!$cvterm) { tripal_report_error('tripal_contact', TRIPAL_ERROR, "Cannot find contact type '%type'", ['%type' => $type]); return FALSE; } $values = [ 'name' => $name, 'description' => '', 'type_id' => $cvterm->cvterm_id, ]; $options = ['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', []); 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( [ 'table' => 'contact', 'id' => $contact['contact_id'], ], [ 'type_name' => 'contact_description', 'cv_name' => 'tripal_contact', 'value' => $description, ], [ 'update_if_present' => TRUE, ] ); } // add in the other properties provided foreach ($properties as $key => $value) { $success = chado_insert_property( ['table' => 'contact', 'id' => $contact['contact_id']], [ 'type_name' => $key, 'cv_name' => 'tripal_contact', 'value' => $value, ], ['update_if_present' => TRUE] ); if (!$success) { tripal_report_error('tripal_contact', TRIPAL_ERROR, "Could not add the contact property '%prop'", ['%prop' => $key]); return FALSE; } } return $contact; } /** * This function is intended to be used in autocomplete forms for contacts. * * @param $text * The string to search for. * * @return * A json array of terms that begin with the provided string. * * @ingroup tripal_contact_api */ function chado_autocomplete_contact($text) { $matches = []; $sql = "SELECT * FROM {contact} WHERE lower(name) like lower(:name) "; $args = []; $args[':name'] = $text . '%'; $sql .= "ORDER BY name "; $sql .= "LIMIT 25 OFFSET 0 "; $results = chado_query($sql, $args); $items = []; foreach ($results as $contact) { // Don't include the null contact if ($contact->name == 'null') { continue; } $items[$contact->name] = $contact->name; } drupal_json_output($items); }