Skip to content

Firefly III: a personal finances manager

Remove view generation and put in trait.

James Coleec2463a3ba1b
diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php
index 5d71f272fc..25dfe38cb4 100644
--- a/app/Http/Controllers/Popup/ReportController.php
+++ b/app/Http/Controllers/Popup/ReportController.php
@@ -22,17 +22,11 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Popup;
-use FireflyIII\Helpers\Collection\BalanceLine;
-use FireflyIII\Helpers\Report\PopupReportInterface;
use FireflyIII\Http\Controllers\Controller;
-use FireflyIII\Repositories\Account\AccountRepositoryInterface;
-use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
-use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Support\Http\Controllers\RenderPartialViews;
use FireflyIII\Support\Http\Controllers\RequestInformation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
-use Log;
-use Throwable;
/**
* Class ReportController.
@@ -41,40 +35,7 @@ use Throwable;
*/
class ReportController extends Controller
{
- use RequestInformation;
- /** @var AccountRepositoryInterface The account repository */
- private $accountRepository;
- /** @var BudgetRepositoryInterface The budget repository */
- private $budgetRepository;
- /** @var CategoryRepositoryInterface The category repository */
- private $categoryRepository;
- /** @var PopupReportInterface Various helper functions. */
- private $popupHelper;
-
- /**
- * ReportController constructor.
- */
- public function __construct()
- {
- parent::__construct();
- $this->middleware(
- function ($request, $next) {
- /** @var AccountRepositoryInterface accountRepository */
- $this->accountRepository = app(AccountRepositoryInterface::class);
-
- /** @var BudgetRepositoryInterface budgetRepository */
- $this->budgetRepository = app(BudgetRepositoryInterface::class);
-
- /** @var CategoryRepositoryInterface categoryRepository */
- $this->categoryRepository = app(CategoryRepositoryInterface::class);
-
- /** @var PopupReportInterface popupHelper */
- $this->popupHelper = app(PopupReportInterface::class);
-
- return $next($request);
- }
- );
- }
+ use RequestInformation, RenderPartialViews;
/**
* Generate popup view.
@@ -116,147 +77,5 @@ class ReportController extends Controller
return response()->json(['html' => $html]);
}
- /**
- * View for balance row.
- *
- * @param array $attributes
- *
- * @return string
- *
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- protected function balanceAmount(array $attributes): string // generate view for report.
- {
- $role = (int)$attributes['role'];
- $budget = $this->budgetRepository->findNull((int)$attributes['budgetId']);
- $account = $this->accountRepository->findNull((int)$attributes['accountId']);
-
-
- switch (true) {
- case BalanceLine::ROLE_DEFAULTROLE === $role && null !== $budget && null !== $account:
- // normal row with a budget:
- $journals = $this->popupHelper->balanceForBudget($budget, $account, $attributes);
- break;
- case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget && null !== $account:
- // normal row without a budget:
- $journals = $this->popupHelper->balanceForNoBudget($account, $attributes);
- $budget->name = (string)trans('firefly.no_budget');
- break;
- case BalanceLine::ROLE_TAGROLE === $role:
- // row with tag info.
- return 'Firefly cannot handle this type of info-button (BalanceLine::TagRole)';
- }
- try {
- $view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Could not render: %s', $e->getMessage()));
- $view = 'Firefly III could not render the view. Please see the log files.';
- }
-
- return $view;
- }
-
- /**
- * View for spent in a single budget.
- *
- * @param array $attributes
- *
- * @return string
- */
- protected function budgetSpentAmount(array $attributes): string // generate view for report.
- {
- $budget = $this->budgetRepository->findNull((int)$attributes['budgetId']);
- if (null === $budget) {
- return 'This is an unknown budget. Apologies.';
- }
- $journals = $this->popupHelper->byBudget($budget, $attributes);
- try {
- $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Could not render: %s', $e->getMessage()));
- $view = 'Firefly III could not render the view. Please see the log files.';
- }
-
- return $view;
- }
-
- /**
- * View for transactions in a category.
- *
- * @param array $attributes
- *
- * @return string
- */
- protected function categoryEntry(array $attributes): string // generate view for report.
- {
- $category = $this->categoryRepository->findNull((int)$attributes['categoryId']);
-
- if (null === $category) {
- return 'This is an unknown category. Apologies.';
- }
-
- $journals = $this->popupHelper->byCategory($category, $attributes);
- try {
- $view = view('popup.report.category-entry', compact('journals', 'category'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Could not render: %s', $e->getMessage()));
- $view = 'Firefly III could not render the view. Please see the log files.';
- }
-
- return $view;
- }
-
- /**
- * Returns all the expenses that went to the given expense account.
- *
- * @param array $attributes
- *
- * @return string
- */
- protected function expenseEntry(array $attributes): string // generate view for report.
- {
- $account = $this->accountRepository->findNull((int)$attributes['accountId']);
-
- if (null === $account) {
- return 'This is an unknown account. Apologies.';
- }
-
- $journals = $this->popupHelper->byExpenses($account, $attributes);
- try {
- $view = view('popup.report.expense-entry', compact('journals', 'account'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Could not render: %s', $e->getMessage()));
- $view = 'Firefly III could not render the view. Please see the log files.';
- }
-
- return $view;
- }
-
- /**
- * Returns all the incomes that went to the given asset account.
- *
- * @param array $attributes
- *
- * @return string
- */
- protected function incomeEntry(array $attributes): string // generate view for report.
- {
- $account = $this->accountRepository->findNull((int)$attributes['accountId']);
-
- if (null === $account) {
- return 'This is an unknown category. Apologies.';
- }
-
- $journals = $this->popupHelper->byIncome($account, $attributes);
- try {
- $view = view('popup.report.income-entry', compact('journals', 'account'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Could not render: %s', $e->getMessage()));
- $view = 'Firefly III could not render the view. Please see the log files.';
- }
-
- return $view;
- }
-
}
diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php
index 22b205bb66..1af1553e60 100644
--- a/app/Http/Controllers/ReportController.php
+++ b/app/Http/Controllers/ReportController.php
@@ -28,15 +28,12 @@ use FireflyIII\Generator\Report\ReportGeneratorFactory;
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Http\Requests\ReportFormRequest;
use FireflyIII\Models\AccountType;
-use FireflyIII\Models\Tag;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
-use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
-use FireflyIII\Repositories\Tag\TagRepositoryInterface;
+use FireflyIII\Support\Http\Controllers\RenderPartialViews;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Log;
-use Throwable;
/**
* Class ReportController.
@@ -45,6 +42,8 @@ use Throwable;
*/
class ReportController extends Controller
{
+ use RenderPartialViews;
+
/** @var ReportHelperInterface Helper interface. */
protected $helper;
@@ -418,112 +417,5 @@ class ReportController extends Controller
return $generator->generate();
}
- /**
- * Get options for account report.
- *
- * @return string
- */
- protected function accountReportOptions(): string // render a view
- {
- /** @var AccountRepositoryInterface $repository */
- $repository = app(AccountRepositoryInterface::class);
- $expense = $repository->getActiveAccountsByType([AccountType::EXPENSE]);
- $revenue = $repository->getActiveAccountsByType([AccountType::REVENUE]);
- $set = new Collection;
- $names = $revenue->pluck('name')->toArray();
- foreach ($expense as $exp) {
- if (\in_array($exp->name, $names, true)) {
- $set->push($exp);
- }
- }
- try {
- $result = view('reports.options.account', compact('set'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
-
- return $result;
- }
-
- /**
- * Get options for budget report.
- *
- * @return string
- */
- protected function budgetReportOptions(): string // render a view
- {
- /** @var BudgetRepositoryInterface $repository */
- $repository = app(BudgetRepositoryInterface::class);
- $budgets = $repository->getBudgets();
- try {
- $result = view('reports.options.budget', compact('budgets'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
- return $result;
- }
-
- /**
- * Get options for category report.
- *
- * @return string
- */
- protected function categoryReportOptions(): string // render a view
- {
- /** @var CategoryRepositoryInterface $repository */
- $repository = app(CategoryRepositoryInterface::class);
- $categories = $repository->getCategories();
- try {
- $result = view('reports.options.category', compact('categories'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Cannot render reports.options.category: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
-
- return $result;
- }
-
- /**
- * Get options for default report.
- *
- * @return string
- */
- protected function noReportOptions(): string // render a view
- {
- try {
- $result = view('reports.options.no-options')->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
-
- return $result;
- }
-
- /**
- * Get options for tag report.
- *
- * @return string
- */
- protected function tagReportOptions(): string // render a view
- {
- /** @var TagRepositoryInterface $repository */
- $repository = app(TagRepositoryInterface::class);
- $tags = $repository->get()->sortBy(
- function (Tag $tag) {
- return $tag->tag;
- }
- );
- try {
- $result = view('reports.options.tag', compact('tags'))->render();
- } catch (Throwable $e) {
- Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
- $result = 'Could not render view.';
- }
-
- return $result;
- }
}
diff --git a/app/Support/Http/Controllers/RenderPartialViews.php b/app/Support/Http/Controllers/RenderPartialViews.php
new file mode 100644
index 0000000000..7d4abdc409
--- /dev/null
+++ b/app/Support/Http/Controllers/RenderPartialViews.php
@@ -0,0 +1,328 @@
+<?php
+/**
+ * RenderPartialViews.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Support\Http\Controllers;
+
+
+use FireflyIII\Helpers\Collection\BalanceLine;
+use FireflyIII\Helpers\Report\PopupReportInterface;
+use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Tag;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Repositories\Tag\TagRepositoryInterface;
+use Illuminate\Support\Collection;
+use Log;
+use Throwable;
+
+/**
+ * Trait RenderPartialViews
+ *
+ * @package FireflyIII\Support\Http\Controllers
+ */
+trait RenderPartialViews
+{
+ /**
+ * Get options for account report.
+ *
+ * @return string
+ */
+ protected function accountReportOptions(): string // render a view
+ {
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class);
+ $expense = $repository->getActiveAccountsByType([AccountType::EXPENSE]);
+ $revenue = $repository->getActiveAccountsByType([AccountType::REVENUE]);
+ $set = new Collection;
+ $names = $revenue->pluck('name')->toArray();
+ foreach ($expense as $exp) {
+ if (\in_array($exp->name, $names, true)) {
+ $set->push($exp);
+ }
+ }
+ try {
+ $result = view('reports.options.account', compact('set'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
+ $result = 'Could not render view.';
+ }
+
+ return $result;
+ }
+
+ /**
+ * View for balance row.
+ *
+ * @param array $attributes
+ *
+ * @return string
+ *
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
+ */
+ protected function balanceAmount(array $attributes): string // generate view for report.
+ {
+ $role = (int)$attributes['role'];
+ /** @var BudgetRepositoryInterface $budgetRepository */
+ $budgetRepository = app(BudgetRepositoryInterface::class);
+
+ /** @var AccountRepositoryInterface $accountRepository */
+ $accountRepository = app(AccountRepositoryInterface::class);
+
+ /** @var PopupReportInterface $popupHelper */
+ $popupHelper = app(PopupReportInterface::class);
+
+ $budget = $budgetRepository->findNull((int)$attributes['budgetId']);
+ $account = $accountRepository->findNull((int)$attributes['accountId']);
+
+
+ switch (true) {
+ case BalanceLine::ROLE_DEFAULTROLE === $role && null !== $budget && null !== $account:
+ // normal row with a budget:
+ $journals = $popupHelper->balanceForBudget($budget, $account, $attributes);
+ break;
+ case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget && null !== $account:
+ // normal row without a budget:
+ $journals = $popupHelper->balanceForNoBudget($account, $attributes);
+ $budget->name = (string)trans('firefly.no_budget');
+ break;
+ case BalanceLine::ROLE_TAGROLE === $role:
+ // row with tag info.
+ return 'Firefly cannot handle this type of info-button (BalanceLine::TagRole)';
+ }
+ try {
+ $view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Could not render: %s', $e->getMessage()));
+ $view = 'Firefly III could not render the view. Please see the log files.';
+ }
+
+ return $view;
+ }
+
+ /**
+ * Get options for budget report.
+ *
+ * @return string
+ */
+ protected function budgetReportOptions(): string // render a view
+ {
+ /** @var BudgetRepositoryInterface $repository */
+ $repository = app(BudgetRepositoryInterface::class);
+ $budgets = $repository->getBudgets();
+ try {
+ $result = view('reports.options.budget', compact('budgets'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
+ $result = 'Could not render view.';
+ }
+
+ return $result;
+ }
+
+ /**
+ * View for spent in a single budget.
+ *
+ * @param array $attributes
+ *
+ * @return string
+ */
+ protected function budgetSpentAmount(array $attributes): string // generate view for report.
+ {
+ /** @var BudgetRepositoryInterface $budgetRepository */
+ $budgetRepository = app(BudgetRepositoryInterface::class);
+
+ /** @var PopupReportInterface $popupHelper */
+ $popupHelper = app(PopupReportInterface::class);
+
+ $budget = $budgetRepository->findNull((int)$attributes['budgetId']);
+ if (null === $budget) {
+ return 'This is an unknown budget. Apologies.';
+ }
+ $journals = $popupHelper->byBudget($budget, $attributes);
+ try {
+ $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Could not render: %s', $e->getMessage()));
+ $view = 'Firefly III could not render the view. Please see the log files.';
+ }
+
+ return $view;
+ }
+
+ /**
+ * View for transactions in a category.
+ *
+ * @param array $attributes
+ *
+ * @return string
+ */
+ protected function categoryEntry(array $attributes): string // generate view for report.
+ {
+ /** @var PopupReportInterface $popupHelper */
+ $popupHelper = app(PopupReportInterface::class);
+
+ /** @var CategoryRepositoryInterface $categoryRepository */
+ $categoryRepository = app(CategoryRepositoryInterface::class);
+ $category = $categoryRepository->findNull((int)$attributes['categoryId']);
+
+ if (null === $category) {
+ return 'This is an unknown category. Apologies.';
+ }
+
+ $journals = $popupHelper->byCategory($category, $attributes);
+ try {
+ $view = view('popup.report.category-entry', compact('journals', 'category'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Could not render: %s', $e->getMessage()));
+ $view = 'Firefly III could not render the view. Please see the log files.';
+ }
+
+ return $view;
+ }
+
+ /**
+ * Get options for category report.
+ *
+ * @return string
+ */
+ protected function categoryReportOptions(): string // render a view
+ {
+ /** @var CategoryRepositoryInterface $repository */
+ $repository = app(CategoryRepositoryInterface::class);
+ $categories = $repository->getCategories();
+ try {
+ $result = view('reports.options.category', compact('categories'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Cannot render reports.options.category: %s', $e->getMessage()));
+ $result = 'Could not render view.';
+ }
+
+ return $result;
+ }
+
+ /**
+ * Returns all the expenses that went to the given expense account.
+ *
+ * @param array $attributes
+ *
+ * @return string
+ */
+ protected function expenseEntry(array $attributes): string // generate view for report.
+ {
+ /** @var AccountRepositoryInterface $accountRepository */
+ $accountRepository = app(AccountRepositoryInterface::class);
+
+ /** @var PopupReportInterface $popupHelper */
+ $popupHelper = app(PopupReportInterface::class);
+
+ $account = $accountRepository->findNull((int)$attributes['accountId']);
+
+ if (null === $account) {
+ return 'This is an unknown account. Apologies.';
+ }
+
+ $journals = $popupHelper->byExpenses($account, $attributes);
+ try {
+ $view = view('popup.report.expense-entry', compact('journals', 'account'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Could not render: %s', $e->getMessage()));
+ $view = 'Firefly III could not render the view. Please see the log files.';
+ }
+
+ return $view;
+ }
+
+ /**
+ * Returns all the incomes that went to the given asset account.
+ *
+ * @param array $attributes
+ *
+ * @return string
+ */
+ protected function incomeEntry(array $attributes): string // generate view for report.
+ {
+ /** @var AccountRepositoryInterface $accountRepository */
+ $accountRepository = app(AccountRepositoryInterface::class);
+
+ /** @var PopupReportInterface $popupHelper */
+ $popupHelper = app(PopupReportInterface::class);
+
+ $account = $accountRepository->findNull((int)$attributes['accountId']);
+
+ if (null === $account) {
+ return 'This is an unknown category. Apologies.';
+ }
+
+ $journals = $popupHelper->byIncome($account, $attributes);
+ try {
+ $view = view('popup.report.income-entry', compact('journals', 'account'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Could not render: %s', $e->getMessage()));
+ $view = 'Firefly III could not render the view. Please see the log files.';
+ }
+
+ return $view;
+ }
+
+ /**
+ * Get options for default report.
+ *
+ * @return string
+ */
+ protected function noReportOptions(): string // render a view
+ {
+ try {
+ $result = view('reports.options.no-options')->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage()));
+ $result = 'Could not render view.';
+ }
+
+ return $result;
+ }
+
+ /**
+ * Get options for tag report.
+ *
+ * @return string
+ */
+ protected function tagReportOptions(): string // render a view
+ {
+ /** @var TagRepositoryInterface $repository */
+ $repository = app(TagRepositoryInterface::class);
+ $tags = $repository->get()->sortBy(
+ function (Tag $tag) {
+ return $tag->tag;
+ }
+ );
+ try {
+ $result = view('reports.options.tag', compact('tags'))->render();
+ } catch (Throwable $e) {
+ Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
+ $result = 'Could not render view.';
+ }
+
+ return $result;
+ }
+}
\ No newline at end of file