tripal_contact.api.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Retrieve properties of a given type for a given contact
  17. *
  18. * @param $contact_id
  19. * The contact_id of the properties you would like to retrieve
  20. * @param $property
  21. * The cvterm name of the properties to retrieve
  22. *
  23. * @return
  24. * An contact chado variable with the specified properties expanded
  25. *
  26. * @ingroup tripal_contact_api
  27. */
  28. function tripal_contact_get_property($contact_id, $property) {
  29. return tripal_core_get_property('contact', $contact_id, $property, 'tripal_contact');
  30. }
  31. /**
  32. * Insert a given property
  33. *
  34. * @param $contact_id
  35. * The contact_id of the property to insert
  36. * @param $property
  37. * The cvterm name of the property to insert
  38. * @param $value
  39. * The value of the property to insert
  40. * @param $update_if_present
  41. * A boolean indicated whether to update the record if it's already present
  42. *
  43. * @return
  44. * True of success, False otherwise
  45. *
  46. * @ingroup tripal_contact_api
  47. */
  48. function tripal_contact_insert_property($contact_id, $property, $value, $update_if_present = 0) {
  49. return tripal_core_insert_property('contact', $contact_id, $property, 'tripal_contact', $value, $update_if_present);
  50. }
  51. /**
  52. * Update a given property
  53. *
  54. * @param $contact_id
  55. * The contact_id of the property to update
  56. * @param $property
  57. * The cvterm name of the property to update
  58. * @param $value
  59. * The value of the property to update
  60. * @param $insert_if_missing
  61. * A boolean indicated whether to insert the record if it's absent
  62. *
  63. * Note: The property will be identified using the unique combination of the $contact_id and $property
  64. * and then it will be updated with the supplied value
  65. *
  66. * @return
  67. * True of success, False otherwise
  68. *
  69. * @ingroup tripal_contact_api
  70. */
  71. function tripal_contact_update_property($contact_id, $property, $value, $insert_if_missing = 0) {
  72. return tripal_core_update_property('contact', $contact_id, $property, 'tripal_contact', $value, $insert_if_missing);
  73. }
  74. /**
  75. * Delete a given property
  76. *
  77. * @param $contact_id
  78. * The contact_id of the property to delete
  79. * @param $property
  80. * The cvterm name of the property to delete
  81. *
  82. * Note: The property will be identified using the unique combination of the $contact_id and $property
  83. * and then it will be deleted
  84. *
  85. * @return
  86. * True of success, False otherwise
  87. *
  88. * @ingroup tripal_contact_api
  89. */
  90. function tripal_contact_delete_property($contact_id, $property) {
  91. return tripal_core_delete_property('contact', $contact_id, $property, 'tripal_contact');
  92. }
  93. /**
  94. * Adds a contact to the Chado contact table
  95. *
  96. * @param $name
  97. * The name of the contact
  98. * @param $description
  99. * Text describing the contact
  100. * @param $type
  101. * The type of contact. Must be a term in the tripal_contact vocabulary
  102. * @param $properties
  103. * An associative array containing a list of key value pairs for the properites.
  104. * The key's must be valid terms in the tripal_contact vocabulary (e.g. Affiliation,
  105. * Address, etc).
  106. *
  107. * @return
  108. * On success, an array is returned containing the fields of the contact
  109. * record including the newly added contact_id. On failure, FALSE is
  110. * returned
  111. *
  112. * @ingroup tripal_contact_api
  113. */
  114. function tripal_contact_add_contact($name, $description, $type, $properties) {
  115. // check to see if this contact name already exists.
  116. $values = array('name' => $name);
  117. $options = array('statement_name' => 'sel_contact_na');
  118. $contact = tripal_core_chado_select('contact', array('contact_id'), $values, $options);
  119. if (count($contact) == 0) {
  120. $cvterm = tripal_cv_get_cvterm_by_name($type, NULL, 'tripal_contact');
  121. if (!$cvterm) {
  122. tripal_core_report_error('tripal_contact', TRIPAL_ERROR, "Cannot find contact type '%type'",
  123. array('%type' => $type));
  124. return FALSE;
  125. }
  126. $values = array(
  127. 'name' => $name,
  128. 'description' => '',
  129. 'type_id' => $cvterm->cvterm_id,
  130. );
  131. $options = array('statement_name' => 'ins_contact_nadety');
  132. $contact = tripal_core_chado_insert('contact', $values, $options);
  133. if (!$contact) {
  134. tripal_core_report_error('tripal_contact', TRIPAL_ERROR, 'Could not add the contact', array());
  135. return FALSE;
  136. }
  137. }
  138. else {
  139. $contact = (array) $contact[0];
  140. }
  141. // add the description property. We don't store this in the contact.description
  142. // field because it is only 255 characters long and may not be enough
  143. if ($description) {
  144. tripal_contact_insert_property($contact['contact_id'], 'contact_description', $description, TRUE);
  145. }
  146. // add in the other properties provided
  147. foreach ($properties as $key => $value) {
  148. $success = tripal_contact_insert_property($contact['contact_id'], $key,$value, TRUE);
  149. if (!$success) {
  150. tripal_core_report_error('tripal_contact', TRIPAL_ERROR,
  151. "Could not add the contact property '%prop'", array('%prop' => $key));
  152. return FALSE;
  153. }
  154. }
  155. return $contact;
  156. }