Skip to content

Commit

Permalink
Convert GET routes to POST.
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Sep 20, 2021
1 parent 8f0a55f commit 578f350
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 71 deletions.
92 changes: 53 additions & 39 deletions app/Http/Controllers/CurrencyController.php
Expand Up @@ -213,52 +213,59 @@ public function destroy(Request $request, TransactionCurrency $currency)
* @return RedirectResponse|Redirector
* @throws FireflyException
*/
public function disableCurrency(Request $request, TransactionCurrency $currency)
public function disableCurrency(Request $request)
{
app('preferences')->mark();
$currencyId = (int)$request->get('id');
if ($currencyId > 0) {
// valid currency?
$currency = $this->repository->find($currencyId);
if (null !== $currency) {
app('preferences')->mark();

/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {
/** @var User $user */
$user = auth()->user();
if (!$this->userRepository->hasRole($user, 'owner')) {

$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is not site owner.', $currency->code));
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is not site owner.', $currency->code));

return redirect(route('currencies.index'));
return redirect(route('currencies.index'));

}
}

if ($this->repository->currencyInUse($currency)) {
if ($this->repository->currencyInUse($currency)) {

$location = $this->repository->currencyInUseAt($currency);
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
$location = $this->repository->currencyInUseAt($currency);
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);

$request->session()->flash('error', $message);
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code));
$request->session()->flash('error', $message);
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code));

return redirect(route('currencies.index'));
}
return redirect(route('currencies.index'));
}

$this->repository->disable($currency);
Log::channel('audit')->info(sprintf('Disabled currency %s.', $currency->code));
// if no currencies are enabled, enable the first one in the DB (usually the EUR)
if (0 === $this->repository->get()->count()) {
/** @var TransactionCurrency $first */
$first = $this->repository->getAll()->first();
if (null === $first) {
throw new FireflyException('No currencies found.');
}
Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $first->code));
$this->repository->enable($first);
app('preferences')->set('currencyPreference', $first->code);
app('preferences')->mark();
}
$this->repository->disable($currency);
Log::channel('audit')->info(sprintf('Disabled currency %s.', $currency->code));
// if no currencies are enabled, enable the first one in the DB (usually the EUR)
if (0 === $this->repository->get()->count()) {
/** @var TransactionCurrency $first */
$first = $this->repository->getAll()->first();
if (null === $first) {
throw new FireflyException('No currencies found.');
}
Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $first->code));
$this->repository->enable($first);
app('preferences')->set('currencyPreference', $first->code);
app('preferences')->mark();
}

if ('EUR' === $currency->code) {
session()->flash('warning', (string)trans('firefly.disable_EUR_side_effects'));
}
if ('EUR' === $currency->code) {
session()->flash('warning', (string)trans('firefly.disable_EUR_side_effects'));
}

session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name]));
session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name]));
}
}

return redirect(route('currencies.index'));
}
Expand Down Expand Up @@ -311,13 +318,20 @@ public function edit(Request $request, TransactionCurrency $currency)
*
* @return RedirectResponse|Redirector
*/
public function enableCurrency(TransactionCurrency $currency)
public function enableCurrency(Request $request)
{
app('preferences')->mark();
$currencyId = (int)$request->get('id');
if ($currencyId > 0) {
// valid currency?
$currency = $this->repository->find($currencyId);
if (null !== $currency) {
app('preferences')->mark();

$this->repository->enable($currency);
session()->flash('success', (string)trans('firefly.currency_is_now_enabled', ['name' => $currency->name]));
Log::channel('audit')->info(sprintf('Enabled currency %s.', $currency->code));
$this->repository->enable($currency);
session()->flash('success', (string)trans('firefly.currency_is_now_enabled', ['name' => $currency->name]));
Log::channel('audit')->info(sprintf('Enabled currency %s.', $currency->code));
}
}

return redirect(route('currencies.index'));
}
Expand Down
45 changes: 28 additions & 17 deletions app/Http/Controllers/Transaction/CreateController.php
Expand Up @@ -28,17 +28,20 @@
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Services\Internal\Update\GroupCloneService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
* Class CreateController
*/
class CreateController extends Controller
{
private TransactionGroupRepositoryInterface $repository;

/**
* CreateController constructor.
*
Expand All @@ -49,38 +52,46 @@ public function __construct()
parent::__construct();

$this->middleware(
static function ($request, $next) {
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-exchange');
$this->repository = app(TransactionGroupRepositoryInterface::class);

return $next($request);
}
);
}

/**
* @param TransactionGroup $group
* @param Request $request
*
* @return RedirectResponse|Redirector
* @return JsonResponse
*/
public function cloneGroup(TransactionGroup $group)
public function cloneGroup(Request $request): JsonResponse
{
$groupId = (int)$request->get('id');
if (0 !== $groupId) {
$group = $this->repository->find($groupId);
if (null !== $group) {
/** @var GroupCloneService $service */
$service = app(GroupCloneService::class);
$newGroup = $service->cloneGroup($group);

/** @var GroupCloneService $service */
$service = app(GroupCloneService::class);
$newGroup = $service->cloneGroup($group);
// event!
event(new StoredTransactionGroup($newGroup));

// event!
event(new StoredTransactionGroup($newGroup));
app('preferences')->mark();

app('preferences')->mark();
$title = $newGroup->title ?? $newGroup->transactionJournals->first()->description;
$link = route('transactions.show', [$newGroup->id]);
session()->flash('success', trans('firefly.stored_journal', ['description' => $title]));
session()->flash('success_url', $link);

$title = $newGroup->title ?? $newGroup->transactionJournals->first()->description;
$link = route('transactions.show', [$newGroup->id]);
session()->flash('success', trans('firefly.stored_journal', ['description' => $title]));
session()->flash('success_url', $link);
return response()->json(['redirect' => route('transactions.show', [$newGroup->id])]);
}
}

return redirect(route('transactions.show', [$newGroup->id]));
return response()->json(['redirect' => route('transactions.show', [$groupId])]);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions public/v1/js/ff/currencies/index.js
Expand Up @@ -25,6 +25,8 @@ $(function () {
"use strict";
$('.make_default').on('click', setDefaultCurrency);

$('.enable-currency').on('click', enableCurrency);
$('.disable-currency').on('click', disableCurrency);
});

function setDefaultCurrency(e) {
Expand All @@ -40,4 +42,37 @@ function setDefaultCurrency(e) {
}).fail(function () {
console.error('I failed :(');
});
return false;
}

function enableCurrency(e) {
var button = $(e.currentTarget);
var currencyId = parseInt(button.data('id'));

$.post(enableCurrencyUrl, {
_token: token,
id: currencyId
}).done(function (data) {
// lame but it works
location.reload();
}).fail(function () {
console.error('I failed :(');
});
return false;
}

function disableCurrency(e) {
var button = $(e.currentTarget);
var currencyId = parseInt(button.data('id'));

$.post(disableCurrencyUrl, {
_token: token,
id: currencyId
}).done(function (data) {
// lame but it works
location.reload();
}).fail(function () {
console.error('I failed :(');
});
return false;
}
18 changes: 17 additions & 1 deletion public/v1/js/ff/list/groups.js
Expand Up @@ -23,6 +23,7 @@ var count = 0;
$(document).ready(function () {
updateListButtons();
addSort();
$('.clone-transaction').click(cloneTransaction);
});

var fixHelper = function (e, tr) {
Expand Down Expand Up @@ -206,4 +207,19 @@ function updateActionButtons() {
if (0 === count) {
$('.action-menu').hide();
}
}
}
function cloneTransaction(e) {
var button = $(e.currentTarget);
var groupId = parseInt(button.data('id'));

$.post(cloneGroupUrl, {
_token: token,
id: groupId
}).done(function (data) {
// lame but it works
location.href = data.redirect;
}).fail(function () {
console.error('I failed :(');
});
return false;
}
17 changes: 17 additions & 0 deletions public/v1/js/ff/transactions/show.js
Expand Up @@ -23,6 +23,7 @@
$(function () {
"use strict";
$('.link-modal').click(getLinkModal);
$('.clone-transaction').click(cloneTransaction);
$('#linkJournalModal').on('shown.bs.modal', function () {
makeAutoComplete();
})
Expand Down Expand Up @@ -80,3 +81,19 @@ function selectedJournal(event, journal) {
$('#selected-journal').html('<a href="' + groupURI.replace('%GROUP%', journal.transaction_group_id) + '">' + journal.description + '</a>').show();
$('input[name="opposing"]').val(journal.id);
}

function cloneTransaction(e) {
var button = $(e.currentTarget);
var groupId = parseInt(button.data('id'));

$.post(cloneGroupUrl, {
_token: token,
id: groupId
}).done(function (data) {
// lame but it works
location.href = data.redirect;
}).fail(function () {
console.error('I failed :(');
});
return false;
}
10 changes: 6 additions & 4 deletions resources/views/v1/currencies/index.twig
Expand Up @@ -69,14 +69,14 @@
class="fa fa-fw fa-star"></span> {{ 'make_default_currency'|_ }}</button>
{% endif %}
{% if currency.enabled %}
<a class="btn btn-default"
href="{{ route('currencies.disable',currency.id) }}">
<a class="btn btn-default disable-currency" data-id="{{ currency.id }}"
href="#">
<span class="fa fa-fw fa-square-o"></span>
{{ 'disable_currency'|_ }}</a>
{% endif %}
{% if not currency.enabled %}
<a class="btn btn-default"
href="{{ route('currencies.enable',currency.id) }}">
<a class="btn btn-default enable-currency" data-id="{{ currency.id }}"
href="#">
<span class="fa fa-fw fa-check-square-o"></span>
{{ 'enable_currency'|_ }}</a>
{% endif %}
Expand All @@ -101,6 +101,8 @@
{% block scripts %}
<script type="text/javascript" nonce="{{ JS_NONCE }}">
var makeDefaultUrl = "{{ route('currencies.default') }}";
var disableCurrencyUrl = "{{ route('currencies.disable') }}";
var enableCurrencyUrl = "{{ route('currencies.enable') }}";
</script>
<script type="text/javascript" src="v1/js/ff/currencies/index.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script>
{% endblock %}
7 changes: 5 additions & 2 deletions resources/views/v1/list/groups.twig
Expand Up @@ -85,7 +85,7 @@
class="fa fa-fw fa-pencil"></span> {{ 'edit'|_ }}</a></li>
<li><a href="{{ route('transactions.delete', [group.id]) }}"><span
class="fa fa-fw fa-trash"></span> {{ 'delete'|_ }}</a></li>
<li><a href="{{ route('transactions.clone', [group.id]) }}"><span
<li><a href="#" data-id="{{ group.id }}" class="clone-transaction"><span
class="fa fa-copy fa-fw"></span> {{ 'clone'|_ }}</a></li>
</ul>
</div>
Expand Down Expand Up @@ -249,7 +249,7 @@
class="fa fa-fw fa-pencil"></span> {{ 'edit'|_ }}</a></li>
<li><a href="{{ route('transactions.delete', [group.id]) }}"><span
class="fa fa-fw fa-trash"></span> {{ 'delete'|_ }}</a></li>
<li><a href="{{ route('transactions.clone', [group.id]) }}"><span
<li><a href="#" data-id="{{ group.id }}" class="clone-transaction"><span
class="fa fa-copy fa-fw"></span> {{ 'clone'|_ }}</a></li>
<li>
<a href="{{ route('rules.create-from-journal', [transaction.transaction_journal_id]) }}"><span
Expand Down Expand Up @@ -309,3 +309,6 @@
</tr>
</tfoot>
</table>
<script type="text/javascript" nonce="{{ JS_NONCE }}">
var cloneGroupUrl = '{{ route('transactions.clone') }}';
</script>

0 comments on commit 578f350

Please sign in to comment.