firefly-iii/app/Models/Tag.php
2022-03-29 14:59:58 +02:00

164 lines
5.9 KiB
PHP

<?php
/**
* Tag.php
* Copyright (c) 2019 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\Models;
use Eloquent;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* FireflyIII\Models\Tag
*
* @property int $id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property int $user_id
* @property string $tag
* @property string $tagMode
* @property Carbon|null $date
* @property string|null $description
* @property float|null $latitude
* @property float|null $longitude
* @property int|null $zoomLevel
* @property-read Collection|Attachment[] $attachments
* @property-read int|null $attachments_count
* @property-read Collection|Location[] $locations
* @property-read int|null $locations_count
* @property-read Collection|TransactionJournal[] $transactionJournals
* @property-read int|null $transaction_journals_count
* @property-read User $user
* @method static \Illuminate\Database\Eloquent\Builder|Tag newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Tag newQuery()
* @method static Builder|Tag onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Tag query()
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereLatitude($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereLongitude($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereTag($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereTagMode($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereZoomLevel($value)
* @method static Builder|Tag withTrashed()
* @method static Builder|Tag withoutTrashed()
* @mixin Eloquent
* @property int|null $user_group_id
* @method static \Illuminate\Database\Eloquent\Builder|Tag whereUserGroupId($value)
*/
class Tag extends Model
{
use SoftDeletes;
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
'date' => 'date',
'zoomLevel' => 'int',
'latitude' => 'float',
'longitude' => 'float',
];
/** @var array Fields that can be filled */
protected $fillable = ['user_id', 'tag', 'date', 'description', 'tagMode'];
protected $hidden = ['zoomLevel', 'latitude', 'longitude'];
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
* @return Tag
* @throws NotFoundHttpException
*/
public static function routeBinder(string $value): Tag
{
if (auth()->check()) {
$tagId = (int) $value;
/** @var User $user */
$user = auth()->user();
/** @var Tag $tag */
$tag = $user->tags()->find($tagId);
if (null !== $tag) {
return $tag;
}
}
throw new NotFoundHttpException;
}
/**
* @codeCoverageIgnore
* @return MorphMany
*/
public function attachments(): MorphMany
{
return $this->morphMany(Attachment::class, 'attachable');
}
/**
* @codeCoverageIgnore
* @return MorphMany
*/
public function locations(): MorphMany
{
return $this->morphMany(Location::class, 'locatable');
}
/**
* @codeCoverageIgnore
* @return BelongsToMany
*/
public function transactionJournals(): BelongsToMany
{
return $this->belongsToMany(TransactionJournal::class);
}
/**
* @codeCoverageIgnore
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}