tripal_ws.api.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file provides the Tripal Web Services API: a set of functions for
  6. * interacting with the Tripal Web Services.
  7. */
  8. /**
  9. * @defgroup tripal_ws_api Tripal Web Services
  10. *
  11. * @ingroup tripal_api
  12. * The Tripal Web Services API provides a set of functions for interacting
  13. * with the Tripal Web Services.
  14. *
  15. */
  16. /**
  17. * Adjust the values of a field for display in web services.
  18. *
  19. * This hook should be used sparingly. It is meant primarily to adjust 3rd
  20. * Party (non Tripal) fields so that they work with web
  21. * services. The caller should adjust the $items array as needed.
  22. * This change only affects the value displayed in web services. Web services
  23. * expect that every field have a 'value' element for each of the items. If a
  24. * field for some reason does not have a 'value' element then this hook will
  25. * allow setting of that element.
  26. *
  27. * @param $items
  28. * The list of items for the field.
  29. * @param $field
  30. * The field array.
  31. * @param $instance
  32. * The field instance array.
  33. *
  34. * @ingroup tripal_ws_api
  35. */
  36. function hook_tripal_ws_value(&$items, $field, $instance) {
  37. // The image module doesn't properly set the 'value' field, so we'll do it
  38. // here.
  39. if($field['type'] == 'image' and $field['module'] == 'image') {
  40. foreach ($items as $delta => $details) {
  41. if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
  42. $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * Retrieves a list of TripalWebService implementations.
  49. *
  50. * The TripalWebService classes can be added by a site developer that wishes
  51. * to create a new Tripal compatible web serivce. The class file should
  52. * be placed in the [module]/includes/TripalWebService directory. Tripal will
  53. * support any service as long as it is in this directory and extends the
  54. * TripalWebService class.
  55. *
  56. * @return
  57. * A list of TripalWebService names.
  58. */
  59. function tripal_get_web_services() {
  60. $services = array();
  61. $modules = module_list(TRUE);
  62. foreach ($modules as $module) {
  63. // Find all of the files in the tripal_chado/includes/fields directory.
  64. $service_path = drupal_get_path('module', $module) . '/includes/TripalWebService';
  65. $service_files = file_scan_directory($service_path, '/.inc$/');
  66. // Iterate through the fields, include the file and run the info function.
  67. foreach ($service_files as $file) {
  68. $class = $file->name;
  69. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  70. if (class_exists($class) and is_subclass_of($class, 'TripalWebService')) {
  71. $services[] = $class;
  72. }
  73. }
  74. }
  75. return $services;
  76. }
  77. /**
  78. * Loads the TripalWebService class file into scope.
  79. *
  80. * @param $class
  81. * The TripalWebService class to include.
  82. *
  83. * @return
  84. * TRUE if the field type class file was found, FALSE otherwise.
  85. */
  86. function tripal_load_include_web_service_class($class) {
  87. $modules = module_list(TRUE);
  88. foreach ($modules as $module) {
  89. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalWebService/' . $class . '.inc';
  90. if (file_exists($file_path)) {
  91. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  92. if (class_exists($class)) {
  93. return TRUE;
  94. }
  95. }
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * Adds a new site to the web services table.
  101. *
  102. * @param $name
  103. * Name of site to be included.
  104. * @param $url
  105. * URL of site to be added.
  106. * @param $version
  107. * Version of the API being used. default to 1
  108. * @param $description
  109. * A description of the site and any additional info that
  110. * would be helpful for admins.
  111. *
  112. * @return
  113. * TRUE if the site is successfully added, FALSE otherwise.
  114. */
  115. function tripal_add_site($name, $url, $version, $description) {
  116. $check_url = NULL;
  117. $check_name = NULL;
  118. $write_to_db = TRUE;
  119. // When inserting a record.
  120. $check_url =
  121. db_select('tripal_sites', 'ts')
  122. ->fields('ts', array('id'))
  123. ->condition('url', $url)
  124. ->condition('version', $version)
  125. ->execute()
  126. ->fetchField();
  127. $check_name =
  128. db_select('tripal_sites', 'ts')
  129. ->fields('ts', array('id'))
  130. ->condition('name', $name)
  131. ->execute()
  132. ->fetchField();
  133. if ($check_url) {
  134. drupal_set_message(t('The URL and version is used by another site.'), 'error');
  135. $write_to_db = FALSE;
  136. }
  137. if ($check_name) {
  138. drupal_set_message(t('The name is used by another site.'), 'error');
  139. $write_to_db = FALSE;
  140. }
  141. if ($write_to_db === TRUE) {
  142. db_insert('tripal_sites')
  143. ->fields(array(
  144. 'name' => $name,
  145. 'url' => $url,
  146. 'version' => $version,
  147. 'description' => $description
  148. ))
  149. ->execute();
  150. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  151. return $write_to_db;
  152. }
  153. return $write_to_db;
  154. }
  155. /**
  156. * Remove a site from the web services table.
  157. *
  158. * @param $record_id
  159. * ID of the record to be deleted.
  160. *
  161. * @return
  162. * TRUE if the record was successfully deleted, FALSE otherwise.
  163. */
  164. function tripal_remove_site($record_id) {
  165. if ($record_id) {
  166. db_delete('tripal_sites')
  167. ->condition('id', $record_id)
  168. ->execute();
  169. drupal_set_message('The Tripal site \'' . $record_id . '\' has been removed.');
  170. return TRUE;
  171. }
  172. return FALSE;
  173. }