<?php declare(strict_types=1);
namespace Tonur\GiftOption;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
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;
class TonurGiftWrap extends Plugin
{
public const BUNDLE_NAME = 'TonurGiftWrap';
public const CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT = "repertus_gift_wrap_product";
public const CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT = "repertus_gift_wrap_exclude_product";
/** @var EntityRepositoryInterface $customFieldSetRepository */
private $customFieldSetRepository;
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->addCustomFieldSet($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->addCustomFieldSet($updateContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeCustomFieldSet($uninstallContext->getContext());
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS `repertus_gift_wrap`');
}
private function addCustomFieldSet(Context $context)
{
$criteria = new Criteria();
$criteria
->addAssociation('customFields')
->addFilter(new EqualsFilter('customFields.name', self::CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT));
$searchResult = $this->getCustomFieldSetRepository()->search($criteria, $context);
if ($searchResult->count() == 0) {
$this->getCustomFieldSetRepository()->create([
[
'name' => self::CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT,
'config' => [
'label' => [
'de-DE' => 'Geschenkverpackung',
'en-GB' => 'Gift Wrap'
]
],
'relations' => [[
'entityName' => 'product'
]],
'customFields' => [
[
'name' => self::CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => CustomFieldTypes::SWITCH,
'label' => [
"de-DE" => "Produkt nicht als Geschenk verpacken",
"en-GB" => "Do not gift wrap the product"
],
"componentName" => "sw-field",
"customFieldType" => CustomFieldTypes::SWITCH,
"customFieldPosition" => 1
]
]
]
]
], $context);
}
}
private function removeCustomFieldSet(Context $context)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT));
$idSearchResult = $this->getCustomFieldSetRepository()->searchIds($criteria, $context);
$ids = \array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
$this->getCustomFieldSetRepository()->delete($ids, $context);
}
private function getCustomFieldSetRepository(): EntityRepositoryInterface
{
if ($this->customFieldSetRepository === null) {
$this->customFieldSetRepository = $this->container->get('custom_field_set.repository');
}
return $this->customFieldSetRepository;
}
}