EngagementType::class ]; protected $casts = [ 'content' => 'array', ]; protected $fillable = [ 'icon', 'pretitle', 'title', 'metaTitle', 'metaDescription', 'content', 'status' ]; protected $attributes = [ 'icon' => '', 'pretitle' => '', 'metaTitle' => '', 'metaDescription' => '', 'type' => 1, // EngagementType article ]; protected static function booted() { static::creating(function (Engagement $engagement) { }); static::updating(function (Engagement $engagement) { }); } 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 [ EngagementBlocType::bloc1()->value => [ 'type' => EngagementBlocType::bloc1(), 'content' => [ 'img' => [ 'dimensions' => 'largeur 792,hauteur 555', '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' => '' ], 'link' => [ 'text' => 'En savoir +', 'label' => 'Lien du bouton en savoir +', 'inputType' => InputType::url(), 'status' => true, 'newtab' => false, 'value' => '' ], 'labels' => [ 'label' => 'Labels avec titre', 'inputType' => InputType::labelArray(), 'value' => [] ] ] ], EngagementBlocType::bloc2()->value => [ 'type' => EngagementBlocType::bloc2(), 'content' => [ 'img' => [ 'dimensions' => 'largeur 384,hauteur 260', 'label' => 'Illustration', 'inputType' => InputType::image(), 'alt' => '', 'value' => '' ], 'content' => [ 'label' => 'Contenu', 'inputType' => InputType::text(), 'value' => '' ], 'link' => [ 'label' => 'Lien du bouton en savoir +', 'inputType' => InputType::url(), 'status' => true, 'newtab' => false, 'value' => '' ] ] ], EngagementBlocType::bloc3()->value => [ 'type' => EngagementBlocType::bloc3(), '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' => '' ], 'link' => [ 'label' => 'Lien du bouton en savoir +', 'inputType' => InputType::url(), 'status' => true, 'newtab' => false, 'value' => '' ], 'labels' => [ 'label' => 'Labels avec titre', 'inputType' => InputType::labelArray(), 'value' => [] ] ] ], EngagementBlocType::bloc4()->value => [ 'type' => EngagementBlocType::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' => '' ], 'side' => [ 'label' => 'Orientation de l\'image (gauche ou droite)', 'inputType' => InputType::boolean(), 'value' => '' ], 'link' => [ 'label' => 'Lien du bouton en savoir +', 'inputType' => InputType::url(), 'status' => true, 'newtab' => false, 'value' => '' ], 'labels' => [ 'label' => 'Labels avec titre', 'inputType' => InputType::labelArray(), 'value' => [] ] ] ], EngagementBlocType::bloc5()->value => [ 'type' => EngagementBlocType::bloc5(), 'content' => [ 'titlePicto' => [ 'dimensionsMax' => 'largeur 130,hauteur 110', 'label' => 'Pictogramme Titre', 'inputType' => InputType::image(), 'value' => '' ], 'mobileVisible' => [ 'label' => 'Visible sur mobile ?', 'inputType' => InputType::boolean(), 'value' => '' ], 'images' => [ 'dimensionsMax' => 'largeur 130,hauteur 110', 'label' => 'Illustrations', 'inputType' => InputType::imageArray(), 'value' => [] ] ] ], ]; } /** * Get Label Template Data * * @return array */ public static function getLabel(): array { return [ 'bigText' => '', 'text' => '', 'fullLine' => false ]; } 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] )); } 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'] = $this->storeImage($input['value']); break; case InputType::imageArray()->value: foreach ($input['value'] as $k => $image) { $content[$i]['content'][$o]['value'][$k] = $this->storeImage($image); } 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::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); } private function storeImage($image): string { if (\is_object($image) and get_class($image) === UploadedFile::class) { /** @var UploadedFile */ $image = $image; $newPath = $this->prepareMoveFile($image->getClientOriginalName()); $newPath = str_replace(storage_path('app'), '', $newPath); $image->storePubliclyAs(pathinfo($newPath, \PATHINFO_DIRNAME), pathinfo($newPath, \PATHINFO_BASENAME)); return '/storage' . str_replace('/public', '', $newPath); } if (\is_object($image) and get_class($image) === SplFileInfo::class) { /** @var SplFileInfo */ $image = $image; $newPath = $this->prepareMoveFile($image->getBaseName()); if (!File::copy($image->getRealPath(), $newPath)) { throw new \Exception(sprintf( 'Failed to copy file %s to %s', $image->getRealPath(), $newPath )); } return str_replace(\storage_path('app/public'), '/storage', $newPath); } if (\is_string($image)) { return $image; } } private function prepareMoveFile(string $filename): string { $pathFolder = \storage_path('app/public/images/engagements'); if (!File::exists($pathFolder)) { File::makeDirectory($pathFolder, 0755, true); } $filename = sprintf('%s-%s', Str::uuid(), $filename); $fullPath = sprintf('%s/%s', $pathFolder, $filename); if (File::exists($fullPath)) { File::delete($fullPath); } return $fullPath; } }