ldc-supportweb/app/Models/Site.php

165 lines
4.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property int $id
* @property string $name
* @property string|null $url_re7
* @property string|null $url_prod
* @property int|null $gitlab_project_id
* @property int|null $brand_id Relation is nullable, since not all site would be affiliate with a brand.
* @property int|null $project_manager_id Project manager relationship
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \App\Models\Brand|null $brand
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Invoice> $invoices
* @property-read \App\Models\User|null $projectManager
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Ticket> $tickets
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $users
* @property-read mixed $value
* @method static \Illuminate\Database\Eloquent\Builder<static>|Site onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Site withTrashed(bool $withTrashed = true)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Site withoutTrashed()
* @mixin \Eloquent
*/
class Site extends Model
{
use HasFactory;
use SoftDeletes;
/**
* The attributes that are fillable.
*
* @var array
*/
protected $fillable = [
'name',
'url_re7',
'url_prod',
'gitlab_project_id',
'project_manager_id'
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'value'
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::deleted(function ($site) {
$site->invoices()->delete();
$site->tickets()->delete();
});
}
// * METHODS
/**
* Common methods function.
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
if ($model->url_re7) {
ToolboxHelper::assertNoInternalUrls('url_re7', $model->url_re7);
}
if ($model->url_prod) {
ToolboxHelper::assertNoInternalUrls('url_prod', $model->url_prod);
}
}
// * RELATIONSHIPS
/**
* HasMany Message relation
* The users of type client that would have access to this site tickets
*
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany
*/
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');
}
/**
* HasMany Invoices relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function invoices()
{
return $this->hasMany(Invoice::class, 'site_id');
}
/**
* BelongsTo Brand relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* BelongsTo User relation
* The project manager of the site
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function projectManager(): BelongsTo
{
return $this->belongsTo(User::class, 'project_manager_id');
}
/**
* Return value attribute for belongsto search component
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function value(): Attribute
{
return new Attribute(
get: fn () => $this->id,
);
}
}