2021-07-24 14:20:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
use App\Enums\ProjectStatus;
|
2021-07-25 19:11:33 +02:00
|
|
|
use App\Events\ProjectUpdated;
|
2022-04-26 12:15:31 +02:00
|
|
|
use App\Models\MacroSchedule\Activity;
|
|
|
|
|
use App\Models\MacroSchedule\Marker;
|
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;
|
2022-04-26 12:15:31 +02:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
2021-07-24 14:20:56 +02:00
|
|
|
|
|
|
|
|
class Project extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
use IsCrudModel;
|
2022-04-26 12:15:31 +02:00
|
|
|
use CastsEnums;
|
2021-07-24 14:20:56 +02:00
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* Crud label column
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* Crud value column
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
protected $crudValueColumn = 'id';
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-07-25 19:11:33 +02:00
|
|
|
protected $fillable = [
|
|
|
|
|
'name', 'status',
|
|
|
|
|
'description', 'todo', 'sites'
|
|
|
|
|
];
|
2021-07-24 14:20:56 +02:00
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast to Enums.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $enumCasts = [
|
|
|
|
|
'status' => ProjectStatus::class
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Project
|
|
|
|
|
*
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $attributes = [])
|
2021-07-24 14:20:56 +02:00
|
|
|
{
|
|
|
|
|
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'),
|
2022-04-26 12:15:31 +02:00
|
|
|
'type' => CrudType::enum(),
|
|
|
|
|
'enum' => ProjectStatus::class,
|
2021-07-24 14:20:56 +02:00
|
|
|
'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(),
|
|
|
|
|
]
|
2022-04-26 12:15:31 +02:00
|
|
|
],
|
|
|
|
|
'markers' => [
|
|
|
|
|
'label' => trans('Marqueurs'),
|
|
|
|
|
'type' => CrudType::hasMany(),
|
|
|
|
|
'hasMany' => Marker::class,
|
|
|
|
|
'hasManyLabel' => trans('Marqueurs'),
|
|
|
|
|
'nullable' => true,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
]
|
|
|
|
|
],
|
2021-07-24 14:20:56 +02:00
|
|
|
];
|
|
|
|
|
parent::__construct($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-07-25 19:11:33 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::updated(function (Project $project) {
|
|
|
|
|
event(new ProjectUpdated($project, \auth()->user()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
// * RELATIONSHIPS.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BelongsToMany Site
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2021-07-25 19:11:33 +02:00
|
|
|
public function sites()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Site::class, 'project_site');
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* MorphMany Log
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
public function logs()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Log::class, 'parent');
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 12:15:31 +02:00
|
|
|
/**
|
|
|
|
|
* MorphMany File
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
public function files()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(File::class, 'parent');
|
|
|
|
|
}
|
2022-04-26 12:15:31 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HasMany Activity
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function activities()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Activity::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HasMany Marker
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function markers()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Marker::class);
|
|
|
|
|
}
|
2021-07-24 14:20:56 +02:00
|
|
|
}
|