<?php declare(strict_types=1);
namespace Tarador;
use Shopware\Core\Framework\Plugin;
use Shopware\Storefront\Framework\ThemeInterface;
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\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class Tarador extends Plugin implements ThemeInterface
{
public function install(InstallContext $installContext): void
{
$this->createCustomFields($installContext->getContext());
parent::install($installContext); // TODO: Change the autogenerated stub
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$this->removeCustomFields($uninstallContext->getContext());
parent::uninstall($uninstallContext);
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext); // TODO: Change the autogenerated stub
}
public function deactivate(DeactivateContext $deactivateContext): void
{
parent::deactivate($deactivateContext); // TODO: Change the autogenerated stub
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext); // TODO: Change the autogenerated stub
}
private function createCustomFields($context){
$customFields = [
[
'name' => 'custom_property_group',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Property group custom fields',
'de-DE' => 'Property group custom fields'
],
],
'customFields' => [
[
'name' => 'custom_property_group_special',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'en-GB' => 'Special property group',
'de-DE' => 'Special property group',
],
'customFieldType' => CustomFieldTypes::SWITCH,
]
],
[
'name' => 'custom_property_group_slider',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'en-GB' => 'Slider property group',
'de-DE' => 'Slider property group',
],
'customFieldType' => CustomFieldTypes::SWITCH,
]
],
[
'name' => 'custom_property_group_feed',
'type' => CustomFieldTypes::SWITCH,
'config' => [
'label' => [
'en-GB' => 'Feed recommendations group',
'de-DE' => 'Feed recommendations group',
],
'customFieldType' => CustomFieldTypes::SWITCH,
]
],
[
'name' => 'custom_property_group_image',
'type' => 'text',
'config' => [
'label' => [
'en-GB' => 'Property group image',
'de-DE' => 'Property group image',
],
'customFieldType' => CustomFieldTypes::MEDIA,
'componentName' => 'sw-media-field'
]
],
],
'relations' => [
['entityName' => 'property_group']
]
],
[
'name' => 'custom_product_field',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Product custom fields',
'de-DE' => 'Produktbezogene Zusatzfelder'
]
],
'customFields' => [
[
'name' => 'box_description',
'type' => CustomFieldTypes::TEXT,
'position' => 2,
'config' => [
'label' => [
'en-GB' => 'Product box description',
'de-DE' => 'Claim',
],
]
],
[
'name' => 'additional_description',
'type' => CustomFieldTypes::HTML,
'position' => 10,
'config' => [
'label' => [
'en-GB' => 'Description on the right',
'de-DE' => 'Beschreibung auf der rechten Seite',
],
'customFieldType' => CustomFieldTypes::HTML,
'componentName' => 'sw-text-editor'
]
],
[
'name' => 'nutritional_additives',
'type' => CustomFieldTypes::HTML,
'position' => 5,
'config' => [
'label' => [
'en-GB' => 'Nutritional additives',
'de-DE' => 'Ernährungsphysiologische Zusatzstoffe',
],
'customFieldType' => CustomFieldTypes::HTML,
'componentName' => 'sw-text-editor'
]
],
[
'name' => 'analytical_components',
'type' => CustomFieldTypes::HTML,
'position' => 7,
'config' => [
'label' => [
'en-GB' => 'Analytical components',
'de-DE' => 'Unsere guten Zutaten',
],
'customFieldType' => CustomFieldTypes::HTML,
'componentName' => 'sw-text-editor'
]
],
[
'name' => 'feeding_recommendations_top',
'type' => CustomFieldTypes::TEXT,
'position' => 8,
'config' => [
'label' => [
'en-GB' => 'Feeding recommendations top text',
'de-DE' => 'Fütterungsempfehlungen oben',
],
]
],
[
'name' => 'feeding_recommendations_left',
'type' => CustomFieldTypes::TEXT,
'position' => 9,
'config' => [
'label' => [
'en-GB' => 'Feeding recommendations left text',
'de-DE' => 'Fütterungsempfehlungen links',
],
]
],
[
'name' => 'feeding_recommendations_right',
'type' => CustomFieldTypes::TEXT,
'position' => 10,
'config' => [
'label' => [
'en-GB' => 'Feeding recommendations right text',
'de-DE' => 'Fütterungsempfehlungen rechts',
],
]
],
[
'name' => 'ingredients_notes',
'type' => CustomFieldTypes::HTML,
'position' => 6,
'config' => [
'label' => [
'en-GB' => 'Ingredients Notes',
'de-DE' => 'Hinweise',
],
'customFieldType' => CustomFieldTypes::HTML,
'componentName' => 'sw-text-editor'
]
],
[
'name' => 'age',
'type' => CustomFieldTypes::TEXT,
'position' => 12,
'config' => [
'label' => [
'en-GB' => 'Age',
'de-DE' => 'Alter',
],
]
],
[
'name' => 'size',
'type' => CustomFieldTypes::TEXT,
'position' => 11,
'config' => [
'label' => [
'en-GB' => 'Size',
'de-DE' => 'Größe',
],
]
],
[
'name' => 'activity',
'type' => CustomFieldTypes::TEXT,
'position' => 20,
'config' => [
'label' => [
'en-GB' => 'Activity',
'de-DE' => 'Aktivität',
],
]
]
],
'relations' => [
['entityName' => 'product']
]
]
];
$repo = $this->container->get('custom_field_set.repository');
//check if entries exist to prevent re-installation error
$propertyCustomFieldSetEntry = false;
$propertyCustomFieldSetEntry = $repo->search((new Criteria())
->addFilter(
new EqualsFilter('name', 'custom_property_group'),
), $context)->first();
if(!$propertyCustomFieldSetEntry) {
foreach ($customFields as $customFieldSet) {
$repo->upsert([$customFieldSet], $context);
}
}
$productCustomFieldSetEntry = false;
$productCustomFieldSetEntry = $repo->search((new Criteria())
->addFilter(
new EqualsFilter('name', 'custom_product_field'),
), $context)->first();
if(!$productCustomFieldSetEntry) {
foreach ($customFields as $customFieldSet) {
$repo->upsert([$customFieldSet], $context);
}
}
}
private function removeCustomFields($context){
$cfsRepo = $this->container->get('custom_field_set.repository');
$cfRepo = $this->container->get('custom_field.repository');
$sRepo = $this->container->get('snippet.repository');
//delete custom_field_set entry
$propertyCfsId = $cfsRepo->search((new Criteria())
->addFilter(
new EqualsFilter('name', 'custom_property_group'),
), $context
)->first();
if ($propertyCfsId) {
$cfsRepo->delete([
['id' => $propertyCfsId->getId()]
], $context);
//delete custom_field entries
$propertyCfsId = $cfRepo->search((new Criteria())
->addFilter(
new EqualsFilter('customFieldSetId', $propertyCfsId->getId())
), $context)->getIds();
$ids = [];
if ($propertyCfsId) {
foreach ($propertyCfsId as $id) {
$ids[] = ['id' => $id];
}
$cfRepo->delete($ids, $context);
}
}
$productCfsId = $cfsRepo->search((new Criteria())
->addFilter(
new EqualsFilter('name', 'custom_product_field'),
), $context
)->first();
if ($productCfsId) {
$cfsRepo->delete([
['id' => $productCfsId->getId()]
], $context);
//delete custom_field entries
$productCfsId = $cfRepo->search((new Criteria())
->addFilter(
new EqualsFilter('customFieldSetId', $productCfsId->getId())
), $context)->getIds();
$ids = [];
if ($productCfsId) {
foreach ($productCfsId as $id) {
$ids[] = ['id' => $id];
}
$cfRepo->delete($ids, $context);
}
}
// delete snippet entries
$sIds = $sRepo->search((new Criteria())
->addFilter(
new MultiFilter(
MultiFilter::CONNECTION_OR,
[
new ContainsFilter('translationKey', 'custom_property_group_special'),
new ContainsFilter('translationKey', 'custom_property_group_slider'),
new ContainsFilter('translationKey', 'custom_property_group_image'),
new ContainsFilter('translationKey', 'custom_property_group_feed'),
new ContainsFilter('translationKey', 'box_description'),
new ContainsFilter('translationKey', 'additional_description'),
new ContainsFilter('translationKey', 'feeding_recommendations_top'),
new ContainsFilter('translationKey', 'feeding_recommendations_left'),
new ContainsFilter('translationKey', 'feeding_recommendations_right'),
new ContainsFilter('translationKey', 'nutritional_additives'),
new ContainsFilter('translationKey', 'analytical_components'),
new ContainsFilter('translationKey', 'ingredients_notes'),
new ContainsFilter('translationKey', 'age'),
new ContainsFilter('translationKey', 'size'),
new ContainsFilter('translationKey', 'activity'),
]
)
), $context)->getIds();
$snippetIds = [];
if ($sIds) {
foreach ($sIds as $id) {
$snippetIds[] = ['id' => $id];
}
$sRepo->delete($snippetIds, $context);
}
}
}