ldc-legaulois/app/Models/NutritionPage.php
Jérémy Munsch 47144bbf7e
Modification Lot 2 page Santé nutrition
- suppression des 4 pages
- ajout de 2 nouvelle pages
- redirection des anciennes pages vers le nouvelles
- Le nouvelle page ne sont plus statiques (basés sur engagement)
- Ajout du Model NutritionPage
- Ajout des Enums NutritionBlocType, NutritionType
- Intégration complete avec responsive des deux nouvelles pages
- modification des composants vu pour créer une instance de Vuejs par composant existant pour ajouter et enlever des dropdowns
- Suppression des balanced menus
- fix login center image
- Modification de l'intégration de CKeditor
- Ajout de UploadController avec methode pour image
- html purifier fix config using ckeditor
- ckeditor set css base style on front and back (for nutrition pages on front)
2021-10-03 12:39:08 +02:00

298 lines
10 KiB
PHP

<?php
namespace App\Models;
use App\Enums\InputType;
use App\Enums\NutritionBlocType;
use App\Enums\NutritionType;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
class NutritionPage extends Model
{
use CastsEnums;
protected $enumCasts = [
'type' => NutritionType::class
];
protected $casts = [
'content' => 'array',
];
protected $fillable = [
'menutitle', 'title',
'metaTitle', 'metaDescription',
'headerContent', 'content'
];
// cast content
public function setContentAttribute($value)
{
$this->setContent($value);
}
public function getContentAttribute()
{
$content = json_decode($this->getRawOriginal('content'), true);
return isset($content->header) ? $content->header : $content;
}
/**
* Fill the model with an array of attributes.
*
* @param array $attributes
* @return $this
*
* @throws \Illuminate\Database\Eloquent\MassAssignmentException
*/
public function fill(array $attributes)
{
parent::fill($attributes);
if (isset($attributes['content'])) {
$this->setContent($attributes['content']);
}
}
/**
* Get Blocks Template Data
*
* @return array
*/
public static function getBlocks(): array
{
return [
NutritionBlocType::bloc1()->value => [
'type' => NutritionBlocType::bloc1(),
'content' => [
'img' => [
'dimensions' => 'largeur 486,hauteur 682',
'label' => 'Illustration',
'inputType' => InputType::image(),
'alt' => '',
'value' => ''
],
'title' => [
'label' => 'Titre (h2)',
'inputType' => InputType::string(),
'value' => ''
],
'content' => [
'label' => 'Contenu',
'inputType' => InputType::text(),
'value' => ''
],
'side' => [
'label' => 'Orientation de l\'image (gauche ou droite)',
'inputType' => InputType::boolean(),
'value' => ''
],
'dropdownArray' => [
'label' => 'Dropdowns',
'inputType' => InputType::dropdownArray(),
'value' => []
]
]
],
NutritionBlocType::bloc2()->value => [
'type' => NutritionBlocType::bloc2(),
'content' => [
'img' => [
'dimensions' => 'largeur 486,hauteur 682',
'label' => 'Illustration',
'inputType' => InputType::image(),
'alt' => '',
'value' => ''
],
'title' => [
'label' => 'Titre (h2)',
'inputType' => InputType::string(),
'value' => ''
],
'content' => [
'label' => 'Contenu',
'inputType' => InputType::text(),
'value' => ''
],
'labels' => [
'label' => 'Labels avec titre',
'inputType' => InputType::labelArray(),
'value' => []
]
]
],
NutritionBlocType::bloc3()->value => [
'type' => NutritionBlocType::bloc3(),
'content' => [
'img' => [
'dimensions' => 'largeur 792,hauteur 792',
'label' => 'Illustration',
'inputType' => InputType::image(),
'alt' => '',
'value' => ''
],
'title' => [
'label' => 'Titre (h2)',
'inputType' => InputType::string(),
'value' => ''
],
'content' => [
'label' => 'Contenu',
'inputType' => InputType::text(),
'value' => ''
],
'link' => [
'label' => 'Lien en bas du bloc',
'inputType' => InputType::url(),
'status' => true,
'newtab' => false,
'text' => '',
'value' => ''
],
'side' => [
'label' => 'Orientation de l\'image (gauche ou droite)',
'inputType' => InputType::boolean(),
'value' => ''
],
]
],
NutritionBlocType::bloc4()->value => [
'type' => NutritionBlocType::bloc4(),
'content' => [
'img' => [
'dimensions' => 'largeur 792,hauteur 792',
'label' => 'Illustration',
'inputType' => InputType::image(),
'alt' => '',
'value' => ''
],
'title' => [
'label' => 'Titre (h2)',
'inputType' => InputType::string(),
'value' => ''
],
'content' => [
'label' => 'Contenu',
'inputType' => InputType::text(),
'value' => ''
],
'dropdownArray' => [
'label' => 'Dropdowns',
'inputType' => InputType::dropdropdownArraydown(),
'value' => []
]
]
],
NutritionBlocType::bloc5()->value => [
'type' => NutritionBlocType::bloc5(),
'content' => [
'img' => [
'dimensionsMax' => 'largeur 384,hauteur 260',
'label' => 'Pictogramme Titre',
'inputType' => InputType::image(),
'alt' => '',
'value' => ''
],
'content' => [
'label' => 'Contenu',
'inputType' => InputType::text(),
'value' => ''
]
]
],
];
}
/**
* Get Label Template Data
*
* @return array
*/
public static function getLabel(): array
{
return [
'bigText' => '',
'text' => '',
'fullLine' => false
];
}
/**
* Get Dropdown Template Data
*
* @return array
*/
public static function getDropdown(): array
{
return [
'title' => '',
'content' => ''
];
}
private function setContent($content): void
{
$attrs = $this->attributes;
if (isset($attrs['content'])) {
unset($attrs['content']);
}
if (is_string($content)) {
$content = sprintf('{"header" : "%s"}', $content);
} else {
$content = $this->serializeContent($content);
}
$this->setRawAttributes(\array_merge_recursive(
$attrs,
['content' => $content]
));
}
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
private function serializeContent(array $content): string
{
foreach ($content as $i => $bloc) {
foreach ($bloc['content'] as $o => $input) {
switch ($input['inputType']->value ?? $input['inputType']['value']) {
case InputType::image()->value:
$content[$i]['content'][$o]['value'] = Utils::storeImage($input['value'], $this->getTable());
break;
case InputType::imageArray()->value:
foreach ($input['value'] as $k => $image) {
$content[$i]['content'][$o]['value'][$k] = Utils::storeImage($image, $this->getTable());
}
break;
case InputType::labelArray()->value:
foreach ($input['value'] as $k => $label) {
$content[$i]['content'][$o]['value'][$k] = \array_replace(
static::getLabel(),
$content[$i]['content'][$o]['value'][$k]
);
}
break;
case InputType::dropdownArray()->value:
foreach ($input['value'] as $k => $label) {
$content[$i]['content'][$o]['value'][$k] = \array_replace(
static::getDropdown(),
$content[$i]['content'][$o]['value'][$k]
);
}
break;
case InputType::boolean()->value:
case InputType::text()->value:
case InputType::string()->value:
break;
case InputType::url()->value:
Utils::assertNoInternalUrls('content', $input['value']);
break;
default:
throw new \Exception(sprintf(
'Unknown input Type %s',
$input['inputType']->label ?? $input['inputType']['label']
));
}
}
}
return json_encode($content);
}
}