remote__data.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Tripal Site';
  19. // The default description for this field.
  20. public static $default_description = 'Allows for inclusion of remote data from another Tripal site.';
  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. // Holds an object describing the remote site that tihs field connects to.
  84. private $remote_site = NULL;
  85. public function __construct($field, $instance) {
  86. parent::__construct($field, $instance);
  87. //Get the site url from the tripal_sites table.
  88. $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  89. $this->remote_site = db_select('tripal_sites', 'ts')
  90. ->fields('ts')
  91. ->condition('ts.id', $site_id_ws)
  92. ->execute()
  93. ->fetchObject();
  94. }
  95. /**
  96. * @see WebServicesField::load()
  97. */
  98. public function load($entity) {
  99. $field_name = $this->field['field_name'];
  100. $field_type = $this->field['type'];
  101. // Set some defaults for the empty record.
  102. $entity->{$field_name}['und'][0] = array(
  103. 'value' => array(),
  104. );
  105. // Get the query set by the admin for this field and replace any tokesn
  106. $query_str = $this->instance['settings']['data_info']['query'];
  107. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  108. $query_str = tripal_replace_entity_tokens($query_str, $entity, $bundle);
  109. // Make the request.
  110. $data = $this->makeRemoteRequest($query_str);
  111. if(!$data){
  112. return;
  113. }
  114. $total_items = $data['totalItems'];
  115. // Iterate through the members returned and save those for the field.
  116. for ($i = 0; $i < count($data['members']); $i++) {
  117. $member = $data['members'][$i];
  118. // Get the cotent type and remote entity id
  119. $content_type = $member['@type'];
  120. $remote_entity_id = $member['@id'];
  121. $remote_entity_id = preg_replace('/^.*\/(\d+)/', '$1', $remote_entity_id);
  122. // Save the member information for use later.
  123. $entity->{$field_name}['und'][$i]['remote_entity'] = $member;
  124. // Next get the the details about this member.
  125. $query_field = 'relationship';
  126. $query_field_url = $content_type . '/' . $remote_entity_id . '/' . $query_field;
  127. $field_data = $this->makeRemoteRequest($query_field_url);
  128. if(!$field_data){
  129. // If we encounter any type of error, we'll reset the field and return.
  130. $entity->{$field_name}['und'] = array();
  131. $entity->{$field_name}['und'][0] = array(
  132. 'value' => array(),
  133. );
  134. return;
  135. }
  136. // Set the field data as the value.
  137. $field_data_type = $field_data['@type'];
  138. $entity->{$field_name}['und'][$i]['value'] = $field_data;
  139. }
  140. }
  141. /**
  142. *
  143. * @see TripalField::settingsForm()
  144. */
  145. public function instanceSettingsForm() {
  146. $element = parent::instanceSettingsForm();
  147. // Get the setting for the option for how this widget.
  148. $instance = $this->instance;
  149. $settings = '';
  150. $site_list = '';
  151. $tokens = array();
  152. // Get the form info from the bundle about to be saved.
  153. $bundle_info = tripal_load_bundle_entity(array($instance['bundle']));
  154. // Retrieve all available tokens.
  155. $tokens = tripal_get_entity_tokens($bundle_info);
  156. $element['data_info'] = array(
  157. '#type' => 'fieldset',
  158. '#title' => 'Remote Data Settings',
  159. '#description' => 'Provide the site name, query and description for the remote data source.',
  160. '#collapsible' => TRUE,
  161. '#collapsed' => FALSE,
  162. '#prefix' => "<div id='set_titles-fieldset'>",
  163. '#suffix' => '</div>',
  164. );
  165. // Get the site info from the tripal_sites table.
  166. $sites = db_select('tripal_sites', 's')
  167. ->fields('s')
  168. ->execute()->fetchAll();
  169. foreach ($sites as $site) {
  170. $rows[$site->id] =$site->name;
  171. }
  172. $element['data_info']['remote_site'] = array(
  173. '#type' => 'select',
  174. '#title' => t('Site'),
  175. '#options' => $rows,
  176. '#default_value' => $this->instance['settings']['data_info']['remote_site'],
  177. );
  178. $element['data_info']['query'] = array(
  179. '#type' => 'textarea',
  180. '#title' => 'Query',
  181. '#description' => 'Build the query string that should be appended after the url. The tokens
  182. listed below may be used in your query build.',
  183. '#default_value' => $this->instance['settings']['data_info']['query'],
  184. '#rows' => 5,
  185. '#required' => TRUE
  186. );
  187. $element['data_info']['rd_field_name'] = array(
  188. '#type' => 'textfield',
  189. '#title' => 'Field',
  190. '#description' => 'Name of the field you would like to display.',
  191. '#default_value' => $this->instance['settings']['data_info']['rd_field_name'],
  192. '#required' => TRUE
  193. );
  194. $element['data_info']['token_display']['tokens'] = array(
  195. '#type' => 'hidden',
  196. '#value' => serialize($tokens)
  197. );
  198. $element['data_info']['token_display'] = array(
  199. '#type' => 'fieldset',
  200. '#title' => 'Available Tokens',
  201. '#description' => 'Copy the token and paste it into the "Query" text field above.',
  202. '#collapsible' => TRUE,
  203. '#collapsed' => TRUE
  204. );
  205. $element['data_info']['token_display']['content'] = array(
  206. '#type' => 'item',
  207. '#markup' => theme_token_list($tokens),
  208. );
  209. $element['data_info']['description'] = array(
  210. '#type' => 'textarea',
  211. '#title' => 'Description',
  212. '#description' => 'Describe the data being pulled in.',
  213. '#default_value' => $this->instance['settings']['data_info']['description'],
  214. '#rows' => 1
  215. );
  216. /*$element['test_button']['data'] = array(
  217. '#prefix' => '<div class="returned-data">',
  218. '#suffix' => '</div>',
  219. '#type' => 'container',
  220. //…
  221. );
  222. $element['test_button'] = array(
  223. '#type' => 'button',
  224. '#value' => t('Test Query'),
  225. '#ajax' => array(
  226. 'wrapper' => 'data-id',
  227. 'callback' => 'tripal_ws_url_query_test',
  228. ),
  229. //…
  230. );*/
  231. return $element;
  232. }
  233. /**
  234. *
  235. * @param unknown $form
  236. * @param unknown $form_state
  237. */
  238. public function instanceSettingsFormValidate($form, &$form_state) {
  239. }
  240. }