<?php
declare(strict_types=1);
namespace CioProductCustomerInputs;
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\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;
class CioProductCustomerInputs extends Plugin
{
const CUSTOMER_INPUT_COUNT = 10;
const CUSTOMER_INPUT_POSITION_FACTOR = 10;
public function install(InstallContext $installContext): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
for ($i = 1; $i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT; $i++) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'cio_customer_input_' . $i));
$result = $customFieldSetRepository->searchIds($criteria, $installContext->getContext());
if (!($result->getTotal() > 0)) {
$customFieldSetRepository->create([[
'name' => 'cio_customer_input_' . $i,
'config' => [
'label' => [
'de-DE' => 'Produkteingabe ' . $i,
'en-GB' => 'product input ' . $i,
],
],
'position' => ($i * self::CUSTOMER_INPUT_POSITION_FACTOR),
'customFields' => [
[
'name' => 'cio_customer_input_' . $i . '_active',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Produkteingabe aktivieren',
'en-GB' => 'Activate product input',
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1,
'type' => 'checkbox',
],
],
[
'name' => 'cio_customer_input_' . $i . '_fieldtype',
'type' => CustomFieldTypes::SELECT,
'config' => [
'label' => [
'de-DE' => 'Feldtyp der Produkteingabe',
'en-GB' => 'type of the field of the product input',
],
'options' => [
[
'label' => [
'de-DE' => 'einzeiliges Eingabefeld',
'en-GB' => 'single-line input field',
],
'value' => 'input',
],
[
'label' => [
'de-DE' => 'einzeiliges Eingabefeld mit Checkbox',
'en-GB' => 'single-line input field with checkbox',
],
'value' => 'input-checkbox',
],
[
'label' => [
'de-DE' => 'mehrzeiliges Eingabefeld',
'en-GB' => 'multi-line input field',
],
'value' => 'textarea',
],
[
'label' => [
'de-DE' => 'Checkboxfeld',
'en-GB' => 'boolean field',
],
'value' => 'boolean',
],
[
'label' => [
'de-DE' => 'Datums- und Uhrzeitfeld',
'en-GB' => 'date and time field',
],
'value' => 'datetime',
],
[
'label' => [
'de-DE' => 'Datumsfeld',
'en-GB' => 'date field',
],
'value' => 'date',
],
[
'label' => [
'de-DE' => 'Uhrzeitfeld',
'en-GB' => 'time field',
],
'value' => 'time',
],
[
'label' => [
'de-DE' => 'Auswahlfeld',
'en-GB' => 'select field',
],
'value' => 'select',
],
],
'componentName' => 'sw-single-select',
'customFieldType' => 'select',
'customFieldPosition' => 2,
'type' => 'select',
],
],
[
'name' => 'cio_customer_input_' . $i . '_title',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Beschriftung oberhalb der Produkteingabe',
'en-GB' => 'label above the product input',
],
'placeholder' => [
'de-DE' => 'Beschriftung oberhalb der Produkteingabe, bspw. "Texteingabe:"',
'en-GB' => 'label above the product input, e.g. "text input:"',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_placeholder',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Platzhalter der Produkteingabe',
'en-GB' => 'placeholder of the product input',
],
'placeholder' => [
'de-DE' => 'Platzhalter der Produkteingabe, bspw. "Bitte geben Sie einen Wert ein"',
'en-GB' => 'placeholder of the product input, e.g. "please enter a value"',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 4,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_required',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Produkteingabe ist ein Pflichtfeld',
'en-GB' => 'product input is a required field',
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 5,
'type' => 'checkbox',
],
],
[
'name' => 'cio_customer_input_' . $i . '_price',
'type' => CustomFieldTypes::FLOAT,
'config' => [
'label' => [
'de-DE' => 'Preis für Veredelung',
'en-GB' => 'price for finishing',
],
'placeholder' => [
'de-DE' => 'Preis in € bspw. "45.00"',
'en-GB' => 'price in €, e.g. "45.00"',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 6,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_startdate',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Startdatum des Datumsfeldes',
'en-GB' => 'start date of the date field',
],
'placeholder' => [
'de-DE' => '+2 days',
'en-GB' => '+2 days',
],
'helpText' => [
'de-DE' => 'Folgende Werte werden bspw. ohne eckige Klammern unterstützt: [today] (für heute), [+2 days] (für heute zzgl. eines festgelegten Zeitraums in der Form [+ 1 day], [+1 week], [+1 month] oder [+1 year]) oder [01.01.2021] (für ein festes Datum)',
'en-GB' => 'For example the following values are supported without square brackets: [today] (for today), [+2 days] (for today plus a specified period in the form [+ 1 day], [+1 week], [+1 month] or [+1 year]) or [01.01.2021] (for a fixed date)',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 7,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_enddate',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Enddatum des Datumsfeldes',
'en-GB' => 'end date of the date field',
],
'placeholder' => [
'de-DE' => '+2 days',
'en-GB' => '+2 days',
],
'helpText' => [
'de-DE' => 'Folgende Werte werden bspw. ohne eckige Klammern unterstützt: [today] (für heute), [+2 days] (für heute zzgl. eines festgelegten Zeitraums in der Form [+ 1 day], [+1 week], [+1 month] oder [+1 year]) oder [31.12.2021] (für ein festes Datum)',
'en-GB' => 'For example the following values are supported without square brackets: [today] (for today), [+2 days] (for today plus a specified period in the form [+ 1 day], [+1 week], [+1 month] or [+1 year]) or [31.12.2021] (for a fixed date)',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 8,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_disableddates',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Ausgeschlossene Daten für das Datumsfeld',
'en-GB' => 'excluded dates for the date field',
],
'placeholder' => [
'de-DE' => '"01.04.2021","01.05.2021"',
'en-GB' => '"01.04.2021","01.05.2021"',
],
'helpText' => [
'de-DE' => 'Folgende Werte werden bspw. ohne eckige Klammern unterstützt: ["01.01.2021"] (für ein ausgeschlossenes Datum), ["01.04.2021","01.05.2021"] (für mehrere ausgeschlossene Daten) [{"from": "02.04.2021", "to": "05.04.2021"},{"from": "03.05.2021", "to": "09.05.2021"}] (für mehrere Zeiträume) oder eine Kombination daraus',
'en-GB' => 'For example the following values are supported without square brackets: ["01.01.2021"] (for one excluded date), ["01.04.2021", "01.05.2021"] (for several excluded dates) [{"from": "02.04.2021", "to": "05.04.2021"},{"from": "03.05.2021", "to": "09.05.2021"}] (for several periods) or a combination of these',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 9,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_starttime',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Startzeit des Uhrzeitfeldes',
'en-GB' => 'start time of the time field',
],
'placeholder' => [
'de-DE' => '08:00',
'en-GB' => '08:00',
],
'helpText' => [
'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [14:00] (für 14:00 Uhr)',
'en-GB' => 'The following value is supported without square brackets: [14:00] (for 2:00 p.m.)',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 10,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_endtime',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Endzeit des Uhrzeitfeldes',
'en-GB' => 'end time of the time field',
],
'placeholder' => [
'de-DE' => '17:00',
'en-GB' => '17:00',
],
'helpText' => [
'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [20:00] (für 20:00 Uhr)',
'en-GB' => 'The following value is supported without square brackets: [20:00] (for 8:00 p.m.)',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 11,
'type' => 'text',
],
],
[
'name' => 'cio_customer_input_' . $i . '_daterange',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Zeitraumauswahl beim Datumsfeld möglich',
'en-GB' => 'period selection is possible for the date field',
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 12,
'type' => 'checkbox',
],
],
[
'name' => 'cio_customer_input_' . $i . '_selectfieldvalues',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Werte für das Auswahlfeld kommasepariert',
'en-GB' => 'Values for the selection field separated by commas',
],
'placeholder' => [
'de-DE' => 'rot,gelb,blau',
'en-GB' => 'red,yellow,blue',
],
'helpText' => [
'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [rot,gelb,blau]',
'en-GB' => 'The following value is supported without square brackets: [red,yellow,blue]',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 13,
'type' => 'text',
],
],
],
'relations' => [
['entityName' => 'product'],
],
]], $installContext->getContext());
}
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'cio_customer_input_general'));
$result = $customFieldSetRepository->searchIds($criteria, $installContext->getContext());
if (!($result->getTotal() > 0)) {
$data = [
'name' => 'cio_customer_input_general',
'config' => [
'label' => [
'de-DE' => 'Produkteingabe Allgemein',
'en-GB' => 'product input general',
],
],
'position' => 100,
'customFields' => [
[
'name' => 'cio_customer_input_general_group',
'type' => CustomFieldTypes::SELECT,
'config' => [
'label' => [
'de-DE' => 'Produkteingabefelder gruppieren',
'en-GB' => 'group product input fields',
],
'options' => [],
'componentName' => 'sw-multi-select',
'customFieldType' => 'select',
'customFieldPosition' => 1,
'type' => 'select',
],
],
[
'name' => 'cio_customer_input_count_rows',
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'de-DE' => 'Anzahl der Reihen',
'en-GB' => 'row count',
],
'componentName' => 'sw-field',
'customFieldType' => 'number',
'numberType' => 'int',
'customFieldPosition' => 2,
'type' => 'number',
'step' => 1,
'min' => 1
],
],
[
'name' => 'cio_customer_input_row_label_prefix',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Reihen Label Prefix',
'en-GB' => 'row label prefix',
],
'placeholder' => [
'de-DE' => 'Reihe %index%',
'en-GB' => 'row %index%',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'type' => 'text',
],
],
],
'relations' => [
['entityName' => 'product'],
],
];
for ($i = 1; $i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT; $i++) {
$data['customFields'][0]['config']['options'][] = [
'label' => [
'de-DE' => 'Produkteingabe ' . $i,
'en-GB' => 'product input ' . $i,
],
'value' => 'cio_customer_input_' . $i,
];
}
$customFieldSetRepository->create([
$data
], $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
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$customFieldSets = [];
$customFieldSets[] = 'cio_customer_input_general';
for ($i = 1; $i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT; ++$i) {
$customFieldSets[] = 'cio_customer_input_' . $i;
}
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', $customFieldSets));
$result = $customFieldSetRepository->searchIds($criteria, $uninstallContext->getContext());
foreach ($result->getIds() as $id) {
$data = $result->getDataOfId($id);
$customFieldSetRepository->delete([$data], $uninstallContext->getContext());
}
parent::uninstall($uninstallContext);
}
}