Browse Source

Fixed several issues with Remote Data Field

Stephen Ficklin 7 years ago
parent
commit
cfe1c3d8de

+ 3 - 1
tripal/includes/TripalFields/TripalField.inc

@@ -109,7 +109,9 @@ class TripalField {
    *   An array containing the instance data as returned by field_instance_info().
    */
   public function __construct($field, $instance) {
-    $vocabulary = $term = NULL;
+    $term = NULL;
+    $vocabulary = NULL;
+    $accession = NULL;
     $this->field = $field;
     $this->instance = $instance;
 

+ 12 - 5
tripal_ws/includes/TripalFields/remote__data/remote__data.inc

@@ -199,17 +199,24 @@ class remote__data extends WebServicesField {
     *   website.
     */
    private function makeRemoteRequest($query) {
+     $ctype = $query;
+     $qdata = '';
+     if (preg_match('/\?/', $query)) {
+       list($ctype, $qdata) = explode('?', $query);
+     }
 
      // Build the URL to the remote web services.
+     $ws_version = $this->remote_site->version;
      $ws_url = $this->remote_site->url;
      $ws_url = trim($ws_url, '/');
-     $ws_url .= '/web-services/content/v0.1';
+     $ws_url .= '/web-services/content/' . $ws_version . '/' . $ctype;
 
      // Build the Query and make and substitions needed.
-     $query_url = $ws_url . '/' . $query;
-
-     $options = array();
-     $data = drupal_http_request($query_url, $options);
+     //dpm($ws_url . '?' . $query);
+     $options = array(
+       'data' => $qdata,
+     );
+     $data = drupal_http_request($ws_url, $options);
 
      if (!$data) {
        tripal_report_error('tripal_ws', TRIPAL_ERROR,

+ 41 - 33
tripal_ws/includes/TripalFields/remote__data/remote__data_formatter.inc

@@ -78,7 +78,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
     $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
     $subfields = explode(',', $rd_field_name);
     $header_label = $this->getHeaderLabel($subfields);
-    array_shift($subfields);
+    $flabel = array_shift($subfields);
 
     // Get the site logo if one is provided
     $site_logo = $this->instance['settings']['data_info']['site_logo'];
@@ -94,47 +94,49 @@ class remote__data_formatter extends WebServicesFieldFormatter {
       ->execute()
       ->fetchObject();
 
-    $content = '';
-    if (array_key_exists('label', $items[0]['remote_entity'])) {
-      $remote_entity_label = $items[0]['remote_entity']['label'];
-      $remote_entity_page = $items[0]['remote_entity']['ItemPage'];
-      $content = t('This content provided by !site.',
-        array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
-      if ($site_logo) {
-        $content .= '<br><img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '">';
-      }
-      $content .= '<br>' . t('View !data on %site',
-        array('!data' => l('this content', $remote_entity_page, array('attributes' => array('target' => '_blank'))),
-               '%site' => $site->name));
-    }
-    else {
-      $content = t('There is no data about this record from the !site database.',
-          array('!site' => l($site->name, $site->uri)));
+      $content = '<p>';
+    if (is_object($site_logo)) {
+      $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
     }
+    $content .=  t('This content provided by !site.',
+        array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
+    $content .= '</p>';
     $rows = array();
     foreach ($items as $index => $item) {
+      $remote_entity_label = $item['remote_entity']['label'];
+      $remote_entity_page = $item['remote_entity']['ItemPage'];
+      $link = t('View !data on %site',
+          array('!data' => l('this content', $remote_entity_page, array('attributes' => array('target' => '_blank'))),
+            '%site' => $site->name));
       $value = $item['value'];
       if (!$value) {
         continue;
       }
-      if (is_array($value)) {
-        $headers = array('');
+      $headers = array('');
 
-        // If this is a collection then handle it as a list of members.
-        if (array_key_exists('members', $value)) {
-          foreach ($value['members'] as $subvalue) {
-            $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
-            $rows[]= array($subvalue);
-          }
-        }
-        else {
-          $subvalue = $this->refineSubValue($value, $subfields, $header_label);
-          $rows[]= array($subvalue);
+      // If this is a collection then handle it as a list of members.
+      if (array_key_exists('members', $value)) {
+        foreach ($value['members'] as $subvalue) {
+          $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
+          $rows[] = array($subvalue);
         }
       }
       else {
-        $rows[] = array($value);
+        if (count($subfields) > 0) {
+          $subvalue = $this->refineSubValue($value, $subfields, $header_label);
+          $rows[] = array($subvalue);
+        }
+        else {
+          if (array_key_exists($flabel, $value)) {
+            $rows[] = array(l($value[$flabel], $remote_entity_page, array('attributes' => array('target' => '_blank'))));
+          }
+          else {
+            $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
+            $rows[] = array($value);
+          }
+        }
       }
+
     }
 
     $has_sub_tables = FALSE;
@@ -164,12 +166,11 @@ class remote__data_formatter extends WebServicesFieldFormatter {
     else {
       for ($i = 0; $i < count($rows); $i++) {
         if (count($rows) > 1) {
-          $content .= '<p class="tripal-remote--data-field-table-label">' . $header_label . ' ' . ($i + 1) . '</p>';
+          $content .= '<span class="tripal-remote--data-field-table-label">' . $header_label . ' ' . ($i + 1) . '</span>';
         }
         $content .= $rows[$i][0];
       }
     }
-
     // Return the content for this field.
     $element[0] = array(
       '#type' => 'markup',
@@ -203,10 +204,17 @@ class remote__data_formatter extends WebServicesFieldFormatter {
    * @param $subfields
    */
   private function refineSubValue($values, $subfields) {
+
+    // Remove unwanted elements.
+    unset($values['@id']);
+    unset($values['@context']);
+    unset($values['@type']);
+    unset($values['remote_entity']);
+
     $subfield = array_shift($subfields);
     if (array_key_exists($subfield, $values)) {
       if (is_array($values[$subfield]) and count($subfields) > 0) {
-        return $this->refineSubvalue($values[$subfield], $subfields);
+        return  $this->refineSubvalue($values[$subfield], $subfields);
       }
       else {
         return $values[$subfield];

+ 5 - 1
tripal_ws/theme/css/tripal_ws.css

@@ -1,6 +1,7 @@
 .tripal-remote--data-field {
   max-height: 400px;
-  overflow: scroll;
+  overflow: auto;
+  width: 100%;
 }
 .tripal-remote--data-field-logo {
   max-height: 100px;
@@ -11,6 +12,9 @@
   padding-right: 5px;
 }
 
+.tripal-remote--data-field-table  {
+  width: 100%;
+}
 .tripal-remote--data-field-table tr {
   background: none;
 }