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

218 lines
6.6 KiB
PHP
Raw Normal View History

2015-02-25 08:57:43 -06:00
<?php namespace FireflyIII\Http\Controllers;
use Auth;
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;
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
{
/**
2015-05-23 13:49:57 -05:00
* @codeCoverageIgnore
2015-02-25 08:57:43 -06:00
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2015-05-14 06:17:53 -05:00
View::share('title', trans('firefly.currencies'));
2015-02-25 08:57:43 -06:00
View::share('mainTitleIcon', 'fa-usd');
}
/**
* @return \Illuminate\View\View
*/
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").
if (Session::get('currency.create.fromStore') !== true) {
Session::put('currency.create.url', URL::previous());
}
Session::forget('currency.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
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);
Preferences::mark();
2015-02-25 08:57:43 -06:00
Session::flash('success', $currency->name . ' is now the default currency.');
Cache::forget('FFCURRENCYSYMBOL');
Cache::forget('FFCURRENCYCODE');
2015-07-06 09:27:21 -05:00
return redirect(route('currency.index'));
2015-02-25 08:57:43 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @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|View
2015-02-25 08:57:43 -06:00
*/
2015-05-03 05:54:39 -05:00
public function delete(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
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-07-06 09:27:21 -05:00
return redirect(route('currency.index'));
2015-02-25 08:57:43 -06:00
}
2015-04-05 13:47:19 -05:00
// put previous url in session
Session::put('currency.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
2015-06-21 08:09:10 -05:00
return view('currency.delete', compact('currency', 'subTitle'));
2015-02-25 08:57:43 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @param CurrencyRepositoryInterface $repository
* @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
*/
2015-05-03 05:54:39 -05:00
public function destroy(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
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
2015-07-06 09:27:21 -05:00
return redirect(route('currency.index'));
2015-02-25 08:57:43 -06:00
}
Session::flash('success', 'Currency "' . e($currency->name) . '" deleted');
if (Auth::user()->hasRole('owner')) {
$currency->delete();
}
2015-02-25 08:57:43 -06:00
2015-07-06 09:27:21 -05:00
return redirect(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';
2015-06-09 10:56:08 -05:00
$subTitle = trans('firefly.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").
if (Session::get('currency.edit.fromUpdate') !== true) {
Session::put('currency.edit.url', URL::previous());
}
Session::forget('currency.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
2015-02-25 08:57:43 -06:00
return view('currency.edit', compact('currency', 'subTitle', 'subTitleIcon'));
}
/**
2015-05-03 05:58:55 -05:00
* @param CurrencyRepositoryInterface $repository
*
2015-02-25 08:57:43 -06:00
* @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
if (!Auth::user()->hasRole('owner')) {
Session::flash('warning', 'Please ask ' . env('SITE_OWNER') . ' to add, remove or edit currencies.');
}
2015-02-25 08:57:43 -06:00
return view('currency.index', compact('currencies', 'defaultCurrency'));
}
/**
*
2015-05-03 05:58:55 -05:00
* @param CurrencyFormRequest $request
* @param CurrencyRepositoryInterface $repository
*
2015-02-25 08:57:43 -06:00
* @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
{
$data = $request->getCurrencyData();
if (Auth::user()->hasRole('owner')) {
$currency = $repository->store($data);
Session::flash('success', 'Currency "' . $currency->name . '" created');
}
2015-02-25 08:57:43 -06:00
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
2015-07-06 09:27:21 -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-07-06 09:27:21 -05:00
return redirect(Session::get('currency.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();
if (Auth::user()->hasRole('owner')) {
$currency = $repository->update($currency, $data);
}
2015-03-02 13:05:28 -06:00
Session::flash('success', 'Currency "' . e($currency->name) . '" updated.');
Preferences::mark();
2015-03-02 13:05:28 -06:00
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-07-06 09:27:21 -05:00
return redirect(route('currency.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.
2015-07-06 09:27:21 -05:00
return redirect(Session::get('currency.edit.url'));
2015-02-25 08:57:43 -06:00
}
}