<?php
declare(strict_types=1);
namespace Econsor\Shopware\AtBitConfiguratorConnector;
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\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
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 Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AtBitConfiguratorConnector extends Plugin
{
public function install(InstallContext $installContext): void
{
if($this->customFieldsExist($installContext->getContext())) {
return;
}
assert($this->container instanceof ContainerInterface);
/** @var SystemConfigService $systemConfigService */
$systemConfigService = $this->container->get(SystemConfigService::class);
$this->getCustomFieldSetRepository()->upsert([
[
'name' => 'ec_atbit',
'config' => [
'label' => [
'en-GB' => 'ECONSOR AtBit Integration',
'de-DE' => 'ECONSOR AtBit Integration'
],
],
'customFields' => [
[
'name' => 'ec_atbit_enable',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Enable AtBit Link?',
'de-DE' => 'AtBit Verlinkung aktivieren?'
],
]
],
[
'name' => 'ec_atbit_name',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldPosition' => 1,
'customFieldType' => CustomFieldTypes::TEXT,
'type' => CustomFieldTypes::TEXT,
'label' => [
'en-GB' => 'AtBit Name (For URL)',
'de-DE' => 'AtBit Name (Für URL)'
]
]
]
],
'relations' => [
[
'entityName' => 'product'
],
]
]
], $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$this->removeCustomField($uninstallContext);
parent::uninstall($uninstallContext);
}
private function removeCustomField(UninstallContext $uninstallContext): void
{
$fieldIds = $this->customFieldsExist($uninstallContext->getContext());
if ($fieldIds) {
$cFieldRepo = $this->getCustomFieldSetRepository();
$fields = array_values($fieldIds->getData());
$cFieldRepo->delete($fields, $uninstallContext->getContext());
}
}
private function customFieldsExist(Context $context): ?IdSearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', ['ec_atbit']));
$ids = $this->getCustomFieldSetRepository()->searchIds($criteria, $context);
return $ids->getTotal() > 0 ? $ids : null;
}
private function getCustomFieldSetRepository(): EntityRepository
{
$container = $this->container;
assert($container instanceof ContainerInterface);
$repository = $container->get('custom_field_set.repository');
assert($repository instanceof EntityRepository);
return $repository;
}
}