ldc-nature-and-respect/app/Models/Range.php

193 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-03-20 10:47:44 +01:00
use App\Enums\Ranges\PictoEnum;
2023-03-15 13:51:01 +01:00
use App\Lib\Helpers\ToolboxHelper;
2023-05-10 10:39:24 +02:00
use App\Traits\Models\HasTranslations;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
2023-03-20 10:47:44 +01:00
use Illuminate\Support\Str;
use Spatie\SchemaOrg\Schema;
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
2023-07-11 09:57:55 +00:00
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
use Uwil\Seo\Laravel\Objects\SeoItem;
use Uwil\Seo\Laravel\Traits\SeoTrait;
2023-03-15 13:51:01 +01:00
/**
* @property string $seo_title
* @property string $seo_description
* @property string $label
* @property string $slug
2023-05-09 19:24:09 +02:00
* @property string $picto
2023-03-15 13:51:01 +01:00
* @property integer $order
* @property boolean $status
* @property string $created_at
* @property string $updated_at
*/
class Range extends Model implements SeoInterface, SchemaOrgInterface
2023-03-15 13:51:01 +01:00
{
2023-03-16 09:47:16 +01:00
use HasFactory;
use HasTranslations;
2023-07-11 09:57:55 +00:00
use SeoTrait;
use SchemaOrgTrait;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
2024-07-16 18:21:29 +02:00
* The attributes that are mass assignable.
*
* @var array<int, string>
2023-03-15 13:51:01 +01:00
*/
protected $fillable = [
'seo_title',
'seo_description',
'label',
'slug',
'picto',
'order',
'status',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
2024-01-29 16:02:03 +01:00
* @var array<string, string>
2023-03-15 13:51:01 +01:00
*/
protected $casts = [
2023-03-20 10:47:44 +01:00
'picto' => PictoEnum::class,
2023-03-15 13:51:01 +01:00
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'label',
'slug',
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
2023-03-20 10:47:44 +01:00
self::assertFieldIsUnique('label', $model->label);
self::setOrder($model);
2023-03-15 13:51:01 +01:00
});
static::updating(function (self $model) {
self::manageFields($model);
});
}
// * METHODS
/**
2023-03-20 10:47:44 +01:00
* Set the slug.
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-20 10:47:44 +01:00
*
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function setSlug(self $model)
2023-03-20 10:47:44 +01:00
{
$model->slug = Str::slug($model->label);
}
/**
* Common methods function
2023-03-15 13:51:01 +01:00
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-15 13:51:01 +01:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function manageFields(self $model)
2023-03-15 13:51:01 +01:00
{
2023-03-20 10:47:44 +01:00
self::setSlug($model);
2023-03-15 13:51:01 +01:00
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
2023-05-09 09:22:19 +02:00
* @param string $valueField
2023-03-15 13:51:01 +01:00
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
2023-03-20 10:47:44 +01:00
/**
* Set order after the last element of the list.
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-20 10:47:44 +01:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function setOrder(self $model): void
2023-03-20 10:47:44 +01:00
{
$model->order = \intval(self::query()->max('order')) + 1;
}
/**
* Create a SchemaOrg.
*
* @return \Spatie\SchemaOrg\DefinedTermSet
*/
public function toSchemaOrg(): \Spatie\SchemaOrg\DefinedTermSet
{
return Schema::definedTermSet()
->hasDefinedTerm(
Schema::definedTerm()
->name($this->seo_title)
->description($this->seo_description)
);
}
2023-03-15 13:51:01 +01:00
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function recipes()
{
return $this->hasMany('App\Models\Recipe');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany('App\Models\Product');
}
2023-07-11 09:57:55 +00:00
/**
* Get the SEO object.
2023-07-11 09:57:55 +00:00
*
2024-01-29 16:02:03 +01:00
* @return \Uwil\Seo\Laravel\Objects\SeoItem
2023-07-11 09:57:55 +00:00
*/
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
2023-07-11 09:57:55 +00:00
{
return new SeoItem(
\trans($this->getTable()),
$this->seo_title,
$this->seo_description,
$this->status ?? false,
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
'#'
);
}
2023-03-15 13:51:01 +01:00
}