Browse Source

Added better error support

Stephen Ficklin 6 years ago
parent
commit
1877fab118

+ 20 - 3
tripal_ws/includes/TripalFields/remote__data/remote__data.inc

@@ -136,7 +136,8 @@ class remote__data extends WebServicesField {
     $entity->{$field_name}['und'][0] = array(
       'value' => array(),
       'remote_entity' => array(),
-      'error' => NULL,
+      'error' => FALSE,
+      'warning' => FALSE,
     );
 
     // If this field is being loaded via web services then just return.
@@ -151,17 +152,26 @@ class remote__data extends WebServicesField {
 
     // Make the request.
     $data = $this->makeRemoteRequest($query_str);
+    dpm($data);
     if(!$data){
       return;
     }
 
     $total_items = $data['totalItems'];
+    if ($total_items == 0) {
+      $entity->{$field_name}['und'][0]['value'] = 'Content from the remote site is currently not available'  .
+        tripal_set_message("The remote content is currently not available. " .
+          "It could be that remote services are unavailable or the query " .
+          "string does not match content appropriate for this content. Please check.",
+          TRIPAL_WARNING, ['return_html' => TRUE]);
+      $entity->{$field_name}['und'][0]['warning'] = TRUE;
+      $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
+    }
 
     // Iterate through the members returned and save those for the field.
     for ($i = 0; $i < $total_items; $i++) {
       $member = $data['member'][$i];
 
-
       // Get the cotent type and remote entity id
       $content_type = $member['@type'];
       $remote_entity_id = $member['@id'];
@@ -169,6 +179,8 @@ class remote__data extends WebServicesField {
 
       // Save the member information for use later.
       $entity->{$field_name}['und'][$i]['remote_entity'] = $member;
+      $entity->{$field_name}['und'][$i]['error'] = FALSE;
+      $entity->{$field_name}['und'][$i]['warning'] = FALSE;
 
       // Separate the query_field if it has subfields.
       $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
@@ -181,7 +193,10 @@ class remote__data extends WebServicesField {
 
       if(!$field_data){
         // If we encounter any type of error, we'll reset the field and return.
-        $entity->{$field_name}['und'][$i]['value'] = 'Unable to retrieve remote content';
+        $entity->{$field_name}['und'][$i]['value'] = 'Unable to retrieve remote content' .
+          tripal_set_message("Something is wrong with the remote site. It returned " . 
+            "a list of matches for the query but did not return details about each match.",
+            TRIPAL_ERROR, ['return_html' => TRUE]);
         $entity->{$field_name}['und'][$i]['error'] = TRUE;
         return;
       }
@@ -204,6 +219,8 @@ class remote__data extends WebServicesField {
      if (preg_match('/\?/', $query)) {
        list($path, $q) = explode('?', $query);
      }
+     dpm($path);
+     dpm($q);
      $data = tripal_get_remote_content($this->remote_site->id, $path, $q);
 
      return $data;

+ 40 - 22
tripal_ws/includes/TripalFields/remote__data/remote__data_formatter.inc

@@ -71,6 +71,8 @@ class remote__data_formatter extends WebServicesFieldFormatter {
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
     $content = '';
     
+    dpm($items);
+    
     // Get the settings
     $settings = $display['settings'];
     $field_name = $this->field['field_name'];
@@ -100,40 +102,50 @@ class remote__data_formatter extends WebServicesFieldFormatter {
     $rows = array();
     $headers = 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) {
+      if (!$item['value'] or empty($item['value'])) {
         continue;
       }
+      $value = $item['value'];
       $error = $item['error'];
+      $warning = $item['warning'];
       if ($error) {
-        $rows[] = [['error' => $value]];
+        $rows[] = [$value];
+        continue;
+      }      
+      if ($warning) {
+        $rows[] = [$value];
         continue;
       }
+      
+      
+      $remote_entity_label = array_key_exists('label', $item) ? $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));
+
 
       // 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 {
-        if (count($subfields) > 0) {
-          $subvalue = $this->refineSubValue($value, $subfields, $header_label);
-          $rows[] = array($subvalue);
+      if (is_array($value)) {
+        if (array_key_exists('members', $value)) {
+          foreach ($value['members'] as $subvalue) {
+            $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
+            $rows[] = array($subvalue);
+          }
         }
         else {
-          if (array_key_exists($flabel, $value)) {
-            $rows[] = array($value[$flabel]);
+          if (count($subfields) > 0) {
+            $subvalue = $this->refineSubValue($value, $subfields, $header_label);
+            $rows[] = array($subvalue);
           }
           else {
-            $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
-            $rows[] = array($value);
+            if (array_key_exists($flabel, $value)) {
+              $rows[] = array($value[$flabel]);
+            }
+            else {
+              $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
+              $rows[] = array($value);
+            }
           }
         }
       }
@@ -145,6 +157,12 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         $rows[$i][0] = $this->createTable($rows[$i]);
         $has_sub_tables = TRUE;
       }
+      else {
+        $rows[$i] = [
+          'colspan' => 2,
+          'data' => $rows[$i],
+        ];
+      }
     }
 
     // If we don't have  tables for each row then we'll put everything into