TripalWebServiceCollection.inc 5.8 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. $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 $servcie provides is a TripalWebServcie 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, array('page', 'limit', 'first', 'last', 'next', 'prev'))) {
  112. continue;
  113. }
  114. $saved_params .= '&' . $pkey . '=' . $pval;
  115. }
  116. $data['totalItems'] = $this->totalItems;
  117. $total_pages = ceil($this->totalItems / $this->itemsPerPage);
  118. $page = $this->page;
  119. $limit = $this->itemsPerPage;
  120. if ($this->totalItems > 0) {
  121. $data['view'] = array(
  122. '@id' => $this->service_path . '?' . implode('&', array_merge(array("page=$page", "limit=$limit"))) . $saved_params,
  123. '@type' => 'PartialCollectionView',
  124. 'first' => $this->service_path . '?' . implode('&', array_merge(array("page=1", "limit=$limit"))) . $saved_params,
  125. 'last' => $this->service_path . '?' . implode('&', array_merge(array("page=$total_pages", "limit=$limit"))) . $saved_params,
  126. );
  127. $prev = $page - 1;
  128. $next = $page + 1;
  129. if ($prev > 0) {
  130. $data['view']['previous'] = $this->service_path .'?' . implode('&', array("page=$prev", "limit=$limit")) . $saved_params;
  131. }
  132. if ($next < $total_pages) {
  133. $data['view']['next'] = $this->service_path . '?' . implode('&', array("page=$next", "limit=$limit")) . $saved_params;
  134. }
  135. }
  136. }
  137. else {
  138. $data['totalItems'] = count($this->members);
  139. }
  140. $member_data = array();
  141. foreach ($this->members as $key => $member) {
  142. $member_data[] = $member->getData();
  143. }
  144. $data['member'] = $member_data;
  145. // If paging of this collection is enabled then add the pager control links.
  146. return $data;
  147. }
  148. /**
  149. * Retrieves the data section of the resource.
  150. *
  151. * The JSON-LD response constists of two sections the '@context' section
  152. * and the data section. This function only returns the data section
  153. * for this resource
  154. *
  155. * @return
  156. * An associative array containing the data section of the response.
  157. */
  158. public function getContext() {
  159. if ($this->doPaging == TRUE) {
  160. $this->addContextItem('view', 'hydra:PartialCollectionView');
  161. }
  162. $context = $this->context;
  163. foreach ($this->members as $key => $member) {
  164. $citems = $member->getContext();
  165. foreach ($citems as $key => $val) {
  166. $context[$key] = $val;
  167. }
  168. }
  169. return $context;
  170. }
  171. }