mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Add and remove exchange rates
This commit is contained in:
51
app/Support/Binder/UserGroupExchangeRate.php
Normal file
51
app/Support/Binder/UserGroupExchangeRate.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* UserGroupTransaction.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Routing\Route;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Class UserGroupTransaction.
|
||||
*/
|
||||
class UserGroupExchangeRate implements BinderInterface
|
||||
{
|
||||
public static function routeBinder(string $value, Route $route): CurrencyExchangeRate
|
||||
{
|
||||
if (auth()->check()) {
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$rate = CurrencyExchangeRate::where('id', (int) $value)
|
||||
->where('user_group_id', $user->user_group_id)
|
||||
->first();
|
||||
if (null !== $rate) {
|
||||
return $rate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ class Domain
|
||||
{
|
||||
public static function getBindables(): array
|
||||
{
|
||||
return config('firefly.bindables');
|
||||
return config('bindables.bindables');
|
||||
}
|
||||
|
||||
public static function getRuleActions(): array
|
||||
|
||||
@@ -47,16 +47,15 @@ class Preferences
|
||||
}
|
||||
|
||||
return Preference::where('user_id', $user->id)
|
||||
->where('name', '!=', 'currencyPreference')
|
||||
->where(function (Builder $q) use ($user): void {
|
||||
$q->whereNull('user_group_id');
|
||||
$q->orWhere('user_group_id', $user->user_group_id);
|
||||
})
|
||||
->get()
|
||||
;
|
||||
->where('name', '!=', 'currencyPreference')
|
||||
->where(function (Builder $q) use ($user): void {
|
||||
$q->whereNull('user_group_id');
|
||||
$q->orWhere('user_group_id', $user->user_group_id);
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
public function get(string $name, null|array|bool|int|string $default = null): ?Preference
|
||||
public function get(string $name, null | array | bool | int | string $default = null): ?Preference
|
||||
{
|
||||
/** @var null|User $user */
|
||||
$user = auth()->user();
|
||||
@@ -70,7 +69,7 @@ class Preferences
|
||||
return $this->getForUser($user, $name, $default);
|
||||
}
|
||||
|
||||
public function getForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
|
||||
public function getForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
|
||||
{
|
||||
// don't care about user group ID, except for some specific preferences.
|
||||
$userGroupId = $this->getUserGroupId($user, $name);
|
||||
@@ -122,14 +121,16 @@ class Preferences
|
||||
Cache::put($key, '', 5);
|
||||
}
|
||||
|
||||
public function setForUser(User $user, string $name, null|array|bool|int|string $value): Preference
|
||||
public function setForUser(User $user, string $name, null | array | bool | int | string $value): Preference
|
||||
{
|
||||
$fullName = sprintf('preference%s%s', $user->id, $name);
|
||||
$groupId = $this->getUserGroupId($user, $name);
|
||||
$fullName = sprintf('preference%s%s', $user->id, $name);
|
||||
$groupId = $this->getUserGroupId($user, $name);
|
||||
$groupId = 0 === (int)$groupId ? null : (int) $groupId;
|
||||
|
||||
Cache::forget($fullName);
|
||||
|
||||
/** @var null|Preference $pref */
|
||||
$pref = Preference::where('user_group_id', $groupId)->where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
|
||||
$pref = Preference::where('user_group_id', $groupId)->where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
|
||||
|
||||
if (null !== $pref && null === $value) {
|
||||
$pref->delete();
|
||||
@@ -144,6 +145,7 @@ class Preferences
|
||||
$pref->user_id = (int) $user->id;
|
||||
$pref->user_group_id = $groupId;
|
||||
$pref->name = $name;
|
||||
|
||||
}
|
||||
$pref->data = $value;
|
||||
$pref->save();
|
||||
@@ -171,13 +173,12 @@ class Preferences
|
||||
{
|
||||
$result = [];
|
||||
$preferences = Preference::where('user_id', $user->id)
|
||||
->where(function (Builder $q) use ($user): void {
|
||||
$q->whereNull('user_group_id');
|
||||
$q->orWhere('user_group_id', $user->user_group_id);
|
||||
})
|
||||
->whereIn('name', $list)
|
||||
->get(['id', 'name', 'data'])
|
||||
;
|
||||
->where(function (Builder $q) use ($user): void {
|
||||
$q->whereNull('user_group_id');
|
||||
$q->orWhere('user_group_id', $user->user_group_id);
|
||||
})
|
||||
->whereIn('name', $list)
|
||||
->get(['id', 'name', 'data']);
|
||||
|
||||
/** @var Preference $preference */
|
||||
foreach ($preferences as $preference) {
|
||||
@@ -215,7 +216,7 @@ class Preferences
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getEncryptedForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
|
||||
public function getEncryptedForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
|
||||
{
|
||||
$result = $this->getForUser($user, $name, $default);
|
||||
if ('' === $result->data) {
|
||||
@@ -236,7 +237,7 @@ class Preferences
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getFresh(string $name, null|array|bool|int|string $default = null): ?Preference
|
||||
public function getFresh(string $name, null | array | bool | int | string $default = null): ?Preference
|
||||
{
|
||||
/** @var null|User $user */
|
||||
$user = auth()->user();
|
||||
@@ -287,7 +288,7 @@ class Preferences
|
||||
return $this->set($name, $encrypted);
|
||||
}
|
||||
|
||||
public function set(string $name, null|array|bool|int|string $value): Preference
|
||||
public function set(string $name, null | array | bool | int | string $value): Preference
|
||||
{
|
||||
/** @var null|User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
Reference in New Issue
Block a user