331 lines
10 KiB
PHP
331 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\InputType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\HasTranslations;
|
|
use App\Traits\Models\MiniCrudModel;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property string $seo_title
|
|
* @property string $seo_description
|
|
* @property string $title
|
|
* @property string $slug
|
|
* @property string $subtitle
|
|
* @property string $content
|
|
* @property \Illuminate\Support\Collection $picture_list
|
|
* @property object $data
|
|
* @property integer $order
|
|
* @property boolean $status
|
|
* @property \Carbon\Carbon|null $publish_at
|
|
* @property \Carbon\Carbon|null $unpublish_at
|
|
* @property \Carbon\Carbon $created_at
|
|
* @property \Carbon\Carbon $updated_at
|
|
*/
|
|
class News extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use MiniCrudModel;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'slug',
|
|
'subtitle',
|
|
'content',
|
|
'data',
|
|
'locales',
|
|
'order',
|
|
'status',
|
|
'publish_at',
|
|
'unpublish_at'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'data' => 'object',
|
|
'locales' => 'array',
|
|
'status' => 'bool',
|
|
'publish_at' => 'timestamp',
|
|
'unpublish_at' => 'timestamp'
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'slug',
|
|
'subtitle',
|
|
'content',
|
|
'data'
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title);
|
|
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->title);
|
|
$model->content = FileStorageHelper::storeModelFilesFromHtml($model, $model->content);
|
|
$model->makeStatusSense();
|
|
self::setOrder($model);
|
|
});
|
|
static::created(function (self $model) {
|
|
$model->flashMessages();
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title);
|
|
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->title);
|
|
$model->content = FileStorageHelper::storeModelFilesFromHtml($model, $model->content);
|
|
$model->makeStatusSense();
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldModelFilesFromHtml(
|
|
$model->content,
|
|
ToolboxHelper::getOriginal($model, 'content')
|
|
);
|
|
$model->flashMessages();
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeModelFilesOfHtml($model->content);
|
|
$model->minicrudDeleted();
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Apply function on data typed text while on deleted event
|
|
*
|
|
* @return void
|
|
*/
|
|
public function minicrudDeleted()
|
|
{
|
|
/** @var array */
|
|
foreach ($this->data as $input) {
|
|
switch ($input['inputType']) {
|
|
case InputType::text->value:
|
|
FileStorageHelper::removeModelFilesOfHtml($input['text']);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
}
|
|
|
|
/**
|
|
* Get picture_list attribute
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getPictureListAttribute(): Collection
|
|
{
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
return \collect($this->data)->filter(fn ($value, $key) => \preg_match('/bloc[0-9]+/u', $key) !== false)
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
->groupBy(fn ($value, $key) => \collect(\explode('_', $key))->take(2)->implode('_'))
|
|
->filter(fn (Collection $blocks) => \collect($blocks->first())->get('value'))
|
|
->map(function (Collection $blocks) {
|
|
$pictureBlock = (object)$blocks->last();
|
|
return (object)[
|
|
'value' => $pictureBlock->value,
|
|
'title' => $pictureBlock->title,
|
|
'alt' => $pictureBlock->alt
|
|
];
|
|
})->values();
|
|
}
|
|
|
|
/**
|
|
* Adjusts model status using current status and publish dates
|
|
*
|
|
* @return void
|
|
*/
|
|
public function makeStatusSense(): void
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $this->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $this->unpublish_at;
|
|
$wantedStatus = $this->status;
|
|
if ($publish_at or $unpublish_at) {
|
|
$this->status = $this->planificated_status;
|
|
}
|
|
if ($wantedStatus !== $this->status) {
|
|
$previousMessage = session()->get('bo.info', '');
|
|
session()->flash('bo.info', \trans(
|
|
'La publication est modifiée en concordance avec les dates enregistrées donc
|
|
pour modifier l\'état de publication, veuillez modifier ou enlever les dates correspondantes.'
|
|
) . " \n{$previousMessage}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Flash boolean attributes modification messages
|
|
*
|
|
* @param boolean $old Ignore old attribute comparison.
|
|
* @return void
|
|
*/
|
|
public function flashMessages(bool $old = true)
|
|
{
|
|
$booleanChecks = [
|
|
'status' => [
|
|
'bo.info' => trans(':model est maintenant publié', [
|
|
'model' => Str::singular(trans('models.classes.products'))
|
|
]),
|
|
'bo.warning' => trans(':model est maintenant dépublié', [
|
|
'model' => Str::singular(trans('models.classes.products'))
|
|
])
|
|
]
|
|
];
|
|
$sessionMessages = [];
|
|
|
|
// * Merge messages
|
|
foreach ($booleanChecks as $attribute => $booleanCheck) {
|
|
if ($old and $this->getOriginal($attribute) == $this->{$attribute}) {
|
|
continue;
|
|
}
|
|
/** @var string $flashType */
|
|
$flashType = \array_keys($booleanCheck)[$this->{$attribute} ? 0 : 1];
|
|
if (!\array_key_exists($flashType, $sessionMessages)) {
|
|
$sessionMessages[$flashType] = [];
|
|
}
|
|
$sessionMessages[$flashType][] = $booleanCheck[$flashType];
|
|
}
|
|
|
|
foreach ($sessionMessages as $flashType => $messages) {
|
|
session()->flash($flashType, collect($messages)->implode("\n"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(Model $model)
|
|
{
|
|
$model->slug = Str::slug($model->name);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(Model $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique
|
|
*
|
|
* @param string|integer|float|boolean|null|array $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an news title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(string|int|float|bool|null|array $title, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Return the structure necessary of data.
|
|
*
|
|
* @param string $img
|
|
* @param string $imgAlt
|
|
* @param string $imgTitle
|
|
* @return ((string|\App\Enums\InputType)[]|(string|int[]|\App\Enums\InputType)[]|(string|\App\Enums\InputType|int|(string|\Illuminate\Validation\Rules\Enum)[])[])[]
|
|
*/
|
|
public static function getEmptyData(
|
|
string $img = '',
|
|
string $imgAlt = '',
|
|
string $imgTitle = ''
|
|
) {
|
|
$data = [];
|
|
for ($i = 1; $i <= 3; $i++) {
|
|
$data = self::emptyDataBloc(
|
|
$i,
|
|
$data,
|
|
$img,
|
|
$imgAlt,
|
|
$imgTitle
|
|
);
|
|
} //end for
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Empty data bloc
|
|
*
|
|
* @param string $index
|
|
* @param array $data
|
|
* @param string $img
|
|
* @param string $imgAlt
|
|
* @param string $imgTitle
|
|
* @return array
|
|
*/
|
|
public static function emptyDataBloc(
|
|
string $index,
|
|
array $data,
|
|
string $img = '',
|
|
string $imgAlt = '',
|
|
string $imgTitle = ''
|
|
): array {
|
|
$data["bloc_{$index}_display_picture"] = [
|
|
'label' => "Bloc {$index} - Display picture",
|
|
'inputType' => InputType::boolean->value,
|
|
'value' => false,
|
|
];
|
|
$data["bloc_{$index}_image"] = [
|
|
'label' => "Bloc {$index} - Image",
|
|
'dimensions' => ['width' => 770, 'height' => 770],
|
|
'inputType' => InputType::image->value,
|
|
'alt' => $imgAlt,
|
|
'title' => $imgTitle,
|
|
'value' => $img,
|
|
'validate' => "required_if:data.bloc_{$index}_display_picture.value,true|nullable",
|
|
'required' => false
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
}
|