153 lines
4.8 KiB
PHP
153 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\News\NewsClubTypeEnum;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
|
|
/**
|
|
* @property \App\Enums\News\NewsClubTypeEnum $type News club type.
|
|
* @property string $title News club title.
|
|
* @property string $slug News club title slug.
|
|
* @property string|null $content News club content.
|
|
* @property string $picture News club picture path.
|
|
* @property string $picture_title Picture title attribute.
|
|
* @property string $picture_alt Picture alt attribute.
|
|
* @property boolean $multiple_answers News multiple answers.
|
|
* @property boolean $published News publish status.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*/
|
|
class NewsClub extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'title',
|
|
'slug',
|
|
'content',
|
|
'picture',
|
|
'picture_title',
|
|
'picture_alt',
|
|
'multiple_answers',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => NewsClubTypeEnum::class,
|
|
'multiple_answers' => 'boolean',
|
|
'published' => 'boolean',
|
|
'publish_at' => 'datetime',
|
|
'unpublish_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $newsClub) {
|
|
self::assertTitleIsUnique($newsClub->title);
|
|
self::manageFields($newsClub);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $newsClub) {
|
|
self::assertTitleIsUnique($newsClub->title, $newsClub->getKey());
|
|
self::manageFields($newsClub);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
self::resetCache();
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
self::resetCache();
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
$model->slug = Str::slug($model->title);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique.
|
|
*
|
|
* @param string|null $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an news category title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(string|null $title, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedNewsClubOrderedByPublishAt');
|
|
self::getPublishedOrderedByPublishAt();
|
|
}
|
|
|
|
/**
|
|
* Get all published news with categories relations.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getPublishedOrderedByPublishAt(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('publishedNewsClubOrderedByPublishAt', 86400, function () {
|
|
return self::query()->where('published', true)->orderBy('publish_at', 'asc')->take(3)->get();
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get all survey answers of the news club (survey type).
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function answers(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(SurveysAnswer::class);
|
|
}
|
|
}
|