<?php declare(strict_types=1);
namespace zenit\PlatformDataBadges;
use Exception;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class zenitPlatformDataBadges extends Plugin
{
const CUSTOM_BADGES_COUNT = 3;
public function install(InstallContext $installContext): void
{
$this->addDefaultConfiguration();
$this->getCustomFields($installContext->getContext());
parent::install($installContext);
}
public function update(UpdateContext $updateContext): void
{
$this->addDefaultConfiguration();
if (version_compare($updateContext->getCurrentPluginVersion(), '2.3.0', '<')) {
$this->updateCustomFields($updateContext->getContext());
}
parent::update($updateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->deleteCustomFields($uninstallContext->getContext());
}
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 updateCustomFields($context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new ContainsFilter('name', 'zenit_data_badge'));
$criteria->setLimit(1);
$criteriaResult = $customFieldSetRepository->search($criteria, $context);
$id = null;
if ($criteriaResult->count() === 1) {
$id = $criteriaResult->first()->getId();
}
$customFields = [];
for($i = 1; $i <= self::CUSTOM_BADGES_COUNT; $i++) {
$customFields = array_merge($customFields,
[
[
'id' => '6b982d312a3f47daa29e841d1731163' . $i,
'name' => 'zenit_data_badge_disabled_' . $i,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 0 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Disabled',
'de-DE' => $i . ') Deaktiviert',
],
],
],
]
);
}
$customFieldSet = [
'id' => $id,
'name' => 'zenit_data_badges',
'config' => [
'label' => [
'en-GB' => 'Custom Labels',
'de-DE' => 'Individuelle Labels'
]
],
'customFields' => $customFields
];
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
try {
$customFieldSetRepository->upsert([$customFieldSet], $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,
[
[
'id' => '6b982d312a3f47daa29e841d1731163' . $i,
'name' => 'zenit_data_badge_disabled_' . $i,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 0 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Disabled',
'de-DE' => $i . ') Deaktiviert',
],
],
],
[
'name' => 'zenit_data_badge_' . $i,
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 1 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Text',
'de-DE' => $i . ') Text',
],
],
],
[
'name' => 'zenit_data_badge_tooltip_' . $i,
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 2 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Tooltip',
'de-DE' => $i . ') Tooltip',
],
],
],
[
'name' => 'zenit_data_badge_color_' . $i,
'type' => CustomFieldTypes::COLORPICKER,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'colorpicker',
'customFieldPosition' => 3 + 10 * 2 * $i,
'label' => [
'en-GB' => $i . ') Background-Color',
'de-DE' => $i . ') Hintergrundfarbe',
],
],
],
[
'name' => 'zenit_data_badge_textcolor_' . $i,
'type' => CustomFieldTypes::COLORPICKER,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'colorpicker',
'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->upsert([$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('stockVariant', 'variant');
$this->setValue('stockBadgeColor', '#5F7285');
$this->setValue('salesListing', false);
$this->setValue('salesDetail', false);
$this->setValue('salesMinimum', 50);
$this->setValue('salesValueType', 'minimum');
$this->setValue('salesIcon', 'pulse');
$this->setValue('salesBadgeColor', '#e52427');
$this->setValue('variantListing', false);
$this->setValue('variantDetail', 'false');
$this->setValue('variantIcon', 'variants');
$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('closeoutBadgeColor', '#E74C3C');
$this->setValue('shippingfreeListing', false);
$this->setValue('shippingfreeDetail', 'false');
$this->setValue('shippingfreeIcon', 'package-closed');
$this->setValue('shippingfreeBadgeColor', '#2ECC71');
$this->setValue('topsellerListing', true);
$this->setValue('topsellerDetail', 'false');
$this->setValue('topsellerIcon', 'star');
$this->setValue('newArticleListing', true);
$this->setValue('newArticleDetail', 'false');
$this->setValue('newArticleIcon', 'plus');
$this->setValue('releaseListing', false);
$this->setValue('releaseDetail', 'false');
$this->setValue('releaseIcon', 'clock');
$this->setValue('releaseBadgeColor', '#ffbd5d');
$this->setValue('preorderListing', false);
$this->setValue('preorderDetail', 'false');
$this->setValue('preorderIcon', 'calendar');
$this->setValue('preorderBadgeColor', '#932AEF');
$this->setValue('manufacturerListing', false);
$this->setValue('manufacturerDetail', 'false');
$this->setValue('manufacturerBadgeColor', '#5F7285');
$this->setValue('custom1Listing', false);
$this->setValue('custom1Detail', 'false');
$this->setValue('custom1BadgeColor', '#F1C40F');
$this->setValue('custom2Listing', false);
$this->setValue('custom2Detail', 'false');
$this->setValue('custom2BadgeColor', '#F1C40F');
$this->setValue('custom3Listing', false);
$this->setValue('custom3Detail', 'false');
$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);
}
}
}