tripal_contact.api.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 contact_insert($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_cv_get_cvterm_by_name($type, NULL, 'tripal_contact');
  45. if (!$cvterm) {
  46. tripal_report_error('tripal_contact', TRIPAL_ERROR, "Cannot find contact type '%type'",
  47. array('%type' => $type));
  48. return FALSE;
  49. }
  50. $values = array(
  51. 'name' => $name,
  52. 'description' => '',
  53. 'type_id' => $cvterm->cvterm_id,
  54. );
  55. $options = array('statement_name' => 'ins_contact_nadety');
  56. $contact = chado_insert_record('contact', $values, $options);
  57. if (!$contact) {
  58. tripal_report_error('tripal_contact', TRIPAL_ERROR, 'Could not add the contact', array());
  59. return FALSE;
  60. }
  61. }
  62. else {
  63. $contact = (array) $contact[0];
  64. }
  65. // add the description property. We don't store this in the contact.description
  66. // field because it is only 255 characters long and may not be enough
  67. if ($description) {
  68. tripal_contact_insert_property($contact['contact_id'], 'contact_description', $description, TRUE);
  69. }
  70. // add in the other properties provided
  71. foreach ($properties as $key => $value) {
  72. $success = tripal_contact_insert_property($contact['contact_id'], $key,$value, TRUE);
  73. if (!$success) {
  74. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  75. "Could not add the contact property '%prop'", array('%prop' => $key));
  76. return FALSE;
  77. }
  78. }
  79. return $contact;
  80. }