remote__data.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @class
  4. * Purpose:
  5. *
  6. * Data:
  7. * Assumptions:
  8. */
  9. class remote__data extends WebServicesField {
  10. // --------------------------------------------------------------------------
  11. // EDITABLE STATIC CONSTANTS
  12. //
  13. // The following constants SHOULD be set for each descendant class. They are
  14. // used by the static functions to provide information to Drupal about
  15. // the field and it's default widget and formatter.
  16. // --------------------------------------------------------------------------
  17. // The default label for this field.
  18. public static $default_label = 'Remote Data';
  19. // The default description for this field.
  20. public static $default_description = 'remote data';
  21. // The default widget for this field.
  22. public static $default_widget = 'remote__data_widget';
  23. // The default formatter for this field.
  24. public static $default_formatter = 'remote__data_formatter';
  25. // The module that manages this field.
  26. public static $module = 'tripal_ws';
  27. // A list of global settings. These can be accessed within the
  28. // globalSettingsForm. When the globalSettingsForm is submitted then
  29. // Drupal will automatically change these settings for all fields.
  30. // Once instances exist for a field type then these settings cannot be
  31. // changed.
  32. public static $default_settings = array(
  33. 'storage' => 'field_tripal_ws_storage',
  34. // It is expected that all fields set a 'value' in the load() function.
  35. // In many cases, the value may be an associative array of key/value pairs.
  36. // In order for Tripal to provide context for all data, the keys should
  37. // be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
  38. // function that are supported by the query() function should be
  39. // listed here.
  40. 'searchable_keys' => array(),
  41. );
  42. // Provide a list of instance specific settings. These can be access within
  43. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  44. // then Drupal with automatically change these settings for the instance.
  45. // It is recommended to put settings at the instance level whenever possible.
  46. // If you override this variable in a child class be sure to replicate the
  47. // term_name, term_vocab, term_accession and term_fixed keys as these are
  48. // required for all TripalFields.
  49. public static $default_instance_settings = array(
  50. // The short name for the vocabulary (e.g. schema, SO, GO, PATO, etc.).
  51. 'term_vocabulary' => 'schema',
  52. // The name of the term.
  53. 'term_name' => 'Thing',
  54. // The unique ID (i.e. accession) of the term.
  55. 'term_accession' => 'property',
  56. // Set to TRUE if the site admin is not allowed to change the term
  57. // type, otherwise the admin can change the term mapped to a field.
  58. 'term_fixed' => FALSE,
  59. // Indicates if this field should be automatically attached to display
  60. // or web services or if this field should be loaded separately. This
  61. // is convenient for speed. Fields that are slow should for loading
  62. // should have auto_attach set to FALSE so tha their values can be
  63. // attached asynchronously.
  64. 'auto_attach' => FALSE,
  65. // Settings to allow the site admin to set the remote data source info.
  66. 'data_info' => array(
  67. 'query' => '',
  68. 'remote_site' => '',
  69. 'description' => '',
  70. ),
  71. );
  72. // A boolean specifying that users should not be allowed to create
  73. // fields and instances of this field type through the UI. Such
  74. // fields can only be created programmatically with field_create_field()
  75. // and field_create_instance().
  76. public static $no_ui = FALSE;
  77. // A boolean specifying that the field will not contain any data. This
  78. // should exclude the field from web services or downloads. An example
  79. // could be a quick search field that appears on the page that redirects
  80. // the user but otherwise provides no data.
  81. public static $no_data = FALSE;
  82. /**
  83. * @see WebServicesField::load()
  84. */
  85. public function load($entity) {
  86. $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  87. $query = $this->instance['settings']['data_info']['query'];
  88. $options = array();
  89. // Check for tripal tokens and replace if present.
  90. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  91. $query = tripal_replace_entity_tokens($query, $entity, $bundle_entity);
  92. // Get the site url from the tripal_sites table.
  93. $site_url_ws = db_select('tripal_sites', 's')
  94. ->fields('s',array('url'))
  95. ->condition('s.id', $site_id_ws, '=')
  96. ->execute()->fetchAll();
  97. $full_url = $site_url_ws[0]->url . '/web-services/content/v0.1/';
  98. //Remove trailing slash if one is included.$_COOKIE
  99. $full_url = trim($full_url, '/');
  100. $full_url = $full_url . '/' . $query;
  101. //Make the call and pull the data down.
  102. $data = drupal_http_request($full_url, $options);
  103. if($data){
  104. $data = drupal_json_decode($data->data);
  105. //Check the returned data is not an error.
  106. if(array_key_exists('error', $data)){
  107. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  108. $data = '';
  109. }
  110. //Check the returned data isn't empty.
  111. if(count($data['members']) >= 1){
  112. //If multiple records, if single follow @id and pull down data
  113. if(array_key_exists('value', $data)){
  114. $members = $data['value'][0]['members'];
  115. if($members){
  116. if(count($members) > 1){
  117. $field_name = $this->field['field_name'];
  118. $entity->{$field_name}['und'][0]['value'] = $data;
  119. }
  120. else {
  121. $single_record_url = $data['members'][0]['@id'];
  122. $data = drupal_http_request($single_record_url, $options);
  123. $data = drupal_json_decode($data->data);
  124. if(array_key_exists('error', $data)){
  125. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  126. $data = '';
  127. }
  128. }
  129. }
  130. }
  131. else {
  132. $single_record_url = $data['members'][0]['@id'];
  133. $data = drupal_http_request($single_record_url, $options);
  134. $data = drupal_json_decode($data->data);
  135. if(array_key_exists('error', $data)){
  136. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  137. $data = '';
  138. }
  139. }
  140. $field_name = $this->field['field_name'];
  141. $entity->{$field_name}['und'][0]['value'] = $data;
  142. }
  143. }
  144. }
  145. /**
  146. *
  147. * @see TripalField::settingsForm()
  148. */
  149. public function instanceSettingsForm() {
  150. $element = parent::instanceSettingsForm();
  151. // Get the setting for the option for how this widget.
  152. $instance = $this->instance;
  153. $settings = '';
  154. $site_list = '';
  155. $tokens = array();
  156. // Get the form info from the bundle about to be saved.
  157. $bundle_info = tripal_load_bundle_entity(array($instance['bundle']));
  158. // Retrieve all available tokens.
  159. $tokens = tripal_get_entity_tokens($bundle_info);
  160. $element['data_info'] = array(
  161. '#type' => 'fieldset',
  162. '#title' => 'Remote Data Settings',
  163. '#description' => 'Provide the site name, query and description for the remote data source.',
  164. '#collapsible' => TRUE,
  165. '#collapsed' => FALSE,
  166. '#prefix' => "<div id='set_titles-fieldset'>",
  167. '#suffix' => '</div>',
  168. );
  169. // Get the site info from the tripal_sites table.
  170. $sites = db_select('tripal_sites', 's')
  171. ->fields('s')
  172. ->execute()->fetchAll();
  173. foreach ($sites as $site) {
  174. $rows[$site->id] =$site->name;
  175. }
  176. $element['data_info']['remote_site'] = array(
  177. '#type' => 'select',
  178. '#title' => t('Site'),
  179. '#options' => $rows,
  180. '#default_value' => $this->instance['settings']['data_info']['remote_site'],
  181. );
  182. $element['data_info']['query'] = array(
  183. '#type' => 'textarea',
  184. '#title' => 'Query',
  185. '#description' => 'Build the query string that should be appended after the url. The tokens
  186. listed below may be used in your query build.',
  187. '#default_value' => $this->instance['settings']['data_info']['query'],
  188. '#rows' => 5
  189. );
  190. $element['data_info']['token_display']['tokens'] = array(
  191. '#type' => 'hidden',
  192. '#value' => serialize($tokens)
  193. );
  194. $element['data_info']['token_display'] = array(
  195. '#type' => 'fieldset',
  196. '#title' => 'Available Tokens',
  197. '#description' => 'Copy the token and paste it into the "Query" text field above.',
  198. '#collapsible' => TRUE,
  199. '#collapsed' => TRUE
  200. );
  201. $element['data_info']['token_display']['content'] = array(
  202. '#type' => 'item',
  203. '#markup' => theme_token_list($tokens),
  204. );
  205. $element['data_info']['description'] = array(
  206. '#type' => 'textarea',
  207. '#title' => 'Description',
  208. '#description' => 'Describe the data being pulled in.',
  209. '#default_value' => $this->instance['settings']['data_info']['description'],
  210. '#rows' => 1
  211. );
  212. /*$element['test_button']['data'] = array(
  213. '#prefix' => '<div class="returned-data">',
  214. '#suffix' => '</div>',
  215. '#type' => 'container',
  216. //…
  217. );
  218. $element['test_button'] = array(
  219. '#type' => 'button',
  220. '#value' => t('Test Query'),
  221. '#ajax' => array(
  222. 'wrapper' => 'data-id',
  223. 'callback' => 'tripal_ws_url_query_test',
  224. ),
  225. //…
  226. );*/
  227. return $element;
  228. }
  229. /**
  230. * Ajax callback.
  231. */
  232. function tripal_ws_url_query_test_ajax($form, $form_state) {
  233. load();
  234. return $element['test_button']['data'];
  235. }
  236. /**
  237. *
  238. * @param unknown $form
  239. * @param unknown $form_state
  240. */
  241. public function instanceSettingsFormValidate($form, &$form_state) {
  242. }
  243. }