<?php declare(strict_types=1);
namespace zenit\PlatformGravity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Framework\ThemeInterface;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use zenit\PlatformGravity\Core\CustomFieldsHelper;
use zenit\PlatformGravity\Service\CustomFieldService;
class zenitPlatformGravity extends Plugin implements ThemeInterface
{
/**
* @var Context
*/
private $context;
public function getThemeConfigPath(): string
{
return 'theme.json';
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->applyUpdates(
$installContext->getContext(),
null,
$installContext->getCurrentPluginVersion()
);
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFields = new CustomFieldsHelper($customFieldSetRepository);
$customFields->getCustomFields($this->container, $installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
if (version_compare($updateContext->getCurrentPluginVersion(), '2.4.6', '<')) {
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFields = new CustomFieldsHelper($customFieldSetRepository);
$customFields->getCustomFields($this->container, $updateContext->getContext());
}
}
public function postUpdate(UpdateContext $updateContext): void
{
parent::postUpdate($updateContext);
$this->updateThemeDuplicates();
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
(new CustomFieldService(
$this->container->get('custom_field_set.repository'),
$uninstallContext->getContext()
))->remove();
if ($uninstallContext->keepUserData()) {
return;
}
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFields = new CustomFieldsHelper($customFieldSetRepository);
$customFields->deleteCustomFields($uninstallContext->getContext());
}
private function updateThemeDuplicates(): void
{
$this->context = Context::createDefaultContext();
$criteriaTheme = new Criteria();
$criteriaTheme->addFilter(new EqualsFilter('technicalName', 'zenitPlatformGravity'));
/** @var EntityRepository $themeRepo */
$themeRepo = $this->container->get('theme.repository');
$parentTheme = $themeRepo->search($criteriaTheme, $this->context)->first();
if (!$parentTheme) {
return;
}
$criteriaThemeDuplicates = new Criteria();
$criteriaThemeDuplicates->addFilter(new EqualsFilter('parentThemeId', $parentTheme->get('id')));
$resultThemeDuplicates = $themeRepo->search($criteriaThemeDuplicates, $this->context)->getElements();
if (!$resultThemeDuplicates) {
return;
}
foreach ($resultThemeDuplicates as $themeDuplicate) {
$data = [
'id' => $themeDuplicate->get('id'),
'baseConfig' => $parentTheme->get('baseConfig')
];
if (!$themeDuplicate->get('previewMediaId')) {
$data['previewMediaId'] = $parentTheme->get('previewMediaId');
}
$themeRepo->update([$data], $this->context);
}
}
/**
* @param ContainerBuilder $container
* @throws \Exception
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator($this->getPath() . '/Core/Content/DependencyInjection'));
$loader->load('services.xml');
}
private function applyUpdates(Context $context, $oldVersion = null, $newVersion = null)
{
$versionClosures = [
'1.0.0' => function () use ($context) {
(new CustomFieldService(
$this->container->get('custom_field_set.repository'),
$context
))->create();
return true;
}
];
foreach ($versionClosures as $version => $versionClosure) {
if ($oldVersion === null ||
(version_compare($oldVersion, $version, '<')
&& version_compare($version, $newVersion, '<='))
) {
if (!$versionClosure($this)) {
return false;
}
}
}
return true;
}
}