2021-07-24 14:20:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-07-25 19:11:33 +02:00
|
|
|
use App\Events\ProjectUpdated;
|
2021-07-24 14:20:56 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
|
|
|
|
|
|
|
|
|
class Project extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
use IsCrudModel;
|
|
|
|
|
|
|
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
|
|
|
|
protected $crudValueColumn = 'id';
|
|
|
|
|
|
2021-07-25 19:11:33 +02:00
|
|
|
protected $fillable = [
|
|
|
|
|
'name', 'status',
|
|
|
|
|
'description', 'todo', 'sites'
|
|
|
|
|
];
|
2021-07-24 14:20:56 +02:00
|
|
|
|
|
|
|
|
public function __construct($attributes = [])
|
|
|
|
|
{
|
|
|
|
|
self::$editableProperties = [
|
|
|
|
|
'name' => [
|
|
|
|
|
'label' => trans('Nom'),
|
|
|
|
|
'type' => CrudType::string(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string',
|
2021-09-29 12:27:22 +02:00
|
|
|
'max:255',
|
2021-10-22 12:27:48 +02:00
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
|
$q = static::query();
|
|
|
|
|
if (request()->project) {
|
|
|
|
|
$q = $q->where('id', '!=', $this->id);
|
|
|
|
|
}
|
|
|
|
|
if ($q->where('name', $value)->exists()) {
|
|
|
|
|
$fail(__(':attribute est déjà utilisé', [
|
|
|
|
|
'attribute' => $attribute
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-24 14:20:56 +02:00
|
|
|
],
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'status' => [
|
|
|
|
|
'label' => trans('État'),
|
|
|
|
|
'type' => CrudType::string(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string',
|
|
|
|
|
'max:255'
|
|
|
|
|
],
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'description' => [
|
|
|
|
|
'label' => trans('Description'),
|
|
|
|
|
'type' => CrudType::text(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string'
|
|
|
|
|
],
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'todo' => [
|
|
|
|
|
'label' => trans('À Faire'),
|
|
|
|
|
'type' => CrudType::text(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string'
|
|
|
|
|
],
|
|
|
|
|
'required' => false,
|
|
|
|
|
'nullable' => true,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'sites' => [
|
|
|
|
|
'label' => trans('Sites'),
|
|
|
|
|
'type' => CrudType::belongsToMany(),
|
|
|
|
|
'belongsToMany' => Site::class,
|
|
|
|
|
'belongsToManyLabel' => 'name',
|
|
|
|
|
'nullable' => true,
|
|
|
|
|
'readonly' => function () {
|
|
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
return !$user->isAdmin();
|
|
|
|
|
},
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
parent::__construct($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 19:11:33 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::updated(function (Project $project) {
|
|
|
|
|
event(new ProjectUpdated($project, \auth()->user()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sites()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Site::class, 'project_site');
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-24 14:20:56 +02:00
|
|
|
public function logs()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Log::class, 'parent');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function files()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(File::class, 'parent');
|
|
|
|
|
}
|
|
|
|
|
}
|