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

193 lines
5.5 KiB
PHP
Raw Normal View History

2015-02-25 08:57:43 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-03-10 11:26:31 -05:00
use Cache;
2015-02-25 08:57:43 -06:00
use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Models\Transaction;
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;
2015-02-25 08:57:43 -06:00
use Preferences;
use Redirect;
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
{
/**
*
*/
public function __construct()
{
View::share('title', 'Currencies');
View::share('mainTitleIcon', 'fa-usd');
}
/**
* @return \Illuminate\View\View
*/
public function create()
{
$subTitleIcon = 'fa-plus';
$subTitle = 'Create a new currency';
2015-04-01 11:59:34 -05:00
// put previous url in session if not redirect from store (not "create another").
if (Session::get('currency.create.fromStore') !== true) {
Session::put('currency.create.url', URL::previous());
}
Session::forget('currency.create.fromStore');
2015-02-25 08:57:43 -06:00
return view('currency.create', compact('subTitleIcon', 'subTitle'));
}
/**
* @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);
2015-02-25 08:57:43 -06:00
Session::flash('success', $currency->name . ' is now the default currency.');
Cache::forget('FFCURRENCYSYMBOL');
Cache::forget('FFCURRENCYCODE');
return Redirect::route('currency.index');
}
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
2015-04-05 13:47:19 -05:00
public function delete(TransactionCurrency $currency, CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
if ($repository->countJournals($currency) > 0) {
Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.');
2015-04-01 11:59:34 -05:00
2015-02-25 08:57:43 -06:00
return Redirect::route('currency.index');
}
2015-04-05 13:47:19 -05:00
// put previous url in session
Session::put('currency.delete.url', URL::previous());
2015-02-25 08:57:43 -06:00
2015-02-27 04:09:23 -06:00
return view('currency.delete', compact('currency'));
2015-02-25 08:57:43 -06:00
}
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-04-05 13:47:19 -05:00
public function destroy(TransactionCurrency $currency, CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
if ($repository->countJournals($currency) > 0) {
Session::flash('error', 'Cannot destroy ' . e($currency->name) . ' because there are still transactions attached to it.');
2015-02-25 08:57:43 -06:00
return Redirect::route('currency.index');
}
Session::flash('success', 'Currency "' . e($currency->name) . '" deleted');
$currency->delete();
2015-04-01 11:59:34 -05:00
return Redirect::to(Session::get('currency.delete.url'));
2015-02-25 08:57:43 -06:00
}
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\View\View
*/
public function edit(TransactionCurrency $currency)
{
$subTitleIcon = 'fa-pencil';
$subTitle = 'Edit currency "' . e($currency->name) . '"';
$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").
if (Session::get('currency.edit.fromUpdate') !== true) {
Session::put('currency.edit.url', URL::previous());
}
Session::forget('currency.edit.fromUpdate');
2015-02-25 08:57:43 -06:00
return view('currency.edit', compact('currency', 'subTitle', 'subTitleIcon'));
}
/**
* @return \Illuminate\View\View
*/
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();
$defaultCurrency = $repository->getCurrencyByPreference(Preferences::get('currencyPreference', 'EUR'));
2015-02-25 08:57:43 -06:00
return view('currency.index', compact('currencies', 'defaultCurrency'));
}
/**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
2015-04-05 13:47:19 -05:00
public function store(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
$data = $request->getCurrencyData();
$currency = $repository->store($data);
2015-02-25 08:57:43 -06:00
Session::flash('success', 'Currency "' . $currency->name . '" created');
2015-03-02 13:05:28 -06:00
if (intval(Input::get('create_another')) === 1) {
2015-04-01 11:59:34 -05:00
Session::put('currency.create.fromStore', true);
2015-04-05 13:47:19 -05:00
return Redirect::route('currency.create')->withInput();
2015-03-02 13:05:28 -06:00
}
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
2015-04-05 13:47:19 -05:00
return Redirect::to(Session::get('currency.create.url'));
2015-02-25 08:57:43 -06:00
}
/**
* @param TransactionCurrency $currency
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
2015-04-05 13:47:19 -05:00
public function update(TransactionCurrency $currency, CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
2015-02-25 08:57:43 -06:00
{
2015-04-05 13:47:19 -05:00
$data = $request->getCurrencyData();
$currency = $repository->update($currency, $data);
2015-02-25 08:57:43 -06:00
2015-03-02 13:05:28 -06:00
Session::flash('success', 'Currency "' . e($currency->name) . '" updated.');
if (intval(Input::get('return_to_edit')) === 1) {
2015-04-01 11:59:34 -05:00
Session::put('currency.edit.fromUpdate', true);
2015-04-05 13:47:19 -05:00
2015-03-02 13:05:28 -06:00
return Redirect::route('currency.edit', $currency->id);
}
2015-02-25 08:57:43 -06:00
2015-04-01 11:59:34 -05:00
// redirect to previous URL.
return Redirect::to(Session::get('currency.edit.url'));
2015-02-25 08:57:43 -06:00
}
}