TripalWebServiceCollection.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = [];
  45. // Add some terms to the context.
  46. $this->addContextItem('Collection', 'hydra:Collection');
  47. $this->addContextItem('totalItems', 'hydra:totalItems');
  48. $this->addContextItem('member', 'hydra:member');
  49. parent::setType('Collection');
  50. // If the totalItems is set to -1 then this means paging is turned off and
  51. // all of the elements in the $memgbers array should be used.
  52. $this->totalItems = 0;
  53. $this->itemsPerPage = 25;
  54. $this->doPaging = FALSE;
  55. }
  56. /**
  57. * Initializes the pager.
  58. *
  59. * @param $totalItems
  60. * The total number of items available.
  61. * @param $itemsPerPage
  62. * The maximum number of items per page.
  63. * @param $path
  64. * The path
  65. */
  66. public function initPager($totalItems, $itemsPerPage, $page) {
  67. $this->doPaging = TRUE;
  68. $this->totalItems = $totalItems;
  69. $this->itemsPerPage = $itemsPerPage;
  70. $this->page = $page;
  71. }
  72. /**
  73. * Adds a new member to this resource if it is a collection.
  74. *
  75. * @param $member
  76. * A TripalWebServiceResource member whose type is the same as this
  77. * resource
  78. */
  79. public function addMember($member) {
  80. // Make sure the $service provides is a TripalWebService class.
  81. if (!is_a($member, 'TripalWebServiceResource')) {
  82. throw new Exception("Cannot add a new member to this resource collection as it is not a TripalWebServiceResource.");
  83. }
  84. $this->members[] = $member;
  85. }
  86. /**
  87. * @see TripalWebServiceResource::setType()
  88. */
  89. public function setType($type) {
  90. // TODO: There should be a check to make sure that the type is a
  91. // subclass of the hydra:Collection term.
  92. parent::setType($type);
  93. }
  94. /**
  95. * Retrieves the data section of the resource.
  96. *
  97. * The JSON-LD response constists of two sections the '@context' section
  98. * and the data section. This function only returns the data section
  99. * for this resource
  100. *
  101. * @return
  102. * An associative array containing the data section of the response.
  103. */
  104. public function getData() {
  105. $data = $this->data;
  106. $data['totalItems'] = 0;
  107. if ($this->doPaging == TRUE) {
  108. // Save any parameters provided by the user
  109. $saved_params = '';
  110. foreach ($this->params as $pkey => $pval) {
  111. if (in_array($pkey, [
  112. 'page',
  113. 'limit',
  114. 'first',
  115. 'last',
  116. 'next',
  117. 'prev',
  118. ])) {
  119. continue;
  120. }
  121. $saved_params .= '&' . $pkey . '=' . $pval;
  122. }
  123. $data['totalItems'] = $this->totalItems;
  124. $total_pages = ceil($this->totalItems / $this->itemsPerPage);
  125. $page = $this->page;
  126. $limit = $this->itemsPerPage;
  127. if ($this->totalItems > 0) {
  128. $data['view'] = [
  129. '@id' => $this->service_path . '?' . implode('&', array_merge([
  130. "page=$page",
  131. "limit=$limit",
  132. ])) . $saved_params,
  133. '@type' => 'PartialCollectionView',
  134. 'first' => $this->service_path . '?' . implode('&', array_merge([
  135. "page=1",
  136. "limit=$limit",
  137. ])) . $saved_params,
  138. 'last' => $this->service_path . '?' . implode('&', array_merge([
  139. "page=$total_pages",
  140. "limit=$limit",
  141. ])) . $saved_params,
  142. ];
  143. $prev = $page - 1;
  144. $next = $page + 1;
  145. if ($prev > 0) {
  146. $data['view']['previous'] = $this->service_path . '?' . implode('&', [
  147. "page=$prev",
  148. "limit=$limit",
  149. ]) . $saved_params;
  150. }
  151. if ($next < $total_pages) {
  152. $data['view']['next'] = $this->service_path . '?' . implode('&', [
  153. "page=$next",
  154. "limit=$limit",
  155. ]) . $saved_params;
  156. }
  157. }
  158. }
  159. else {
  160. $data['totalItems'] = count($this->members);
  161. }
  162. $member_data = [];
  163. foreach ($this->members as $key => $member) {
  164. $member_data[] = $member->getData();
  165. }
  166. $data['member'] = $member_data;
  167. // If paging of this collection is enabled then add the pager control links.
  168. return $data;
  169. }
  170. /**
  171. * Retrieves the data section of the resource.
  172. *
  173. * The JSON-LD response constists of two sections the '@context' section
  174. * and the data section. This function only returns the data section
  175. * for this resource
  176. *
  177. * @return
  178. * An associative array containing the data section of the response.
  179. */
  180. public function getContext() {
  181. if ($this->doPaging == TRUE) {
  182. $this->addContextItem('view', 'hydra:PartialCollectionView');
  183. }
  184. $context = $this->context;
  185. foreach ($this->members as $key => $member) {
  186. $citems = $member->getContext();
  187. foreach ($citems as $key => $val) {
  188. $context[$key] = $val;
  189. }
  190. }
  191. return $context;
  192. }
  193. }