2023-03-15 16:04:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-05-20 10:16:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2023-03-15 16:04:29 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2025-05-20 16:20:23 +02:00
|
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
2023-03-15 16:04:29 +01:00
|
|
|
|
2023-03-31 15:20:10 +02:00
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* @property integer $id Directory id.
|
|
|
|
|
* @property array<string,string> $name Directory name.
|
2025-05-26 11:33:47 +02:00
|
|
|
* @property string|null $tenant_id Associated tenant.
|
2025-05-20 10:16:12 +02:00
|
|
|
* @property integer|null $directory_id Associated directory.
|
|
|
|
|
* @property integer $order Directory order.
|
|
|
|
|
* @property boolean $needAuthorization Directory authorization.
|
|
|
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
2023-03-31 15:20:10 +02:00
|
|
|
*/
|
2023-03-15 16:04:29 +01:00
|
|
|
class Directory extends Model
|
|
|
|
|
{
|
2025-05-20 16:20:23 +02:00
|
|
|
use BelongsToTenant;
|
2023-03-15 16:04:29 +01:00
|
|
|
use HasFactory;
|
2023-03-31 15:20:10 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* The attributes that are mass assignable.
|
2023-03-31 15:20:10 +02:00
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var list<string>
|
2023-03-31 15:20:10 +02:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
2023-04-05 17:20:30 +02:00
|
|
|
'directory_id',
|
2023-03-31 15:20:10 +02:00
|
|
|
'order',
|
2025-09-05 10:21:40 +02:00
|
|
|
'needAuthorization'
|
2023-03-31 15:20:10 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var array<string, string>
|
2023-03-31 15:20:10 +02:00
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'name' => 'array'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
protected static function booted(): void
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
|
|
|
|
self::setOrder($model);
|
|
|
|
|
});
|
2023-04-05 17:20:30 +02:00
|
|
|
static::deleting(function (self $model) {
|
|
|
|
|
$model->appusers()->detach();
|
2025-05-20 10:16:12 +02:00
|
|
|
foreach ($model->documents as $document) {
|
2023-04-05 17:20:30 +02:00
|
|
|
$document->delete();
|
|
|
|
|
};
|
2023-03-31 15:20:10 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set order after the last element of the list.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @param self $model
|
2023-03-31 15:20:10 +02:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function setOrder(self $model): void
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
$model->order = \intval(
|
|
|
|
|
self::query()->where('tenant_id', \tenant() ?? Tenant::first()->getKey())->max('order')
|
|
|
|
|
) + 1;
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-31 17:38:44 +02:00
|
|
|
/**
|
|
|
|
|
* Render directories.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $directories
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
2023-03-31 17:38:44 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public static function renderDirectories(Collection $directories): \Illuminate\Database\Eloquent\Collection
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
|
|
|
|
foreach ($directories as $k => $directory) {
|
2025-05-20 10:16:12 +02:00
|
|
|
/** @var \App\Models\Directory $directory */
|
|
|
|
|
$directory = $directories[$k];
|
|
|
|
|
$directory->name = Directory::renameDirectory($directory, "");
|
2023-03-31 17:38:44 +02:00
|
|
|
}
|
|
|
|
|
return $directories;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rename directory.
|
|
|
|
|
*
|
|
|
|
|
* @param Directory $model
|
|
|
|
|
* @param string $nameDirectory
|
2025-05-20 10:16:12 +02:00
|
|
|
* @return array|string
|
2023-03-31 17:38:44 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function renameDirectory(Directory $model, string $nameDirectory): array|string
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
|
|
|
|
if (strlen($nameDirectory)) {
|
|
|
|
|
$nameDirectory = $model->name['fr'] . " / " . $nameDirectory;
|
|
|
|
|
} else {
|
|
|
|
|
$nameDirectory = $model->name['fr'];
|
|
|
|
|
}
|
|
|
|
|
if (isset($model->parent)) {
|
2025-05-20 10:16:12 +02:00
|
|
|
/** @var \App\Models\Directory $parent */
|
|
|
|
|
$parent = $model->parent;
|
|
|
|
|
$nameDirectory = $model->renameDirectory($parent, $nameDirectory);
|
2023-03-31 17:38:44 +02:00
|
|
|
}
|
|
|
|
|
return $nameDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 17:20:30 +02:00
|
|
|
/**
|
|
|
|
|
* Rename directory.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
2023-04-05 17:20:30 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public static function getChildrenDirectories(): \Illuminate\Database\Eloquent\Collection
|
2023-04-05 17:20:30 +02:00
|
|
|
{
|
2025-05-20 10:16:12 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $directories */
|
2026-10-20 16:03:44 +02:00
|
|
|
$directories = Directory::query()->orderBy('order', 'asc')->get();
|
2025-05-20 10:16:12 +02:00
|
|
|
// @phpstan-ignore argument.type
|
|
|
|
|
$directoriesFiltered = $directories->filter(function (Directory $directory) {
|
|
|
|
|
if ($directory->children->count() === 0) {
|
|
|
|
|
return $directory;
|
2023-04-05 17:20:30 +02:00
|
|
|
}
|
2025-05-20 10:16:12 +02:00
|
|
|
});
|
|
|
|
|
return Directory::renderDirectories($directoriesFiltered);
|
2023-04-05 17:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-31 15:20:10 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* One To Many Relationship Method.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function parent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
2023-09-13 16:04:12 +02:00
|
|
|
return $this->belongsTo(Directory::class, 'directory_id');
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Has Many Relationship Method.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
2023-09-13 16:04:12 +02:00
|
|
|
return $this->hasMany(Directory::class, 'directory_id');
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* MorphedByMany appusers relation.
|
2023-03-31 15:20:10 +02:00
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
2023-03-31 15:20:10 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function appusers(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
2023-07-13 08:34:54 +00:00
|
|
|
return $this->morphedByMany(AppUser::class, 'directorable');
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
2023-03-31 17:38:44 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Has Many Relationship Method.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
public function documents(): \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, 'directory_id');
|
2023-03-31 17:38:44 +02:00
|
|
|
}
|
2023-03-15 16:04:29 +01:00
|
|
|
}
|