<?php declare(strict_types=1);
namespace digi1\digi1Custombutton;
use Shopware\Core\Framework\Plugin;
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;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class digi1Custombutton extends Plugin {
public function install(InstallContext $installContext): void {
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteriaButton = new Criteria();
$criteriaButton->addFilter(new EqualsFilter('name', 'digi1_custombuttons'));
$resultButton = $customFieldSetRepository->searchIds($criteriaButton, $installContext->getContext());
$criteriaText = new Criteria();
$criteriaText->addFilter(new EqualsFilter('name', 'digi1_customtexts'));
$resultText = $customFieldSetRepository->searchIds($criteriaText, $installContext->getContext());
if ($resultButton->getTotal() == 0) {
$customFieldSetRepository->create([[
'name' => 'digi1_custombuttons',
'config' => [
'label' => [
'de-DE' => 'Individuelle Schaltfläche',
'en-GB' => 'Custom button'
],
'translated' => true
],
'customFields' => [
[
'name' => 'digi1_custombuttons_custombutton',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Individuelle Schaltfläche für dieses Produkt (falls der Wert ausgewählte Produkte ausgewählt wurde)',
'en-GB' => 'Custom button for this product (if the value selected products is selected)'
],
'componentName' => 'sw-field',
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'type' => 'switch'
]
],
[
'name' => 'digi1_custombuttons_buttontext',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Text der individuellen Schaltfläche',
'en-GB' => 'Text of the custom button'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'type' => 'text'
]
],
[
'name' => 'digi1_custombuttons_buttonaction',
'type' => 'select',
'config' => [
'label' => [
'de-DE' => 'Aktion der Individuellen Schaltfläche',
'en-GB' => 'action of the custom button'
],
'options' => [
[
'label' => [
'de-DE' => 'Verlinken auf eine URL',
'en-GB' => 'Link to a URL'
],
'value' => 'url'
],
[
'label' => [
'de-DE' => 'Verlinken auf eine E-Mail-Adresse',
'en-GB' => 'Link to a email address'
],
'value' => 'email'
],
[
'label' => [
'de-DE' => 'Verlinken auf ein Formular',
'en-GB' => 'Link to a form'
],
'value' => 'form'
]
],
'componentName' => 'sw-single-select',
'customFieldType' => 'select',
'customFieldPosition' => 3,
'type' => 'select'
]
],
[
'name' => 'digi1_custombuttons_actionvalue',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'URL, E-Mail-Adresse oder die CMS - ID des Formulars',
'en-GB' => 'URL, email address or the cms id of the form'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 4,
'type' => 'text'
]
],
[
'name' => 'digi1_custombuttons_actionvaluesubject',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Betreff der E-Mail',
'en-GB' => 'Subject of the email'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 5,
'type' => 'text'
]
],
[
'name' => 'digi1_custombuttons_actionvaluebody',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Inhalt der E-Mail',
'en-GB' => 'Body of the email'
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 6
]
],
[
'name' => 'digi1_custombuttons_actiontarget',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'URL in einem neuen Fenster öffnen',
'en-GB' => 'Open the url in a new window'
],
'componentName' => 'sw-field',
'customFieldType' => 'switch',
'customFieldPosition' => 7,
'type' => 'switch'
]
]
],
'relations' => [
['entityName' => 'product']
]
]], $installContext->getContext());
}
if ($resultButton->getTotal() == 0) {
$customFieldSetRepository->create([[
'name' => 'digi1_customtexts',
'config' => [
'label' => [
'de-DE' => 'Individueller Text',
'en-GB' => 'Custom text'
],
'translated' => true
],
'customFields' => [
[
'name' => 'digi1_customtexts_showcustomtext',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Individueller Text für dieses Produkt anzeigen (falls der Wert ausgewählte Produkte ausgewählt wurde)',
'en-GB' => 'Show custom text for this product (if the value selected products is selected)'
],
'componentName' => 'sw-field',
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'type' => 'switch'
]
],
[
'name' => 'digi1_customtexts_customtext',
'type' => CustomFieldTypes::HTML,
'config' => [
'label' => [
'de-DE' => 'Individueller Text',
'en-GB' => 'Custom text'
],
'componentName' => 'sw-text-editor',
'customFieldType' => 'textEditor',
'customFieldPosition' => 2
]
]
],
'relations' => [
['entityName' => 'product']
]
]], $installContext->getContext());
}
parent::install($installContext);
}
public function postInstall(InstallContext $installContext): void {
parent::postInstall($installContext);
}
public function update(UpdateContext $updateContext): void {
}
public function postUpdate(UpdateContext $updateContext): void {
}
public function activate(ActivateContext $activateContext): void {
parent::activate($activateContext);
}
public function deactivate(DeactivateContext $deactivateContext): void {
parent::deactivate($deactivateContext);
}
public function uninstall(UninstallContext $uninstallContext): void {
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteriaButton = new Criteria();
$criteriaButton->addFilter(new EqualsFilter('name', 'digi1_custombuttons'));
$resultButton = $customFieldSetRepository->searchIds($criteriaButton, $uninstallContext->getContext());
if ($resultButton->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$dataButton = $resultButton->getDataOfId($resultButton->firstId());
$customFieldSetRepository->delete([$dataButton], $uninstallContext->getContext());
}
$criteriaText = new Criteria();
$criteriaText->addFilter(new EqualsFilter('name', 'digi1_customtexts'));
$resultText = $customFieldSetRepository->searchIds($criteriaText, $uninstallContext->getContext());
if ($resultText->getTotal() > 0 && !$uninstallContext->keepUserData()) {
$dataText = $resultText->getDataOfId($resultText->firstId());
$customFieldSetRepository->delete([$dataText], $uninstallContext->getContext());
}
parent::uninstall($uninstallContext);
}
}