remote__data.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. 'rd_field_name' => '',
  71. ),
  72. );
  73. // A boolean specifying that users should not be allowed to create
  74. // fields and instances of this field type through the UI. Such
  75. // fields can only be created programmatically with field_create_field()
  76. // and field_create_instance().
  77. public static $no_ui = FALSE;
  78. // A boolean specifying that the field will not contain any data. This
  79. // should exclude the field from web services or downloads. An example
  80. // could be a quick search field that appears on the page that redirects
  81. // the user but otherwise provides no data.
  82. public static $no_data = FALSE;
  83. /**
  84. * @see WebServicesField::load()
  85. */
  86. public function load($entity) {
  87. $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  88. $query = $this->instance['settings']['data_info']['query'];
  89. $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
  90. $options = array();
  91. $field_name = $this->field['field_name'];
  92. // Check for tripal tokens and replace if present.
  93. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  94. $query = tripal_replace_entity_tokens($query, $entity, $bundle_entity);
  95. // Get the site url from the tripal_sites table.
  96. $site_url_ws = db_select('tripal_sites', 's')
  97. ->fields('s',array('url'))
  98. ->condition('s.id', $site_id_ws, '=')
  99. ->execute()->fetchAll();
  100. $full_url = $site_url_ws[0]->url . '/web-services/content/v0.1/';
  101. //Remove trailing slash if one is included.$_COOKIE
  102. $full_url = trim($full_url, '/');
  103. $full_url = $full_url . '/' . $query;
  104. //Make the call and pull the data down.
  105. $data = drupal_http_request($full_url, $options);
  106. //Check the returned data is not an error.
  107. if(property_exists($data, 'error')){
  108. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data->error, TRUE) .'</pre>');
  109. $data = '';
  110. }
  111. if($data){
  112. $data = drupal_json_decode($data->data);
  113. //Check the returned data is not an error.
  114. if(array_key_exists('error', $data)){
  115. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  116. $data = '';
  117. }
  118. // Check if the returned results are paged, if so iterate through all pages.
  119. //$paged_data = array();
  120. if(array_key_exists('next', $data['view'])){
  121. //First page of results.
  122. foreach($data['members'] as $member){
  123. $value = tripal_ws_remote_data_single_field_pull($rd_field_name, $member['@id']);
  124. $entity->{$field_name}['und'][$member['label']]['value'] = $value;
  125. $entity->{$field_name}['und'][$member['label']]['remote_entity'] = $member;
  126. //array_push($paged_data, $member);
  127. }
  128. //'next' pages.
  129. $next_page = $data['view']['next'];
  130. while(!empty($next_page)){
  131. $data = drupal_http_request($next_page, $options);
  132. if(!empty($data)){
  133. $data = drupal_json_decode($data->data);
  134. foreach($data['members'] as $member){
  135. $value = tripal_ws_remote_data_single_field_pull($rd_field_name, $member['@id']);
  136. $entity->{$field_name}['und'][$member['label']]['value'] = $value;
  137. $entity->{$field_name}['und'][$member['label']]['remote_entity'] = $member;
  138. //array_push($paged_data, $member);
  139. }
  140. if(array_key_exists('next', $data['view'])){
  141. $next_page = $data['view']['next'];
  142. }
  143. else {
  144. $next_page = NULL;
  145. }
  146. }
  147. }
  148. //'last' page.
  149. $last_page = $data['view']['last'];
  150. $data = drupal_http_request($last_page, $options);
  151. if(!empty($data)){
  152. $data = drupal_json_decode($data->data);
  153. foreach($data['members'] as $member){
  154. $value = tripal_ws_remote_data_single_field_pull($rd_field_name, $member['@id']);
  155. $entity->{$field_name}['und'][$member['label']]['value'] = $value;
  156. $entity->{$field_name}['und'][$member['label']]['remote_entity'] = $member;
  157. //array_push($paged_data, $member);
  158. }
  159. }
  160. }
  161. else {
  162. //Check the returned data isn't empty.
  163. if(!empty($data['members'])){
  164. //If multiple records, if single follow @id and pull down data
  165. /*if(array_key_exists('value', $data)){
  166. $members = $data['value'][0]['members'];
  167. if($members){
  168. if(count($members) > 1){
  169. $data = $data;
  170. }
  171. else {
  172. $single_record_url = $data['members'][0]['@id'];
  173. $data = drupal_http_request($single_record_url, $options);
  174. if(!empty($data)){
  175. $data = drupal_json_decode($data->data);
  176. if(array_key_exists('error', $data)){
  177. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  178. $data = '';
  179. }
  180. }
  181. }
  182. }
  183. }
  184. else */
  185. if(count($data['members']) > 1) {
  186. foreach($data['members'] as $member){
  187. $value = tripal_ws_remote_data_single_field_pull($rd_field_name, $member['@id']);
  188. $entity->{$field_name}['und'][$member['label']]['value'] = $value;
  189. $entity->{$field_name}['und'][$member['label']]['remote_entity'] = $member;
  190. //array_push($paged_data, $member);
  191. }
  192. }
  193. else {
  194. $member = $data['members'][0];
  195. $single_record_url = $member['@id'];
  196. $value = tripal_ws_remote_data_single_field_pull($rd_field_name, $single_record_url);
  197. if(!empty($data)){
  198. $entity->{$field_name}['und'][$member['label']]['value'] = $value;
  199. $entity->{$field_name}['und'][$member['label']]['remote_entity'] = $member;
  200. if(array_key_exists('error', $data)){
  201. watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
  202. $data = '';
  203. }
  204. }
  205. }
  206. }
  207. }
  208. $field_name = $this->field['field_name'];
  209. $entity->{$field_name}['und'][0]['value'] = $data;
  210. }
  211. }
  212. /**
  213. *
  214. * @see TripalField::settingsForm()
  215. */
  216. public function instanceSettingsForm() {
  217. $element = parent::instanceSettingsForm();
  218. // Get the setting for the option for how this widget.
  219. $instance = $this->instance;
  220. $settings = '';
  221. $site_list = '';
  222. $tokens = array();
  223. // Get the form info from the bundle about to be saved.
  224. $bundle_info = tripal_load_bundle_entity(array($instance['bundle']));
  225. // Retrieve all available tokens.
  226. $tokens = tripal_get_entity_tokens($bundle_info);
  227. $element['data_info'] = array(
  228. '#type' => 'fieldset',
  229. '#title' => 'Remote Data Settings',
  230. '#description' => 'Provide the site name, query and description for the remote data source.',
  231. '#collapsible' => TRUE,
  232. '#collapsed' => FALSE,
  233. '#prefix' => "<div id='set_titles-fieldset'>",
  234. '#suffix' => '</div>',
  235. );
  236. // Get the site info from the tripal_sites table.
  237. $sites = db_select('tripal_sites', 's')
  238. ->fields('s')
  239. ->execute()->fetchAll();
  240. foreach ($sites as $site) {
  241. $rows[$site->id] =$site->name;
  242. }
  243. $element['data_info']['remote_site'] = array(
  244. '#type' => 'select',
  245. '#title' => t('Site'),
  246. '#options' => $rows,
  247. '#default_value' => $this->instance['settings']['data_info']['remote_site'],
  248. );
  249. $element['data_info']['query'] = array(
  250. '#type' => 'textarea',
  251. '#title' => 'Query',
  252. '#description' => 'Build the query string that should be appended after the url. The tokens
  253. listed below may be used in your query build.',
  254. '#default_value' => $this->instance['settings']['data_info']['query'],
  255. '#rows' => 5,
  256. '#required' => TRUE
  257. );
  258. $element['data_info']['rd_field_name'] = array(
  259. '#type' => 'textfield',
  260. '#title' => 'Field',
  261. '#description' => 'Name of the field you would like to display.',
  262. '#default_value' => $this->instance['settings']['data_info']['rd_field_name'],
  263. '#required' => TRUE
  264. );
  265. $element['data_info']['token_display']['tokens'] = array(
  266. '#type' => 'hidden',
  267. '#value' => serialize($tokens)
  268. );
  269. $element['data_info']['token_display'] = array(
  270. '#type' => 'fieldset',
  271. '#title' => 'Available Tokens',
  272. '#description' => 'Copy the token and paste it into the "Query" text field above.',
  273. '#collapsible' => TRUE,
  274. '#collapsed' => TRUE
  275. );
  276. $element['data_info']['token_display']['content'] = array(
  277. '#type' => 'item',
  278. '#markup' => theme_token_list($tokens),
  279. );
  280. $element['data_info']['description'] = array(
  281. '#type' => 'textarea',
  282. '#title' => 'Description',
  283. '#description' => 'Describe the data being pulled in.',
  284. '#default_value' => $this->instance['settings']['data_info']['description'],
  285. '#rows' => 1
  286. );
  287. /*$element['test_button']['data'] = array(
  288. '#prefix' => '<div class="returned-data">',
  289. '#suffix' => '</div>',
  290. '#type' => 'container',
  291. //…
  292. );
  293. $element['test_button'] = array(
  294. '#type' => 'button',
  295. '#value' => t('Test Query'),
  296. '#ajax' => array(
  297. 'wrapper' => 'data-id',
  298. 'callback' => 'tripal_ws_url_query_test',
  299. ),
  300. //…
  301. );*/
  302. return $element;
  303. }
  304. /**
  305. *
  306. * @param unknown $form
  307. * @param unknown $form_state
  308. */
  309. public function instanceSettingsFormValidate($form, &$form_state) {
  310. }
  311. }