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

280 lines
9.6 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* CurrencyController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 08:57:43 -06:00
2015-03-10 11:26:31 -05:00
use Cache;
2015-02-25 08:57:43 -06:00
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Models\TransactionCurrency;
2015-04-05 13:47:19 -05:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-19 11:54:21 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-02-17 13:14:22 -06:00
use Illuminate\Http\Request;
2016-01-24 08:58:16 -06:00
use Log;
2015-02-25 08:57:43 -06:00
use Preferences;
2015-04-05 13:47:19 -05:00
use View;
2015-02-25 08:57:43 -06:00
/**
* Class CurrencyController
*
* @package FireflyIII\Http\Controllers
*/
class CurrencyController extends Controller
{
2017-03-19 11:54:21 -05:00
/** @var CurrencyRepositoryInterface */
protected $repository;
/** @var UserRepositoryInterface */
protected $userRepository;
2015-02-25 08:57:43 -06:00
/**
2016-02-04 00:27:03 -06:00
*
2015-02-25 08:57:43 -06:00
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.currencies'));
View::share('mainTitleIcon', 'fa-usd');
2017-03-19 11:54:21 -05:00
$this->repository = app(CurrencyRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-25 08:57:43 -06:00
}
/**
2017-02-24 22:57:01 -06:00
* @param Request $request
*
2017-03-19 11:54:21 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2015-02-25 08:57:43 -06:00
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
$request->session()->flash('error', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
return redirect(route('currencies.index'));
}
2015-02-25 08:57:43 -06:00
$subTitleIcon = 'fa-plus';
2015-06-09 10:56:08 -05:00
$subTitle = trans('firefly.create_currency');
2015-02-25 08:57:43 -06:00
2015-04-01 11:59:34 -05:00
// put previous url in session if not redirect from store (not "create another").
2016-12-05 13:01:01 -06:00
if (session('currencies.create.fromStore') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('currencies.create.uri');
2015-04-01 11:59:34 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('currencies.create.fromStore');
$request->session()->flash('gaEventCategory', 'currency');
$request->session()->flash('gaEventAction', 'create');
2015-04-01 11:59:34 -05:00
2016-12-05 13:01:01 -06:00
return view('currencies.create', compact('subTitleIcon', 'subTitle'));
2015-02-25 08:57:43 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-02-25 08:57:43 -06:00
* @param TransactionCurrency $currency
*
2017-02-17 13:14:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:57:43 -06:00
*/
2017-02-17 13:14:22 -06:00
public function defaultCurrency(Request $request, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
Preferences::set('currencyPreference', $currency->code);
Preferences::mark();
2015-02-25 08:57:43 -06:00
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', trans('firefly.new_default_currency', ['name' => $currency->name]));
2015-02-25 08:57:43 -06:00
Cache::forget('FFCURRENCYSYMBOL');
Cache::forget('FFCURRENCYCODE');
2016-12-05 13:01:01 -06:00
return redirect(route('currencies.index'));
2015-02-25 08:57:43 -06:00
}
2016-12-11 09:25:46 -06:00
2015-02-25 08:57:43 -06:00
/**
2017-03-19 11:54:21 -05:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2016-12-11 09:25:46 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2015-02-25 08:57:43 -06:00
*/
2017-03-19 11:54:21 -05:00
public function delete(Request $request, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
// @codeCoverageIgnoreStart
$request->session()->flash('error', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
return redirect(route('currencies.index'));
// @codeCoverageIgnoreEnd
}
if (!$this->repository->canDeleteCurrency($currency)) {
2017-02-17 13:14:22 -06:00
$request->session()->flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
2015-04-01 11:59:34 -05:00
2016-12-05 13:01:01 -06:00
return redirect(route('currencies.index'));
2015-02-25 08:57:43 -06:00
}
2015-04-05 13:47:19 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('currencies.delete.uri');
2017-02-17 13:14:22 -06:00
$request->session()->flash('gaEventCategory', 'currency');
$request->session()->flash('gaEventAction', 'delete');
2015-06-21 08:09:10 -05:00
$subTitle = trans('form.delete_currency', ['name' => $currency->name]);
2015-04-05 13:47:19 -05:00
2015-02-25 08:57:43 -06:00
2016-12-05 13:01:01 -06:00
return view('currencies.delete', compact('currency', 'subTitle'));
2015-02-25 08:57:43 -06:00
}
/**
2017-03-19 11:54:21 -05:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2016-12-11 09:25:46 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:57:43 -06:00
*/
2017-03-19 11:54:21 -05:00
public function destroy(Request $request, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
// @codeCoverageIgnoreStart
$request->session()->flash('error', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
return redirect(route('currencies.index'));
// @codeCoverageIgnoreEnd
}
if (!$this->repository->canDeleteCurrency($currency)) {
2017-02-17 13:14:22 -06:00
$request->session()->flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
2015-02-25 08:57:43 -06:00
2016-12-05 13:01:01 -06:00
return redirect(route('currencies.index'));
2015-02-25 08:57:43 -06:00
}
2017-03-19 11:54:21 -05:00
$this->repository->destroy($currency);
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', trans('firefly.deleted_currency', ['name' => $currency->name]));
2015-02-25 08:57:43 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('currencies.delete.uri'));
2015-02-25 08:57:43 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-02-25 08:57:43 -06:00
* @param TransactionCurrency $currency
*
2017-03-19 11:54:21 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2015-02-25 08:57:43 -06:00
*/
2017-02-17 13:14:22 -06:00
public function edit(Request $request, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
// @codeCoverageIgnoreStart
$request->session()->flash('error', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
return redirect(route('currencies.index'));
// @codeCoverageIgnoreEnd
}
2015-02-25 08:57:43 -06:00
$subTitleIcon = 'fa-pencil';
$subTitle = trans('breadcrumbs.edit_currency', ['name' => $currency->name]);
2015-02-25 08:57:43 -06:00
$currency->symbol = htmlentities($currency->symbol);
2015-04-01 11:59:34 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2016-12-05 13:01:01 -06:00
if (session('currencies.edit.fromUpdate') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('currencies.edit.uri');
2015-04-01 11:59:34 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('currencies.edit.fromUpdate');
$request->session()->flash('gaEventCategory', 'currency');
$request->session()->flash('gaEventAction', 'edit');
2015-04-01 11:59:34 -05:00
2016-12-05 13:01:01 -06:00
return view('currencies.edit', compact('currency', 'subTitle', 'subTitleIcon'));
2015-02-25 08:57:43 -06:00
}
/**
2017-03-19 11:54:21 -05:00
* @param Request $request
2015-05-03 05:58:55 -05:00
*
* @return View
2015-02-25 08:57:43 -06:00
*/
2017-03-19 11:54:21 -05:00
public function index(Request $request)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
$currencies = $this->repository->get();
$defaultCurrency = $this->repository->getCurrencyByPreference(Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR')));
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
2017-02-17 13:14:22 -06:00
$request->session()->flash('info', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
}
2016-12-05 13:01:01 -06:00
return view('currencies.index', compact('currencies', 'defaultCurrency'));
2015-02-25 08:57:43 -06:00
}
/**
2017-03-19 11:54:21 -05:00
* @param CurrencyFormRequest $request
2015-02-25 08:57:43 -06:00
*
2017-03-19 11:54:21 -05:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:57:43 -06:00
*/
2017-03-19 11:54:21 -05:00
public function store(CurrencyFormRequest $request)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
// @codeCoverageIgnoreStart
2016-09-16 05:15:58 -05:00
Log::error('User ' . auth()->user()->id . ' is not admin, but tried to store a currency.');
2016-04-29 01:56:56 -05:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('currencies.create.uri'));
2017-03-19 11:54:21 -05:00
// @codeCoverageIgnoreEnd
}
2015-02-25 08:57:43 -06:00
2016-04-29 01:56:56 -05:00
$data = $request->getCurrencyData();
2017-03-19 11:54:21 -05:00
$currency = $this->repository->store($data);
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', trans('firefly.created_currency', ['name' => $currency->name]));
2016-04-29 01:56:56 -05:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('create_another')) === 1) {
2017-03-19 11:54:21 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('currencies.create.fromStore', true);
2015-04-05 13:47:19 -05:00
2016-12-05 13:01:01 -06:00
return redirect(route('currencies.create'))->withInput();
2017-03-19 11:54:21 -05:00
// @codeCoverageIgnoreEnd
2015-03-02 13:05:28 -06:00
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('currencies.create.uri'));
2015-02-25 08:57:43 -06:00
}
/**
2017-03-19 11:54:21 -05:00
* @param CurrencyFormRequest $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2017-03-19 11:54:21 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:57:43 -06:00
*/
2017-03-19 11:54:21 -05:00
public function update(CurrencyFormRequest $request, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
2017-03-19 11:54:21 -05:00
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
// @codeCoverageIgnoreStart
$request->session()->flash('error', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
return redirect(route('currencies.index'));
// @codeCoverageIgnoreEnd
}
2017-03-19 11:54:21 -05:00
$data = $request->getCurrencyData();
$currency = $this->repository->update($currency, $data);
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', trans('firefly.updated_currency', ['name' => $currency->name]));
Preferences::mark();
2015-03-02 13:05:28 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('return_to_edit')) === 1) {
2017-03-19 11:54:21 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('currencies.edit.fromUpdate', true);
2015-04-05 13:47:19 -05:00
2016-12-05 13:01:01 -06:00
return redirect(route('currencies.edit', [$currency->id]));
2017-03-19 11:54:21 -05:00
// @codeCoverageIgnoreEnd
2015-03-02 13:05:28 -06:00
}
2015-02-25 08:57:43 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('currencies.edit.uri'));
2015-02-25 08:57:43 -06:00
}
}