ldc-com-app-bo/app/Models/Document.php

189 lines
5.5 KiB
PHP
Raw Normal View History

2023-03-31 17:38:44 +02:00
<?php
namespace App\Models;
use App\Enums\Documents\LangEnum;
2025-06-17 12:29:15 +02:00
use App\Traits\Models\IsRelatedToTenant;
2023-03-31 17:38:44 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-04-05 17:20:30 +02:00
use Illuminate\Support\Facades\Storage;
2025-05-20 16:20:23 +02:00
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
2023-03-31 17:38:44 +02:00
/**
* @property integer $id Document id.
* @property integer $uuid Document uuid.
* @property string $title Document title.
* @property string $type Document type.
* @property string|null $tenant_id Associated tenant.
* @property integer $directory_id Associated directory.
* @property integer|null $document_id Associated document.
* @property string $path Document path.
* @property \App\Enums\Documents\LangEnum $lang Document lang.
* @property boolean $status Document status.
* @property integer $order Document order.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
2023-03-31 17:38:44 +02:00
*/
class Document extends Model
{
2025-05-20 16:20:23 +02:00
use BelongsToTenant;
2025-06-17 12:29:15 +02:00
use IsRelatedToTenant;
2023-03-31 17:38:44 +02:00
use HasFactory;
/**
* The attributes that are mass assignable.
2023-03-31 17:38:44 +02:00
*
* @var list<string>
2023-03-31 17:38:44 +02:00
*/
protected $fillable = [
'uuid',
'title',
'type',
'directory_id',
2023-04-05 17:20:30 +02:00
'document_id',
2023-03-31 17:38:44 +02:00
'path',
'lang',
'status',
'order',
];
2023-07-13 08:34:54 +00:00
/**
* The attributes that should be cast.
*
* @var array<string, string>
2023-07-13 08:34:54 +00:00
*/
protected $casts = [
'status' => 'bool',
'lang' => LangEnum::class,
2023-03-31 17:38:44 +02:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
2023-03-31 17:38:44 +02:00
{
static::creating(function (self $model) {
self::assertFieldIsUnique('title', $model->title);
self::setOrder($model);
});
2023-07-13 08:34:54 +00:00
static::updating(function (self $model) {
2025-06-17 12:29:15 +02:00
if ($model->getOriginal('uuid') !== $model->uuid) {
$path = \ltrim(\urldecode(sprintf(
'docs/%s/%s.%s',
\tenant()->getKey(),
$model->getOriginal('uuid'),
$model->getOriginal('type')
)), '/');
if (Storage::disk('public')->exists($path)) {
Storage::disk('public')->delete($path);
}
2023-07-13 08:34:54 +00:00
}
});
2023-04-05 17:20:30 +02:00
static::deleting(function (self $model) {
self::removeDocument($model);
});
2023-03-31 17:38:44 +02:00
}
// * METHODS
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null): void
2023-03-31 17:38:44 +02:00
{
$table = (new self())->getTable();
2025-06-17 12:29:15 +02:00
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id, self::whereTenantQuery());
2023-03-31 17:38:44 +02:00
}
/**
* Set order after the last element of the list.
*
* @param self $model
2023-03-31 17:38:44 +02:00
* @return void
*/
private static function setOrder(self $model): void
2023-03-31 17:38:44 +02:00
{
$model->order = \intval(
2025-06-17 12:29:15 +02:00
self::query()->where(self::whereTenantQuery())->max('order')
) + 1;
2023-03-31 17:38:44 +02:00
}
2023-04-05 17:20:30 +02:00
/**
* Set order after the last element of the list.
*
* @param self $model
2023-04-05 17:20:30 +02:00
* @return void
*/
public static function removeDocument(self $model): void
2023-04-05 17:20:30 +02:00
{
$path = \ltrim(\urldecode("storage/docs/{$model->uuid}.{$model->type}"), '/');
2023-04-05 17:20:30 +02:00
if (\strpos($path, 'storage') !== 0) {
return;
}
$path = ToolboxHelper::mbReplace('storage/', '', $path);
if (Storage::disk('public')->exists($path)) {
Storage::disk('public')->delete($path);
}
}
2023-03-31 17:38:44 +02:00
// * RELATIONSHIPS
/**
* One To Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function directory(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2023-03-31 17:38:44 +02:00
{
2023-09-13 16:04:12 +02:00
return $this->belongsTo(Directory::class, 'directory_id');
2023-03-31 17:38:44 +02:00
}
/**
* One To Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parentDocument(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2023-03-31 17:38:44 +02:00
{
2023-09-13 16:04:12 +02:00
return $this->belongsTo(Document::class, 'document_id');
2023-03-31 17:38:44 +02:00
}
/**
* Has Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
2023-03-31 17:38:44 +02:00
{
2023-09-13 16:04:12 +02:00
return $this->hasMany(Document::class, 'document_id');
2023-03-31 17:38:44 +02:00
}
/**
* MorphedByMany appusers relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
2023-03-31 17:38:44 +02:00
*/
public function appusers(): \Illuminate\Database\Eloquent\Relations\MorphToMany
2023-03-31 17:38:44 +02:00
{
return $this->morphedByMany(AppUser::class, 'taggable');
}
2023-07-13 08:34:54 +00:00
/**
* MorphToMany tags relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
2023-03-31 17:38:44 +02:00
}