<?php declare(strict_types=1);
namespace Uvala\uvalaBadgeManagerVsix;
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\ContainsFilter;
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\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class uvalaBadgeManagerVsix extends Plugin {
public const PROPERTY_GROUP_EXCLUDED_CF_NAME = 'uvala_bdmg_property_group_excluded_';
public function install(InstallContext $installContext): void {
parent::install($installContext);
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
try {
$customFieldSetRepository->upsert($this->uvoutputfields(), $installContext->getContext());
} catch (\Exception $e) {}
}
public function activate(ActivateContext $activateContext): void {
parent::activate($activateContext);
}
public function deactivate(DeactivateContext $deactivateContext): void {
parent::deactivate($deactivateContext);
}
public function uninstall(UninstallContext $uninstallContext): void {
parent::uninstall($uninstallContext);
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$outputResIds = [];
$outputResIds[] = ['id' => $customFieldSetRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', 'uvala_bdmg_product')),
\Shopware\Core\Framework\Context::createDefaultContext()
, $uninstallContext->getContext())->firstId()];
$outputResIds[] = ['id' => $customFieldSetRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('name', 'uvala_bmgh_property_excluded_set')),
\Shopware\Core\Framework\Context::createDefaultContext()
, $uninstallContext->getContext())->firstId()];
$customFieldSetRepository->delete($outputResIds, $uninstallContext->getContext());
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeConfiguration($uninstallContext->getContext());
}
private function removeConfiguration(Context $context): void {
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
$criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey', $this->getName() . '.config.'));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
if ($ids === []) {
return;
}
$systemConfigRepository->delete($ids, $context);
}
private function uvoutputfields() {
$customFieldSets = [];
$propertyGroupCustomFields = [];
for ($i = 1; $i <= 8; $i++) {
$propertyGroupCustomFields[] = [
'name' => self::PROPERTY_GROUP_EXCLUDED_CF_NAME.$i,
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Property-Gruppe für Badge ausgeschlossen '. $i,
'en-GB' => 'Property-group excluded for badge '.$i,
],
'helpText' => [
'de-DE' => 'Deaktivieren Sie die Anzeige der Eigenschaftsgruppe.',
'en-GB' => 'Disable the display of the property group.',
],
'customFieldPosition' => $i,
]
];
}
$customFieldSets[] = [
'name' => 'uvala_bdmg_product',
'config' => [
'label' => [
'de-DE' => 'Badge Manager',
'en-GB' => 'Badge Manager'
]
],
'customFields' => [
[
'name' => 'uvala_bdmg_plugin_disable',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Badge Manager für dieses Produkt deaktivieren',
'en-GB' => 'Disable badge manager for this product',
],
'helpText' => [
'de-DE' => 'Deaktiviert den Badge Manager nur für dieses Produkt (Gilt bei Varianten NUR für das Produkt, welches im Listing erscheint!).',
'en-GB' => 'Deactivates the Badge Manager only for this product (only applies to variants for the product that appears in the listing!).',
],
'customFieldPosition' => 1
]
]
],
'relations' => [
['entityName' => 'product']
],
];
$customFieldSets[] = [
'name' => 'uvala_bmgh_property_excluded_set',
'config' => [
'label' => [
'de-DE' => 'Abzeichen für Eigentumsgruppe ausgeschlossen',
'en-GB' => 'Badge for property group excluded'
]
],
'customFields' => $propertyGroupCustomFields,
'relations' => [
['entityName' => 'property_group']
],
'global' => true
];
return $customFieldSets;
}
}