tripal_chado.contact.api.inc

Provides API functions specificially for managing contact records in Chado.

File

tripal_chado/api/modules/tripal_chado.contact.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides API functions specificially for managing contact records in Chado.
  5. *
  6. * @ingroup tripal_chado
  7. */
  8. /**
  9. * @defgroup tripal_contact_api Chado Contact
  10. * @ingroup tripal_chado_api
  11. * @{
  12. * Provides API functions for working with chado records in Chado that
  13. * go beyond the generic Chado API functions.
  14. * @}
  15. */
  16. /**
  17. * Adds a contact to the Chado contact table.
  18. *
  19. * @param $values
  20. * An array of values to be inserted. Valid keys include:
  21. * - name: The name of the contact.
  22. * - description: Text describing the contact.
  23. * - type_name: The type of contact. Must be a term in the tripal_contact
  24. * vocabulary.
  25. * - properties: An associative array containing a list of key value pairs for
  26. * the properites. The key's must be valid terms in the tripal_contact
  27. * vocabulary (e.g. Affiliation, Address, etc).
  28. *
  29. * @return
  30. * On success, an array is returned containing the fields of the contact
  31. * record including the newly added contact_id. On failure, FALSE is
  32. * returned.
  33. *
  34. * @ingroup tripal_contact_api
  35. */
  36. function chado_insert_contact($values) {
  37. $name = $values['name'];
  38. $description = $values['description'];
  39. $type = $values['type_name'];
  40. $properties = $values['properties'];
  41. // check to see if this contact name already exists.
  42. $values = array('name' => $name);
  43. $options = array('statement_name' => 'sel_contact_na');
  44. $contact = chado_select_record('contact', array('contact_id'), $values, $options);
  45. if (count($contact) == 0) {
  46. $cvterm = chado_get_cvterm(array(
  47. 'name' => $type,
  48. 'cv_id' => array('name' => 'tripal_contact')
  49. ));
  50. if (!$cvterm) {
  51. tripal_report_error('tripal_contact', TRIPAL_ERROR, "Cannot find contact type '%type'",
  52. array('%type' => $type));
  53. return FALSE;
  54. }
  55. $values = array(
  56. 'name' => $name,
  57. 'description' => '',
  58. 'type_id' => $cvterm->cvterm_id,
  59. );
  60. $options = array('statement_name' => 'ins_contact_nadety');
  61. $contact = chado_insert_record('contact', $values, $options);
  62. if (!$contact) {
  63. tripal_report_error('tripal_contact', TRIPAL_ERROR, 'Could not add the contact', array());
  64. return FALSE;
  65. }
  66. }
  67. else {
  68. $contact = (array) $contact[0];
  69. }
  70. // add the description property. We don't store this in the contact.description
  71. // field because it is only 255 characters long and may not be enough
  72. if ($description) {
  73. chado_insert_property(
  74. array(
  75. 'table' => 'contact',
  76. 'id' => $contact['contact_id'],
  77. ),
  78. array(
  79. 'type_name' => 'contact_description',
  80. 'cv_name' => 'tripal_contact',
  81. 'value' => $description,
  82. ),
  83. array(
  84. 'update_if_present' => TRUE,
  85. )
  86. );
  87. }
  88. // add in the other properties provided
  89. foreach ($properties as $key => $value) {
  90. $success = chado_insert_property(
  91. array('table' => 'contact', 'id' => $contact['contact_id']),
  92. array(
  93. 'type_name' => $key,
  94. 'cv_name' => 'tripal_contact',
  95. 'value' => $value,
  96. ),
  97. array('update_if_present' => TRUE)
  98. );
  99. if (!$success) {
  100. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  101. "Could not add the contact property '%prop'", array('%prop' => $key));
  102. return FALSE;
  103. }
  104. }
  105. return $contact;
  106. }
  107. /**
  108. * This function is intended to be used in autocomplete forms for contacts.
  109. *
  110. * @param $text
  111. * The string to search for.
  112. *
  113. * @return
  114. * A json array of terms that begin with the provided string.
  115. *
  116. * @ingroup tripal_contact_api
  117. */
  118. function chado_autocomplete_contact($text) {
  119. $matches = array();
  120. $sql = "SELECT * FROM {contact} WHERE lower(name) like lower(:name) ";
  121. $args = array();
  122. $args[':name'] = $text . '%';
  123. $sql .= "ORDER BY name ";
  124. $sql .= "LIMIT 25 OFFSET 0 ";
  125. $results = chado_query($sql, $args);
  126. $items = array();
  127. foreach ($results as $contact) {
  128. // Don't include the null contact
  129. if ($contact->name == 'null') {
  130. continue;
  131. }
  132. $items[$contact->name] = $contact->name;
  133. }
  134. drupal_json_output($items);
  135. }

Related topics