TripalWebServiceCollection.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. class TripalWebServiceCollection extends TripalWebServiceResource {
  3. /**
  4. * Holds the data portion of the JSON-LD response if this resource is
  5. * a collection.
  6. */
  7. protected $members;
  8. /**
  9. * Set to TRUE if paging should be enabled. Paging is disabled by default.
  10. */
  11. protected $doPaging;
  12. /**
  13. * The total number of items. This variable is used if paging is turned on.
  14. * Otherwise it's ignored and the total number of items reported by the
  15. * collection will be the number of elements in the $members array.
  16. */
  17. protected $totalItems;
  18. /**
  19. * When the collection contains more than the itemsPerpage amount then
  20. * the results will be paged.
  21. */
  22. protected $itemsPerPage;
  23. /**
  24. * The current page of the pager.
  25. */
  26. protected $page;
  27. /**
  28. * The parameters as collected by the parent TripalWebService. We need
  29. * the parameters because this Resource generates first, next, prev, and last
  30. * links and we need to maintain the parameters a user may have
  31. * provided in those links.
  32. */
  33. protected $params;
  34. /**
  35. * Implements the constructor.
  36. *
  37. * @param TripalWebService $service
  38. * An instance of a TripalWebService or class that extends it.
  39. */
  40. public function __construct($service_path, $params) {
  41. parent::__construct($service_path);
  42. // Initialize private member variables.
  43. $this->params = $params;
  44. $this->members = array();
  45. // Add some terms to the context.
  46. $term = tripal_get_term_details('hydra', 'Collection');
  47. $this->addContextItem('Collection', $term['url']);
  48. $term = tripal_get_term_details('hydra', 'totalItems');
  49. $this->addContextItem('totalItems', $term['url']);
  50. $term = tripal_get_term_details('hydra', 'member');
  51. $this->addContextItem('member', $term['url']);
  52. parent::setType('Collection');
  53. // If the totalItems is set to -1 then this means paging is turned off and
  54. // all of the elements in the $memgbers array should be used.
  55. $this->totalItems = 0;
  56. $this->itemsPerPage = 25;
  57. $this->doPaging = FALSE;
  58. }
  59. /**
  60. * Initializes the pager.
  61. *
  62. * @param $totalItems
  63. * The total number of items available.
  64. * @param $itemsPerPage
  65. * The maximum number of items per page.
  66. * @param $path
  67. * The path
  68. */
  69. public function initPager($totalItems, $itemsPerPage, $page) {
  70. $this->doPaging = TRUE;
  71. $this->totalItems = $totalItems;
  72. $this->itemsPerPage = $itemsPerPage;
  73. $this->page = $page;
  74. }
  75. /**
  76. * Adds a new member to this resource if it is a collection.
  77. *
  78. * @param $member
  79. * A TripalWebServiceResource member whose type is the same as this
  80. * resource
  81. */
  82. public function addMember($member) {
  83. // Make sure the $servcie provides is a TripalWebServcie class.
  84. if (!is_a($member, 'TripalWebServiceResource')) {
  85. throw new Exception("Cannot add a new member to this resource collection as it is not a TripalWebServiceResource.");
  86. }
  87. $this->members[] = $member;
  88. }
  89. /**
  90. * @see TripalWebServiceResource::setType()
  91. */
  92. public function setType($type) {
  93. throw new Exception("The type for a Collection can only be collection.");
  94. }
  95. /**
  96. * Retrieves the data section of the resource.
  97. *
  98. * The JSON-LD response constists of two sections the '@context' section
  99. * and the data section. This function only returns the data section
  100. * for this resource
  101. *
  102. * @return
  103. * An associative array containing the data section of the response.
  104. */
  105. public function getData() {
  106. $data = $this->data;
  107. $data['totalItems'] = 0;
  108. if ($this->doPaging == TRUE) {
  109. // Save any parameters provided by the user
  110. $saved_params = '';
  111. foreach ($this->params as $pkey => $pval) {
  112. if (in_array($pkey, array('page', 'limit', 'first', 'last', 'next', 'prev'))) {
  113. continue;
  114. }
  115. $saved_params .= '&' . $pkey . '=' . $pval;
  116. }
  117. $data['totalItems'] = $this->totalItems;
  118. $total_pages = ceil($this->totalItems / $this->itemsPerPage);
  119. $page = $this->page;
  120. $limit = $this->itemsPerPage;
  121. if ($this->totalItems > 0) {
  122. $data['view'] = array(
  123. '@id' => $this->service_path . '?' . implode('&', array_merge(array("page=$page", "limit=$limit"))) . $saved_params,
  124. '@type' => 'PartialCollectionView',
  125. 'first' => $this->service_path . '?' . implode('&', array_merge(array("page=1", "limit=$limit"))) . $saved_params,
  126. 'last' => $this->service_path . '?' . implode('&', array_merge(array("page=$total_pages", "limit=$limit"))) . $saved_params,
  127. );
  128. $prev = $page - 1;
  129. $next = $page + 1;
  130. if ($prev > 0) {
  131. $data['view']['previous'] = $this->service_path .'?' . implode('&', array("page=$prev", "limit=$limit")) . $saved_params;
  132. }
  133. if ($next < $total_pages) {
  134. $data['view']['next'] = $this->service_path . '?' . implode('&', array("page=$next", "limit=$limit")) . $saved_params;
  135. }
  136. }
  137. }
  138. else {
  139. $data['totalItems'] = count($this->members);
  140. }
  141. $member_data = array();
  142. foreach ($this->members as $key => $member) {
  143. $member_data[] = $member->getData();
  144. }
  145. $data['members'] = $member_data;
  146. // If paging of this collection is enabled then add the pager control links.
  147. return $data;
  148. }
  149. /**
  150. * Retrieves the data section of the resource.
  151. *
  152. * The JSON-LD response constists of two sections the '@context' section
  153. * and the data section. This function only returns the data section
  154. * for this resource
  155. *
  156. * @return
  157. * An associative array containing the data section of the response.
  158. */
  159. public function getContext() {
  160. if ($this->doPaging == TRUE) {
  161. $this->addContextItem('view', 'hydra:PartialCollectionView');
  162. }
  163. $context = $this->context;
  164. foreach ($this->members as $key => $member) {
  165. $citems = $member->getContext();
  166. foreach ($citems as $key => $val) {
  167. $context[$key] = $val;
  168. }
  169. }
  170. return $context;
  171. }
  172. }