Browse Source

Fixed bug in WS with navigation links not maintaining the user provide params

Stephen Ficklin 7 years ago
parent
commit
827d3e2714

+ 5 - 5
tripal_ws/includes/TripalWebService/TripalEntityService_v0_1.inc

@@ -322,7 +322,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
 
       // If this is the expanded field page then we need to swap out
       // the resource for a collection.
-      $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield));
+      $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield), $this->params);
       $label = tripal_get_term_details('rdfs', 'label');
       $response->addContextItem('label', $label['url']);
       $response->addProperty('label', $instance['label']);
@@ -536,7 +536,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
               );
             }
             else {
-              throw new Exception("The term, '$criteria', is not available for sorting.");
+              throw new Exception("The value, '$criteria', is not available for sorting.");
             }
           }
           // If this field is not a TripalField then it should just have
@@ -552,7 +552,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
 
         }
         else {
-          throw new Exception("The term, '$key', is not available for sorting.");
+          throw new Exception("The value, '$key', is not available for sorting.");
         }
       }
     }
@@ -740,7 +740,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
   private function doContentTypeList($ctype) {
     $service_path = $this->getServicePath() . '/' . urlencode($ctype);
     $label = tripal_get_term_details('rdfs', 'label');
-    $this->resource = new TripalWebServiceCollection($service_path);
+    $this->resource = new TripalWebServiceCollection($service_path, $this->params);
     $this->resource->addContextItem('label', $label['url']);
 
     // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
@@ -841,7 +841,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
   private function doAllTypesList() {
     $service_path = $this->getServicePath();
     $label = tripal_get_term_details('rdfs', 'label');
-    $this->resource = new TripalWebServiceCollection($service_path);
+    $this->resource = new TripalWebServiceCollection($service_path, $this->params);
     $this->resource->addContextItem('label', $label['url']);
     $this->resource->addProperty('label', 'Content Types');
 

+ 28 - 6
tripal_ws/includes/TripalWebServiceCollection.inc

@@ -32,15 +32,28 @@ class TripalWebServiceCollection extends TripalWebServiceResource {
   protected $page;
 
 
+  /**
+   * The parameters as collected by the parent TripalWebService.  We need
+   * the parameters because this Resource generates first, next, prev, and last
+   * links and we need to maintain the parameters a user may have
+   * provided in those links.
+   */
+  protected $params;
+
   /**
    * Implements the constructor.
    *
    * @param TripalWebService $service
    *   An instance of a TripalWebService or class that extends it.
    */
-  public function __construct($service_path) {
+  public function __construct($service_path, $params) {
     parent::__construct($service_path);
+
+    // Initialize private member variables.
+    $this->params = $params;
     $this->members = array();
+
+    // Add some terms to the context.
     $term = tripal_get_term_details('hydra', 'Collection');
     $this->addContextItem('Collection', $term['url']);
     $term = tripal_get_term_details('hydra', 'totalItems');
@@ -111,6 +124,15 @@ class TripalWebServiceCollection extends TripalWebServiceResource {
 
     if ($this->doPaging == TRUE) {
 
+      // Save any parameters provided by the user
+      $saved_params = '';
+      foreach ($this->params as $pkey => $pval) {
+        if (in_array($pkey, array('page', 'limit', 'first', 'last', 'next', 'prev'))) {
+          continue;
+        }
+        $saved_params .= '&' . $pkey . '=' . $pval;
+      }
+
       $data['totalItems'] = $this->totalItems;
       $total_pages = ceil($this->totalItems / $this->itemsPerPage);
       $page = $this->page;
@@ -118,18 +140,18 @@ class TripalWebServiceCollection extends TripalWebServiceResource {
 
       if ($this->totalItems > 0) {
         $data['view'] = array(
-          '@id' => $this->service_path . '?' . implode('&', array_merge(array("page=$page", "limit=$limit"))),
+          '@id' => $this->service_path . '?' . implode('&', array_merge(array("page=$page", "limit=$limit"))) . $saved_params,
           '@type' => 'PartialCollectionView',
-          'first' => $this->service_path . '?' . implode('&', array_merge(array("page=1", "limit=$limit"))),
-          'last' => $this->service_path . '?' . implode('&', array_merge(array("page=$total_pages", "limit=$limit"))),
+          'first' => $this->service_path . '?' . implode('&', array_merge(array("page=1", "limit=$limit"))) . $saved_params,
+          'last' => $this->service_path . '?' . implode('&', array_merge(array("page=$total_pages", "limit=$limit"))) . $saved_params,
         );
         $prev = $page - 1;
         $next = $page + 1;
         if ($prev > 0) {
-          $data['view']['previous'] = $this->service_path .'?' . implode('&', array("page=$prev", "limit=$limit"));
+          $data['view']['previous'] = $this->service_path .'?' . implode('&', array("page=$prev", "limit=$limit")) . $saved_params;
         }
         if ($next < $total_pages) {
-          $data['view']['next'] = $this->service_path . '?' . implode('&', array("page=$next", "limit=$limit"));
+          $data['view']['next'] = $this->service_path . '?' . implode('&', array("page=$next", "limit=$limit")) . $saved_params;
         }
       }
     }