179 lines
5.2 KiB
PHP
179 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Documents\LangEnum;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
class Document extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'uuid',
|
|
'title',
|
|
'type',
|
|
'directory_id',
|
|
'document_id',
|
|
'path',
|
|
'lang',
|
|
'status',
|
|
'order',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'status' => 'bool',
|
|
'lang' => LangEnum::class,
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('title', $model->title);
|
|
self::setOrder($model);
|
|
});
|
|
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);
|
|
}
|
|
});
|
|
static::deleting(function (self $model) {
|
|
self::removeDocument($model);
|
|
});
|
|
}
|
|
|
|
// * 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
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(
|
|
self::query()->where('tenant_id', \tenant() ?? Tenant::first()->getKey())->max('order')
|
|
) + 1;
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
public static function removeDocument(self $model): void
|
|
{
|
|
$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);
|
|
}
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* One To Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function directory(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Directory::class, 'directory_id');
|
|
}
|
|
|
|
/**
|
|
* One To Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function parentDocument(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Document::class, 'document_id');
|
|
}
|
|
|
|
/**
|
|
* Has Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Document::class, 'document_id');
|
|
}
|
|
|
|
/**
|
|
* MorphedByMany appusers relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function appusers(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(AppUser::class, 'taggable');
|
|
}
|
|
|
|
/**
|
|
* MorphToMany tags relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
}
|