<?php
declare(strict_types=1);
namespace LinkMobility\ShopwareSmsApiPlugin;
if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
require_once dirname(__DIR__) . '/vendor/autoload.php';
}
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
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;
class LinkMobilityShopwareSmsApiPlugin extends Plugin
{
public const CUSTOM_FIELD_SET_ID = '6548179eb61a4e6a895badc15b2d7196';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomFields($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeCustomFields($uninstallContext->getContext());
}
private function createCustomFields(Context $context): void
{
if ($this->customFieldSetExists($context)) {
return;
}
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create([
[
'id' => self::CUSTOM_FIELD_SET_ID,
'name' => 'marketingConsent',
'global' => true,
'config' => [
'label' => [
'en-GB' => 'Consent',
],
],
'customFields' => [
[
'name' => 'link_mobility_shopware_sms_api_plugin_consent',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'en-GB' => 'I consent for marketing',
],
'customFieldPosition' => 1,
],
],
[
'name' => 'link_mobility_shopware_sms_api_plugin_customer_id',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'customerSmsApiId',
],
'customFieldPosition' => 2,
],
],
],
'relations' => [
[
'entityName' => CustomerDefinition::ENTITY_NAME,
],
],
],
], $context);
}
private function removeCustomFields(Context $context): void
{
if (!$this->customFieldSetExists($context)) {
return;
}
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->delete([
['id' => self::CUSTOM_FIELD_SET_ID],
], $context);
}
private function customFieldSetExists(Context $context): bool
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
return null !== $customFieldSetRepository->search(new Criteria([self::CUSTOM_FIELD_SET_ID]), $context)->first();
}
}