2015-04-28 08:58:01 +02:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
|
* Tag.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:01:53 +01:00
|
|
|
declare(strict_types=1);
|
2015-04-28 08:58:01 +02:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
|
|
|
|
|
2026-03-06 13:52:11 +01:00
|
|
|
use Carbon\Carbon;
|
2024-11-09 12:19:01 +01:00
|
|
|
use FireflyIII\Casts\SeparateTimezoneCaster;
|
2026-02-02 20:08:37 +01:00
|
|
|
use FireflyIII\Handlers\Observer\DeletedTagObserver;
|
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;
|
2026-06-13 15:09:34 +02:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2025-09-14 08:55:08 +02:00
|
|
|
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
2017-03-04 07:18:35 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-08-04 17:30:06 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2019-12-30 12:12:08 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2016-12-24 17:36:51 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2016-01-09 16:09:26 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2023-12-10 06:51:59 +01:00
|
|
|
|
2026-03-06 13:52:11 +01:00
|
|
|
/**
|
2026-03-06 15:31:45 +01:00
|
|
|
* @property null|Carbon $date
|
2026-03-06 13:52:11 +01:00
|
|
|
*/
|
2026-02-02 20:08:37 +01:00
|
|
|
#[ObservedBy([DeletedTagObserver::class])]
|
2017-03-04 07:18:35 +01:00
|
|
|
class Tag extends Model
|
2015-04-28 08:58:01 +02:00
|
|
|
{
|
2023-11-05 19:41:37 +01:00
|
|
|
use ReturnsIntegerIdTrait;
|
|
|
|
|
use ReturnsIntegerUserIdTrait;
|
2023-11-05 20:18:16 +01:00
|
|
|
use SoftDeletes;
|
2017-03-04 07:18:35 +01:00
|
|
|
|
2025-07-10 07:04:20 +02:00
|
|
|
protected $fillable = ['user_id', 'user_group_id', 'tag', 'date', 'date_tz', 'description', 'tag_mode'];
|
2019-12-30 12:12:08 +01:00
|
|
|
|
2026-01-23 15:14:29 +01:00
|
|
|
protected $hidden = ['zoomLevel', 'zoom_level', 'latitude', 'longitude'];
|
2019-12-30 12:12:08 +01:00
|
|
|
|
2015-06-14 11:52:07 +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-03-21 09:15:40 +01:00
|
|
|
* @throws NotFoundHttpException
|
2015-06-14 11:52:07 +02:00
|
|
|
*/
|
2026-01-25 09:02:47 +01:00
|
|
|
public static function routeBinder(self|string $value): self
|
2015-06-14 11:52:07 +02:00
|
|
|
{
|
2026-01-25 10:55:27 +01:00
|
|
|
if ($value instanceof self) {
|
|
|
|
|
$value = (int) $value->id;
|
2026-01-25 09:02:47 +01:00
|
|
|
}
|
2018-02-09 19:28:16 +01:00
|
|
|
if (auth()->check()) {
|
2026-01-23 15:09:50 +01:00
|
|
|
$tagId = (int) $value;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-07-22 16:35:46 +02:00
|
|
|
/** @var User $user */
|
2026-01-23 15:14:29 +01:00
|
|
|
$user = auth()->user();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
|
/** @var null|Tag $tag */
|
2026-01-23 15:14:29 +01:00
|
|
|
$tag = $user->tags()->find($tagId);
|
2018-04-02 15:10:40 +02:00
|
|
|
if (null !== $tag) {
|
2017-12-25 08:45:23 +01:00
|
|
|
return $tag;
|
2016-01-27 19:35:00 +01:00
|
|
|
}
|
2026-08-09 19:07:13 +02:00
|
|
|
|
2026-08-09 06:35:47 +02:00
|
|
|
throw new NotFoundHttpException();
|
2015-06-14 11:52:07 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2026-06-13 15:09:34 +02:00
|
|
|
throw new AuthenticationException();
|
2015-06-14 11:52:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
public function attachments(): MorphMany
|
2020-03-17 16:46:00 +01:00
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
return $this->morphMany(Attachment::class, 'attachable');
|
2020-03-17 16:46:00 +01:00
|
|
|
}
|
2015-04-28 08:58:01 +02:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
public function locations(): MorphMany
|
2020-03-19 08:32:42 +01:00
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
return $this->morphMany(Location::class, 'locatable');
|
2020-03-19 08:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-06 13:55:17 +01:00
|
|
|
public function primaryPeriodStatistics(): MorphMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(PeriodStatistic::class, 'primary_statable');
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-04 17:30:06 +02:00
|
|
|
public function transactionJournals(): BelongsToMany
|
2015-04-28 08:58:01 +02:00
|
|
|
{
|
2018-04-28 06:23:13 +02:00
|
|
|
return $this->belongsToMany(TransactionJournal::class);
|
2015-04-28 08:58:01 +02:00
|
|
|
}
|
2025-05-29 19:58:55 +02:00
|
|
|
|
2026-02-06 13:55:17 +01:00
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-29 15:01:06 +02:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'created_at' => 'datetime',
|
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
|
'deleted_at' => 'datetime',
|
|
|
|
|
'date' => SeparateTimezoneCaster::class,
|
|
|
|
|
'zoomLevel' => 'int',
|
|
|
|
|
'latitude' => 'float',
|
|
|
|
|
'longitude' => 'float',
|
|
|
|
|
'user_id' => 'integer',
|
2026-01-23 15:14:29 +01:00
|
|
|
'user_group_id' => 'integer',
|
2025-05-29 15:01:06 +02:00
|
|
|
];
|
|
|
|
|
}
|
2015-05-05 12:51:57 +02:00
|
|
|
}
|