2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
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;
|
|
|
|
|
|
2021-04-18 10:43:36 +02:00
|
|
|
protected $fillable = ['name'];
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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 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
|
|
|
}
|