From be33f6c987f25c5d80d902def385e834693689ca Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 11 Dec 2020 05:17:32 +0100 Subject: [PATCH] Fix pagination. --- app/Helpers/Collector/GroupCollector.php | 52 +++++---- .../Controllers/Account/ShowController.php | 1 + resources/views/v1/currencies/index.twig | 4 +- resources/views/v1/list/accounts.twig | 4 +- resources/views/v1/list/bills.twig | 4 +- resources/views/v1/list/categories.twig | 4 +- resources/views/v1/list/groups.twig | 8 +- .../views/v1/pagination/bootstrap-4.blade.php | 46 ++++++++ .../views/v1/pagination/default.blade.php | 46 ++++++++ .../views/v1/pagination/semantic-ui.blade.php | 36 +++++++ .../pagination/simple-bootstrap-4.blade.php | 27 +++++ .../v1/pagination/simple-default.blade.php | 19 ++++ .../v1/pagination/simple-tailwind.blade.php | 25 +++++ .../views/v1/pagination/tailwind.blade.php | 102 ++++++++++++++++++ resources/views/v1/recurring/index.twig | 4 +- 15 files changed, 349 insertions(+), 33 deletions(-) create mode 100644 resources/views/v1/pagination/bootstrap-4.blade.php create mode 100644 resources/views/v1/pagination/default.blade.php create mode 100644 resources/views/v1/pagination/semantic-ui.blade.php create mode 100644 resources/views/v1/pagination/simple-bootstrap-4.blade.php create mode 100644 resources/views/v1/pagination/simple-default.blade.php create mode 100644 resources/views/v1/pagination/simple-tailwind.blade.php create mode 100644 resources/views/v1/pagination/tailwind.blade.php diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php index aba9652544..99378123c8 100644 --- a/app/Helpers/Collector/GroupCollector.php +++ b/app/Helpers/Collector/GroupCollector.php @@ -165,6 +165,16 @@ class GroupCollector implements GroupCollectorInterface */ public function getGroups(): Collection { + $filterQuery = false; + + // now filter the query according to the page and the limit (if necessary) + if ($filterQuery) { + if (null !== $this->limit && null !== $this->page) { + $offset = ($this->page - 1) * $this->limit; + $this->query->take($this->limit)->skip($offset); + } + } + /** @var Collection $result */ $result = $this->query->get($this->fields); @@ -173,10 +183,12 @@ class GroupCollector implements GroupCollectorInterface $this->total = $collection->count(); // now filter the array according to the page and the limit (if necessary) - if (null !== $this->limit && null !== $this->page) { - $offset = ($this->page - 1) * $this->limit; + if (!$filterQuery) { + if (null !== $this->limit && null !== $this->page) { + $offset = ($this->page - 1) * $this->limit; - return $collection->slice($offset, $this->limit); + return $collection->slice($offset, $this->limit); + } } return $collection; @@ -190,9 +202,10 @@ class GroupCollector implements GroupCollectorInterface public function getPaginatedGroups(): LengthAwarePaginator { $set = $this->getGroups(); - if(0===$this->limit) { + if (0 === $this->limit) { $this->setLimit(50); } + return new LengthAwarePaginator($set, $this->total, $this->limit, $this->page); } @@ -422,6 +435,7 @@ class GroupCollector implements GroupCollectorInterface $this->startQuery(); } + return $this; } @@ -466,7 +480,7 @@ class GroupCollector implements GroupCollectorInterface private function convertToInteger(array $array): array { foreach ($this->integerFields as $field) { - $array[$field] = array_key_exists($field, $array) ? (int) $array[$field] : null; + $array[$field] = array_key_exists($field, $array) ? (int)$array[$field] : null; } return $array; @@ -498,7 +512,7 @@ class GroupCollector implements GroupCollectorInterface ->where( static function (EloquentBuilder $q1) { $q1->where('attachments.attachable_type', TransactionJournal::class); - $q1->where('attachments.uploaded',1); + $q1->where('attachments.uploaded', 1); $q1->orWhereNull('attachments.attachable_type'); } ); @@ -515,7 +529,7 @@ class GroupCollector implements GroupCollectorInterface { $newArray = $newJournal->toArray(); if (array_key_exists('attachment_id', $newArray)) { - $attachmentId = (int) $newJournal['tag_id']; + $attachmentId = (int)$newJournal['tag_id']; $existingJournal['attachments'][$attachmentId] = [ 'id' => $attachmentId, ]; @@ -534,7 +548,7 @@ class GroupCollector implements GroupCollectorInterface { $newArray = $newJournal->toArray(); if (array_key_exists('tag_id', $newArray)) { // assume the other fields are present as well. - $tagId = (int) $newJournal['tag_id']; + $tagId = (int)$newJournal['tag_id']; $tagDate = null; try { @@ -544,7 +558,7 @@ class GroupCollector implements GroupCollectorInterface } $existingJournal['tags'][$tagId] = [ - 'id' => (int) $newArray['tag_id'], + 'id' => (int)$newArray['tag_id'], 'name' => $newArray['tag_name'], 'date' => $tagDate, 'description' => $newArray['tag_description'], @@ -570,21 +584,21 @@ class GroupCollector implements GroupCollectorInterface // make new array $parsedGroup = $this->parseAugmentedJournal($augumentedJournal); $groupArray = [ - 'id' => (int) $augumentedJournal->transaction_group_id, - 'user_id' => (int) $augumentedJournal->user_id, + 'id' => (int)$augumentedJournal->transaction_group_id, + 'user_id' => (int)$augumentedJournal->user_id, 'title' => $augumentedJournal->transaction_group_title, 'transaction_type' => $parsedGroup['transaction_type_type'], 'count' => 1, 'sums' => [], 'transactions' => [], ]; - $journalId = (int) $augumentedJournal->transaction_journal_id; + $journalId = (int)$augumentedJournal->transaction_journal_id; $groupArray['transactions'][$journalId] = $parsedGroup; $groups[$groupId] = $groupArray; continue; } // or parse the rest. - $journalId = (int) $augumentedJournal->transaction_journal_id; + $journalId = (int)$augumentedJournal->transaction_journal_id; if (array_key_exists($journalId, $groups[$groupId]['transactions'])) { @@ -631,9 +645,9 @@ class GroupCollector implements GroupCollectorInterface // convert values to integers: $result = $this->convertToInteger($result); - $result['reconciled'] = 1 === (int) $result['reconciled']; + $result['reconciled'] = 1 === (int)$result['reconciled']; if (isset($augumentedJournal['tag_id'])) { // assume the other fields are present as well. - $tagId = (int) $augumentedJournal['tag_id']; + $tagId = (int)$augumentedJournal['tag_id']; $tagDate = null; try { $tagDate = Carbon::parse($augumentedJournal['tag_date']); @@ -642,7 +656,7 @@ class GroupCollector implements GroupCollectorInterface } $result['tags'][$tagId] = [ - 'id' => (int) $result['tag_id'], + 'id' => (int)$result['tag_id'], 'name' => $result['tag_name'], 'date' => $tagDate, 'description' => $result['tag_description'], @@ -651,7 +665,7 @@ class GroupCollector implements GroupCollectorInterface // also merge attachments: if (isset($augumentedJournal['attachment_id'])) { - $attachmentId = (int) $augumentedJournal['attachment_id']; + $attachmentId = (int)$augumentedJournal['attachment_id']; $result['attachments'][$attachmentId] = [ 'id' => $attachmentId, ]; @@ -674,7 +688,7 @@ class GroupCollector implements GroupCollectorInterface foreach ($groups as $groudId => $group) { /** @var array $transaction */ foreach ($group['transactions'] as $transaction) { - $currencyId = (int) $transaction['currency_id']; + $currencyId = (int)$transaction['currency_id']; // set default: if (!isset($groups[$groudId]['sums'][$currencyId])) { @@ -687,7 +701,7 @@ class GroupCollector implements GroupCollectorInterface $groups[$groudId]['sums'][$currencyId]['amount'] = bcadd($groups[$groudId]['sums'][$currencyId]['amount'], $transaction['amount'] ?? '0'); if (null !== $transaction['foreign_amount'] && null !== $transaction['foreign_currency_id']) { - $currencyId = (int) $transaction['foreign_currency_id']; + $currencyId = (int)$transaction['foreign_currency_id']; // set default: if (!isset($groups[$groudId]['sums'][$currencyId])) { diff --git a/app/Http/Controllers/Account/ShowController.php b/app/Http/Controllers/Account/ShowController.php index fee7b826b0..b5bb85941d 100644 --- a/app/Http/Controllers/Account/ShowController.php +++ b/app/Http/Controllers/Account/ShowController.php @@ -125,6 +125,7 @@ class ShowController extends Controller ->setPage($page)->withAccountInformation()->withCategoryInformation() ->setRange($start, $end); $groups = $collector->getPaginatedGroups(); + $groups->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')])); $showAll = false; $balance = app('steam')->balance($account, $end); diff --git a/resources/views/v1/currencies/index.twig b/resources/views/v1/currencies/index.twig index 09ee3ad63b..3f56d1a294 100644 --- a/resources/views/v1/currencies/index.twig +++ b/resources/views/v1/currencies/index.twig @@ -21,7 +21,7 @@

{% if currencies|length > 0 %}
- {{ currencies.render()|raw }} + {{ currencies.links('pagination.bootstrap-4')|raw }}
@@ -87,7 +87,7 @@
- {{ currencies.render()|raw }} + {{ currencies.links('pagination.bootstrap-4')|raw }}
{% endif %} diff --git a/resources/views/v1/list/accounts.twig b/resources/views/v1/list/accounts.twig index 32e486fb56..0d0482e6eb 100644 --- a/resources/views/v1/list/accounts.twig +++ b/resources/views/v1/list/accounts.twig @@ -1,5 +1,5 @@
- {{ accounts.render()|raw }} + {{ accounts.links('pagination.bootstrap-4')|raw }}
@@ -104,5 +104,5 @@
- {{ accounts.render()|raw }} + {{ accounts.links('pagination.bootstrap-4')|raw }}
diff --git a/resources/views/v1/list/bills.twig b/resources/views/v1/list/bills.twig index 2f59864bd0..d8c0c386b4 100644 --- a/resources/views/v1/list/bills.twig +++ b/resources/views/v1/list/bills.twig @@ -1,5 +1,5 @@
- {{ paginator.render()|raw }} + {{ paginator.links('pagination.bootstrap-4')|raw }}
@@ -206,5 +206,5 @@
- {{ paginator.render()|raw }} + {{ paginator.links('pagination.bootstrap-4')|raw }}
diff --git a/resources/views/v1/list/categories.twig b/resources/views/v1/list/categories.twig index a11ec8efc8..96a6ad827d 100644 --- a/resources/views/v1/list/categories.twig +++ b/resources/views/v1/list/categories.twig @@ -1,5 +1,5 @@
- {{ categories.render()|raw }} + {{ categories.links('pagination.bootstrap-4')|raw }}
@@ -43,5 +43,5 @@
- {{ categories.render()|raw }} + {{ categories.links('pagination.bootstrap-4')|raw }}
diff --git a/resources/views/v1/list/groups.twig b/resources/views/v1/list/groups.twig index 2663cb406d..c3a39a591d 100644 --- a/resources/views/v1/list/groups.twig +++ b/resources/views/v1/list/groups.twig @@ -2,9 +2,9 @@ {% if showCategory or showBudget %} - {{ groups.render()|raw }} + {{ groups.links('pagination.bootstrap-4')|raw }} {% else %} - {{ groups.render()|raw }} + {{ groups.links('pagination.bootstrap-4')|raw }} {% endif %} @@ -279,9 +279,9 @@ {% if showCategory or showBudget %} - {{ groups.render()|raw }} + {{ groups.links('pagination.bootstrap-4')|raw }} {% else %} - {{ groups.render()|raw }} + {{ groups.links('pagination.bootstrap-4')|raw }} {% endif %} diff --git a/resources/views/v1/pagination/bootstrap-4.blade.php b/resources/views/v1/pagination/bootstrap-4.blade.php new file mode 100644 index 0000000000..63c6f56b59 --- /dev/null +++ b/resources/views/v1/pagination/bootstrap-4.blade.php @@ -0,0 +1,46 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/default.blade.php b/resources/views/v1/pagination/default.blade.php new file mode 100644 index 0000000000..0db70b5627 --- /dev/null +++ b/resources/views/v1/pagination/default.blade.php @@ -0,0 +1,46 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/semantic-ui.blade.php b/resources/views/v1/pagination/semantic-ui.blade.php new file mode 100644 index 0000000000..ef0dbb184c --- /dev/null +++ b/resources/views/v1/pagination/semantic-ui.blade.php @@ -0,0 +1,36 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/simple-bootstrap-4.blade.php b/resources/views/v1/pagination/simple-bootstrap-4.blade.php new file mode 100644 index 0000000000..4bb491742a --- /dev/null +++ b/resources/views/v1/pagination/simple-bootstrap-4.blade.php @@ -0,0 +1,27 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/simple-default.blade.php b/resources/views/v1/pagination/simple-default.blade.php new file mode 100644 index 0000000000..36bdbc18c6 --- /dev/null +++ b/resources/views/v1/pagination/simple-default.blade.php @@ -0,0 +1,19 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/simple-tailwind.blade.php b/resources/views/v1/pagination/simple-tailwind.blade.php new file mode 100644 index 0000000000..6872cca360 --- /dev/null +++ b/resources/views/v1/pagination/simple-tailwind.blade.php @@ -0,0 +1,25 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/pagination/tailwind.blade.php b/resources/views/v1/pagination/tailwind.blade.php new file mode 100644 index 0000000000..2dd4d0ef33 --- /dev/null +++ b/resources/views/v1/pagination/tailwind.blade.php @@ -0,0 +1,102 @@ +@if ($paginator->hasPages()) + +@endif diff --git a/resources/views/v1/recurring/index.twig b/resources/views/v1/recurring/index.twig index e4ad78183d..b59f02b817 100644 --- a/resources/views/v1/recurring/index.twig +++ b/resources/views/v1/recurring/index.twig @@ -32,7 +32,7 @@
- {{ paginator.render()|raw }} + {{ paginator.links('pagination.bootstrap-4')|raw }}
@@ -140,7 +140,7 @@
- {{ paginator.render()|raw }} + {{ paginator.links('pagination.bootstrap-4')|raw }}