summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/api/sharees.php
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2015-08-13 10:57:08 +0200
committerJoas Schilling <nickvergessen@owncloud.com>2015-08-26 11:54:25 +0200
commita0ab7a257858b305434255df2f33ef4b323ac81d (patch)
tree79065827343d1371e57f9cf89e2299a4ce5faac3 /apps/files_sharing/api/sharees.php
parentc6ed40c9f891454d8f9ceeb67c45ff3d8ce22537 (diff)
downloadnextcloud-server-a0ab7a257858b305434255df2f33ef4b323ac81d.tar.gz
nextcloud-server-a0ab7a257858b305434255df2f33ef4b323ac81d.zip
Add all possible links next, prev, first and last
Diffstat (limited to 'apps/files_sharing/api/sharees.php')
-rw-r--r--apps/files_sharing/api/sharees.php54
1 files changed, 39 insertions, 15 deletions
diff --git a/apps/files_sharing/api/sharees.php b/apps/files_sharing/api/sharees.php
index de5f9c8cbf4..714b4950c59 100644
--- a/apps/files_sharing/api/sharees.php
+++ b/apps/files_sharing/api/sharees.php
@@ -285,7 +285,6 @@ class Sharees {
//Pagination
$start = ($page - 1) * $perPage;
- $end = $page * $perPage;
$total = sizeof($sharees);
$sharees = array_slice($sharees, $start, $perPage);
@@ -294,18 +293,16 @@ class Sharees {
$response->setTotalItems($total);
$response->setItemsPerPage($perPage);
- if ($total > $end) {
- $params = [
- 'search' => $search,
- 'itemType' => $itemType,
- 'existingShares' => $existingShares,
- 'shareType' => $shareTypes,
- 'page' => $page + 1,
- 'limit' => $perPage,
- ];
+ $links = $this->getPaginationLinks($page, $total, [
+ 'search' => $search,
+ 'itemType' => $itemType,
+ 'existingShares' => $existingShares,
+ 'shareType' => $shareTypes,
+ 'limit' => $perPage,
+ ]);
- $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?' . http_build_query($params);
- $response->addHeader('Link', '<' . $url . '> rel="next"');
+ if (!empty($links)) {
+ $response->addHeader('Link', implode(', ', $links));
}
return $response;
@@ -319,10 +316,37 @@ class Sharees {
* @return array
*/
protected function filterSharees($potentialSharees, $existingSharees) {
- $sharees = array_map(function ($sharee) use ($existingSharees) {
+ $sharees = array_filter($potentialSharees, function ($sharee) use ($existingSharees) {
return in_array($sharee['value']['shareWith'], $existingSharees) ? null : $sharee;
- }, $potentialSharees);
+ });
- return array_filter($sharees);
+ return $sharees;
+ }
+
+ /**
+ * Generates a bunch of pagination links for the current page
+ *
+ * @param int $page Current page
+ * @param int $total Number of total items that need to be paginated
+ * @param array $params Parameters for the URL
+ * @return array
+ */
+ protected function getPaginationLinks($page, $total, array $params) {
+ $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
+
+ $links = [];
+ if ($page > 1) {
+ $params['page'] = 1;
+ $links[] = '<' . $url . http_build_query($params) . '>; rel="first"';
+ $params['page'] = $page - 1;
+ $links[] = '<' . $url . http_build_query($params) . '>; rel="prev"';
+ }
+ if ($page * $params['limit'] < $total) {
+ $params['page'] = $page + 1;
+ $links[] = '<' . $url . http_build_query($params) . '>; rel="next"';
+ $params['page'] = ceil($total / $params['limit']);
+ $links[] = '<' . $url . http_build_query($params) . '>; rel="last"';
+ }
+ return $links;
}
}