Skip to content

Commit f69e33b

Browse files
authored
Merge pull request #6948 from ProcessMaker/bugfix/FOUR-16632
Add query-based pagination to tasks controller
2 parents 1d0240d + e9ca6aa commit f69e33b

2 files changed

Lines changed: 5 additions & 60 deletions

File tree

‎ProcessMaker/Http/Controllers/Api/TaskController.php‎

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,47 +122,29 @@ public function index(Request $request, $getTotal = false, User $user = null)
122122

123123
$this->applyForCurrentUser($query, $user);
124124

125-
try {
126-
// Count up the total number of results, but
127-
// watch for PMQL query exceptions and handle
128-
// them if they occur
129-
$totalResultCount = $query->count();
130-
} catch (QueryException $e) {
131-
return $this->handleQueryException($e);
132-
}
133-
134-
// If only the total is being requested (by a Saved Search), send it now
135-
if ($getTotal === true) {
136-
return $totalResultCount;
137-
}
138-
139125
// Apply filter overdue
140126
$query->overdue($request->input('overdue'));
141127

142-
// If we should manually add pagination to the
143-
// query in advance (also used by saved search)
144-
if ($this->isPaginationEnabled()) {
145-
$query->limit($request->input('per_page', 10));
128+
// If only the total is being requested (by a Saved Search), send it now
129+
if ($getTotal === true) {
130+
return $query->count();
146131
}
147132

148133
try {
149-
$response = $query->get();
134+
$response = $query->paginate($request->input('per_page', 10));
150135
} catch (QueryException $e) {
151136
return $this->handleQueryException($e);
152137
}
153138

154139
$response = $this->applyUserFilter($response, $request, $user);
155140

156-
// Map each item through its resource
157-
$response = $this->applyResource($response);
158-
159141
$inOverdueQuery = ProcessRequestToken::query()
160142
->whereIn('id', $response->pluck('id'))
161143
->where('due_at', '<', Carbon::now());
162144

163145
$response->inOverdue = $inOverdueQuery->count();
164146

165-
return new TaskCollection($response, $totalResultCount);
147+
return new TaskCollection($response);
166148
}
167149

168150
/**

‎ProcessMaker/Traits/TaskControllerIndexMethods.php‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,6 @@
1515

1616
trait TaskControllerIndexMethods
1717
{
18-
/**
19-
* Manually enable paginated results from the
20-
* index method()
21-
*
22-
* @return void
23-
*/
24-
public function enableIndexPagination(): void
25-
{
26-
static::$paginate = true;
27-
}
28-
29-
/**
30-
* Determine if pagination was manually set for the
31-
* index() method results
32-
*
33-
* @return bool
34-
*/
35-
public function isPaginationEnabled(): bool
36-
{
37-
return static::$paginate === true;
38-
}
39-
40-
/**
41-
* Used by saved search to paginate the results
42-
* from the index() method
43-
*
44-
* @var bool
45-
*/
46-
protected static bool $paginate = false;
47-
4818
private function indexBaseQuery($request)
4919
{
5020
$query = ProcessRequestToken::with(['processRequest', 'user', 'draft']);
@@ -300,13 +270,6 @@ private function applyUserFilter($response, $request, $user)
300270
return $response;
301271
}
302272

303-
private function applyResource($response)
304-
{
305-
return $response->map(function ($processRequestToken) {
306-
return new Resource($processRequestToken);
307-
});
308-
}
309-
310273
private function applyForCurrentUser($query, $user)
311274
{
312275
if ($user->is_administrator) {

0 commit comments

Comments
 (0)