tripal_contact.api.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 'contact',
  73. $contact['contact_id'],
  74. 'contact_description',
  75. 'tripal_contact',
  76. $description,
  77. TRUE
  78. );
  79. }
  80. // add in the other properties provided
  81. foreach ($properties as $key => $value) {
  82. $success = chado_insert_property(
  83. 'contact',
  84. $contact['contact_id'],
  85. $key,
  86. 'tripal_contact',
  87. $value,
  88. TRUE
  89. );
  90. if (!$success) {
  91. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  92. "Could not add the contact property '%prop'", array('%prop' => $key));
  93. return FALSE;
  94. }
  95. }
  96. return $contact;
  97. }