2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* Preference.php
|
2020-02-16 13:55:32 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-03-24 15:15:12 +01:00
|
|
|
declare(strict_types=1);
|
2016-05-20 08:57:45 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
2015-02-06 04:52:16 +01:00
|
|
|
|
2023-12-10 06:51:59 +01:00
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
|
2023-11-05 19:41:37 +01:00
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;
|
2018-06-06 21:23:00 +02:00
|
|
|
use FireflyIII\User;
|
2015-02-06 04:52:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-08-04 17:30:06 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2018-06-29 12:11:44 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2023-12-10 06:51:59 +01:00
|
|
|
|
2015-02-06 05:04:06 +01:00
|
|
|
class Preference extends Model
|
|
|
|
{
|
2023-11-05 19:41:37 +01:00
|
|
|
use ReturnsIntegerIdTrait;
|
|
|
|
use ReturnsIntegerUserIdTrait;
|
2023-11-05 09:54:53 +01:00
|
|
|
|
2016-12-24 17:36:51 +01:00
|
|
|
protected $casts
|
2024-12-27 07:29:17 +01:00
|
|
|
= [
|
2024-03-18 20:25:30 +01:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
'data' => 'array',
|
|
|
|
];
|
2017-11-03 16:04:17 +01:00
|
|
|
|
2024-12-27 07:24:47 +01:00
|
|
|
protected $fillable = ['user_id', 'data', 'name', 'user_group_id'];
|
2016-01-15 23:12:52 +01:00
|
|
|
|
2018-06-29 12:11:44 +02:00
|
|
|
/**
|
2018-08-04 17:30:06 +02:00
|
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
|
|
*
|
2021-04-13 19:01:43 +02:00
|
|
|
* @throws NotFoundHttpException
|
2018-06-29 12:11:44 +02:00
|
|
|
*/
|
2023-11-04 14:18:49 +01:00
|
|
|
public static function routeBinder(string $value): self
|
2018-06-29 12:11:44 +02:00
|
|
|
{
|
|
|
|
if (auth()->check()) {
|
2018-07-22 16:35:46 +02:00
|
|
|
/** @var User $user */
|
2024-12-27 07:29:17 +01:00
|
|
|
$user = auth()->user();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2024-04-01 19:59:21 +02:00
|
|
|
// some preferences do not have an administration ID.
|
|
|
|
// some need it, to make sure the correct one is selected.
|
2024-12-22 08:43:12 +01:00
|
|
|
$userGroupId = (int) $user->user_group_id;
|
2024-04-02 07:47:24 +02:00
|
|
|
$userGroupId = 0 === $userGroupId ? null : $userGroupId;
|
|
|
|
|
|
|
|
/** @var null|Preference $preference */
|
2024-12-27 07:29:17 +01:00
|
|
|
$preference = null;
|
|
|
|
$items = config('firefly.admin_specific_prefs');
|
2024-04-01 19:59:21 +02:00
|
|
|
if (null !== $userGroupId && in_array($value, $items, true)) {
|
|
|
|
// find a preference with a specific user_group_id
|
|
|
|
$preference = $user->preferences()->where('user_group_id', $userGroupId)->where('name', $value)->first();
|
|
|
|
}
|
|
|
|
if (!in_array($value, $items, true)) {
|
|
|
|
// find any one.
|
|
|
|
$preference = $user->preferences()->where('name', $value)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
// try again with ID, but this time don't care about the preferred user_group_id
|
2022-06-06 14:40:19 +02:00
|
|
|
if (null === $preference) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$preference = $user->preferences()->where('id', (int) $value)->first();
|
2022-06-06 14:40:19 +02:00
|
|
|
}
|
2018-06-29 12:11:44 +02:00
|
|
|
if (null !== $preference) {
|
2025-01-04 07:10:37 +01:00
|
|
|
/** @var Preference $preference */
|
2018-06-29 12:11:44 +02:00
|
|
|
return $preference;
|
|
|
|
}
|
2024-12-27 07:29:17 +01:00
|
|
|
$default = config('firefly.default_preferences');
|
2021-04-13 19:01:43 +02:00
|
|
|
if (array_key_exists($value, $default)) {
|
2024-04-01 19:59:21 +02:00
|
|
|
$preference = new self();
|
|
|
|
$preference->name = $value;
|
|
|
|
$preference->data = $default[$value];
|
2024-12-22 08:43:12 +01:00
|
|
|
$preference->user_id = (int) $user->id;
|
2024-04-01 19:59:21 +02:00
|
|
|
$preference->user_group_id = in_array($value, $items, true) ? $userGroupId : null;
|
2021-04-13 19:01:43 +02:00
|
|
|
$preference->save();
|
2025-01-04 07:39:16 +01:00
|
|
|
|
2021-04-13 19:01:43 +02:00
|
|
|
return $preference;
|
|
|
|
}
|
2018-06-29 12:11:44 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-10-30 14:24:28 +01:00
|
|
|
throw new NotFoundHttpException();
|
2018-06-29 12:11:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 17:30:06 +02:00
|
|
|
public function user(): BelongsTo
|
2015-02-06 05:14:27 +01:00
|
|
|
{
|
2018-04-14 09:59:04 +02:00
|
|
|
return $this->belongsTo(User::class);
|
2015-02-06 05:14:27 +01:00
|
|
|
}
|
2015-02-06 04:52:16 +01:00
|
|
|
}
|