firefly-iii/app/Http/Controllers/Budget/EditController.php

161 lines
6.0 KiB
PHP
Raw Normal View History

2018-07-14 08:22:21 -05:00
<?php
/**
* EditController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 08:22:21 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 08:22:21 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-14 08:22:21 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 08:22:21 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-14 08:22:21 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-14 08:22:21 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Budget;
2021-03-28 04:46:23 -05:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2018-07-14 08:22:21 -05:00
use FireflyIII\Http\Controllers\Controller;
2020-03-13 15:35:22 -05:00
use FireflyIII\Http\Requests\BudgetFormUpdateRequest;
2020-03-14 01:55:00 -05:00
use FireflyIII\Models\AutoBudget;
2018-07-14 08:22:21 -05:00
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Contracts\View\Factory;
2018-07-14 08:22:21 -05:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
2018-07-14 08:22:21 -05:00
/**
*
* Class EditController
*/
class EditController extends Controller
{
/** @var AttachmentHelperInterface Helper for attachments. */
private $attachments;
2021-03-28 04:46:23 -05:00
/** @var BudgetRepositoryInterface The budget repository */
private $repository;
2018-07-14 08:22:21 -05:00
/**
2018-07-21 01:06:24 -05:00
* EditController constructor.
*
* @codeCoverageIgnore
2018-07-14 08:22:21 -05:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2021-03-28 04:46:23 -05:00
app('view')->share('title', (string)trans('firefly.budgets'));
app('view')->share('mainTitleIcon', 'fa-pie-chart');
2021-03-28 04:46:23 -05:00
$this->repository = app(BudgetRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
2018-07-14 08:22:21 -05:00
return $next($request);
}
);
}
/**
2018-07-21 01:06:24 -05:00
* Budget edit form.
*
2018-07-14 08:22:21 -05:00
* @param Request $request
* @param Budget $budget
*
* @return Factory|View
2018-07-14 08:22:21 -05:00
*/
public function edit(Request $request, Budget $budget)
{
2021-03-28 04:46:23 -05:00
$subTitle = (string)trans('firefly.edit_budget', ['name' => $budget->name]);
$autoBudget = $this->repository->getAutoBudget($budget);
2020-03-14 01:55:00 -05:00
// auto budget types
$autoBudgetTypes = [
2021-03-28 04:46:23 -05:00
0 => (string)trans('firefly.auto_budget_none'),
AutoBudget::AUTO_BUDGET_RESET => (string)trans('firefly.auto_budget_reset'),
AutoBudget::AUTO_BUDGET_ROLLOVER => (string)trans('firefly.auto_budget_rollover'),
];
$autoBudgetPeriods = [
2021-03-28 04:46:23 -05:00
'daily' => (string)trans('firefly.auto_budget_period_daily'),
'weekly' => (string)trans('firefly.auto_budget_period_weekly'),
'monthly' => (string)trans('firefly.auto_budget_period_monthly'),
'quarterly' => (string)trans('firefly.auto_budget_period_quarterly'),
'half_year' => (string)trans('firefly.auto_budget_period_half_year'),
'yearly' => (string)trans('firefly.auto_budget_period_yearly'),
];
2018-07-14 08:22:21 -05:00
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
2020-03-14 01:55:00 -05:00
$currency = app('amount')->getDefaultCurrency();
2018-07-14 08:22:21 -05:00
$preFilled = [
2021-03-28 04:46:23 -05:00
'active' => $hasOldInput ? (bool)$request->old('active') : $budget->active,
'auto_budget_currency_id' => $hasOldInput ? (int)$request->old('auto_budget_currency_id') : $currency->id,
2018-07-14 08:22:21 -05:00
];
if ($autoBudget) {
$preFilled['auto_budget_amount'] = $hasOldInput ? $request->old('auto_budget_amount') : $autoBudget->amount;
}
2018-07-14 08:22:21 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('budgets.edit.fromUpdate')) {
$this->rememberPreviousUri('budgets.edit.uri');
}
$request->session()->forget('budgets.edit.fromUpdate');
$request->session()->flash('preFilled', $preFilled);
return prefixView('budgets.edit', compact('budget', 'subTitle', 'autoBudgetTypes', 'autoBudgetPeriods', 'autoBudget'));
2018-07-14 08:22:21 -05:00
}
/**
2018-07-21 01:06:24 -05:00
* Budget update routine.
*
2020-03-13 15:35:22 -05:00
* @param BudgetFormUpdateRequest $request
* @param Budget $budget
2018-07-14 08:22:21 -05:00
*
* @return RedirectResponse
2018-07-14 08:22:21 -05:00
*/
2020-03-13 15:35:22 -05:00
public function update(BudgetFormUpdateRequest $request, Budget $budget): RedirectResponse
2018-07-14 08:22:21 -05:00
{
$data = $request->getBudgetData();
$this->repository->update($budget, $data);
2021-03-28 04:46:23 -05:00
$request->session()->flash('success', (string)trans('firefly.updated_budget', ['name' => $budget->name]));
2018-07-14 08:22:21 -05:00
$this->repository->cleanupBudgets();
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('budgets.edit.uri'));
// store new attachment(s):
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2020-05-06 23:44:01 -05:00
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($budget, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
2021-03-28 04:46:23 -05:00
session()->flash('info', (string)trans('firefly.no_att_demo_user'));
2020-05-06 23:44:01 -05:00
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
}
2021-03-28 04:46:23 -05:00
if (1 === (int)$request->get('return_to_edit')) {
2018-07-14 08:22:21 -05:00
// @codeCoverageIgnoreStart
$request->session()->put('budgets.edit.fromUpdate', true);
$redirect = redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]);
// @codeCoverageIgnoreEnd
}
return $redirect;
}
}