2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-07-25 19:11:33 +02:00
|
|
|
use App\Events\SiteUpdated;
|
2020-11-18 10:23:51 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-06-28 17:04:15 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-04-18 10:43:36 +02:00
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
|
use Illuminate\Support\Str;
|
2021-07-24 14:20:56 +02:00
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2021-07-23 11:34:16 +02:00
|
|
|
class Site extends Model
|
2020-11-18 10:23:51 +01:00
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2021-06-18 15:39:19 +02:00
|
|
|
use IsCrudModel;
|
2021-06-28 17:04:15 +02:00
|
|
|
use SoftDeletes;
|
2021-06-18 15:39:19 +02:00
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* Crud Label
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* Crud Value
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
protected $crudValueColumn = 'id';
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-07-27 14:50:54 +02:00
|
|
|
protected $fillable = [
|
|
|
|
|
'name', 'status',
|
|
|
|
|
'description', 'todo'
|
|
|
|
|
];
|
2021-04-18 10:43:36 +02:00
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* The model's attributes.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-10-22 12:27:48 +02:00
|
|
|
protected $attributes = [
|
|
|
|
|
'status' => '',
|
|
|
|
|
];
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* Site
|
|
|
|
|
*
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $attributes = [])
|
2021-06-18 15:39:19 +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()->site) {
|
|
|
|
|
$q = $q->where('id', '!=', $this->id);
|
|
|
|
|
}
|
|
|
|
|
if ($q->where('name', $value)->exists()) {
|
|
|
|
|
$fail(__(':attribute est déjà utilisé', [
|
|
|
|
|
'attribute' => $attribute
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-18 15:39:19 +02:00
|
|
|
],
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
2021-07-27 14:50:54 +02:00
|
|
|
'status' => [
|
|
|
|
|
'label' => trans('État'),
|
|
|
|
|
'type' => CrudType::string(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string',
|
|
|
|
|
'max:255'
|
|
|
|
|
],
|
2022-04-13 12:21:07 +02:00
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
2021-07-27 14:50:54 +02:00
|
|
|
'actions' => [
|
|
|
|
|
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(),
|
|
|
|
|
'required' => false,
|
|
|
|
|
'nullable' => true,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
2021-06-18 15:39:19 +02:00
|
|
|
'users' => [
|
|
|
|
|
'label' => trans('Utilisateurs'),
|
|
|
|
|
'type' => CrudType::belongsToMany(),
|
|
|
|
|
'belongsToMany' => User::class,
|
|
|
|
|
'belongsToManyLabel' => 'name',
|
2021-06-28 17:04:15 +02:00
|
|
|
'nullable' => true,
|
2021-06-18 15:39:19 +02:00
|
|
|
'readonly' => function () {
|
2021-07-23 11:04:09 +02:00
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
2021-07-24 14:20:56 +02:00
|
|
|
return !$user->isAdmin();
|
2021-06-18 15:39:19 +02:00
|
|
|
},
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
2021-07-24 14:20:56 +02:00
|
|
|
],
|
2021-07-25 19:11:33 +02:00
|
|
|
'projects' => [
|
2022-05-31 14:06:07 +02:00
|
|
|
'label' => trans('models.classes.projects'),
|
2021-07-24 14:20:56 +02:00
|
|
|
'type' => CrudType::belongsToMany(),
|
2021-07-25 19:11:33 +02:00
|
|
|
'belongsToMany' => Project::class,
|
2021-07-24 14:20:56 +02:00
|
|
|
'belongsToManyLabel' => 'name',
|
|
|
|
|
'nullable' => true,
|
|
|
|
|
'readonly' => function () {
|
|
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
return !$user->isAdmin();
|
|
|
|
|
},
|
|
|
|
|
'actions' => [
|
2021-07-25 19:11:33 +02:00
|
|
|
CrudAction::viewAny(),
|
2021-07-24 14:20:56 +02:00
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
2021-07-25 19:11:33 +02:00
|
|
|
CrudAction::update()
|
2021-07-24 14:20:56 +02:00
|
|
|
]
|
2021-06-18 15:39:19 +02:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
parent::__construct($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-04-18 10:43:36 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
2022-05-26 13:22:50 +02:00
|
|
|
static::updated(function (self $site) {
|
2021-07-25 19:11:33 +02:00
|
|
|
event(new SiteUpdated($site, \auth()->user()));
|
|
|
|
|
});
|
2021-04-18 10:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HasMany Message relation
|
|
|
|
|
* The users of type client that would have access to this site tickets
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
public function users()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* HasMany Ticket relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2021-04-18 10:43:36 +02:00
|
|
|
public function tickets()
|
|
|
|
|
{
|
2021-07-23 11:34:16 +02:00
|
|
|
return $this->hasMany(Ticket::class, 'site_id');
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
2021-04-18 10:43:36 +02:00
|
|
|
|
2022-05-31 14:06:07 +02:00
|
|
|
/**
|
|
|
|
|
* BelongsTo Brand relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
|
|
|
|
public function brand()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Brand::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* BelongsToMany Project relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2021-07-25 19:11:33 +02:00
|
|
|
public function projects()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Project::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* MorphMany Log relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
public function logs()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Log::class, 'parent');
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* MorphMany File relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
|
*/
|
2021-07-24 14:20:56 +02:00
|
|
|
public function files()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(File::class, 'parent');
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Getter slugName
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getSlugNameAttribute(): string
|
2021-04-18 10:43:36 +02:00
|
|
|
{
|
|
|
|
|
return Str::slug($this->name);
|
|
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|