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

182 lines
4.3 KiB
PHP
Raw Normal View History

2023-03-31 17:38:44 +02:00
<?php
namespace App\Models;
use App\Enums\TypeEnum;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-04-05 17:20:30 +02:00
use Illuminate\Support\Facades\Storage;
2023-03-31 17:38:44 +02:00
/**
* @property integer $id
* @property integer $uuid
* @property string $title
* @property TypeEnum $type
2023-03-31 17:38:44 +02:00
* @property integer $directory_id
2023-04-05 17:20:30 +02:00
* @property integer $document_id
2023-03-31 17:38:44 +02:00
* @property string $path
* @property LangEnum $lang
2023-03-31 17:38:44 +02:00
* @property boolean $status
* @property integer $order
*/
class Document extends Model
{
use HasFactory;
/**
* Fillable;
*
* @var string[]
*/
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
*/
protected $casts = [
'status' => 'bool'
];
2023-03-31 17:38:44 +02:00
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
2023-04-05 17:20:30 +02:00
'lang' => LangEnum::class,
2023-03-31 17:38:44 +02:00
'type' => TypeEnum::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
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) {
$path = \ltrim(\urldecode("docs/" . $model->getOriginal('uuid') . $model->getOriginal('type')), '/');
if (Storage::disk('public')->exists($path)) {
Storage::disk('public')->delete($path);
}
});
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)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function setOrder(Model $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
2023-04-05 17:20:30 +02:00
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
2023-07-13 08:34:54 +00:00
public static function removeDocument(Model $model): void
2023-04-05 17:20:30 +02:00
{
$path = \ltrim(\urldecode("storage/docs/" . $model->uuid . $model->type), '/');
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()
{
return $this->belongsTo('App\Models\Directory', 'directory_id');
}
/**
* One To Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
2023-07-13 08:34:54 +00:00
return $this->belongsTo('App\Models\Document', 'document_id');
2023-03-31 17:38:44 +02:00
}
/**
* Has Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children()
{
2023-07-13 08:34:54 +00:00
return $this->hasMany('App\Models\Document', 'document_id');
2023-03-31 17:38:44 +02:00
}
/**
* MorphedByMany appusers relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphedByMany
*/
public function appusers()
{
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
}