ldc-supportweb/app/Models/Site.php

158 lines
4.3 KiB
PHP
Raw Normal View History

2026-05-19 12:44:30 +02:00
<?php
declare(strict_types=1);
2026-05-19 12:44:30 +02:00
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2026-05-19 12:44:30 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Uwil\LaravelActivityLogs\Traits\HasActivityLog;
use Uwil\LaravelBackend\Traits\Models\HasCache;
use Uwil\LaravelBackend\Traits\Models\HasModelNaming;
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
use Uwil\LaravelI18n\Helpers\TranslationHelper;
2026-05-19 12:44:30 +02:00
use Uwil\LaravelI18n\Traits\Models\HasTranslations;
/**
* @property int $id Primary key of the site.
* @property string $name Name of the site.
* @property string $url_re7 Re7 url of the site.
* @property string $url_prod Prod url of the site.
* @property \App\Models\User $project_manager_id Project manager associated.
* @property-read \Illuminate\Support\Carbon $created_at Date the site was created.
* @property-read \Illuminate\Support\Carbon $updated_at Date the site was updated.
* @property-read \Illuminate\Support\Carbon $deleted_at Date the site was deleted.
2026-05-19 12:44:30 +02:00
*/
class Site extends Model
{
use HasActivityLog;
use HasCache;
2026-05-19 12:44:30 +02:00
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\SiteFactory> */
use HasFactory;
2026-05-19 12:44:30 +02:00
use HasModelNaming;
use HasTranslations;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
2026-05-26 13:12:45 +02:00
#[\Override]
2026-05-19 12:44:30 +02:00
protected $fillable = [
'name',
'url_re7',
'url_prod',
'project_manager_id',
'gitlab_project_id',
];
/**
* The translatable columns.
*
* @var list<string>
*/
public $translatable = [
'name',
];
/**
* Perform any actions required after the model boots.
*/
#[\Override]
protected static function booted(): void
{
static::creating(function (self $model): void {
self::manageFields($model);
});
static::updating(function (self $model): void {
self::manageFields($model);
});
}
// * METHODS.
2026-05-19 12:44:30 +02:00
/**
* Manage model fields.
*/
private static function manageFields(self $model): void
{
TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name');
if ($model->url_re7) {
ToolboxHelper::assertNoInternalUrls('url_re7', $model->url_re7);
}
if ($model->url_prod) {
ToolboxHelper::assertNoInternalUrls('url_prod', $model->url_prod);
}
2026-05-19 12:44:30 +02:00
}
2026-05-26 13:12:45 +02:00
/**
* Get relations to include in the cache.
*
* @return list<string>
*/
public static function getCacheRelations(): array
{
return ['projectManager', 'gitlabProject', 'users'];
}
2026-05-19 12:44:30 +02:00
// * RELATIONS.
/**
* Get users associated to the site.
*
2026-05-26 13:12:45 +02:00
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\User,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
2026-05-19 12:44:30 +02:00
*/
public function users(): MorphToMany
2026-05-19 12:44:30 +02:00
{
return $this->morphedByMany(
User::class,
'sitable',
'sitables',
'site_id',
2026-05-19 16:44:38 +02:00
'sitable_id',
)->withPivot('created_at');
}
/**
* Get brands associated to the site.
*
2026-05-26 13:12:45 +02:00
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Brand,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
2026-05-19 16:44:38 +02:00
*/
public function brands(): MorphToMany
2026-05-19 16:44:38 +02:00
{
return $this->morphedByMany(
Brand::class,
'sitable',
'sitables',
'site_id',
'sitable_id',
2026-05-19 12:44:30 +02:00
)->withPivot('created_at');
}
/**
* Get project manager of the site.
*
2026-05-26 13:12:45 +02:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User,$this>
2026-05-19 12:44:30 +02:00
*/
public function projectManager(): BelongsTo
2026-05-19 12:44:30 +02:00
{
return $this->belongsTo(User::class, 'project_manager_id');
}
/**
* Get project manager of the site.
*
2026-05-26 13:12:45 +02:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\GitlabProject,$this>
2026-05-19 12:44:30 +02:00
*/
public function gitlabProject(): BelongsTo
2026-05-19 12:44:30 +02:00
{
return $this->belongsTo(GitlabProject::class, 'gitlab_project_id');
}
}