2023-03-15 16:04:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
2023-03-31 15:20:10 +02:00
|
|
|
/**
|
|
|
|
|
* @property integer $id
|
|
|
|
|
* @property string $name
|
2023-04-05 17:20:30 +02:00
|
|
|
* @property integer $directory_id
|
2023-03-31 15:20:10 +02:00
|
|
|
* @property integer $order
|
|
|
|
|
* @property boolean $needAuthorization
|
|
|
|
|
*/
|
2023-03-15 16:04:29 +01:00
|
|
|
class Directory extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2023-03-31 15:20:10 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fillable;
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
2023-04-05 17:20:30 +02:00
|
|
|
'directory_id',
|
2023-03-31 15:20:10 +02:00
|
|
|
'order',
|
2023-07-13 08:34:54 +00:00
|
|
|
'needAuthorization',
|
|
|
|
|
'is_system'
|
2023-03-31 15:20:10 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'name' => 'array'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
2023-07-13 08:34:54 +00:00
|
|
|
self::setIsSystem($model);
|
2023-03-31 15:20:10 +02:00
|
|
|
self::setOrder($model);
|
|
|
|
|
});
|
2023-04-05 17:20:30 +02:00
|
|
|
static::deleting(function (self $model) {
|
|
|
|
|
$model->appusers()->detach();
|
|
|
|
|
foreach ($model->documents as $key => $document) {
|
|
|
|
|
$document->delete();
|
|
|
|
|
};
|
2023-03-31 15:20:10 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-07-13 08:34:54 +00:00
|
|
|
* Set order after the last element of the list.
|
2023-03-31 15:20:10 +02:00
|
|
|
*
|
2023-07-13 08:34:54 +00:00
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
|
|
|
* @return void
|
2023-03-31 15:20:10 +02:00
|
|
|
*/
|
2023-07-13 08:34:54 +00:00
|
|
|
private static function setIsSystem(Model $model): void
|
2023-03-31 15:20:10 +02:00
|
|
|
{
|
2023-07-13 08:34:54 +00:00
|
|
|
$model->is_system = ($model->is_system) ? $model->is_system : 0;
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-31 17:38:44 +02:00
|
|
|
/**
|
|
|
|
|
* Render directories.
|
|
|
|
|
*
|
2023-07-13 11:46:23 +02:00
|
|
|
* @param \Illuminate\Database\Eloquent\Collection | array $directories
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection | array
|
2023-03-31 17:38:44 +02:00
|
|
|
*/
|
2023-07-13 11:46:23 +02:00
|
|
|
public static function renderDirectories($directories)
|
2023-03-31 17:38:44 +02:00
|
|
|
{
|
|
|
|
|
foreach ($directories as $k => $directory) {
|
|
|
|
|
$directories[$k]->name = Directory::renameDirectory($directory, "");
|
|
|
|
|
}
|
|
|
|
|
return $directories;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rename directory.
|
|
|
|
|
*
|
|
|
|
|
* @param Directory $model
|
|
|
|
|
* @param string $nameDirectory
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private static function renameDirectory(Directory $model, string $nameDirectory)
|
|
|
|
|
{
|
|
|
|
|
if (strlen($nameDirectory)) {
|
|
|
|
|
$nameDirectory = $model->name['fr'] . " / " . $nameDirectory;
|
|
|
|
|
} else {
|
|
|
|
|
$nameDirectory = $model->name['fr'];
|
|
|
|
|
}
|
|
|
|
|
if (isset($model->parent)) {
|
|
|
|
|
$nameDirectory = $model->renameDirectory($model->parent, $nameDirectory);
|
|
|
|
|
}
|
|
|
|
|
return $nameDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 17:20:30 +02:00
|
|
|
/**
|
|
|
|
|
* Rename directory.
|
|
|
|
|
*
|
|
|
|
|
* @return LaravelModelList
|
|
|
|
|
*/
|
|
|
|
|
public static function getChildrenDirectories()
|
|
|
|
|
{
|
|
|
|
|
$directories = [];
|
|
|
|
|
foreach (Directory::orderBy('order', 'ASC')->get()->toArray() as $key => $value) {
|
|
|
|
|
if (count(Directory::get()[$key]->children) === 0) {
|
|
|
|
|
array_push($directories, Directory::get()[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Directory::renderDirectories($directories);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 15:20:10 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* One To Many Relationship Method.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
|
|
|
|
public function parent()
|
|
|
|
|
{
|
2023-04-05 17:20:30 +02:00
|
|
|
return $this->belongsTo('App\Models\Directory', 'directory_id');
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Has Many Relationship Method.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function children()
|
|
|
|
|
{
|
2023-04-05 17:20:30 +02:00
|
|
|
return $this->hasMany('App\Models\Directory', 'directory_id');
|
2023-03-31 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MorphedByMany appusers relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphedByMany
|
|
|
|
|
*/
|
|
|
|
|
public function appusers()
|
|
|
|
|
{
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function documents()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany('App\Models\Document', 'directory_id');
|
|
|
|
|
}
|
2023-03-15 16:04:29 +01:00
|
|
|
}
|