<?php
declare(strict_types=1);
namespace Ks\KsAdditions;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
/**
* Class KsAdditions
* @package Ks\KsAdditions
*/
class KsAdditions extends Plugin
{
const PRODUCT_SET = 'ks_product_set';
public const ADDVISOR_PASSUNG_MIN = 'addvisor_passung_min';
public const ADDVISOR_PASSUNG_MAX = 'addvisor_passung_max';
public const MIGRATION_ADDVISOR_PASSUNG = 'migration_geuther_product_addvisor_passung';
const CATEGORY_SET = 'ks_category_set';
public const HIDE_IN_NAVIGATION = 'hide_in_navigation';
public const SWITCH_OFF_ALL_CUSTOM_FILTERS = 'switch_off_all_custom_filters';
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
// $this->clean();
$this->installCustomFields($installContext->getContext());
}
/**
* @param UpdateContext $updateContext
*/
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$currentVersion = $updateContext->getCurrentPluginVersion();
if (version_compare($currentVersion, '1.0.1', '<')) {
$this->installProductCustomFields($updateContext->getContext());
}
if (version_compare($currentVersion, '1.0.2', '<')) {
$this->installCategoryCustomFields($updateContext->getContext());
}
}
protected function clean(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeQuery(
"DELETE FROM custom_field_set WHERE name = '" . static::PRODUCT_SET . "'"
);
$connection->executeQuery(
"DELETE FROM custom_field_set WHERE name = '" . static::CATEGORY_SET . "'"
);
}
/**
* @param Context $context
*/
private function installCustomFields(Context $context)
{
$this->installProductCustomFields($context);
$this->installCategoryCustomFields($context);
}
/**
* @param Context $context
*/
private function installProductCustomFields(Context $context)
{
$customFieldsArray[] = [
'name' => self::ADDVISOR_PASSUNG_MIN,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Addvisor passung min, cm',
'de-DE' => 'Addvisor passung min, cm'
],
]
];
$customFieldsArray[] = [
'name' => self::ADDVISOR_PASSUNG_MAX,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'Addvisor passung max, cm',
'de-DE' => 'Addvisor passung max, cm'
],
]
];
$customFieldsRepository = $this->container->get('custom_field_set.repository');
$customFieldsRepository->upsert([
[
'name' => static::PRODUCT_SET,
'global' => false,
'config' => [
'label' => [
'de-DE' => 'Product custom field',
'en-GB' => 'Product custom field'
],
],
'relations' => [
[
'entityName' => 'product'
]
],
'customFields' => $customFieldsArray
]
], $context);
}
/**
* @param Context $context
*/
private function installCategoryCustomFields(Context $context)
{
$customFieldsRepository = $this->container->get('custom_field_set.repository');
$customFieldsRepository->upsert([
[
'name' => static::CATEGORY_SET,
'global' => false,
'config' => [
'label' => [
'de-DE' => 'Category custom fields',
'en-GB' => 'Category custom fields'
],
],
'relations' => [
[
'entityName' => 'category'
]
],
'customFields' => [
[
'name' => static::CATEGORY_SET . '_' . self::HIDE_IN_NAVIGATION,
'type' => CustomFieldTypes::SWITCH,
'config' => [
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Hide in navigation (in current lang)',
'de-DE' => 'In der Navigation ausblenden (in der aktuellen Sprache)'
],
]
],
[
'name' => static::CATEGORY_SET . '_' . self::SWITCH_OFF_ALL_CUSTOM_FILTERS,
'type' => CustomFieldTypes::SWITCH,
'config' => [
'customFieldPosition' => 2,
'label' => [
'en-GB' => 'Switch off all custom filters',
'de-DE' => 'Alle benutzerdefinierten Filter ausschalten'
],
]
]
]
]
], $context);
}
}