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

287 lines
10 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* CurrencyController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
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;
2017-12-28 02:53:21 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
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
/**
2017-11-15 05:25:49 -06:00
* Class CurrencyController.
2015-02-25 08:57:43 -06:00
*/
class CurrencyController extends Controller
{
2017-03-19 11:54:21 -05:00
/** @var CurrencyRepositoryInterface */
protected $repository;
2017-11-15 05:25:49 -06:00
/** @var UserRepositoryInterface */
2017-03-19 11:54:21 -05:00
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) {
2017-12-16 12:46:36 -06:00
app('view')->share('title', trans('firefly.currencies'));
app('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").
2017-11-15 05:25:49 -06:00
if (true !== session('currencies.create.fromStore')) {
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');
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
}
/**
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');
2015-06-21 08:09:10 -05:00
$subTitle = trans('form.delete_currency', ['name' => $currency->name]);
2015-04-05 13:47:19 -05: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").
2017-11-15 05:25:49 -06:00
if (true !== session('currencies.edit.fromUpdate')) {
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');
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-12-28 02:53:21 -06:00
$page = 0 === intval($request->get('page')) ? 1 : intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
$collection = $this->repository->get();
$total = $collection->count();
$collection = $collection->sortBy(
2017-12-08 12:35:09 -06:00
function (TransactionCurrency $currency) {
return $currency->name;
}
);
2017-12-28 02:53:21 -06:00
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
$currencies = new LengthAwarePaginator($collection, $total, $pageSize, $page);
$currencies->setPath(route('currencies.index'));
2017-03-19 11:54:21 -05:00
$defaultCurrency = $this->repository->getCurrencyByPreference(Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR')));
2017-11-05 12:49:20 -06:00
$isOwner = true;
2017-03-19 11:54:21 -05:00
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')]));
2017-10-20 01:00:13 -05:00
$isOwner = false;
}
2017-11-05 12:49:20 -06:00
return view('currencies.index', compact('currencies', 'defaultCurrency', 'isOwner'));
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
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('create_another'))) {
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
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('return_to_edit'))) {
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
}
}