TripalWebServiceCollection.inc 5.0 KB

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