<?php
declare(strict_types=1);
namespace FourtwosixThemeCustomization;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
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 FourtwosixThemeCustomization extends Plugin
{
public const ATTRIBUTE_SET_NAME = "custom_navigation_identity";
public const ATTRIBUTE_SET_LABEL_EN = "Category Identification (426)";
public const ATTRIBUTE_SET_LABEL_DE = "Kategorien Identifikation (426)";
public const CATEGORY_TYPE_ATTRIBUTE_NAME = "custom_navigation_identity_category_type";
public const CATEGORY_TYPE_ATTRIBUTE_LABEL_EN = "Category types assignment";
public const CATEGORY_TYPE_ATTRIBUTE_LABEL_DE = "Kategorietypenzuordung";
public const TWO_COLUMNS_ATTRIBUTE_NAME = "custom_navigation_identity_columns2";
public const TWO_COLUMNS_ATTRIBUTE_LABEL_EN = "Category with 2 columns";
public const TWO_COLUMNS_ATTRIBUTE_LABEL_DE = "Kategorie mit 2 Spalten";
public const ONLY_MOBILE_ATTRIBUTE_NAME = "custom_navigation_identity_onlyMobile";
public const ONLY_MOBILE_ATTRIBUTE_LABEL_EN = "show on Mobile";
public const ONLY_MOBILE_ATTRIBUTE_LABEL_DE = "Angezeigt nur Mobile";
/* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1204101678103036/f */
public const AFFECTED_CATEGORY_ID = 'FourtwosixThemeCustomization.config.affectedCategoryId';
public const HIDE_SUB_CATEGORIES = 'FourtwosixThemeCustomization.config.hideSubCategories';
public const HIDDEN_CATEGORY_ID = 'FourtwosixThemeCustomization.config.hiddenCategoryId';
/* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1204080601957727/f */
const GUEST_RULE_ID = '9126c4c1239d4058afe53a8e345175d3'; // Shopkunden RULE ID
const CUSTOMER_RULE_ID = '6d01f06cf4c147b690f7658336ef9563'; // Shopkunden 2017 RULE ID
/* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1203817432613932/f */
public const CUSTOMER_GROUP_TRADER_ID = '698a7ea2cc7e4d05a961816644a0e27b';
public const CUSTOMER_GROUP_FOREIGN_TRADER_ID = '94023f1ea4524e43b37aaef6c74c6853';
public const MANUFACTURER_PLATINLUX_ID = 'c7d3d3af44fa47b6974f61879953392a';
public const MANUFACTURER_LUCIDE_ID = 'f858c0cc167f422daab4dfdf77ab92d3';
public const REGISTERED_CUSTOMER_GROUP_ID = '46840a25121f47c49af3e8ea0086cc3e';
public function install(InstallContext $installContext): void
{
$this->createCustomFields($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$this->deleteCustomFields($uninstallContext->getContext());
}
private function createCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::ATTRIBUTE_SET_NAME));
if ($customFieldSetRepository->search($criteria, $context)->first()) {
return;
}
$customFieldData = $this->getCustomFieldData();
$customFieldSetRepository->upsert([$customFieldData], $context);
}
private function deleteCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (!$customFields = $this->getCustomFields($customFieldSetRepository, $context)) {
return;
}
$customFieldSetRepository->delete(array_values($customFields->getData()), $context);
}
private function getCustomFields(EntityRepository &$customFieldSetRepository, Context &$context): ?IdSearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', [self::ATTRIBUTE_SET_NAME]));
$customFields = $customFieldSetRepository->searchIds($criteria, $context);
var_dump($customFields);
return $customFields->getTotal() > 0 ? $customFields : null;
}
private function getCustomFieldData(): array
{
return [
'name' => self::ATTRIBUTE_SET_NAME,
'config' => [
'label' => [
'en-GB' => self::ATTRIBUTE_SET_LABEL_EN,
'de-DE' => self::ATTRIBUTE_SET_LABEL_DE,
]
],
'customFields' => [
$this->getCustomFieldSelect(
self::CATEGORY_TYPE_ATTRIBUTE_NAME,
CustomFieldTypes::SELECT,
self::CATEGORY_TYPE_ATTRIBUTE_LABEL_EN,
self::CATEGORY_TYPE_ATTRIBUTE_LABEL_DE,
1
),
$this->getCustomField(
self::TWO_COLUMNS_ATTRIBUTE_NAME,
CustomFieldTypes::SWITCH,
self::TWO_COLUMNS_ATTRIBUTE_LABEL_EN,
self::TWO_COLUMNS_ATTRIBUTE_LABEL_DE,
2
),
$this->getCustomField(
self::ONLY_MOBILE_ATTRIBUTE_NAME,
CustomFieldTypes::SWITCH,
self::ONLY_MOBILE_ATTRIBUTE_LABEL_EN,
self::ONLY_MOBILE_ATTRIBUTE_LABEL_DE,
3
),
],
'relations' => [
[
'entityName' => 'category',
]
],
];
}
private function getCustomField(string $name, string $type, string $labelEN, string $labelDE, int $position): array
{
return [
'name' => $name,
'type' => $type,
'config' => [
'label' => [
'en-GB' => $labelEN,
'de-DE' => $labelDE
],
"componentName" => "sw-field",
"customFieldType" => "switch",
'customFieldPosition' => $position,
],
];
}
private function getCustomFieldSelect(string $name, string $type, string $labelEN, string $labelDE, int $position): array
{
return [
'name' => $name,
'type' => $type,
'config' => [
'label' => [
'en-GB' => $labelEN,
'de-DE' => $labelDE
],
'options' => [
[
'label' => [
'en-GB' => 'First category',
'de-DE' => 'Erste Kategorie'
],
'value' => 'first_category',
],
[
'label' => [
'en-GB' => 'Rooms category',
'de-DE' => 'Raeume Kategorie'
],
'value' => 'rooms_category',
],
[
'label' => [
'en-GB' => 'styles category',
'de-DE' => 'Stile Kategorie'
],
'value' => 'styles_category',
],
[
'label' => [
'en-GB' => 'Manufacturer category',
'de-DE' => 'Hersteller Kategorie'
],
'value' => 'manufacturer_category'
],
],
"componentName" => "sw-single-select",
"customFieldType" => "select",
'customFieldPosition' => $position,
],
];
}
}