<?php declare(strict_types=1);
namespace MasterFFL\Checkout;
use MasterFFL\Checkout\Service\FFLAttributeService;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class MasterFFLCheckout extends Plugin
{
public const PLUGIN_NAME = 'MasterFFLCheckout';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createFFLAttribute($installContext->getContext());
$this->createCustomFields($installContext);
}
private function createCustomFields(InstallContext $installContext): void
{
$ctx = $installContext->getContext();
$setRepo = $this->container->get('custom_field_set.repository');
$fieldRepo = $this->container->get('custom_field.repository');
$setId = $setRepo->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', 'masterffl_order_fields')),
$ctx
)->firstId();
if ($setId) {
return;
}
$setRepo->create([[
'name' => 'masterffl_order_fields',
'config' => ['label' => [
'en-GB' => 'MasterFFL Order Fields',
'de-DE' => 'MasterFFL Bestellfelder'
]],
'customFields' => [
[
'name' => 'masterffl_license_number',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'FFL License Number',
'de-DE' => 'FFL Lizenznummer'
],
'customFieldPosition' => 1,
'componentName' => 'sw-text-field',
'customFieldType' => 'text',
],
],
[
'name' => 'masterffl_transfer_url',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'FFL Transfer URL',
'de-DE' => 'FFL Transfer URL'
],
'customFieldPosition' => 2,
'componentName' => 'sw-text-field',
'customFieldType' => 'text',
],
],
],
'relations' => [['entityName' => 'order']],
]], $ctx);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeFFLAttribute($uninstallContext->getContext());
$this->removeCustomFields($uninstallContext);
}
private function createFFLAttribute(Context $context): void
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var EntityRepository $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
/** @var SystemConfigService $systemConfigService */
$systemConfigService = $this->container->get(SystemConfigService::class);
$attributeService = new FFLAttributeService(
$customFieldSetRepository,
$customFieldRepository,
$systemConfigService
);
$attributeService->createFFLAttribute($context);
}
private function removeFFLAttribute(Context $context): void
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var EntityRepository $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
/** @var SystemConfigService $systemConfigService */
$systemConfigService = $this->container->get(SystemConfigService::class);
$attributeService = new FFLAttributeService(
$customFieldSetRepository,
$customFieldRepository,
$systemConfigService
);
$attributeService->removeFFLAttribute($context);
}
private function removeCustomFields(UninstallContext $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new \Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria();
$criteria->addFilter(new \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter('name', 'masterffl_order_fields'));
$customFieldSetIds = $customFieldSetRepository->searchIds($criteria, $context->getContext());
if ($customFieldSetIds->getTotal() > 0) {
$customFieldSetRepository->delete([
['id' => $customFieldSetIds->getIds()[0]]
], $context->getContext());
}
}
}