187 lines
5.3 KiB
PHP
187 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
|
|
/**
|
|
* @property integer $id Directory id.
|
|
* @property array<string,string> $name Directory name.
|
|
* @property integer|null $directory_id Associated directory.
|
|
* @property integer $order Directory order.
|
|
* @property boolean $needAuthorization Directory authorization.
|
|
* @property boolean $is_system Directory system status.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Directory extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'directory_id',
|
|
'order',
|
|
'needAuthorization',
|
|
'is_system'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'name' => 'array'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setIsSystem($model);
|
|
self::setOrder($model);
|
|
});
|
|
static::deleting(function (self $model) {
|
|
$model->appusers()->detach();
|
|
foreach ($model->documents as $document) {
|
|
$document->delete();
|
|
};
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* 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()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setIsSystem(self $model): void
|
|
{
|
|
$model->is_system = ($model->is_system) ? $model->is_system : 0;
|
|
}
|
|
|
|
/**
|
|
* Render directories.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Collection $directories
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function renderDirectories(Collection $directories): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
foreach ($directories as $k => $directory) {
|
|
/** @var \App\Models\Directory $directory */
|
|
$directory = $directories[$k];
|
|
$directory->name = Directory::renameDirectory($directory, "");
|
|
}
|
|
return $directories;
|
|
}
|
|
|
|
/**
|
|
* Rename directory.
|
|
*
|
|
* @param Directory $model
|
|
* @param string $nameDirectory
|
|
* @return array|string
|
|
*/
|
|
private static function renameDirectory(Directory $model, string $nameDirectory): array|string
|
|
{
|
|
if (strlen($nameDirectory)) {
|
|
$nameDirectory = $model->name['fr'] . " / " . $nameDirectory;
|
|
} else {
|
|
$nameDirectory = $model->name['fr'];
|
|
}
|
|
if (isset($model->parent)) {
|
|
/** @var \App\Models\Directory $parent */
|
|
$parent = $model->parent;
|
|
$nameDirectory = $model->renameDirectory($parent, $nameDirectory);
|
|
}
|
|
return $nameDirectory;
|
|
}
|
|
|
|
/**
|
|
* Rename directory.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getChildrenDirectories(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection $directories */
|
|
$directories = Directory::query()->orderBy('order', 'ASC')->get();
|
|
// @phpstan-ignore argument.type
|
|
$directoriesFiltered = $directories->filter(function (Directory $directory) {
|
|
if ($directory->children->count() === 0) {
|
|
return $directory;
|
|
}
|
|
});
|
|
return Directory::renderDirectories($directoriesFiltered);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* One To Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function parent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Directory::class, 'directory_id');
|
|
}
|
|
|
|
/**
|
|
* Has Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Directory::class, 'directory_id');
|
|
}
|
|
|
|
/**
|
|
* MorphedByMany appusers relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function appusers(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(AppUser::class, 'directorable');
|
|
}
|
|
|
|
/**
|
|
* Has Many Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function documents(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Document::class, 'directory_id');
|
|
}
|
|
}
|