<?php declare(strict_types=1);
namespace Stutt\NewsletterPromotion;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
class StuttNewsletterPromotion extends Plugin
{
public function update(\Shopware\Core\Framework\Plugin\Context\UpdateContext $updateContext): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', 'stutt_newsletter')
);
$criteria->addAssociation('customFields');
$sets = $customFieldSetRepository->search($criteria, $updateContext->getContext());
if ($sets->count() === 0) {
$this->addCustomFields($updateContext->getContext());
}
else {
/** @var \Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity $set */
$set = $sets->first();
$customFields = $set->getCustomFields();
$found = FALSE;
foreach ($customFields->getIterator() as $customField) {
if ($customField->getName() === 'stutt_voucher_perm') {
$found = TRUE;
}
}
if (!$found) {
/** @var EntityRepositoryInterface $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', 'stutt_voucher_perm')
);
$idDel = $customFieldRepository->searchIds($criteria, $updateContext->getContext())->firstId();
$customFieldRepository->delete([
[
'id' => $idDel
]
], $updateContext->getContext());
$id = Uuid::randomHex();
$result = $customFieldRepository->create([[
'id' => $id,
'name' => 'stutt_voucher_perm',
'type' => CustomFieldTypes::TEXT
]], $updateContext->getContext());
$customFieldSetRepository->update([
[
'id' => $set->getId(),
'customFields' => [
[
'id' => $id
]
]
]
], $updateContext->getContext());
}
}
parent::update($updateContext);
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->addCustomFields($installContext->getContext());
}
private function addCustomFields(\Shopware\Core\Framework\Context $context)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', 'stutt_newsletter')
);
$sets = $customFieldSetRepository->search($criteria, $context);
if (!$sets->count()) {
$customFieldSetRepository->create([[
'name' => 'stutt_newsletter',
'customFields' => [
[
'name' => 'stutt_voucher',
'type' => CustomFieldTypes::TEXT
],
[
'name' => 'stutt_voucher_perm',
'type' => CustomFieldTypes::TEXT
]
],
'relations' => [
[
'entityName' => 'newsletter_recipient'
]
],
]], $context);
}
}
}