From ef48a79d0c944d3e2d7bdc823f47211d548b9679 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 21 Apr 2016 10:23:19 +0200 Subject: [PATCH] Take page size into account. [skip ci] --- app/Http/Controllers/BillController.php | 5 +++- app/Repositories/Bill/BillRepository.php | 27 ++++++++++++------- .../Bill/BillRepositoryInterface.php | 8 ++++-- resources/views/bills/show.twig | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index 87be25b6e6..2d644726dc 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -174,7 +174,10 @@ class BillController extends Controller */ public function show(BillRepositoryInterface $repository, Bill $bill) { - $journals = $repository->getJournals($bill); + $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); + $pageSize = Preferences::get('transactionPageSize', 50)->data; + $journals = $repository->getJournals($bill, $page, $pageSize); + $journals->setPath('/bills/show/' . $bill->id); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $hideBill = true; $subTitle = e($bill->name); diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index b371d9df23..44bafbd58b 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -13,6 +13,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\User; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinClause; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Navigation; @@ -77,7 +78,7 @@ class BillRepository implements BillRepositoryInterface ->get( [ 'bills.*', - DB::raw('((`bills`.`amount_min` + `bills`.`amount_max`) / 2) as `expectedAmount`'), + DB::raw('((`bills`.`amount_min` + `bills`.`amount_max`) / 2) as `expectedAmount`'), ] )->sortBy('name'); @@ -304,18 +305,24 @@ class BillRepository implements BillRepositoryInterface * * @param Bill $bill * - * @return Collection + * @param int $page + * @param int $pageSize + * + * @return LengthAwarePaginator|Collection */ - public function getJournals(Bill $bill): Collection + public function getJournals(Bill $bill, int $page, int $pageSize = 50): LengthAwarePaginator { - $set = $bill->transactionjournals() - ->expanded() - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.order', 'ASC') - ->orderBy('transaction_journals.id', 'DESC') - ->get(TransactionJournal::QUERYFIELDS); + $offset = ($page - 1) * $pageSize; + $query = $bill->transactionjournals() + ->expanded() + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC'); + $count = $query->count(); + $set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::QUERYFIELDS); + $paginator = new LengthAwarePaginator($set, $count, $pageSize, $page); - return $set; + return $paginator; } /** diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index 948e0c67fd..e1667fe83e 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -6,6 +6,7 @@ namespace FireflyIII\Repositories\Bill; use Carbon\Carbon; use FireflyIII\Models\Bill; use FireflyIII\Models\TransactionJournal; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; /** @@ -97,9 +98,12 @@ interface BillRepositoryInterface /** * @param Bill $bill * - * @return Collection + * @param int $page + * @param int $pageSize + * + * @return LengthAwarePaginator */ - public function getJournals(Bill $bill): Collection; + public function getJournals(Bill $bill, int $page, int $pageSize = 50): LengthAwarePaginator; /** * Get all journals that were recorded on this bill between these dates. diff --git a/resources/views/bills/show.twig b/resources/views/bills/show.twig index 30ffc95380..254a5efee6 100644 --- a/resources/views/bills/show.twig +++ b/resources/views/bills/show.twig @@ -96,7 +96,7 @@
-

{{ 'connected_journals' }}

+

{{ 'connected_journals'|_ }}

{% include 'list/journals' %}