Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix sorting and order for account lists.
James Cole69d839997a1e
diff --git a/app/Api/V2/Controllers/Model/Account/IndexController.php b/app/Api/V2/Controllers/Model/Account/IndexController.phpindex 73008ebbd2..23e8c2fc0c 100644--- a/app/Api/V2/Controllers/Model/Account/IndexController.php+++ b/app/Api/V2/Controllers/Model/Account/IndexController.php@@ -33,7 +33,7 @@ use Illuminate\Pagination\LengthAwarePaginator;class IndexController extends Controller{- public const string RESOURCE_KEY = 'accounts';+ public const string RESOURCE_KEY = 'accounts';private AccountRepositoryInterface $repository;protected array $acceptedRoles = [UserRoleEnum::READ_ONLY, UserRoleEnum::MANAGE_TRANSACTIONS];@@ -48,7 +48,7 @@ class IndexController extends Controllerfunction ($request, $next) {$this->repository = app(AccountRepositoryInterface::class);// new way of user group validation- $userGroup = $this->validateUserGroup($request);+ $userGroup = $this->validateUserGroup($request);$this->repository->setUserGroup($userGroup);return $next($request);@@ -63,23 +63,32 @@ class IndexController extends Controllerpublic function index(IndexRequest $request): JsonResponse{$this->repository->resetAccountOrder();- $types = $request->getAccountTypes();- $sorting = $request->getSortInstructions('accounts');- $filters = $request->getFilterInstructions('accounts');- $accounts = $this->repository->getAccountsByType($types, $sorting, $filters);- $pageSize = $this->parameters->get('limit');- $count = $accounts->count();- $accounts = $accounts->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);+ $types = $request->getAccountTypes();+ $sorting = $request->getSortInstructions('accounts');+ $filters = $request->getFilterInstructions('accounts');+ $accounts = $this->repository->getAccountsByType($types, $sorting, $filters);+ $pageSize = $this->parameters->get('limit');+ $count = $accounts->count();++ // depending on the sort parameters, this list must not be split, because the+ // order is calculated in the account transformer and by that time it's too late.+ $first = array_key_first($sorting);+ $disablePagination = in_array($first, ['last_activity', 'balance_difference'], true);+ if (!$disablePagination) {+ $accounts = $accounts->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);+ }$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));$transformer = new AccountTransformer();+ $this->parameters->set('disablePagination', $disablePagination);+ $this->parameters->set('pageSize', $pageSize);$this->parameters->set('sort', $sorting);+$this->parameters->set('filters', $filters);$transformer->setParameters($this->parameters); // give params to transformerreturn response()->json($this->jsonApiList('accounts', $paginator, $transformer))- ->header('Content-Type', self::CONTENT_TYPE)- ;+ ->header('Content-Type', self::CONTENT_TYPE);}}diff --git a/app/Transformers/V2/AccountTransformer.php b/app/Transformers/V2/AccountTransformer.phpindex d51da4da3b..db5f2065c5 100644--- a/app/Transformers/V2/AccountTransformer.php+++ b/app/Transformers/V2/AccountTransformer.php@@ -63,6 +63,10 @@ class AccountTransformer extends AbstractTransformer$this->convertedBalances = [];$this->balanceDifferences = [];+ // first collect all the "heavy" stuff that relies on ALL data to be present.+ // get last activity:+ $this->getLastActivity($objects);+// get balances of all accounts$this->getMetaBalances($objects);@@ -75,17 +79,21 @@ class AccountTransformer extends AbstractTransformer// get account types:$this->collectAccountTypes($objects);- // get last activity:- $this->getLastActivity($objects);-// add balance differenceif (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {$this->getBalanceDifference($objects, $this->parameters->get('start'), $this->parameters->get('end'));}- // get object groups"+ // get object groups$this->getObjectGroups($objects);+ // if pagination is disabled, do it now:+ if(true === $this->parameters->get('disablePagination')) {+ $page = (int) $this->parameters->get('page');+ $size = (int) $this->parameters->get('pageSize');+ $objects = $objects->slice(($page-1) * $size, $size);+ }+return $this->sortAccounts($objects);}diff --git a/resources/lang/en_US/email.php b/resources/lang/en_US/email.phpindex 05ee4907db..7c0ef0d32e 100644--- a/resources/lang/en_US/email.php+++ b/resources/lang/en_US/email.php@@ -111,7 +111,7 @@ return ['error_ip' => 'The IP address related to this error is: :ip','error_url' => 'URL is: :url','error_user_agent' => 'User agent: :userAgent',- 'error_stacktrace' => 'The full stacktrace is below. If you think this is a bug in Firefly III, you can forward this message to <a href="mailto:[email protected]?subject=BUG!">[email protected]</a>. This can help fix the bug you just encountered.',+ 'error_stacktrace' => 'The full stacktrace is below. If you think this is a bug in Firefly III, you can forward this message to <a href="mailto:[email protected]?subject=I%20found%20a%20bug!">[email protected]</a>. This can help fix the bug you just encountered.','error_github_html' => 'If you prefer, you can also open a new issue on <a href="https://github.com/firefly-iii/firefly-iii/issues">GitHub</a>.','error_github_text' => 'If you prefer, you can also open a new issue on https://github.com/firefly-iii/firefly-iii/issues.','error_stacktrace_below' => 'The full stacktrace is below:',