<?php declare(strict_types=1);
namespace HochwarthCheckoutCheckbox;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class HochwarthCheckoutCheckbox extends Plugin
{
public const CUSTOMER_MARKETING_ALLOWED = 'hochwarth_checkout_checkbox';
public const CUSTOMER_MARKETING_ALLOWED_MULTI = 'hochwarth_checkout_checkbox_multi';
public function activate(ActivateContext $activateContext): void
{
/** @var EntityRepository $repo */
$repo = $this->container->get('custom_field.repository');
$result = $repo->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOMER_MARKETING_ALLOWED)),
$activateContext->getContext()
);
if ($result->getTotal() > 0) {
return;
}
$repo->create(
[
[
'name' => self::CUSTOMER_MARKETING_ALLOWED,
'type' => CustomFieldTypes::BOOL,
],
],
$activateContext->getContext()
);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
/** @var EntityRepository $repo */
$repo = $this->container->get('custom_field.repository');
$result = $repo->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOMER_MARKETING_ALLOWED)),
$uninstallContext->getContext()
);
if (!$result->getTotal()) {
return;
}
$result = array_map(function ($v) {
return ['id' => $v];
}, $result->getIds());
$repo->delete($result, $uninstallContext->getContext());
$connection = $this->container->get(Connection::class);
$connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox_sales_channels`");
$connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox_translation`");
$connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox`");
}
}