123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- <?php
- class remote__data extends WebServicesField {
-
-
-
-
-
-
-
-
- public static $default_label = 'Remote Tripal Site';
-
- public static $default_description = 'Allows for inclusion of remote data from another Tripal site.';
-
- public static $default_widget = 'remote__data_widget';
-
- public static $default_formatter = 'remote__data_formatter';
-
- public static $module = 'tripal_ws';
-
-
-
-
-
- public static $default_settings = [
- 'storage' => 'field_tripal_ws_storage',
-
-
-
-
-
-
- 'searchable_keys' => [],
- ];
-
-
-
-
-
-
-
- public static $default_instance_settings = [
-
- 'term_vocabulary' => 'schema',
-
- 'term_name' => 'Thing',
-
- 'term_accession' => 'property',
-
-
- 'term_fixed' => FALSE,
-
-
-
-
-
- 'auto_attach' => FALSE,
-
- 'data_info' => [
- 'query' => '',
- 'remote_site' => '',
- 'description' => '',
- 'rd_field_name' => '',
- 'site_logo' => '',
- ],
- ];
-
-
-
-
- public static $no_ui = FALSE;
-
-
-
-
- public static $no_data = TRUE;
-
- private $remote_site = NULL;
-
-
- private $loaded_via_ws = FALSE;
- public function __construct($field, $instance) {
- parent::__construct($field, $instance);
-
-
-
-
-
- if (preg_match('/web-services/', $_SERVER['REQUEST_URI'])) {
- $this->loaded_via_ws = TRUE;
- return;
- }
-
- if (array_key_exists('data_info', $instance['settings'])) {
- $site_id_ws = $instance['settings']['data_info']['remote_site'];
- if ($site_id_ws) {
- $this->remote_site = db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->condition('ts.id', $site_id_ws)
- ->execute()
- ->fetchObject();
- }
- }
- }
-
- public function load($entity) {
-
- if ($this->loaded_via_ws == TRUE) {
- return;
- }
- $field_name = $this->field['field_name'];
- $field_type = $this->field['type'];
-
- $entity->{$field_name}['und'][0] = [
- 'value' => '',
- 'remote_entity' => NULL,
- 'error' => FALSE,
- 'warning' => FALSE,
- 'admin_message' => '',
- 'query_str' => '',
- ];
-
- $query_str = $this->instance['settings']['data_info']['query'];
- $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
- $query_str = tripal_replace_entity_tokens($query_str, $entity, $bundle);
-
- $data = $this->makeRemoteRequest($query_str);
- $context = [];
- if (is_array($data['@context'])) {
- $contenxt = $data['@context'];
- }
- else {
- $context = json_decode(file_get_contents($data['@context']), TRUE);
- $context = $context['@context'];
- }
- if (!$data) {
- $entity->{$field_name}['und'][0]['value'] = 'ERROR: there was a problem retrieving content for this field.';
- $entity->{$field_name}['und'][0]['admin_message'] = "The remote service returned no data.";
- $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
- $entity->{$field_name}['und'][0]['error'] = TRUE;
- $entity->{$field_name}['und'][0]['warning'] = FALSE;
- $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);
- return;
- }
-
- if (array_key_exists('error', $data)) {
- $entity->{$field_name}['und'][0]['value'] = 'ERROR: there was a problem retrieving content for this field.';
- $entity->{$field_name}['und'][0]['admin_message'] = "The content is currently not available because the " .
- "remote service reported the following error: " . $data['error'] . ".";
- $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
- $entity->{$field_name}['und'][0]['error'] = TRUE;
- $entity->{$field_name}['und'][0]['warning'] = FALSE;
- $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);
- return;
- }
- $num_items = count($data['member']);
- if ($num_items == 0) {
- $entity->{$field_name}['und'][0]['value'] = 'Content is unavailable on the remote service.';
- $entity->{$field_name}['und'][0]['admin_message'] = "The query to the remote service returned an empty result set. If you " .
- "think this is an error, please check the query string and the remote service to verify. ";
- $entity->{$field_name}['und'][0]['warning'] = TRUE;
- $entity->{$field_name}['und'][0]['error'] = FALSE;
- $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
- $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);
- return;
- }
-
- for ($i = 0; $i < $num_items; $i++) {
- $member = $data['member'][$i];
-
- $content_type = $member['@type'];
- $remote_entity_id = $member['@id'];
- $remote_entity_id = preg_replace('/^.*\/(\d+)/', '$1', $remote_entity_id);
-
- $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
- $subfields = explode(',', $rd_field_name);
- $query_field = $subfields[0];
-
- $query_field_url = $context[$content_type] . '/' . $remote_entity_id . '/' . $query_field;
- $field_data = $this->makeRemoteRequest($query_field_url);
-
- if ($field_data && array_key_exists('error', $field_data)) {
- $entity->{$field_name} = [];
- $entity->{$field_name}['und'][0]['value'] = 'ERROR: there was a problem retrieving secific content for this field.';
- $entity->{$field_name}['und'][0]['admin_message'] = "While iterating through the list of results, the " .
- "remote service reported the following error: " . $field_data['error'] . ". ";
- $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
- $entity->{$field_name}['und'][0]['error'] = TRUE;
- $entity->{$field_name}['und'][0]['warning'] = FALSE;
- $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_field_url);
- return;
- }
-
- $field_data_type = $field_data['@type'];
- $entity->{$field_name}['und'][$i]['value'] = $field_data;
- $entity->{$field_name}['und'][$i]['remote_entity'] = $member;
- $entity->{$field_name}['und'][$i]['error'] = FALSE;
- $entity->{$field_name}['und'][$i]['warning'] = FALSE;
- $entity->{$field_name}['und'][$i]['admin_message'] = '';
- $entity->{$field_name}['und'][$i]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_field_url);;
- }
- }
-
- private function buildRemoteURL($remote_site, $query) {
- $path = $query;
- $q = '';
- if (preg_match('/\?/', $query)) {
- list($path, $q) = explode('?', $query);
- }
- if (empty($remote_site)) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote_site in remote__data field while attempting to build the remote URL.');
- return NULL;
- }
- return tripal_build_remote_content_url($remote_site, $path, $q);
- }
-
- private function makeRemoteRequest($query) {
- $path = $query;
- $q = '';
- if (preg_match('/\?/', $query)) {
- list($path, $q) = explode('?', $query);
- }
- if (empty($this->remote_site)) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote_site while attempting to make the request.');
- return NULL;
- }
- try {
- $data = tripal_get_remote_content($this->remote_site->id, $path, $q);
- } catch (Exception $exception) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR, $exception->getMessage());
- return NULL;
- }
- return $data;
- }
-
- public function instanceSettingsForm() {
- $element = parent::instanceSettingsForm();
-
- $instance = $this->instance;
- $settings = '';
- $site_list = '';
- $tokens = [];
-
- $bundle = tripal_load_bundle_entity(['name' => $instance['bundle']]);
-
- $tokens = tripal_get_entity_tokens($bundle);
- $element['data_info'] = [
- '#type' => 'fieldset',
- '#title' => 'Remote Data Settings',
- '#description' => 'These settings allow you to provide a Tripal web
- services query to identify content on another Tripal site and display
- that here within this field. You must specify the query to execute and
- the field to display.',
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#prefix' => "<div id='set_titles-fieldset'>",
- '#suffix' => '</div>',
- ];
-
- $sites = db_select('tripal_sites', 's')
- ->fields('s')
- ->execute()->fetchAll();
- foreach ($sites as $site) {
- $rows[$site->id] = $site->name;
- }
- $element['data_info']['remote_site'] = [
- '#type' => 'select',
- '#title' => t('Remote Tripal Site'),
- '#options' => $rows,
- '#default_value' => $this->instance['settings']['data_info']['remote_site'],
- ];
- $element['data_info']['query'] = [
- '#type' => 'textarea',
- '#title' => 'Query to Execute',
- '#description' => 'Enter the query that will retreive the remote records. ',
- '#default_value' => $this->instance['settings']['data_info']['query'],
- '#rows' => 3,
- '#required' => TRUE,
- ];
- $element['data_info']['query_instructions'] = [
- '#type' => 'fieldset',
- '#title' => 'Query to Execute Instructions',
- '#description' => 'If the full URL to the remote tripal content web ' .
- 'service is "https://[tripal_site]/web-services/content/v0.1/". Then ' .
- 'this field should contain the text immediately after the ' .
- '"content/v0.1" portion of the URL. For information about building ' .
- 'web services queries see the online documentation at ' .
- l('The Tripal v3 User\'s Guide', 'http://tripal.info/tutorials/v3.x/web-services') .
- '. For example, suppose this field is attached to an Organism content ' .
- 'type on the local site, and you want to retrieve a field for the ' .
- 'same organism on a remote Tripal site. To retrieve the matching ' .
- 'record, you will want to query on the genus and species, since it ' .
- 'is unique and, you want them to match the organism for each specific ' .
- 'local organism page. You can use tokens to do this (see the ' .
- '"Available Tokens" fieldset below). For this example, the full ' .
- 'remote web service endpoint would be ' .
- '"https://[tripal_site]/web-services/content/v0.1/Organism" ' .
- 'and the query text should be ' .
- '"Organism?genus=[taxrank__genus]&species=[taxrank__species]".',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- ];
- $element['data_info']['query_instructions']['instructions'] = [
- '#type' => 'markup',
- '#markup' => '',
- ];
- $element['data_info']['token_display']['tokens'] = [
- '#type' => 'hidden',
- '#value' => serialize($tokens),
- ];
- $element['data_info']['token_display'] = [
- '#type' => 'fieldset',
- '#title' => 'Available Tokens',
- '#description' => 'Copy the token and paste it into the "Query" text field above.',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- ];
- $element['data_info']['token_display']['content'] = [
- '#type' => 'item',
- '#markup' => theme_token_list($tokens),
- ];
- $element['data_info']['rd_field_name'] = [
- '#type' => 'textfield',
- '#title' => 'Field to Display',
- '#description' => 'Enter the key from the results returned by the "Query to Execute" that should be displayed. See the example below for more details.',
- '#default_value' => $this->instance['settings']['data_info']['rd_field_name'],
- '#required' => TRUE,
- ];
- $element['data_info']['rd_field_name_instructions'] = [
- '#type' => 'fieldset',
- '#title' => 'Field to Display Instructions',
- '#description' => '',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- ];
- $element['data_info']['rd_field_name_instructions']['insructions'] = [
- '#type' => 'markup',
- '#markup' => 'The query from a Tripal web service response is always
- in JSON array arranged in key/value pairs. The key is the name of a
- controlled vocabulary term.
- <br><br>Suppose you want to query details about an organism.
- Consider the following JSON result
- <pre style="height: 200px; overflow: auto;">
- "@type": "organism",
- "label": "Anopheles gambiae",
- "ItemPage": "http://demo.tripal.info/3.x/bio_data/642",
- "type": "Organism",
- "abbreviation": "A.gambiae",
- "genus": "Anopheles",
- "species": "gambiae",
- "common_name": "mosquito",
- "database_cross_reference": "http://demo.tripal.info/3.x/web-services/content/v0.1/Organism/642/database+cross+reference",
- "equivalent_name": "Anopheles gambiae sensu stricto",
- "division": "Invertebrates",
- "mitochondrial_genetic_code_name": "Invertebrate Mitochondrial",
- "synonym": "Anopheles gambiae S",
- "genetic_code": "1",
- "lineage": "cellular organisms; Eukaryota; Opisthokonta; Metazoa; Eumetazoa; Bilateria; Protostomia; Ecdysozoa; Panarthropoda; Arthropoda; Mandibulata; Pancrustacea; Hexapoda; Insecta; Dicondylia; Pterygota; Neoptera; Holometabola; Diptera; Nematocera; Culicomorpha; Culicoidea; Culicidae; Anophelinae; Anopheles; Cellia; Pyretophorus; gambiae species complex",
- "genetic_code_name": "Standard",
- "genbank_common_name": "African malaria mosquito"
- </pre>
- To display the "common_name" from the JSON above you would enter the word
- "common_name" in the Field to Display textbox.
- ',
- ];
- $element['data_info']['description'] = [
- '#type' => 'textarea',
- '#title' => 'Description',
- '#description' => 'Describe the data being pulled in.',
- '#default_value' => $this->instance['settings']['data_info']['description'],
- '#rows' => 1,
- ];
- $fid = $this->instance['settings']['data_info']['site_logo'] ? $this->instance['settings']['data_info']['site_logo'] : NULL;
- $file = NULL;
- if ($fid) {
- $file = file_load($fid);
- }
- $element['data_info']['site_logo'] = [
- '#title' => 'Remote Site Logo',
- '#description' => t('When data is taken from a remote site and shown to the user,
- the site from which the data was retrieved is indicated. If you would like to
- include the logo for the remote site, please upload an image here.'),
- '#type' => 'managed_file',
- '#default_value' => $file ? $file->fid : NULL,
- '#theme' => 'image_widget',
- '#attached' => [
- 'css' => [
- 'image-preview' => drupal_get_path('module', 'image') . '/image.css',
- ],
- ],
- 'preview' => [
- '#markup' => theme('image_style', [
- 'style_name' => 'thumbnail',
- 'path' => $file ? $file->uri : '',
- ]),
- ],
- ];
- return $element;
- }
-
- public function instanceSettingsFormValidate($form, &$form_state) {
- $site_logo = $form_state['values']['instance']['settings']['data_info']['site_logo'];
-
- if ($site_logo) {
- $file = file_load($site_logo);
- $file_usage = file_usage_list($file);
- if (!array_key_exists('tripal_ws', $file_usage)) {
- file_usage_add($file, 'tripal_ws', 'site-logo', 1);
- }
- }
- }
- }
|