Update index to handle new fields.

This commit is contained in:
James Cole
2023-10-22 07:17:53 +02:00
parent 0d65e396d4
commit 5425dac180
3 changed files with 163 additions and 60 deletions

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\TransactionCurrency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class IndexController extends Controller
{
protected CurrencyRepositoryInterface $repository;
protected UserRepositoryInterface $userRepository;
/**
* CurrencyController constructor.
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string)trans('firefly.currencies'));
app('view')->share('mainTitleIcon', 'fa-usd');
$this->repository = app(CurrencyRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Show overview of currencies.
*
* @param Request $request
*
* @return Factory|View
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function index(Request $request)
{
/** @var User $user */
$user = auth()->user();
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
$collection = $this->repository->getAll();
$total = $collection->count();
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
// order so default is on top:
$collection = $collection->sortBy(
function (TransactionCurrency $currency) {
$default = true === $currency->userDefault ? 0 : 1;
$enabled = true === $currency->userEnabled ? 0 : 1;
return sprintf('%s-%s-%s',$default, $enabled, $currency->code);
}
);
$currencies = new LengthAwarePaginator($collection, $total, $pageSize, $page);
$currencies->setPath(route('currencies.index'));
$isOwner = true;
if (!$this->userRepository->hasRole($user, 'owner')) {
$request->session()->flash('info', (string)trans('firefly.ask_site_owner', ['owner' => config('firefly.site_owner')]));
$isOwner = false;
}
return view('currencies.index', compact('currencies', 'isOwner'));
}
}

View File

@@ -24,55 +24,70 @@
$(function () {
"use strict";
$('.make_default').on('click', setDefaultCurrency);
$('.enable-currency').on('click', enableCurrency);
$('.disable-currency').on('click', disableCurrency);
});
function setDefaultCurrency(e) {
var button = $(e.currentTarget);
var currencyId = parseInt(button.data('id'));
var currencyCode = button.data('code');
$.post(makeDefaultUrl, {
_token: token,
id: currencyId
}).done(function (data) {
// lame but it works
location.reload();
}).fail(function () {
console.error('I failed :(');
var params = {
default: true,
enabled: true
}
$.ajax({
url: updateCurrencyUrl + '/' + currencyCode,
data: JSON.stringify(params),
type: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
},
});
window.location = redirectUrl + '?message=default&code=' + currencyCode;
return false;
}
function enableCurrency(e) {
var button = $(e.currentTarget);
var currencyId = parseInt(button.data('id'));
var currencyCode = button.data('code');
$.post(enableCurrencyUrl, {
_token: token,
id: currencyId
}).done(function (data) {
// lame but it works
location.reload();
}).fail(function () {
console.error('I failed :(');
var params = {
enabled: true
}
$.ajax({
url: updateCurrencyUrl + '/' + currencyCode,
data: JSON.stringify(params),
type: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
},
});
window.location = redirectUrl + '?message=enabled&code=' + currencyCode;
return false;
}
function disableCurrency(e) {
var button = $(e.currentTarget);
var currencyId = parseInt(button.data('id'));
var currencyCode = button.data('code');
$.post(disableCurrencyUrl, {
_token: token,
id: currencyId
}).done(function (data) {
// lame but it works
location.reload();
}).fail(function () {
console.error('I failed :(');
var params = {
enabled: true
}
$.ajax({
url: updateCurrencyUrl + '/' + currencyCode,
data: JSON.stringify(params),
type: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
},
});
window.location = redirectUrl + '?message=disabled&code=' + currencyCode;
return false;
}

View File

@@ -26,62 +26,64 @@
<table class="table table-hover">
<thead>
<tr>
{% if isOwner %}
<th>&nbsp;</th>
{% endif %}
<th>&nbsp;</th>
<th>{{ 'currency'|_ }}</th>
<th>{{ 'number_of_decimals'|_ }}</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
{% for currency in currencies %}
<tr>
{% if isOwner %}
<td>
<div class="btn-group btn-group-xs">
{% if isOwner %}
<a class="btn btn-default" href="{{ route('currencies.edit',currency.id) }}"><span
class="fa fa-fw fa-pencil"></span></a>
<a class="btn btn-danger" href="{{ route('currencies.delete',currency.id) }}"><span
class="fa fa-fw fa-trash"></span></a>
{% endif %}
{# Disable the currency. #}
{% if currency.userEnabled %}
<a class="btn btn-default disable-currency" data-code="{{ currency.code }}"
href="#">
<span class="fa fa-fw fa-square-o"></span>
{{ 'disable_currency'|_ }}</a>
{% endif %}
{# Enable the currency. #}
{% if not currency.userEnabled %}
<a class="btn btn-default enable-currency" data-code="{{ currency.code }}"
href="#">
<span class="fa fa-fw fa-check-square-o"></span>
{{ 'enable_currency'|_ }}</a>
{% endif %}
{# Make currency default. #}
{% if currency.id != defaultCurrency.id %}
<button data-code="{{ currency.code }}" class="make_default btn btn-default"><span
class="fa fa-fw fa-star"></span> {{ 'make_default_currency'|_ }}</button>
{% endif %}
</div>
</td>
{% endif %}
<td>
{% if currency.enabled == false %}
{% if currency.userEnabled == false %}
<span class="text-muted">
{% endif %}
{{ currency.name }} ({{ currency.code }}) ({{ currency.symbol|raw }})
{% if currency.id == defaultCurrency.id %}
<span class="label label-success" id="default-currency">{{ 'default_currency'|_ }}</span>
{% endif %}
{% if currency.enabled == false %}
{% if currency.userEnabled == false %}
<span class="label label-default">{{ 'currency_is_disabled'|_ }}</span>
{% endif %}
{% if currency.userEnabled == false %}
</span>
<br><small class="text-danger">{{ 'currency_is_disabled'|_ }}</small>
{% endif %}
</td>
<td>{{ currency.decimal_places }}</td>
<td class="buttons">
<div class="btn-group">
{% if currency.id != defaultCurrency.id %}
<button data-id="{{ currency.id }}" class="make_default btn btn-default"><span
class="fa fa-fw fa-star"></span> {{ 'make_default_currency'|_ }}</button>
{% endif %}
{% if currency.enabled %}
<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 enable-currency" data-id="{{ currency.id }}"
href="#">
<span class="fa fa-fw fa-check-square-o"></span>
{{ 'enable_currency'|_ }}</a>
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</tbody>
@@ -100,9 +102,8 @@
{% endblock %}
{% block scripts %}
<script type="text/javascript" nonce="{{ JS_NONCE }}">
var makeDefaultUrl = "{{ route('currencies.default') }}";
var disableCurrencyUrl = "{{ route('currencies.disable') }}";
var enableCurrencyUrl = "{{ route('currencies.enable') }}";
var redirectUrl = "{{ route('currencies.index') }}";
var updateCurrencyUrl = "{{ route('api.v1.currencies.update', ['']) }}";
</script>
<script type="text/javascript" src="v1/js/ff/currencies/index.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script>
{% endblock %}