ldc-le-gaulois-pro/app/Models/News.php
2023-01-18 11:31:49 +00:00

219 lines
6 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
/**
* @property-read string $reading_time Time reading of a news' page
*/
class News extends Authenticatable
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'slug',
'subtitle',
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'picture2',
'picture2_alt',
'picture2_title',
'content',
'content2',
'event_start',
'event_end',
'event_text',
'status'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'event_start' => 'datetime',
'event_end' => 'datetime',
'status' => 'bool'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
self::assertLimitNewsCategory($model->categories()->count());
self::assertLimitProducts($model->products()->count());
self::assertLimitNews($model->news()->count());
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture2');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture2');
});
}
// * METHODS
/**
* Manage fields on this model event.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function manageFields(Model $model)
{
self::assertTitleIsUnique($model->title, $model->id);
$model->slug = Str::slug($model->title);
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
$model->content = clean($model->content);
if ($model->content2) {
ToolboxHelper::assertNoInternalBoUrls('content2', $model->content2);
$model->content2 = clean($model->content2);
}
$model->picture = FileStorageHelper::storeFile($model, $model->picture);
$model->picture2 = FileStorageHelper::storeFile($model, $model->picture2);
}
/**
* Limit products associated.
* @param integer $nbProductsInBdd
* @return void
* @throws \Illuminate\Validation\ValidationException Error exception.
*/
private static function assertLimitProducts(int $nbProductsInBdd)
{
if ($nbProductsInBdd > 3) {
throw ValidationException::withMessages([
'Vous devez associer au maximum 3 produits.'
]);
}
}
/**
* Limit news associated.
* @param integer $nbNewsInBdd
* @return void
* @throws \Illuminate\Validation\ValidationException Error exception.
*/
private static function assertLimitNews(int $nbNewsInBdd)
{
if ($nbNewsInBdd > 3) {
throw ValidationException::withMessages([
'Vous devez associer au maximum 3 actualités.'
]);
}
}
/**
* Limit news categories associated.
* @param integer $nbNewsCategoriesInBdd
* @return void
* @throws \Illuminate\Validation\ValidationException Error exception.
*/
private static function assertLimitNewsCategory(int $nbNewsCategoriesInBdd)
{
if ($nbNewsCategoriesInBdd < 1 or $nbNewsCategoriesInBdd > 2) {
throw ValidationException::withMessages([
trans('Vous devez associer une actualité à 1 ou 2 catégories.')
]);
}
}
/**
* Asserts using validation that the title is unique.
*
* @param mixed $title
* @param integer|null $id
* @return void
*/
private static function assertTitleIsUnique($title, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
}
/**
* Getter reading_time.
*
* * USED
*
* @return string
*/
public function getReadingTime()
{
$allWords = count(explode(
" ",
($this->title . " " .
$this->subtitle . " " .
$this->content . " " .
$this->content2 . " " .
\Carbon\Carbon::parse($this->event_start)->locale('fr_FR')->isoFormat('D MMM Y')
)
));
$time = round($allWords / 140, 0);
return ($time < 1) ? round(($allWords / 140) * 60, 0) . " seconde(s)" : $time . " minute(s)";
}
// * RELATIONSHIPS
/**
* BelongsToMany news
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function news()
{
return $this->belongsToMany(News::class, 'news_news', 'news_id', 'news_related_id');
}
/**
* BelongsToMany products
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany(Product::class);
}
/**
* BelongsToMany categories
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categories()
{
return $this->belongsToMany(NewsCategory::class);
}
}