262 lines
9.3 KiB
PHP
262 lines
9.3 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;
|
|
|
|
/**
|
|
* @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_function 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 $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 string $map_picture Path of the map picture.
|
|
* @property string $map_picture_alt Map picture alt attribute.
|
|
* @property string $map_picture_title Map picture 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.
|
|
*/
|
|
class Site extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'name',
|
|
'slug',
|
|
'description',
|
|
'description_2',
|
|
'employees',
|
|
'pictos',
|
|
'address',
|
|
'phone_number',
|
|
'access_line',
|
|
'director_name',
|
|
'director_speach',
|
|
'employee_name',
|
|
'employee_function',
|
|
'employee_speach',
|
|
'highlights',
|
|
'latitude',
|
|
'longitude',
|
|
'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',
|
|
'map_picture',
|
|
'map_picture_alt',
|
|
'map_picture_title',
|
|
'order',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'pictos' => 'array',
|
|
'access_line' => 'array',
|
|
];
|
|
|
|
/**
|
|
* 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');
|
|
FileStorageHelper::removeOldFile($model, 'map_picture');
|
|
});
|
|
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'logo');
|
|
FileStorageHelper::removeFile($model, 'picture_1');
|
|
FileStorageHelper::removeFile($model, 'picture_2');
|
|
FileStorageHelper::removeFile($model, 'picture_3');
|
|
FileStorageHelper::removeFile($model, 'map_picture');
|
|
});
|
|
}
|
|
|
|
// * 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);
|
|
$model->map_picture = FileStorageHelper::storeFile($model, $model->map_picture, 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);
|
|
}
|
|
|
|
/**
|
|
* Adjusts model status using current status and publish dates
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function makeStatusSense(self $model): void
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $model->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $model->unpublish_at;
|
|
$wantedStatus = $model->published;
|
|
if ($publish_at or $unpublish_at) {
|
|
$model->published = $model->planificated_status;
|
|
}
|
|
if ($wantedStatus !== $model->published) {
|
|
$previousMessage = session()->get('bo.info', '');
|
|
session()->flash('bo.info', \trans(
|
|
'La publication est modifiée en concordance avec les dates enregistrées.
|
|
Pour modifier l\'état de publication, veuillez modifier ou enlever les dates correspondantes.'
|
|
) . " \n{$previousMessage}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getter the current status following planification dates planificated_status
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function getPlanificatedStatusAttribute(): bool
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $this->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $this->unpublish_at;
|
|
if ($publish_at and $unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(now()) and $publish_at->lessThan(now());
|
|
}
|
|
if ($publish_at) {
|
|
return $publish_at->lessThan(now());
|
|
}
|
|
if ($unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(now());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* The logos that belong to the site.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function logos(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Logo::class);
|
|
}
|
|
}
|