156 lines
4.6 KiB
PHP
156 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
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;
|
|
use Uwil\LaravelI18n\Traits\Models\HasTranslations;
|
|
|
|
/**
|
|
* @property integer $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.
|
|
*/
|
|
class Site extends Model
|
|
{
|
|
use HasActivityLog;
|
|
use HasCache;
|
|
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\SiteFactory> */
|
|
use HasFactory;
|
|
use HasModelNaming;
|
|
use HasTranslations;
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
#[\Override]
|
|
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.
|
|
*
|
|
* @return void
|
|
*/
|
|
#[\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.
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get relations to include in the cache.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
public static function getCacheRelations(): array
|
|
{
|
|
return ['projectManager', 'gitlabProject', 'users'];
|
|
}
|
|
|
|
// * RELATIONS.
|
|
|
|
/**
|
|
* Get users associated to the site.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\User,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
|
|
*/
|
|
public function users(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(
|
|
User::class,
|
|
'sitable',
|
|
'sitables',
|
|
'site_id',
|
|
'sitable_id',
|
|
)->withPivot('created_at');
|
|
}
|
|
|
|
/**
|
|
* Get brands associated to the site.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Brand,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
|
|
*/
|
|
public function brands(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(
|
|
Brand::class,
|
|
'sitable',
|
|
'sitables',
|
|
'site_id',
|
|
'sitable_id',
|
|
)->withPivot('created_at');
|
|
}
|
|
|
|
/**
|
|
* Get project manager of the site.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User,$this>
|
|
*/
|
|
public function projectManager(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'project_manager_id');
|
|
}
|
|
|
|
/**
|
|
* Get project manager of the site.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\GitlabProject,$this>
|
|
*/
|
|
public function gitlabProject(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(GitlabProject::class, 'gitlab_project_id');
|
|
}
|
|
}
|