<?php
declare(strict_types=1);
namespace Econsor\Shopware\AtBitConfiguratorConnector;
use Doctrine\DBAL\Connection;
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\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AtBitConfiguratorConnector extends Plugin
{
public function install(InstallContext $installContext): void
{
if ($this->customFieldsExist($installContext->getContext())) {
return;
}
$this->getCustomFieldSetRepository()->upsert([
[
'name' => 'ec_atbit',
'config' => [
'label' => [
'en-GB' => 'ECONSOR AtBit Integration',
'de-DE' => 'ECONSOR AtBit Integration',
],
],
'customFields' => [
self::getEnableFieldDefinition(),
[
'name' => 'ec_atbit_name',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldPosition' => 2,
'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 update(UpdateContext $updateContext): void
{
if (!$this->container instanceof ContainerInterface) {
throw new \RuntimeException('Service container not available.');
}
$connection = $this->container->get(Connection::class);
if (!$connection instanceof Connection) {
throw new \RuntimeException('DBAL Connection not available.');
}
$fieldDef = self::getEnableFieldDefinition();
$connection->executeUpdate(
"UPDATE `custom_field`
SET `type` = :type,
`config` = :config,
`updated_at` = NOW(3)
WHERE `name` = 'ec_atbit_enable'",
[
'type' => $fieldDef['type'],
'config' => json_encode($fieldDef['config']),
]
);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$this->removeCustomField($uninstallContext);
parent::uninstall($uninstallContext);
}
private static function getEnableFieldDefinition(): array
{
return [
'name' => 'ec_atbit_enable',
'type' => CustomFieldTypes::SELECT,
'config' => [
'componentName' => 'sw-single-select',
'customFieldType' => 'select',
'customFieldPosition' => 1,
'options' => [
['value' => 'off', 'label' => ['en-GB' => 'Off', 'de-DE' => 'Aus']],
['value' => 'loft', 'label' => ['en-GB' => 'Loft', 'de-DE' => 'Loft']],
['value' => 'groke', 'label' => ['en-GB' => 'Groke', 'de-DE' => 'Groke']],
],
'label' => [
'en-GB' => 'AtBit Mode',
'de-DE' => 'AtBit Modus',
],
],
];
}
private function removeCustomField(UninstallContext $uninstallContext): void
{
$fieldIds = $this->customFieldsExist($uninstallContext->getContext());
if ($fieldIds) {
$fields = array_values($fieldIds->getData());
$this->getCustomFieldSetRepository()->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
{
if (!$this->container instanceof ContainerInterface) {
throw new \RuntimeException('Service container not available.');
}
$repository = $this->container->get('custom_field_set.repository');
if (!$repository instanceof EntityRepository) {
throw new \RuntimeException('custom_field_set.repository is not an EntityRepository.');
}
return $repository;
}
}