<?php declare(strict_types=1);
namespace EconsorBundles;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class EconsorBundles extends Plugin
{
/**
* @param ContainerBuilder $container
* @inheritDoc
* @throws \Exception
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/DependencyInjection/'));
$loader->load('controller.xml');
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$bundles = $customFieldSetRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name', 'Bundle Step')), $installContext->getContext())->getIds();
if (empty($bundles)) {
$customFieldSetRepository->upsert(
[
[
'name' => 'Bundle Step',
'customFields' => [
['name' => 'step_1', 'type' => CustomFieldTypes::BOOL, 'config' => [
'type' => 'checkbox',
'label' => 'Step 1',
'customFieldPosition' => '1',
'customFieldType' => 'checkbox'
]
],
['name' => 'step_2', 'type' => CustomFieldTypes::BOOL, 'config' => [
'type' => 'checkbox',
'label' => 'Step 2',
'customFieldPosition' => '2',
'customFieldType' => 'checkbox'
]
],
['name' => 'step_3', 'type' => CustomFieldTypes::BOOL, 'config' => [
'type' => 'checkbox',
'label' => 'Step 3',
'customFieldPosition' => '3',
'customFieldType' => 'checkbox'
]
],
],
'relations' => [
[
'entityName' => 'product'
]
]
]
], $installContext->getContext());
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
$connection = $this->container->get(Connection::class);
$customFieldSetId = $connection->fetchAll('SELECT LOWER(HEX(`id`)) AS id FROM `custom_field_set` WHERE `name` = "Bundle Step"');
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (isset($customFieldSetId[0])) {
$customFieldSetRepository->delete(
[
[
'id' => $customFieldSetId[0]['id']
]
],
$uninstallContext->getContext()
);
}
$this->removeMigrations();
$connection->executeQuery('DROP TABLE IF EXISTS `ec_bundles_translation`');
$connection->executeQuery('DROP TABLE IF EXISTS `ec_bundles_product`');
$connection->executeQuery('DROP TABLE IF EXISTS `ec_bundles`');
$connection->executeQuery('DROP TABLE IF EXISTS `ec_collection`');
}
}