TripalWebServiceCollection.inc 4.8 KB

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