Skip to content

Commit

Permalink
updated getPagination method on MsGraph.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dcblogdev committed Aug 28, 2021
1 parent d3fc1fc commit a564b27
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
37 changes: 37 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,41 @@ MsGraphAdmin::events()->userid($userId)->find($eventId);
MsGraphAdmin::events()->userid($userId)->store($data);
MsGraphAdmin::events()->userid($userId)->update($data);
MsGraphAdmin::events()->userid($userId)->delete($data);
```

### 3.1.0

Changed getPagination() to return array containing only previous and next page numbers.

This method needs the data but also the total number of records, the limit ($top) and the offset ($skip)

```php
$limit = 5;
$skip = request('next', 0);

$messageQueryParams = [
"\$orderby" => "displayName",
"\$count" => "true",
"\$skip" => $skip,
"\$top" => $limit,
];

$contacts = MsGraph::get('me/contacts?'.http_build_query($messageQueryParams));
$total = $contacts['@odata.count'] ?? 0;

$response = MsGraph::getPagination($contacts, $total, $limit, $skip);
$previous = $response['previous'];
$next = $response['next'];
```

The in a view the previous and next links can be displayed:

```php
@if (request('next') > 0)
<a href='{{ url()->current().'?next='.$previous }}'>Previous Page</a>
@endif

@if ($next != 0)
<a href='{{ url()->current().'?next='.$next }}'>Next Page</a>
@endif
```
44 changes: 28 additions & 16 deletions src/MsGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,34 +291,46 @@ protected function guzzle($type, $request, $data = [], $headers = [], $id = null
}

/**
* return array containing total, top and skip params
* return array containing previous and next page counts
* @param $data array
* @param $top integer
* @param $total array
* @param $limit integer
* @param $skip integer
* @return array
*/
public function getPagination($data, $top, $skip)
public function getPagination(array $data, int $total, int $limit, int $skip)
{
if (! is_array($data))
{
dd($data);
}

$total = isset($data['@odata.count']) ? $data['@odata.count'] : 0;
$previous = 0;
$next = 0;

if (isset($data['@odata.nextLink'])) {
$parts = explode('skip=', $data['@odata.nextLink']);

if (isset($parts[1])) {
$previous = $parts[1] - $limit;
$next = $parts[1];
}

if ($previous < 0) {
$previous = 0;
}

$parts = parse_url($data['@odata.nextLink']);
parse_str($parts['query'], $query);
if ($next == $total) {
$next = 0;
}
}

if ($total > $limit) {
$previous = $skip - $limit;
}

$top = isset($query['$top']) ? $query['$top'] : 0;
$skip = isset($query['$skip']) ? $query['$skip'] : 0;
if ($previous < 0) {
$previous = 0;
}

return [
'total' => $total,
'top' => $top,
'skip' => $skip
'previous' => $previous,
'next' => $next
];
}
}

0 comments on commit a564b27

Please sign in to comment.