ldc-supportweb/app/Models/Project.php

117 lines
3.3 KiB
PHP
Raw Normal View History

2020-11-18 10:23:51 +01:00
<?php
namespace App\Models;
use App\Enums\CrudAction;
use App\Enums\CrudType;
use App\Enums\Role;
use App\Traits\IsCrudModel;
2020-11-18 10:23:51 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2021-04-18 10:43:36 +02:00
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
2020-11-18 10:23:51 +01:00
class Project extends Model
{
use HasFactory;
use IsCrudModel;
use SoftDeletes;
protected $crudLabelColumn = 'name';
protected $crudValueColumn = 'id';
2020-11-18 10:23:51 +01:00
2021-04-18 10:43:36 +02:00
protected $fillable = ['name'];
public function __construct($attributes = [])
{
self::$editableProperties = [
'name' => [
'label' => trans('Nom'),
'type' => CrudType::string(),
'validate' => [
'string',
'max:255'
],
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'users' => [
'label' => trans('Utilisateurs'),
'type' => CrudType::belongsToMany(),
'belongsToMany' => User::class,
'belongsToManyLabel' => 'name',
'nullable' => true,
'readonly' => function () {
return auth()->user()->role->equals(Role::admin()) ? false : true;
},
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
]
];
parent::__construct($attributes);
}
2021-04-18 10:43:36 +02:00
protected static function booted()
{
static::created(function (Project $project) {
2021-04-18 20:31:07 +02:00
$path = Project::ticketAssetsFullPath($project->slug_name);
2021-04-18 10:43:36 +02:00
if (!File::exists($path)) {
File::makeDirectory($path, 0755, true);
2021-04-18 10:43:36 +02:00
}
});
static::updating(function (Project $project) {
$oldName = Project::where('id', $project->id)->first()->name;
2021-04-18 20:31:07 +02:00
$oldPath = Project::ticketAssetsFullPath($oldName);
$newPath = Project::ticketAssetsFullPath($project->slug_name);
2021-04-18 10:43:36 +02:00
if (File::exists($oldPath)) {
File::move($oldPath, $newPath);
} else {
File::makeDirectory($newPath, 0755, true);
2021-04-18 10:43:36 +02:00
}
});
static::deleted(function (Project $project) {
2021-04-18 20:31:07 +02:00
$path = Project::ticketAssetsFullPath($project->slug_name);
2021-04-18 10:43:36 +02:00
if (File::exists($path)) {
File::deleteDirectory($path);
}
});
}
public function users()
{
return $this->belongsToMany(User::class);
}
2021-04-18 10:43:36 +02:00
public function tickets()
{
2021-04-18 20:31:07 +02:00
return $this->hasMany(Ticket::class, 'project_id');
2020-11-18 10:23:51 +01:00
}
2021-04-18 10:43:36 +02:00
public function getSlugNameAttribute()
{
return Str::slug($this->name);
}
2021-04-18 20:31:07 +02:00
public static function ticketAssetsFullPath(string $str)
2021-04-18 10:43:36 +02:00
{
2021-04-18 20:31:07 +02:00
return public_path(self::ticketAssetsPath($str));
}
public static function ticketAssetsPath(string $str)
{
return sprintf('fichiers/%s', $str);
2021-04-18 10:43:36 +02:00
}
2020-11-18 10:23:51 +01:00
}