82 lines
2.3 KiB
PHP
82 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Site;
|
|
use App\Traits\HasUniqueField;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property string $name Name of the archived campaign.
|
|
* @property string $campaign_id Id of the archived campaign.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\ArchiveStat $archiveStat ArchiveStat Has many relationship.
|
|
* @property-read \App\Models\ArchiveSite $archiveSite ArchiveSite Belongs to relationship.
|
|
*/
|
|
class ArchiveCampaign extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasUniqueField;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'campaign_id',
|
|
'archive_site_id'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
$model->assertFieldIsUnique('campaign_id', $model->campaign_id);
|
|
$site = Site::query()->where('site_id', $model->archive_site_id)->firstOrFail();
|
|
$archiveSite = ArchiveSite::query()->firstOrCreate(
|
|
[
|
|
'site_id' => $model->archive_site_id
|
|
],
|
|
[
|
|
'name' => $site->site_nom
|
|
]
|
|
);
|
|
$model->archiveSite()->associate($archiveSite);
|
|
});
|
|
static::updating(function () {
|
|
throw new \RuntimeException('Updating an archived campaign is not authorized');
|
|
});
|
|
static::deleting(function () {
|
|
throw new \RuntimeException('Deleting an archived campaign is not authorized');
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* ArchiveSite model relation
|
|
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function archiveSite()
|
|
{
|
|
return $this->belongsTo(ArchiveSite::class);
|
|
}
|
|
|
|
/**
|
|
* ArchiveSite model relation
|
|
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function archiveStat()
|
|
{
|
|
return $this->hasMany(ArchiveStat::class);
|
|
}
|
|
}
|