<?php declare(strict_types=1);
namespace Ew\GrecoBundle;
use Doctrine\DBAL\Exception;
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\System\CustomField\CustomFieldTypes;
class EwGrecoBundle extends Plugin
{
protected const BUNDLE_ARTICLE = 'greco_bundle_article';
protected const DNA_ARTICLE = 'greco_dna_article';
protected const PSEUDO_ARTICLE = 'greco_pseudo_article';
protected const PSEUDO_ARTICLES = 'greco_pseudo_articles';
/**
* @param InstallContext $context
* @throws Exception
*/
public function install(InstallContext $context) : void
{
parent::install($context);
$customerFieldsRepository = $this->container->get('custom_field_set.repository');
$this->addCustomFieldsToCustomer($context, $customerFieldsRepository);
}
/**
* @return array[]
*/
private function getCustomFields(): array
{
return [
[
'name' => self::BUNDLE_ARTICLE,
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'de-DE' => 'Bundle Rabatt',
'en-GB' => 'Bundle Article'
],
'customFieldPosition' => 1
]
],
[
'name' => self::DNA_ARTICLE,
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'de-DE' => 'DNA Produkt',
'en-GB' => 'DNA Article'
],
'helpText' => [
'de-DE' => 'Dann nur einmal pro Benutzer(Konto) bestellt werden',
'en-GB' => 'Can only be ordered once per user(account)',
],
'customFieldPosition' => 2
]
],
[
'name' => self::PSEUDO_ARTICLE,
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'de-DE' => 'Pseudo-Artikel',
'en-GB' => 'Pseudo Article'
],
'helpText' => [
'de-DE' => 'When this product is added to cart it will add products that are selected in dropdown instead of this one.',
'en-GB' => 'Wenn dieses Produkt in den Warenkorb gelegt wird, werden anstelle dieses Produkts Produkte hinzugefügt, die in der Dropdown-Liste ausgewählt wurden.',
],
'customFieldPosition' => 3
]
],
[
'name' => self::PSEUDO_ARTICLES,
'type' => CustomFieldTypes::SELECT,
'config' => [
'componentName' => 'sw-entity-multi-id-select',
'customFieldType' => 'entity',
'entity' => 'product',
'label' => [
'de-DE' => 'Pseudo-Produkte',
'en-GB' => 'Pseudo Products'
],
'helpText' => [
'de-DE' => 'Choose products that will replace pseudo article.',
'en-GB' => 'Wählen Sie Produkte, die Pseudoartikel ersetzen.',
],
'customFieldPosition' => 4
]
]
];
}
/**
* @throws Exception
*/
private function addCustomFieldsToCustomer($context, ?object $customFieldSetRepository): void
{
$this->uninstallCustomFieldset($context->getContext());
$customFieldSetRepository->create([[
'name' => 'ew-greco-bundle',
'position' => 0,
'global' => true,
'active' => true,
'config' => [
'label' => [
'en-GB' => 'EW Bundle Fields',
'de-DE' => 'Bundle-Rabatt-Produkt'
],
],
'customFields' => $this->getCustomFields(),
'relations' => [
[
'entityName' => 'product'
]
]
]], $context->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$this->uninstallCustomFieldset($uninstallContext->getContext());
}
private function uninstallCustomFieldset($context){
$cfsRepo = $this->container->get('custom_field_set.repository');
$cfRepo = $this->container->get('custom_field.repository');
//delete custom_field_set entry
$cfsId = $cfsRepo->search((new Criteria())
->addFilter(new EqualsFilter('name', 'ew-greco-bundle')), $context
)->first();
if ($cfsId) {
$cfsRepo->delete([
['id' => $cfsId->getId()]
], $context);
//delete custom_field entries
$cfIds = $cfRepo->search((new Criteria())
->addFilter(
new EqualsFilter('customFieldSetId', $cfsId->getId())
), $context)->getIds();
$ids = [];
foreach ($cfIds as $id) {
$ids[] = ['id' => $id];
}
$cfRepo->delete($ids, $context);
}
}
}