ldc-volena/app/Models/Site.php
2026-04-24 15:06:55 +02:00

229 lines
7.9 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\Str;
use Spatie\Sitemap\Contracts\Sitemapable;
use Uwil\SitemapGenerator\Lib\SitemapHelper;
/**
* @property string $seo_title SEO title.
* @property string $seo_description SEO description.
* @property string $name Name.
* @property string $slug Slug of the name.
* @property string $description First level of description.
* @property string $description_2 Second level of description.
* @property integer $employees Employees count.
* @property string $pictos JSON - SpeciesEnum Values. Between 0 and n.
* @property string $address Address of the site.
* @property string $phone_number Phone number of the site.
* @property string $access_line JSON - How to go to the site. 3 entries.
* @property string $director_name Name of the director.
* @property string $director_speach WYSIWYG - Director speach.
* @property string $employee_name Name of the employee.
* @property string $employee_functi Function of the employee.
* @property string $employee_speach WYSIWYG - Employee speach.
* @property string $highlights WYSIWYG - Highlights part content.
* @property double $latitude Latitude in degrees.
* @property double $longitude Longitude in degrees.
* @property string $sticky_title Sticky item title.
* @property string $sticky_link Sticky item link.
* @property string $logo Path of the logo.
* @property string $logo_alt Logo alt attribute.
* @property string $logo_title Logo title attribute.
* @property string $picture_1 Path of the picture 1.
* @property string $picture_1_alt Picture 1 alt attribute.
* @property string $picture_1_title Picture 1 title attribute.
* @property string $picture_2 Path of the picture 2.
* @property string $picture_2_alt Picture 2 alt attribute.
* @property string $picture_2_title Picture 2 title attribute.
* @property string $picture_3 Path of the picture 3.
* @property string $picture_3_alt Picture 3 alt attribute.
* @property string $picture_3_title Picture 3 title attribute.
* @property integer $order Order.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Logo $logos Logos Many-to-many relationship.
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Site extends Model implements Sitemapable
{
use ActivityLog;
/** @use HasFactory<\Database\Factories\SiteFactory> */
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'seo_title',
'seo_description',
'name',
'slug',
'description',
'description_2',
'employees',
'pictos',
'address',
'phone_number',
'access_line',
'director_name',
'director_function',
'director_speach',
'invert_director_block',
'employee_name',
'employee_function',
'employee_speach',
'highlights',
'latitude',
'longitude',
'sticky_title',
'sticky_link',
'logo',
'logo_alt',
'logo_title',
'picture_1',
'picture_1_alt',
'picture_1_title',
'picture_2',
'picture_2_alt',
'picture_2_title',
'picture_3',
'picture_3_alt',
'picture_3_title',
'order',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'pictos' => 'array',
'access_line' => 'array',
'invert_director_block' => 'boolean'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::setOrder($model);
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'logo');
FileStorageHelper::removeOldFile($model, 'picture_1');
FileStorageHelper::removeOldFile($model, 'picture_2');
FileStorageHelper::removeOldFile($model, 'picture_3');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'logo');
FileStorageHelper::removeFile($model, 'picture_1');
FileStorageHelper::removeFile($model, 'picture_2');
FileStorageHelper::removeFile($model, 'picture_3');
});
}
// * METHODS.
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
// * ASSERTIONS
self::assertFieldIsUnique('name', $model->name, $model->id);
self::assertFieldIsUnique('slug', Str::slug($model->name), $model->id);
// * TRANSFORMATIONS
self::setSlug($model, $model->name);
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
$model->picture_1 = FileStorageHelper::storeFile($model, $model->picture_1, true);
$model->picture_2 = FileStorageHelper::storeFile($model, $model->picture_2, true);
$model->picture_3 = FileStorageHelper::storeFile($model, $model->picture_3, true);
}
/**
* Set the slug.
*
* @param self $model
* @param string $original
*
* @return void
*/
private static function setSlug(self $model, string $original): void
{
$model->slug = Str::slug($original);
}
/**
* 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;
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null): void
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
/**
* To sitemap tag method.
*
* @return \Spatie\Sitemap\Tags\Url|string|array<int|string, mixed>
*/
public function toSitemapTag(): \Spatie\Sitemap\Tags\Url | string | array
{
$model = $this;
$route = 'fo.sites.show';
$params = ['site' => $model];
return SitemapHelper::generateUrl($route, $params, $model);
}
// * RELATIONS.
/**
* The logos that belong to the site.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Logo,$this>
*/
public function logos(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Logo::class);
}
}