189 lines
6.7 KiB
PHP
189 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Migrations;
|
|
|
|
use App\Enums\BlockTypeEnum;
|
|
use App\Enums\InputType;
|
|
use App\Enums\StaticPageType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Models\Block;
|
|
use App\Models\Picto;
|
|
use App\Models\SectionPage;
|
|
use App\Models\StaticPage;
|
|
|
|
trait HasStaticPageMigration
|
|
{
|
|
private function getCommonSection(StaticPageType $type): array
|
|
{
|
|
$sourcePage = StaticPage::where('type', $type)->first();
|
|
|
|
if (! $sourcePage) {
|
|
return [];
|
|
}
|
|
$sourceData = is_array($sourcePage->data)
|
|
? $sourcePage->data
|
|
: json_decode($sourcePage->data, true);
|
|
|
|
return $sourceData['common_section'] ?? [];
|
|
}
|
|
|
|
private function storeImage(StaticPage $page, string $relativePath): string
|
|
{
|
|
return FileStorageHelper::storeFile(
|
|
$page,
|
|
new \SplFileInfo(resource_path('assets/images/' . $relativePath))
|
|
);
|
|
}
|
|
|
|
private function processImageFields(StaticPage $page, array &$sectionData): void
|
|
{
|
|
foreach ($sectionData as $key => &$value) {
|
|
if (isset($value['inputType']) && $value['inputType'] === InputType::image->value && ! empty($value['value'])) {
|
|
$value['value'] = $this->storeImage($page, $value['value']);
|
|
}
|
|
|
|
if (isset($value['inputType']) && $value['inputType'] === InputType::selectImageVideo->value) {
|
|
if (! empty($value['value']['picture']['path'])) {
|
|
$value['value']['picture']['path'] = $this->storeImage($page, $value['value']['picture']['path']);
|
|
}
|
|
}
|
|
}
|
|
unset($value);
|
|
}
|
|
|
|
private function createOrUpdateStaticPage(array $pageData, array $data, StaticPageType $type): StaticPage
|
|
{
|
|
$staticPage = StaticPage::query()
|
|
->where('type', $type)
|
|
->with(['sections.blocks', 'sections.pictos'])
|
|
->first();
|
|
|
|
if ($staticPage) {
|
|
$staticPage->data = $data;
|
|
$staticPage->saveQuietly();
|
|
} else {
|
|
$staticPage = StaticPage::query()->forceCreateQuietly($pageData);
|
|
}
|
|
|
|
return $staticPage;
|
|
}
|
|
|
|
private function createOrUpdateSection(StaticPage $page, array $sectionConfig): SectionPage
|
|
{
|
|
$section = $page->sections->firstWhere('name', $sectionConfig['name']);
|
|
|
|
if (! $section) {
|
|
$section = $page->sections()->createQuietly([
|
|
'name' => $sectionConfig['name'],
|
|
'visibility' => true,
|
|
'data' => [],
|
|
]);
|
|
}
|
|
|
|
$sectionData = $sectionConfig['data'];
|
|
|
|
foreach ($sectionData as $key => &$value) {
|
|
if (isset($value['inputType']) && $value['inputType'] === InputType::image->value && ! empty($value['value'])) {
|
|
$value['value'] = FileStorageHelper::storeFile(
|
|
$page,
|
|
new \SplFileInfo(resource_path('assets/images/' . $value['value']))
|
|
);
|
|
}
|
|
|
|
if (isset($value['inputType']) && $value['inputType'] === InputType::selectImageVideo->value) {
|
|
if (! empty($value['value']['picture']['path'])) {
|
|
$value['value']['picture']['path'] = FileStorageHelper::storeFile(
|
|
$page,
|
|
new \SplFileInfo(resource_path('assets/images/' . $value['value']['picture']['path']))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
unset($value);
|
|
|
|
if (! empty($sectionConfig['blocks']['has_pictos'])) {
|
|
$sectionData['has_pictos'] = true;
|
|
$sectionData['max_pictos'] = $sectionConfig['blocks']['max_pictos'] ?? 6;
|
|
}
|
|
|
|
$section->update([
|
|
'data' => $sectionData,
|
|
'visibility' => true,
|
|
]);
|
|
|
|
return $section;
|
|
}
|
|
|
|
private function createOrUpdateBlocks(StaticPage $page, SectionPage $section, array $sectionConfig): void
|
|
{
|
|
$blocks = [];
|
|
|
|
if (! empty($sectionConfig['blocks']['reductionBlock'])) {
|
|
$blocks[] = BlockTypeEnum::redirection->defaultConfig();
|
|
}
|
|
|
|
if (! empty($sectionConfig['blocks']['bienfaitBlock'])) {
|
|
foreach ($sectionConfig['blocks']['bienfaitBlock'] as $i => $data) {
|
|
$blockConfig = BlockTypeEnum::bienfait->defaultConfig();
|
|
|
|
if (! is_null($data['name_onglet'] ?? null)) {
|
|
$blockConfig['data']['name_onglet']['value'] = $data['name_onglet'];
|
|
} else {
|
|
unset($blockConfig['data']['name_onglet']);
|
|
}
|
|
|
|
$blockConfig['data']['title']['value'] = $data['title'] ?? '';
|
|
$blockConfig['data']['description']['value'] = $data['description'] ?? '';
|
|
$blockConfig['data']['picture']['value'] = $data['picture']['value'] ?? '';
|
|
$blockConfig['data']['picture']['title'] = $data['picture']['title'] ?? '';
|
|
$blockConfig['data']['picture']['alt'] = $data['picture']['alt'] ?? '';
|
|
|
|
if (! empty($blockConfig['data']['picture']['value'])) {
|
|
$blockConfig['data']['picture']['value'] = $this->storeImage(
|
|
$page,
|
|
$blockConfig['data']['picture']['value']
|
|
);
|
|
}
|
|
|
|
$blocks[] = $blockConfig;
|
|
}
|
|
}
|
|
|
|
foreach ($blocks as $index => $blockConfig) {
|
|
Block::updateOrCreate([
|
|
'section_id' => $section->id,
|
|
'type' => $blockConfig['type']->value,
|
|
'data' => $blockConfig['data'],
|
|
'order' => $index,
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function createOrUpdatePictos(StaticPage $page, array $pictos): void
|
|
{
|
|
foreach ($pictos as $sectionName => $sectionPictos) {
|
|
$section = $page->sections->firstWhere('name', $sectionName);
|
|
|
|
if (! $section) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($sectionPictos as $order => $pictoData) {
|
|
$pictoPath = FileStorageHelper::storeFile(
|
|
$page,
|
|
new \SplFileInfo(resource_path($pictoData['picto']))
|
|
);
|
|
|
|
Picto::updateOrCreate([
|
|
'title' => $pictoData['title'],
|
|
'description' => $pictoData['description'],
|
|
'picture' => $pictoPath,
|
|
'picture_alt' => $pictoData['picto_alt'],
|
|
'picture_title' => $pictoData['picto_title'],
|
|
'section_id' => $section->id,
|
|
'order' => $order + 1,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|