firefly-iii/app/controllers/CurrencyController.php

200 lines
5.9 KiB
PHP
Raw Normal View History

2014-12-23 15:27:14 -06:00
<?php
use FireflyIII\Database\TransactionCurrency\TransactionCurrency as Repository;
2014-12-24 05:32:18 -06:00
2014-12-23 15:27:14 -06:00
/**
2014-12-24 07:02:21 -06:00
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
*
2014-12-23 15:27:14 -06:00
* Class CurrencyController
*/
class CurrencyController extends BaseController
{
2014-12-24 05:32:18 -06:00
/** @var Repository */
2014-12-23 15:27:14 -06:00
protected $_repository;
2014-12-24 05:32:18 -06:00
/**
* @param Repository $repository
*/
2014-12-23 15:27:14 -06:00
public function __construct(Repository $repository)
{
$this->_repository = $repository;
2014-12-24 05:32:18 -06:00
2014-12-23 15:27:14 -06:00
View::share('title', 'Currencies');
View::share('mainTitleIcon', 'fa-usd');
}
2014-12-24 05:32:18 -06:00
/**
* @return \Illuminate\View\View
*/
public function create()
{
$subTitleIcon = 'fa-plus';
$subTitle = 'Create a new currency';
return View::make('currency.create', compact('subTitleIcon', 'subTitle'));
}
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse
*/
public function defaultCurrency(TransactionCurrency $currency)
{
/** @var \FireflyIII\Shared\Preferences\Preferences $preferences */
$preferences = App::make('FireflyIII\Shared\Preferences\Preferences');
$currencyPreference = $preferences->get('currencyPreference', 'EUR');
$currencyPreference->data = $currency->code;
$currencyPreference->save();
2014-12-24 07:02:21 -06:00
Session::flash('success', $currency->name.' is now the default currency.');
2014-12-24 05:32:18 -06:00
Cache::forget('FFCURRENCYSYMBOL');
Cache::forget('FFCURRENCYCODE');
return Redirect::route('currency.index');
}
/**
* @param TransactionCurrency $currency
2014-12-25 01:07:17 -06:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
2014-12-24 05:32:18 -06:00
*/
public function delete(TransactionCurrency $currency)
{
if ($currency->transactionJournals()->count() > 0) {
Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.');
return Redirect::route('currency.index');
}
return View::make('currency.delete', compact('currency'));
}
2014-12-25 01:07:17 -06:00
/**
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse
*/
2014-12-24 05:32:18 -06:00
public function destroy(TransactionCurrency $currency)
{
Session::flash('success', 'Currency "' . e($currency->name) . '" deleted');
$this->_repository->destroy($currency);
return Redirect::route('currency.index');
}
/**
* @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);
return View::make('currency.edit', compact('currency', 'subTitle', 'subTitleIcon'));
}
2014-12-25 01:07:17 -06:00
/**
* @return \Illuminate\View\View
*/
2014-12-23 15:27:14 -06:00
public function index()
{
$currencies = $this->_repository->get();
2014-12-24 05:32:18 -06:00
/** @var \FireflyIII\Shared\Preferences\Preferences $preferences */
$preferences = App::make('FireflyIII\Shared\Preferences\Preferences');
$currencyPreference = $preferences->get('currencyPreference', 'EUR');
$defaultCurrency = $this->_repository->findByCode($currencyPreference->data);
return View::make('currency.index', compact('currencies', 'defaultCurrency'));
}
2014-12-25 01:07:17 -06:00
/**
2015-01-17 03:05:43 -06:00
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
2014-12-25 01:07:17 -06:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
2014-12-24 05:32:18 -06:00
public function store()
{
$data = Input::except('_token');
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not store currency: ' . $messages['errors']->first());
}
// return to create screen:
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
return Redirect::route('currency.create')->withInput();
}
2015-01-01 22:52:38 -06:00
// store
2014-12-24 05:32:18 -06:00
$this->_repository->store($data);
Session::flash('success', 'Currency "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('currency.index');
}
return Redirect::route('currency.create')->withInput();
}
2014-12-25 01:07:17 -06:00
/**
* @param TransactionCurrency $currency
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
2014-12-24 05:32:18 -06:00
public function update(TransactionCurrency $currency)
{
2014-12-24 07:02:21 -06:00
$data = Input::except('_token');
2014-12-24 05:32:18 -06:00
// always validate:
$messages = $this->_repository->validate($data);
// flash messages:
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update currency: ' . $messages['errors']->first());
2015-01-17 03:05:43 -06:00
return Redirect::route('currency.edit', $currency->id)->withInput();
2014-12-24 05:32:18 -06:00
}
// return to update screen:
2015-01-17 03:05:43 -06:00
if ($data['post_submit_action'] == 'validate_only') {
2014-12-24 05:32:18 -06:00
return Redirect::route('currency.edit', $currency->id)->withInput();
}
// update
$this->_repository->update($currency, $data);
Session::flash('success', 'Currency "' . e($data['name']) . '" updated.');
// go back to list
if ($data['post_submit_action'] == 'update') {
return Redirect::route('currency.index');
}
return Redirect::route('currency.edit', $currency->id)->withInput(['post_submit_action' => 'return_to_edit']);
2014-12-23 15:27:14 -06:00
}
2015-01-01 23:16:49 -06:00
}