Files
firefly-iii/app/Http/Controllers/CurrencyController.php

304 lines
12 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
2022-12-29 19:41:57 +01:00
/**
* CurrencyController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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.
2017-10-21 08:40:00 +02: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/>.
*/
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Http\Controllers;
use FireflyIII\Exceptions\FireflyException;
2015-02-25 15:57:43 +01:00
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Models\TransactionCurrency;
2015-04-05 20:47:19 +02:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-19 17:54:21 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-07-09 19:24:08 +02:00
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
2022-01-02 08:01:59 +01:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2017-02-17 20:14:22 +01:00
use Illuminate\Http\Request;
2017-12-28 09:53:21 +01:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Redirector;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2023-05-29 13:56:55 +02:00
use Illuminate\View\View;
2022-12-29 19:41:57 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2015-02-25 15:57:43 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class CurrencyController.
2015-02-25 15:57:43 +01:00
*/
class CurrencyController extends Controller
{
2021-05-13 05:52:06 +02:00
protected CurrencyRepositoryInterface $repository;
protected UserRepositoryInterface $userRepository;
2015-02-25 15:57:43 +01:00
/**
2018-07-22 08:10:16 +02:00
* CurrencyController constructor.
*
2015-02-25 15:57:43 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2022-12-29 19:41:57 +01:00
app('view')->share('title', (string)trans('firefly.currencies'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-usd');
2017-03-19 17:54:21 +01:00
$this->repository = app(CurrencyRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-02-25 15:57:43 +01:00
}
2015-02-25 15:57:43 +01:00
/**
2018-07-22 08:10:16 +02:00
* Create a currency.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
2017-02-25 05:57:01 +01:00
*
2021-05-24 08:50:17 +02:00
* @return Factory|RedirectResponse|Redirector|View
2015-02-25 15:57:43 +01:00
*/
2017-02-17 20:14:22 +01:00
public function create(Request $request)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2017-03-19 17:54:21 +01:00
return redirect(route('currencies.index'));
}
2015-02-25 15:57:43 +01:00
$subTitleIcon = 'fa-plus';
2022-12-29 19:41:57 +01:00
$subTitle = (string)trans('firefly.create_currency');
2015-02-25 15:57:43 +01:00
2015-04-01 18:59:34 +02:00
// put previous url in session if not redirect from store (not "create another").
2017-11-15 12:25:49 +01:00
if (true !== session('currencies.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('currencies.create.url');
2015-04-01 18:59:34 +02:00
}
2017-02-17 20:14:22 +01:00
$request->session()->forget('currencies.create.fromStore');
2015-04-01 18:59:34 +02:00
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('Create new currency.');
return view('currencies.create', compact('subTitleIcon', 'subTitle'));
2015-02-25 15:57:43 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Deletes a currency.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 15:57:43 +01:00
*
2021-05-24 08:50:17 +02:00
* @return Factory|RedirectResponse|Redirector|View
2015-02-25 15:57:43 +01:00
*/
2017-03-19 17:54:21 +01:00
public function delete(Request $request, TransactionCurrency $currency)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Tried to visit page to delete currency %s but is not site owner.', $currency->code));
2017-03-19 17:54:21 +01:00
return redirect(route('currencies.index'));
}
if ($this->repository->currencyInUse($currency)) {
2019-09-13 17:17:57 +02:00
$location = $this->repository->currencyInUseAt($currency);
2022-12-29 19:41:57 +01:00
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
2019-09-13 17:17:57 +02:00
$request->session()->flash('error', $message);
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Tried to visit page to delete currency %s but currency is in use.', $currency->code));
2015-04-01 18:59:34 +02:00
2016-12-05 20:01:01 +01:00
return redirect(route('currencies.index'));
2015-02-25 15:57:43 +01:00
}
2015-04-05 20:47:19 +02:00
// put previous url in session
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('currencies.delete.url');
2022-12-29 19:41:57 +01:00
$subTitle = (string)trans('form.delete_currency', ['name' => $currency->name]);
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Visit page to delete currency %s.', $currency->code));
2015-04-05 20:47:19 +02:00
return view('currencies.delete', compact('currency', 'subTitle'));
2015-02-25 15:57:43 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Destroys a currency.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 15:57:43 +01:00
*
* @return RedirectResponse|Redirector
2015-02-25 15:57:43 +01:00
*/
2017-03-19 17:54:21 +01:00
public function destroy(Request $request, TransactionCurrency $currency)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is not site owner.', $currency->code));
2017-03-19 17:54:21 +01:00
return redirect(route('currencies.index'));
}
if ($this->repository->currencyInUse($currency)) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.cannot_delete_currency', ['name' => e($currency->name)]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is in use.', $currency->code));
2015-02-25 15:57:43 +01:00
2016-12-05 20:01:01 +01:00
return redirect(route('currencies.index'));
2015-02-25 15:57:43 +01:00
}
2020-02-13 20:09:27 +01:00
if ($this->repository->isFallbackCurrency($currency)) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.cannot_delete_fallback_currency', ['name' => e($currency->name)]));
2020-02-13 20:09:27 +01:00
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is FALLBACK.', $currency->code));
return redirect(route('currencies.index'));
}
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Deleted currency %s.', $currency->code));
2017-03-19 17:54:21 +01:00
$this->repository->destroy($currency);
2019-02-08 07:13:59 +01:00
2022-12-29 19:41:57 +01:00
$request->session()->flash('success', (string)trans('firefly.deleted_currency', ['name' => $currency->name]));
2015-02-25 15:57:43 +01:00
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('currencies.delete.url'));
2015-02-25 15:57:43 +01:00
}
2018-07-08 12:08:53 +02:00
2015-02-25 15:57:43 +01:00
/**
2018-07-22 08:10:16 +02:00
* Edit a currency.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 15:57:43 +01:00
*
2021-05-24 08:50:17 +02:00
* @return Factory|RedirectResponse|Redirector|View
2015-02-25 15:57:43 +01:00
*/
2017-02-17 20:14:22 +01:00
public function edit(Request $request, TransactionCurrency $currency)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info(sprintf('Tried to edit currency %s but is not owner.', $currency->code));
2017-03-19 17:54:21 +01:00
return redirect(route('currencies.index'));
}
2015-02-25 15:57:43 +01:00
$subTitleIcon = 'fa-pencil';
2022-12-29 19:41:57 +01:00
$subTitle = (string)trans('breadcrumbs.edit_currency', ['name' => $currency->name]);
2015-02-25 15:57:43 +01:00
$currency->symbol = htmlentities($currency->symbol);
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
$preFilled = [
2022-12-29 19:41:57 +01:00
'enabled' => $hasOldInput ? (bool)$request->old('enabled') : $currency->enabled,
];
$request->session()->flash('preFilled', $preFilled);
Log::channel('audit')->info('Edit currency.', $currency->toArray());
2015-04-01 18:59:34 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 12:25:49 +01:00
if (true !== session('currencies.edit.fromUpdate')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('currencies.edit.url');
2015-04-01 18:59:34 +02:00
}
2017-02-17 20:14:22 +01:00
$request->session()->forget('currencies.edit.fromUpdate');
2015-04-01 18:59:34 +02:00
return view('currencies.edit', compact('currency', 'subTitle', 'subTitleIcon'));
2015-02-25 15:57:43 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Store new currency.
*
2023-06-21 12:34:58 +02:00
* @param CurrencyFormRequest $request
2015-02-25 15:57:43 +01:00
*
* @return $this|RedirectResponse|Redirector
2015-02-25 15:57:43 +01:00
*/
2017-03-19 17:54:21 +01:00
public function store(CurrencyFormRequest $request)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
2019-02-08 07:13:59 +01:00
$data = $request->getCurrencyData();
2018-07-09 19:24:08 +02:00
if (!$this->userRepository->hasRole($user, 'owner')) {
2023-06-21 12:34:58 +02:00
Log::error('User ' . auth()->user()->id . ' is not admin, but tried to store a currency.');
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('Tried to create (POST) currency without admin rights.', $data);
2016-04-29 08:56:56 +02:00
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('currencies.create.url'));
}
2015-02-25 15:57:43 +01:00
$data['enabled'] = true;
2020-01-05 19:29:28 +01:00
try {
$currency = $this->repository->store($data);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::channel('audit')->info('Could not store (POST) currency without admin rights.', $data);
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.could_not_store_currency'));
2020-01-05 19:29:28 +01:00
$currency = null;
}
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('currencies.create.url'));
2019-02-08 07:13:59 +01:00
2018-07-09 19:24:08 +02:00
if (null !== $currency) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('success', (string)trans('firefly.created_currency', ['name' => $currency->name]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('Created (POST) currency.', $data);
2022-12-29 19:41:57 +01:00
if (1 === (int)$request->get('create_another')) {
2018-07-09 19:24:08 +02:00
$request->session()->put('currencies.create.fromStore', true);
$redirect = redirect(route('currencies.create'))->withInput();
}
}
2015-03-02 20:05:28 +01:00
2018-07-09 19:24:08 +02:00
return $redirect;
2015-02-25 15:57:43 +01:00
}
2015-02-25 15:57:43 +01:00
/**
2018-07-22 08:10:16 +02:00
* Updates a currency.
*
2023-06-21 12:34:58 +02:00
* @param CurrencyFormRequest $request
* @param TransactionCurrency $currency
2015-02-25 15:57:43 +01:00
*
* @return RedirectResponse|Redirector
2015-02-25 15:57:43 +01:00
*/
2017-03-19 17:54:21 +01:00
public function update(CurrencyFormRequest $request, TransactionCurrency $currency)
2015-02-25 15:57:43 +01:00
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
2019-02-08 07:13:59 +01:00
$data = $request->getCurrencyData();
2019-08-02 05:25:24 +02:00
if (false === $data['enabled'] && $this->repository->currencyInUse($currency)) {
$data['enabled'] = true;
}
2018-07-09 19:24:08 +02:00
if (!$this->userRepository->hasRole($user, 'owner')) {
2022-12-29 19:41:57 +01:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('Tried to update (POST) currency without admin rights.', $data);
2017-03-19 17:54:21 +01:00
return redirect(route('currencies.index'));
}
2017-03-19 17:54:21 +01:00
$currency = $this->repository->update($currency, $data);
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('Updated (POST) currency.', $data);
2022-12-29 19:41:57 +01:00
$request->session()->flash('success', (string)trans('firefly.updated_currency', ['name' => $currency->name]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2015-03-02 20:05:28 +01:00
2022-12-29 19:41:57 +01:00
if (1 === (int)$request->get('return_to_edit')) {
2017-02-17 20:14:22 +01:00
$request->session()->put('currencies.edit.fromUpdate', true);
2015-04-05 20:47:19 +02:00
2016-12-05 20:01:01 +01:00
return redirect(route('currencies.edit', [$currency->id]));
2015-03-02 20:05:28 +01:00
}
2015-02-25 15:57:43 +01:00
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('currencies.edit.url'));
2015-02-25 15:57:43 +01:00
}
}