diff --git a/app/Http/Controllers/TransactionCurrency/IndexController.php b/app/Http/Controllers/TransactionCurrency/IndexController.php new file mode 100644 index 0000000000..7467b37d0a --- /dev/null +++ b/app/Http/Controllers/TransactionCurrency/IndexController.php @@ -0,0 +1,87 @@ +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')); + } + +} diff --git a/public/v1/js/ff/currencies/index.js b/public/v1/js/ff/currencies/index.js index dbccfae942..e173b5c3e6 100644 --- a/public/v1/js/ff/currencies/index.js +++ b/public/v1/js/ff/currencies/index.js @@ -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; } diff --git a/resources/views/currencies/index.twig b/resources/views/currencies/index.twig index d542f5fe6a..1a30c963ad 100644 --- a/resources/views/currencies/index.twig +++ b/resources/views/currencies/index.twig @@ -26,62 +26,64 @@ - {% if isOwner %} - - {% endif %} + - {% for currency in currencies %} - {% if isOwner %} - {% endif %} - {% endfor %} @@ -100,9 +102,8 @@ {% endblock %} {% block scripts %} {% endblock %}
   {{ 'currency'|_ }} {{ 'number_of_decimals'|_ }} 
+ {% if isOwner %} + {% endif %} + {# Disable the currency. #} + {% if currency.userEnabled %} + + + {{ 'disable_currency'|_ }} + {% endif %} + + {# Enable the currency. #} + {% if not currency.userEnabled %} + + + {{ 'enable_currency'|_ }} + {% endif %} + + {# Make currency default. #} + {% if currency.id != defaultCurrency.id %} + + {% endif %}
- {% if currency.enabled == false %} + {% if currency.userEnabled == false %} {% endif %} {{ currency.name }} ({{ currency.code }}) ({{ currency.symbol|raw }}) {% if currency.id == defaultCurrency.id %} {{ 'default_currency'|_ }} {% endif %} - {% if currency.enabled == false %} + {% if currency.userEnabled == false %} + + {{ 'currency_is_disabled'|_ }} + {% endif %} + + {% if currency.userEnabled == false %} -
{{ 'currency_is_disabled'|_ }} {% endif %}
{{ currency.decimal_places }} -
- {% if currency.id != defaultCurrency.id %} - - {% endif %} - {% if currency.enabled %} - - - {{ 'disable_currency'|_ }} - {% endif %} - {% if not currency.enabled %} - - - {{ 'enable_currency'|_ }} - {% endif %} -
-