ldc-supportweb/app/Models/Site.php
2022-05-31 17:37:37 +02:00

259 lines
6.9 KiB
PHP

<?php
namespace App\Models;
use App\Events\SiteUpdated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
class Site extends Model
{
use HasFactory;
use IsCrudModel;
use SoftDeletes;
/**
* Crud Label
*
* @var string
*/
protected $crudLabelColumn = 'name';
/**
* Crud Value
*
* @var string
*/
protected $crudValueColumn = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'status',
'description', 'todo'
];
/**
* The model's attributes.
*
* @var array
*/
protected $attributes = [
'status' => '',
];
/**
* Site
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
self::$editableProperties = [
'name' => [
'label' => trans('Nom'),
'type' => CrudType::string(),
'validate' => [
'string',
'max:255',
// @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
]));
}
}
],
'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::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(),
]
],
'users' => [
'label' => trans('Utilisateurs'),
'type' => CrudType::belongsToMany(),
'belongsToMany' => User::class,
'belongsToManyLabel' => 'name',
'nullable' => true,
'readonly' => function () {
/** @var \App\Models\User */
$user = auth()->user();
return !$user->isAdmin();
},
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'projects' => [
'label' => trans('models.classes.projects'),
'type' => CrudType::belongsToMany(),
'belongsToMany' => Project::class,
'belongsToManyLabel' => 'name',
'nullable' => true,
'readonly' => function () {
/** @var \App\Models\User */
$user = auth()->user();
return !$user->isAdmin();
},
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
]
];
parent::__construct($attributes);
}
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::created(function (self $site) {
Label::createDefaultLabelsFor($site);
});
static::updated(function (self $site) {
event(new SiteUpdated($site, \auth()->user()));
});
}
// * RELATIONSHIPS
/**
* HasMany Message relation
* The users of type client that would have access to this site tickets
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function users()
{
return $this->belongsToMany(User::class);
}
/**
* HasMany Ticket relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tickets()
{
return $this->hasMany(Ticket::class, 'site_id');
}
/**
* BelongsTo Brand relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* BelongsToMany Project relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function projects()
{
return $this->belongsToMany(Project::class);
}
/**
* MorphMany Log relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function logs()
{
return $this->morphMany(Log::class, 'parent');
}
/**
* MorphMany File relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function files()
{
return $this->morphMany(File::class, 'parent');
}
// * METHODS
/**
* Getter slugName
*
* @return string
*/
public function getSlugNameAttribute(): string
{
return Str::slug($this->name);
}
}