first commit
This commit is contained in:
231
bundles/External/ZNZBundle/Resources/script/ImportSectionsByJSONScript.php
vendored
Normal file
231
bundles/External/ZNZBundle/Resources/script/ImportSectionsByJSONScript.php
vendored
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace External\ZNZBundle\Resources\script;
|
||||
|
||||
use KupShop\AdminBundle\Util\Script\Script;
|
||||
use KupShop\CatalogBundle\Section\SectionTree;
|
||||
use KupShop\ContentBundle\Util\Block;
|
||||
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
||||
use KupShop\KupShopBundle\Util\Functional\Mapping;
|
||||
use KupShop\KupShopBundle\Util\System\PathFinder;
|
||||
|
||||
class ImportSectionsByJSONScript extends Script
|
||||
{
|
||||
protected static $name = '[ZNZ] Import sections by JSON data';
|
||||
protected static $defaultParameters = [
|
||||
'run' => false,
|
||||
'jsons' => [
|
||||
'sections' => 'data/files/sections.json',
|
||||
'parameters_list' => 'data/files/parameters_list.json',
|
||||
'sections_relation' => 'data/files/sections_relation.json',
|
||||
'parameters_sections' => 'data/files/parameters_sections.json',
|
||||
],
|
||||
];
|
||||
|
||||
protected function run(array $arguments)
|
||||
{
|
||||
if (!($arguments['run'] ?? false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$files = $arguments['jsons'] ?? [];
|
||||
|
||||
$blockUtil = ServiceContainer::getService(Block::class);
|
||||
|
||||
$sections = json_decode(file_get_contents($files['sections']), true)[2]['data'] ?? [];
|
||||
$parametersList = json_decode(file_get_contents($files['parameters_list']), true)[2]['data'] ?? [];
|
||||
$parametersList = Mapping::mapKeys($parametersList, fn ($k, $v) => [$v['id'], $v]);
|
||||
|
||||
$this->log('Start');
|
||||
|
||||
sqlGetConnection()->transactional(function () use ($files, $blockUtil, $sections, $parametersList) {
|
||||
sqlQueryBuilder()
|
||||
->delete('blocks')
|
||||
->where('id IN (SELECT id_block FROM sections WHERE id_block IS NOT NULL)')
|
||||
->execute();
|
||||
|
||||
sqlQueryBuilder()
|
||||
->delete('sections')
|
||||
->execute();
|
||||
|
||||
foreach ($sections as $section) {
|
||||
$this->log("Importing section: {$section['id']}");
|
||||
|
||||
$blocks = Mapping::mapKeys(
|
||||
json_decode($section['blocks'] ?: '', true) ?: [],
|
||||
fn ($k, $v) => [$v['id'], $v]
|
||||
);
|
||||
$blocksTranslations = json_decode($section['blocks_translations'] ?: '', true) ?: [];
|
||||
$sectionTranslations = Mapping::mapKeys(
|
||||
json_decode($section['translations'] ?: '', true) ?: [],
|
||||
fn ($k, $v) => [$v['id'], $v]
|
||||
);
|
||||
|
||||
unset($section['blocks'], $section['blocks_translations'], $section['translations']);
|
||||
|
||||
$data = json_decode($section['data'] ?: '', true) ?: [];
|
||||
|
||||
if (!empty($data['virtual_settings'])) {
|
||||
foreach ($data['virtual_settings']['parameters'] ?? [] as $key => $value) {
|
||||
unset($data['virtual_settings']['parameters'][$key]);
|
||||
if (!empty($parametersList[$value]['description'])) {
|
||||
if ($valueId = $this->getParameterValueByZNZId($parametersList[$value]['description'])) {
|
||||
$data['virtual_settings']['parameters'][$key] = $valueId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$section['id_block'] = null;
|
||||
$section['data'] = json_encode($data);
|
||||
|
||||
if (!empty($section['photo'])) {
|
||||
$this->downloadSectionPhoto($section['photo']);
|
||||
}
|
||||
|
||||
sqlQueryBuilder()
|
||||
->insert('sections')
|
||||
->directValues($section)
|
||||
->execute();
|
||||
|
||||
if ($section['id'] == 0) {
|
||||
$rootSectionId = sqlInsertId();
|
||||
sqlQueryBuilder()
|
||||
->update('sections')
|
||||
->directValues(['id' => 0])
|
||||
->where(\Query\Operator::equals(['id' => $rootSectionId]))
|
||||
->execute();
|
||||
}
|
||||
|
||||
$sectionId = (int) $section['id'];
|
||||
|
||||
foreach ($sectionTranslations as $translation) {
|
||||
unset($translation['id']);
|
||||
$translation['id_section'] = $sectionId;
|
||||
sqlQueryBuilder()
|
||||
->insert('sections_translations')
|
||||
->directValues($translation)
|
||||
->execute();
|
||||
}
|
||||
|
||||
if (!empty($blocks)) {
|
||||
$rootBlockId = $blockUtil->getRootBlockId('sections', $sectionId);
|
||||
foreach ($blocks as $block) {
|
||||
$blockTranslations = $this->getBlockTranslations((int) $block['id'], $blocksTranslations);
|
||||
|
||||
unset($block['id']);
|
||||
$block['id_root'] = $rootBlockId;
|
||||
$block['id_parent'] = $rootBlockId;
|
||||
|
||||
sqlQueryBuilder()
|
||||
->insert('blocks')
|
||||
->directValues($block)
|
||||
->execute();
|
||||
|
||||
$blockId = (int) sqlInsertId();
|
||||
|
||||
foreach ($blockTranslations as $translation) {
|
||||
unset($translation['id'], $translation['id_block']);
|
||||
$translation['id_block'] = $blockId;
|
||||
sqlQueryBuilder()
|
||||
->insert('blocks_translations')
|
||||
->directValues($translation)
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->log('Creating relations');
|
||||
|
||||
$sectionsRelations = json_decode(file_get_contents($files['sections_relation']), true)[2]['data'] ?? [];
|
||||
foreach ($sectionsRelations as $relation) {
|
||||
try {
|
||||
sqlQueryBuilder()
|
||||
->insert('sections_relation')
|
||||
->directValues($relation)
|
||||
->execute();
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
$this->log('Creating parameters sections');
|
||||
|
||||
$parametersSections = json_decode(file_get_contents($files['parameters_sections']), true)[2]['data'] ?? [];
|
||||
foreach ($parametersSections as $relation) {
|
||||
$znzId = $relation['id_znz'];
|
||||
|
||||
unset($relation['id_znz']);
|
||||
|
||||
$parameterId = sqlQueryBuilder()
|
||||
->select('id_parameter')
|
||||
->from('znz_parameters')
|
||||
->where(\Query\Operator::equals(['id_znz' => $znzId]))
|
||||
->execute()->fetchOne();
|
||||
|
||||
if (!$parameterId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$relation['id_parameter'] = $parameterId;
|
||||
|
||||
sqlQueryBuilder()
|
||||
->insert('parameters_sections')
|
||||
->directValues($relation)
|
||||
->execute();
|
||||
}
|
||||
});
|
||||
|
||||
$sectionTree = ServiceContainer::getService(SectionTree::class);
|
||||
$sectionTree->clearCache();
|
||||
|
||||
$this->log('Done');
|
||||
}
|
||||
|
||||
private function downloadSectionPhoto(string $file): void
|
||||
{
|
||||
$downloader = new \Downloader();
|
||||
$downloader->setMethod('curl');
|
||||
|
||||
$url = 'https://www.profi-parfemy.cz/data/section/'.$file;
|
||||
|
||||
$downloader->copyRemoteFile(
|
||||
$url,
|
||||
PathFinder::getService()->dataPath("section/{$file}")
|
||||
);
|
||||
}
|
||||
|
||||
private function getBlockTranslations(int $blockId, array $blocksTranslations): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($blocksTranslations as $translation) {
|
||||
if ($translation['id_block'] == $blockId) {
|
||||
$result[] = $translation;
|
||||
}
|
||||
}
|
||||
|
||||
return Mapping::mapKeys($result, fn ($k, $v) => [$v['id'], $v]);
|
||||
}
|
||||
|
||||
private function getParameterValueByZNZId(string $znzId): ?int
|
||||
{
|
||||
static $cache = [];
|
||||
|
||||
if (($cache[$znzId] ?? false) !== false) {
|
||||
return $cache[$znzId];
|
||||
}
|
||||
|
||||
$valueId = sqlQueryBuilder()
|
||||
->select('id_parameters_value')
|
||||
->from('znz_parameters_values')
|
||||
->where(\Query\Operator::equals(['id_znz' => $znzId]))
|
||||
->execute()->fetchOne() ?: null;
|
||||
|
||||
return $cache[$znzId] = $valueId;
|
||||
}
|
||||
}
|
||||
|
||||
return ImportSectionsByJSONScript::class;
|
||||
Reference in New Issue
Block a user