110 lines
2.8 KiB
PHP
110 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Lib\Helpers\FileStorageHelper;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Certification
|
||
|
|
*
|
||
|
|
* @property string $name The slug.
|
||
|
|
* @property string $sticker The sticker path.
|
||
|
|
* @property string $sticker_alt The sticker alternative text.
|
||
|
|
* @property string $sticker_title The sticker alternative title.
|
||
|
|
* @property boolean $status The publication status.
|
||
|
|
*
|
||
|
|
* @package App\Models
|
||
|
|
*/
|
||
|
|
class Certification extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that are mass assignable.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $fillable = [
|
||
|
|
'name',
|
||
|
|
'sticker',
|
||
|
|
'sticker_alt',
|
||
|
|
'sticker_title',
|
||
|
|
'status'
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that should be cast.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $casts = [
|
||
|
|
'status' => 'bool'
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Perform any actions required after the model boots.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
static::creating(function (self $model) {
|
||
|
|
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
|
||
|
|
});
|
||
|
|
static::created(function (self $model) {
|
||
|
|
self::flashStatus($model);
|
||
|
|
});
|
||
|
|
static::updating(function (self $model) {
|
||
|
|
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
|
||
|
|
});
|
||
|
|
static::updated(function (self $model) {
|
||
|
|
$model->sticker = FileStorageHelper::removeOldFile($model, 'sticker');
|
||
|
|
self::flashStatus($model, true);
|
||
|
|
});
|
||
|
|
static::deleting(function (self $model) {
|
||
|
|
FileStorageHelper::removeFile($model, 'sticker');
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// * METHODS
|
||
|
|
/**
|
||
|
|
* Flash publication status
|
||
|
|
*
|
||
|
|
* @param self $model
|
||
|
|
* @param boolean $old
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public static function flashStatus(self $model, bool $old = false)
|
||
|
|
{
|
||
|
|
if ($old and $model->getOriginal('status') == $model->status) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if ($model->status) {
|
||
|
|
session()->flash('info', trans(
|
||
|
|
':model est maintenant publié',
|
||
|
|
['model' => Str::singular(trans('models.classes.' . $model->getTable()))]
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
session()->flash('warning', trans(
|
||
|
|
':model est maintenant dépublié',
|
||
|
|
['model' => Str::singular(trans('models.classes.' . $model->getTable()))]
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// * RELATIONSHIPS
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get products for the certification.
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||
|
|
*/
|
||
|
|
public function products()
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(Product::class);
|
||
|
|
}
|
||
|
|
}
|