<?php declare(strict_types=1);
namespace zenit\PlatformDataBadges;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
class zenitPlatformDataBadges extends Plugin
{
const CUSTOM_BADGES_COUNT = 3;
public function install(InstallContext $context): void
{
$this->addDefaultConfiguration();
$this->getCustomFields($context->getContext());
}
public function activate(ActivateContext $context): void
{
$this->addDefaultConfiguration();
}
public function update(UpdateContext $context): void
{
$this->addDefaultConfiguration();
}
public function deactivate(DeactivateContext $context): void
{
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeConfiguration($context->getContext());
$this->deleteCustomFields($context->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 deleteCustomFields($context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new ContainsFilter('name', 'zenit_data_badge'));
$criteriaResult = $customFieldSetRepository->search($criteria, $context);
$ids = $criteriaResult->getIds();
if(!empty($ids)) {
$data = [];
foreach ($ids as $id) {
$data[] = [
'id' => $id
];
}
try {
$customFieldSetRepository->delete($data, $context);
} catch (\Exception $e) {}
}
}
private function getCustomFields($context): void
{
$relation = [
[
'entityName' => $this->container->get(ProductDefinition::class)->getEntityName()
]
];
$customFields = [];
for($i = 1; $i <= self::CUSTOM_BADGES_COUNT; $i++) {
$customFields = array_merge($customFields,
[
[
'name' => 'zenit_data_badge_' . $i,
'type' => 'CustomFieldTypes::TEXT',
'config' => [
'customFieldPosition' => 1 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Text',
'de-DE' => $i . ') Text',
],
],
],
[
'name' => 'zenit_data_badge_tooltip_' . $i,
'type' => 'string',
'config' => [
'customFieldPosition' => 2 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Tooltip',
'de-DE' => $i . ') Tooltip',
],
],
],
[
'name' => 'zenit_data_badge_color_' . $i,
'type' => 'string',
'config' => [
'customFieldPosition' => 3 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Background-Color',
'de-DE' => $i . ') Hintergrundfarbe',
],
],
],
[
'name' => 'zenit_data_badge_textcolor_' . $i,
'type' => 'string',
'config' => [
'customFieldPosition' => 4 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Text-Color',
'de-DE' => $i . ') Textfarbe',
],
],
]
]
);
}
$customFieldSet = [
'name' => 'zenit_data_badges',
'config' => [
'label' => [
'en-GB' => 'Custom Labels',
'de-DE' => 'Individuelle Labels'
]
],
'customFields' => $customFields,
'relations' => $relation
];
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
try {
$customFieldSetRepository->create([$customFieldSet], $context);
} catch (\Exception $e) {}
}
private function addDefaultConfiguration(): void
{
$this->setValue('active', true);
$this->setValue('hideTooltips', false);
$this->setValue('stockListing', false);
$this->setValue('stockDetail', 'false');
$this->setValue('stockMinimum', 50);
$this->setValue('stockMaximum', 999999);
$this->setValue('stockValueType', 'minimum');
$this->setValue('stockIcon', 'products');
$this->setValue('stockText', 'Stück');
$this->setValue('stockTooltip', 'Lagerbestand');
$this->setValue('stockVariant', 'variant');
$this->setValue('stockBadgeColor', '#5F7285');
$this->setValue('variantListing', false);
$this->setValue('variantDetail', 'false');
$this->setValue('variantIcon', 'variants');
$this->setValue('variantText', 'Variante');
$this->setValue('variantTooltip', 'Variantenartikel');
$this->setValue('variantBadgeColor', '#bcc1c7');
$this->setValue('discountListing', true);
$this->setValue('discountDetail', 'false');
$this->setValue('discountMinimum', 10);
$this->setValue('discountTooltip', 'Ihr Vorteil');
$this->setValue('closeoutListing', false);
$this->setValue('closeoutSoldOutOnly', false);
$this->setValue('closeoutDetail', 'false');
$this->setValue('closeoutIcon', 'blocked');
$this->setValue('closeoutText', 'Abverkauf');
$this->setValue('closeoutTooltip', 'Ausverkauf');
$this->setValue('closeoutBadgeColor', '#E74C3C');
$this->setValue('shippingfreeListing', false);
$this->setValue('shippingfreeDetail', 'false');
$this->setValue('shippingfreeIcon', 'package-closed');
$this->setValue('shippingfreeText', 'Versandkostenfrei');
$this->setValue('shippingfreeTooltip', 'Versand');
$this->setValue('shippingfreeBadgeColor', '#2ECC71');
$this->setValue('topsellerListing', true);
$this->setValue('topsellerDetail', 'false');
$this->setValue('topsellerIcon', 'star');
$this->setValue('topsellerText', 'Tipp!');
$this->setValue('topsellerTooltip', 'Topseller');
$this->setValue('newArticleListing', true);
$this->setValue('newArticleDetail', 'false');
$this->setValue('newArticleIcon', 'plus');
$this->setValue('newArticleText', 'Neu');
$this->setValue('newArticleTooltip', 'Neu im Sortiment');
$this->setValue('manufacturerListing', false);
$this->setValue('manufacturerDetail', 'false');
$this->setValue('manufacturerTooltip', 'Hersteller');
$this->setValue('manufacturerBadgeColor', '#5F7285');
$this->setValue('custom1Listing', false);
$this->setValue('custom1Detail', 'false');
$this->setValue('custom1Tooltip');
$this->setValue('custom1BadgeColor', '#F1C40F');
$this->setValue('custom2Listing', false);
$this->setValue('custom2Detail', 'false');
$this->setValue('custom2Tooltip');
$this->setValue('custom2BadgeColor', '#F1C40F');
$this->setValue('custom3Listing', false);
$this->setValue('custom3Detail', 'false');
$this->setValue('custom3Tooltip');
$this->setValue('custom3BadgeColor', '#F1C40F');
$this->setValue('listingsFontSize', 14);
$this->setValue('listingsPadding', 5);
$this->setValue('listingsRadiusLeft', 0);
$this->setValue('listingsRadiusRight', 3);
$this->setValue('detailsFontSize', 14);
$this->setValue('detailsPadding', 5);
$this->setValue('detailsRadiusLeft', 0);
$this->setValue('detailsRadiusRight', 3);
$this->setValue('customCSS');
}
/**
* @param string $configName
* @param null $default
*/
public function setValue(string $configName, $default = null) : void
{
$systemConfigService = $this->container->get(SystemConfigService::class);
$domain = $this->getName() . '.config.';
if( $systemConfigService->get($domain . $configName) === null )
{
$systemConfigService->set($domain . $configName, $default);
}
}
}