2015-07-18 02:49:19 -05:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Attachment.php
|
2020-02-16 06:55:32 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-07-18 02:49:19 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
|
|
|
|
2023-11-05 12:41:37 -06:00
|
|
|
use Carbon\Carbon;
|
2020-03-17 10:46:00 -05:00
|
|
|
use Eloquent;
|
2023-11-05 12:41:37 -06:00
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
|
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;
|
2018-06-06 14:23:00 -05:00
|
|
|
use FireflyIII\User;
|
2023-11-05 12:41:37 -06:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-03-17 10:46:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2015-07-18 02:49:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-04-06 02:27:45 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2018-07-25 23:10:17 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2016-04-06 02:27:45 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
2015-07-18 02:49:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2020-03-17 10:46:00 -05:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2016-01-09 08:53:11 -06:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2015-07-18 02:49:19 -05:00
|
|
|
|
2016-11-18 13:06:08 -06:00
|
|
|
/**
|
2020-12-03 23:20:44 -06:00
|
|
|
* FireflyIII\Models\Attachment
|
2018-06-23 23:51:22 -05:00
|
|
|
*
|
2023-12-20 12:35:52 -06:00
|
|
|
* @property int $id
|
|
|
|
* @property null|Carbon $created_at
|
|
|
|
* @property null|Carbon $updated_at
|
|
|
|
* @property null|Carbon $deleted_at
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int $attachable_id
|
|
|
|
* @property string $attachable_type
|
|
|
|
* @property bool $file_exists
|
|
|
|
* @property string $md5
|
|
|
|
* @property string $filename
|
|
|
|
* @property null|string $title
|
|
|
|
* @property null|string $description
|
|
|
|
* @property string $mime
|
|
|
|
* @property int|string $size
|
|
|
|
* @property bool $uploaded
|
|
|
|
* @property string $notes_text
|
|
|
|
* @property \Eloquent|Model $attachable
|
|
|
|
* @property Collection|Note[] $notes
|
|
|
|
* @property null|int $notes_count
|
|
|
|
* @property User $user
|
|
|
|
*
|
2020-03-17 10:46:00 -05:00
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment newQuery()
|
2023-12-20 12:35:52 -06:00
|
|
|
* @method static Builder|Attachment onlyTrashed()
|
2020-03-17 10:46:00 -05:00
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereAttachableId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereAttachableType($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereDeletedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereDescription($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereFilename($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereMd5($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereMime($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereSize($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereTitle($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereUploaded($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereUserId($value)
|
2023-12-20 12:35:52 -06:00
|
|
|
* @method static Builder|Attachment withTrashed()
|
|
|
|
* @method static Builder|Attachment withoutTrashed()
|
|
|
|
*
|
|
|
|
* @property int $user_group_id
|
|
|
|
*
|
2021-09-18 03:08:10 -05:00
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Attachment whereUserGroupId($value)
|
2023-12-20 12:35:52 -06:00
|
|
|
*
|
2023-03-08 23:33:23 -06:00
|
|
|
* @mixin Eloquent
|
2016-11-18 13:06:08 -06:00
|
|
|
*/
|
2015-07-18 02:49:19 -05:00
|
|
|
class Attachment extends Model
|
|
|
|
{
|
2023-11-05 12:41:37 -06:00
|
|
|
use ReturnsIntegerIdTrait;
|
|
|
|
use ReturnsIntegerUserIdTrait;
|
2023-11-05 13:18:16 -06:00
|
|
|
use SoftDeletes;
|
2023-11-05 02:54:53 -06:00
|
|
|
|
2016-12-24 10:36:51 -06:00
|
|
|
protected $casts
|
|
|
|
= [
|
2017-11-03 10:04:17 -05:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
'deleted_at' => 'datetime',
|
2016-12-24 10:36:51 -06:00
|
|
|
'uploaded' => 'boolean',
|
|
|
|
];
|
2023-11-05 02:54:53 -06:00
|
|
|
|
2018-03-19 09:28:35 -05:00
|
|
|
protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'description', 'size', 'uploaded'];
|
2015-07-18 02:49:19 -05:00
|
|
|
|
2016-02-22 23:54:51 -06:00
|
|
|
/**
|
2018-08-04 10:30:06 -05:00
|
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
|
|
*
|
2021-04-06 22:55:51 -05:00
|
|
|
* @throws NotFoundHttpException
|
2016-02-22 23:54:51 -06:00
|
|
|
*/
|
2023-11-04 08:18:49 -05:00
|
|
|
public static function routeBinder(string $value): self
|
2016-02-22 23:54:51 -06:00
|
|
|
{
|
2018-02-09 12:28:16 -06:00
|
|
|
if (auth()->check()) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$attachmentId = (int)$value;
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2018-07-22 09:35:46 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
2023-12-20 12:35:52 -06:00
|
|
|
|
|
|
|
/** @var null|Attachment $attachment */
|
2018-08-06 12:14:30 -05:00
|
|
|
$attachment = $user->attachments()->find($attachmentId);
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null !== $attachment) {
|
2017-12-25 01:45:23 -06:00
|
|
|
return $attachment;
|
2016-02-22 23:54:51 -06:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2022-10-30 08:24:28 -05:00
|
|
|
throw new NotFoundHttpException();
|
2016-02-22 23:54:51 -06:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:49:19 -05:00
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* Get all of the owning attachable models.
|
2015-07-18 02:49:19 -05:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function attachable(): MorphTo
|
2015-07-18 02:49:19 -05:00
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-22 23:54:51 -06:00
|
|
|
* Returns the expected filename for this attachment.
|
2015-07-18 02:49:19 -05:00
|
|
|
*/
|
2016-02-22 23:54:51 -06:00
|
|
|
public function fileName(): string
|
2015-07-18 02:49:19 -05:00
|
|
|
{
|
2022-12-29 12:42:26 -06:00
|
|
|
return sprintf('at-%s.data', (string)$this->id);
|
2015-07-18 02:49:19 -05:00
|
|
|
}
|
|
|
|
|
2015-07-19 02:37:28 -05:00
|
|
|
/**
|
2023-09-21 04:29:09 -05:00
|
|
|
* Get all the notes.
|
2015-07-19 02:37:28 -05:00
|
|
|
*/
|
2018-07-25 23:10:17 -05:00
|
|
|
public function notes(): MorphMany
|
2015-07-19 02:37:28 -05:00
|
|
|
{
|
2018-03-19 09:28:35 -05:00
|
|
|
return $this->morphMany(Note::class, 'noteable');
|
2015-07-19 02:37:28 -05:00
|
|
|
}
|
2023-11-05 12:41:37 -06:00
|
|
|
|
|
|
|
protected function attachableId(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-12-20 12:35:52 -06:00
|
|
|
get: static fn ($value) => (int)$value,
|
2023-11-05 12:41:37 -06:00
|
|
|
);
|
|
|
|
}
|
2015-07-19 02:38:44 -05:00
|
|
|
}
|