TripalWebServiceCollection.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Implements the constructor.
  29. *
  30. * @param TripalWebService $service
  31. * An instance of a TripalWebService or class that extends it.
  32. */
  33. public function __construct($service_path) {
  34. parent::__construct($service_path);
  35. $this->members = array();
  36. $term = tripal_get_term_details('hydra', 'Collection');
  37. $this->addContextItem('Collection', $term['url']);
  38. $term = tripal_get_term_details('hydra', 'totalItems');
  39. $this->addContextItem('totalItems', $term['url']);
  40. $term = tripal_get_term_details('hydra', 'member');
  41. $this->addContextItem('member', $term['url']);
  42. parent::setType('Collection');
  43. // If the totalItems is set to -1 then this means paging is turned off and
  44. // all of the elements in the $memgbers array should be used.
  45. $this->totalItems = 0;
  46. $this->itemsPerPage = 25;
  47. $this->doPaging = FALSE;
  48. }
  49. /**
  50. * Initializes the pager.
  51. *
  52. * @param $totalItems
  53. * The total number of items available.
  54. * @param $itemsPerPage
  55. * The maximum number of items per page.
  56. * @param $path
  57. * The path
  58. */
  59. public function initPager($totalItems, $itemsPerPage, $page) {
  60. $this->doPaging = TRUE;
  61. $this->totalItems = $totalItems;
  62. $this->itemsPerPage = $itemsPerPage;
  63. $this->page = $page;
  64. }
  65. /**
  66. * Adds a new member to this resource if it is a collection.
  67. *
  68. * @param $member
  69. * A TripalWebServiceResource member whose type is the same as this
  70. * resource
  71. */
  72. public function addMember($member) {
  73. // Make sure the $servcie provides is a TripalWebServcie class.
  74. if (!is_a($member, 'TripalWebServiceResource')) {
  75. throw new Exception("Cannot add a new member to this resource collection as it is not a TripalWebServiceResource.");
  76. }
  77. $this->members[] = $member;
  78. }
  79. /**
  80. * @see TripalWebServiceResource::setType()
  81. */
  82. public function setType($type) {
  83. throw new Exception("The type for a Collection can only be collection.");
  84. }
  85. /**
  86. * Retrieves the data section of the resource.
  87. *
  88. * The JSON-LD response constists of two sections the '@context' section
  89. * and the data section. This function only returns the data section
  90. * for this resource
  91. *
  92. * @return
  93. * An associative array containing the data section of the response.
  94. */
  95. public function getData() {
  96. $data = $this->data;
  97. $data['totalItems'] = 0;
  98. if ($this->doPaging == TRUE) {
  99. $data['totalItems'] = $this->totalItems;
  100. $total_pages = ceil($this->totalItems / $this->itemsPerPage);
  101. $page = $this->page;
  102. $limit = $this->itemsPerPage;
  103. if ($this->totalItems > 0) {
  104. $data['view'] = array(
  105. '@id' => $this->service_path . '?' . implode('&', array_merge(array("page=$page", "limit=$limit"))),
  106. '@type' => 'PartialCollectionView',
  107. 'first' => $this->service_path . '?' . implode('&', array_merge(array("page=1", "limit=$limit"))),
  108. 'last' => $this->service_path . '?' . implode('&', array_merge(array("page=$total_pages", "limit=$limit"))),
  109. );
  110. $prev = $page - 1;
  111. $next = $page + 1;
  112. if ($prev > 0) {
  113. $data['view']['previous'] = $this->service_path .'?' . implode('&', array("page=$prev", "limit=$limit"));
  114. }
  115. if ($next < $total_pages) {
  116. $data['view']['next'] = $this->service_path . '?' . implode('&', array("page=$next", "limit=$limit"));
  117. }
  118. }
  119. }
  120. else {
  121. $data['totalItems'] = count($this->members);
  122. }
  123. $member_data = array();
  124. foreach ($this->members as $key => $member) {
  125. $member_data[] = $member->getData();
  126. }
  127. $data['members'] = $member_data;
  128. // If paging of this collection is enabled then add the pager control links.
  129. return $data;
  130. }
  131. /**
  132. * Retrieves the data section of the resource.
  133. *
  134. * The JSON-LD response constists of two sections the '@context' section
  135. * and the data section. This function only returns the data section
  136. * for this resource
  137. *
  138. * @return
  139. * An associative array containing the data section of the response.
  140. */
  141. public function getContext() {
  142. if ($this->doPaging == TRUE) {
  143. $this->addContextItem('view', 'hydra:PartialCollectionView');
  144. }
  145. $context = $this->context;
  146. foreach ($this->members as $key => $member) {
  147. $citems = $member->getContext();
  148. foreach ($citems as $key => $val) {
  149. $context[$key] = $val;
  150. }
  151. }
  152. return $context;
  153. }
  154. }