196 lines
5.7 KiB
PHP
196 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
|
|
/**
|
|
* @property string $label Subrange label.
|
|
* @property string $subtitle Subrange subtitle.
|
|
* @property string $slug Subrange slug.
|
|
* @property string $description Subrange description.
|
|
* @property string $picture Subrange picture.
|
|
* @property string $picture_alt Picture alt attribute.
|
|
* @property string $picture_title Picture title attribute.
|
|
* @property boolean $published Subrange publish status.
|
|
* @property int $order Subrange order.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Range $range Range Belongs to relationship.
|
|
* @property-read \App\Models\Product $products Product polymorphic relationship.
|
|
*/
|
|
class Subrange extends Model implements SchemaOrgInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SchemaOrgTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'label',
|
|
'subtitle',
|
|
'slug',
|
|
'description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'range_id',
|
|
'published',
|
|
'order',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertLabelIsUnique($model->label);
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::assertLabelIsUnique($model->label, $model->getKey());
|
|
self::manageFields($model);
|
|
});
|
|
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->label);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the label is unique
|
|
*
|
|
* @param string|null $label
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If a tange label already exists.
|
|
*/
|
|
private static function assertLabelIsUnique(string|null $label, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedSubrangesOrderedByOrder');
|
|
self::getPublishedOrdered();
|
|
}
|
|
|
|
/**
|
|
* Get all published Subranges ordered by order.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getPublishedOrdered(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('publishedSubrangesOrderedByOrder', 86400, function () {
|
|
return self::query()->where('published', true)->orderBy('order')->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg.
|
|
*
|
|
* @return \Spatie\SchemaOrg\DefinedTerm
|
|
*/
|
|
public function toSchemaOrg(): \Spatie\SchemaOrg\DefinedTerm
|
|
{
|
|
return Schema::definedTerm()
|
|
->name($this->label)
|
|
->description(strip_tags($this->description))
|
|
->image(
|
|
Schema::imageObject()
|
|
->url(asset($this->picture))
|
|
->headline($this->picture_title)
|
|
)
|
|
->inDefinedTermSet($this->range->toSchemaOrg());
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get all of the products for the subrange.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Product::class, 'productable');
|
|
}
|
|
|
|
/**
|
|
* Get the range that owns the subrange.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Range::class);
|
|
}
|
|
}
|