125 lines
4.2 KiB
PHP
125 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\ZNZBundle\Resources\script;
|
|
|
|
use External\ZNZBundle\Util\ZNZUtil;
|
|
use KupShop\AdminBundle\Util\Script\Script;
|
|
use KupShop\CatalogBundle\Section\SectionTree;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\KupShopBundle\Util\FileUtil;
|
|
|
|
class ImportSectionsScript extends Script
|
|
{
|
|
protected static $name = '[ZNZ] Import sekci';
|
|
protected static $defaultParameters = [
|
|
'parameterId' => 26,
|
|
'sections' => 'https://docs.google.com/spreadsheets/d/17uSZJgHL_V3f-1MwosdH4JVt21BBXdchTsscDrJqdOg/export?format=csv&gid=1378451953',
|
|
'mapping' => 'https://docs.google.com/spreadsheets/d/17uSZJgHL_V3f-1MwosdH4JVt21BBXdchTsscDrJqdOg/export?format=csv&gid=1395428523',
|
|
];
|
|
|
|
protected function run(array $arguments)
|
|
{
|
|
$sectionTree = ServiceContainer::getService(SectionTree::class);
|
|
$znzUtil = ServiceContainer::getService(ZNZUtil::class);
|
|
|
|
$sections = [];
|
|
foreach (FileUtil::loadCSV($arguments['sections']) as $key => $item) {
|
|
if ($key == 0) {
|
|
continue;
|
|
}
|
|
|
|
$sectionNames = array_filter([$item[6], $item[7], $item[8], $item[9]]);
|
|
$sectionName = end($sectionNames);
|
|
|
|
if (empty($sectionName)) {
|
|
continue;
|
|
}
|
|
|
|
$parents = array_filter([$item[1], $item[2], $item[3], $item[4], $item[5]]);
|
|
|
|
$uniqueId = implode('-', $parents);
|
|
|
|
$parentUniqueId = null;
|
|
if (count($parents) > 2) {
|
|
array_pop($parents);
|
|
$parentUniqueId = implode('-', $parents);
|
|
}
|
|
|
|
$sections[$uniqueId] = [
|
|
'name' => $sectionName,
|
|
'parent' => $parentUniqueId,
|
|
'position' => $key,
|
|
];
|
|
}
|
|
|
|
$sectionParameters = [];
|
|
foreach (FileUtil::loadCSV($arguments['mapping']) as $key => $item) {
|
|
if ($key == 0) {
|
|
continue;
|
|
}
|
|
|
|
$uniqueId = implode('-', array_filter([$item[2], $item[3], $item[4], $item[5], $item[6]]));
|
|
|
|
$znzValueId = $item[7];
|
|
if (!($valueId = $znzUtil->getParameterValueMapping($arguments['parameterId'], $znzValueId))) {
|
|
continue;
|
|
}
|
|
|
|
$sectionParameters[$uniqueId][] = $valueId;
|
|
}
|
|
|
|
sqlQueryBuilder()
|
|
->delete('sections')
|
|
->where('id > 0')
|
|
->execute();
|
|
|
|
sqlQuery('ALTER TABLE sections AUTO_INCREMENT=1');
|
|
|
|
$sectionsMap = [];
|
|
|
|
foreach ($sections as $uid => $section) {
|
|
$virtualParameterValueId = array_unique($sectionParameters[$uid] ?? []);
|
|
|
|
$sectionsMap[$uid] = sqlGetConnection()->transactional(function () use ($section, $virtualParameterValueId) {
|
|
sqlQueryBuilder()
|
|
->insert('sections')
|
|
->directValues(
|
|
[
|
|
'name' => $section['name'],
|
|
'virtual' => !empty($virtualParameterValueId) ? 'Y' : 'N',
|
|
'data' => json_encode(
|
|
[
|
|
'virtual_settings' => [
|
|
'figure' => ['Y'],
|
|
'parameters' => $virtualParameterValueId,
|
|
'parametersValuesOperator' => 'OR',
|
|
'inStore' => ['InStore'],
|
|
],
|
|
]
|
|
),
|
|
]
|
|
)
|
|
->execute();
|
|
|
|
return (int) sqlInsertId();
|
|
});
|
|
|
|
sqlQueryBuilder()
|
|
->insert('sections_relation')
|
|
->directValues(
|
|
[
|
|
'id_section' => $sectionsMap[$uid],
|
|
'id_topsection' => $section['parent'] ? $sectionsMap[$section['parent']] : null,
|
|
'position' => $section['position'],
|
|
]
|
|
)->execute();
|
|
}
|
|
|
|
$sectionTree->clearCache();
|
|
}
|
|
}
|
|
|
|
return ImportSectionsScript::class;
|