tripal_chado.contact.api.inc 3.8 KB

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