tripal_contact.api.inc

  1. 2.x tripal_contact/api/tripal_contact.api.inc
  2. 1.x tripal_contact/api/tripal_contact.api.inc

Functions to interact with contacts.

File

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

Related topics