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

275 lines
8.0 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;
2015-03-10 11:26:31 -05:00
use Input;
2016-01-24 08:58:16 -06:00
use Log;
2015-02-25 08:57:43 -06:00
use Preferences;
use Session;
2015-04-01 11:59:34 -05:00
use URL;
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
{
/**
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');
return $next($request);
}
);
2015-02-25 08:57:43 -06:00
}
/**
* @return View
2015-02-25 08:57:43 -06:00
*/
public function create()
{
$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) {
Session::put('currencies.create.url', URL::previous());
2015-04-01 11:59:34 -05:00
}
2016-12-05 13:01:01 -06:00
Session::forget('currencies.create.fromStore');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'currency');
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
}
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse
*/
public function defaultCurrency(TransactionCurrency $currency)
{
2015-04-05 13:47:19 -05:00
Preferences::set('currencyPreference', $currency->code);
Preferences::mark();
2015-02-25 08:57:43 -06:00
2016-01-23 02:10:22 -06:00
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-09-16 05:07:45 -05:00
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2015-05-03 05:54:39 -05:00
* @return \Illuminate\Http\RedirectResponse|View
2015-02-25 08:57:43 -06:00
*/
public function delete(TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
if (!$this->canDeleteCurrency($currency)) {
2016-01-23 02:10:22 -06:00
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
2016-12-05 13:01:01 -06:00
Session::put('currencies.delete.url', URL::previous());
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'currency');
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
}
/**
2016-09-16 05:07:45 -05:00
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
* @return \Illuminate\Http\RedirectResponse
2015-05-03 05:54:39 -05:00
* @throws \Exception
2015-02-25 08:57:43 -06:00
*/
public function destroy(TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
if (!$this->canDeleteCurrency($currency)) {
2016-01-23 02:10:22 -06:00
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
}
2016-01-23 02:10:22 -06:00
Session::flash('success', trans('firefly.deleted_currency', ['name' => $currency->name]));
2016-09-16 05:15:58 -05:00
if (auth()->user()->hasRole('owner')) {
2016-11-03 15:07:12 -05:00
$currency->forceDelete();
}
2015-02-25 08:57:43 -06:00
2016-12-05 13:01:01 -06:00
return redirect(session('currencies.delete.url'));
2015-02-25 08:57:43 -06:00
}
/**
* @param TransactionCurrency $currency
*
* @return View
2015-02-25 08:57:43 -06:00
*/
public function edit(TransactionCurrency $currency)
{
$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) {
Session::put('currencies.edit.url', URL::previous());
2015-04-01 11:59:34 -05:00
}
2016-12-05 13:01:01 -06:00
Session::forget('currencies.edit.fromUpdate');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'currency');
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
}
/**
2015-05-03 05:58:55 -05:00
* @param CurrencyRepositoryInterface $repository
*
* @return View
2015-02-25 08:57:43 -06:00
*/
2015-04-05 13:47:19 -05:00
public function index(CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
$currencies = $repository->get();
2016-09-18 12:57:21 -05:00
$defaultCurrency = $repository->getCurrencyByPreference(Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR')));
2015-02-25 08:57:43 -06:00
2016-09-16 05:15:58 -05:00
if (!auth()->user()->hasRole('owner')) {
2016-01-23 02:10:22 -06:00
Session::flash('warning', 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
}
/**
*
2015-05-03 05:58:55 -05:00
* @param CurrencyFormRequest $request
* @param CurrencyRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:57:43 -06:00
*/
2015-04-05 13:47:19 -05:00
public function store(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2016-09-16 05:15:58 -05:00
if (!auth()->user()->hasRole('owner')) {
Log::error('User ' . auth()->user()->id . ' is not admin, but tried to store a currency.');
2016-04-29 01:56:56 -05:00
2016-12-05 13:01:01 -06:00
return redirect(session('currencies.create.url'));
}
2015-02-25 08:57:43 -06:00
2016-04-29 01:56:56 -05:00
$data = $request->getCurrencyData();
$currency = $repository->store($data);
Session::flash('success', trans('firefly.created_currency', ['name' => $currency->name]));
2015-03-02 13:05:28 -06:00
if (intval(Input::get('create_another')) === 1) {
2016-12-05 13:01:01 -06:00
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();
2015-03-02 13:05:28 -06:00
}
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
2016-12-05 13:01:01 -06:00
return redirect(session('currencies.create.url'));
2015-02-25 08:57:43 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @param CurrencyFormRequest $request
* @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2015-05-03 05:54:39 -05:00
* @return \Illuminate\Http\RedirectResponse
2015-02-25 08:57:43 -06:00
*/
2015-05-03 05:54:39 -05:00
public function update(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
2015-02-25 08:57:43 -06:00
{
$data = $request->getCurrencyData();
2016-09-16 05:15:58 -05:00
if (auth()->user()->hasRole('owner')) {
$currency = $repository->update($currency, $data);
}
2016-01-23 02:10:22 -06:00
Session::flash('success', trans('firefly.updated_currency', ['name' => $currency->name]));
Preferences::mark();
2015-03-02 13:05:28 -06:00
if (intval(Input::get('return_to_edit')) === 1) {
2016-12-05 13:01:01 -06:00
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]));
2015-03-02 13:05:28 -06:00
}
2015-02-25 08:57:43 -06:00
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
2016-12-05 13:01:01 -06:00
return redirect(session('currencies.edit.url'));
2015-02-25 08:57:43 -06:00
}
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
private function canDeleteCurrency(TransactionCurrency $currency): bool
{
$repository = app(CurrencyRepositoryInterface::class);
// has transactions still
if ($repository->countJournals($currency) > 0) {
return false;
}
// is the only currency left
if ($repository->get()->count() === 1) {
return false;
}
// is the default currency for the user or the system
2016-09-18 12:57:21 -05:00
$defaultCode = Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR'))->data;
if ($currency->code === $defaultCode) {
return false;
}
// is the default currency for the system
2016-09-18 12:57:21 -05:00
$defaultSystemCode = config('firefly.default_currency', 'EUR');
if ($currency->code === $defaultSystemCode) {
return false;
}
// can be deleted
return true;
}
2015-02-25 08:57:43 -06:00
}