custom/plugins/CioProductCustomerInputs/src/CioProductCustomerInputs.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioProductCustomerInputs;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. class CioProductCustomerInputs extends Plugin
  15. {
  16.     const CUSTOMER_INPUT_COUNT 10;
  17.     const CUSTOMER_INPUT_POSITION_FACTOR 10;
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  21.         for ($i 1$i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT$i++) {
  22.             $criteria = new Criteria();
  23.             $criteria->addFilter(new EqualsFilter('name''cio_customer_input_' $i));
  24.             $result $customFieldSetRepository->searchIds($criteria$installContext->getContext());
  25.             if (!($result->getTotal() > 0)) {
  26.                 $customFieldSetRepository->create([[
  27.                     'name' => 'cio_customer_input_' $i,
  28.                     'config' => [
  29.                         'label' => [
  30.                             'de-DE' => 'Produkteingabe ' $i,
  31.                             'en-GB' => 'product input ' $i,
  32.                         ],
  33.                     ],
  34.                     'position' => ($i self::CUSTOMER_INPUT_POSITION_FACTOR),
  35.                     'customFields' => [
  36.                         [
  37.                             'name' => 'cio_customer_input_' $i '_active',
  38.                             'type' => CustomFieldTypes::BOOL,
  39.                             'config' => [
  40.                                 'label' => [
  41.                                     'de-DE' => 'Produkteingabe aktivieren',
  42.                                     'en-GB' => 'Activate product input',
  43.                                 ],
  44.                                 'componentName' => 'sw-field',
  45.                                 'customFieldType' => 'checkbox',
  46.                                 'customFieldPosition' => 1,
  47.                                 'type' => 'checkbox',
  48.                             ],
  49.                         ],
  50.                         [
  51.                             'name' => 'cio_customer_input_' $i '_fieldtype',
  52.                             'type' => CustomFieldTypes::SELECT,
  53.                             'config' => [
  54.                                 'label' => [
  55.                                     'de-DE' => 'Feldtyp der Produkteingabe',
  56.                                     'en-GB' => 'type of the field of the product input',
  57.                                 ],
  58.                                 'options' => [
  59.                                     [
  60.                                         'label' => [
  61.                                             'de-DE' => 'einzeiliges Eingabefeld',
  62.                                             'en-GB' => 'single-line input field',
  63.                                         ],
  64.                                         'value' => 'input',
  65.                                     ],
  66.                                     [
  67.                                         'label' => [
  68.                                             'de-DE' => 'einzeiliges Eingabefeld mit Checkbox',
  69.                                             'en-GB' => 'single-line input field with checkbox',
  70.                                         ],
  71.                                         'value' => 'input-checkbox',
  72.                                     ],
  73.                                     [
  74.                                         'label' => [
  75.                                             'de-DE' => 'mehrzeiliges Eingabefeld',
  76.                                             'en-GB' => 'multi-line input field',
  77.                                         ],
  78.                                         'value' => 'textarea',
  79.                                     ],
  80.                                     [
  81.                                         'label' => [
  82.                                             'de-DE' => 'Checkboxfeld',
  83.                                             'en-GB' => 'boolean field',
  84.                                         ],
  85.                                         'value' => 'boolean',
  86.                                     ],
  87.                                     [
  88.                                         'label' => [
  89.                                             'de-DE' => 'Datums- und Uhrzeitfeld',
  90.                                             'en-GB' => 'date and time field',
  91.                                         ],
  92.                                         'value' => 'datetime',
  93.                                     ],
  94.                                     [
  95.                                         'label' => [
  96.                                             'de-DE' => 'Datumsfeld',
  97.                                             'en-GB' => 'date field',
  98.                                         ],
  99.                                         'value' => 'date',
  100.                                     ],
  101.                                     [
  102.                                         'label' => [
  103.                                             'de-DE' => 'Uhrzeitfeld',
  104.                                             'en-GB' => 'time field',
  105.                                         ],
  106.                                         'value' => 'time',
  107.                                     ],
  108.                                     [
  109.                                         'label' => [
  110.                                             'de-DE' => 'Auswahlfeld',
  111.                                             'en-GB' => 'select field',
  112.                                         ],
  113.                                         'value' => 'select',
  114.                                     ],
  115.                                 ],
  116.                                 'componentName' => 'sw-single-select',
  117.                                 'customFieldType' => 'select',
  118.                                 'customFieldPosition' => 2,
  119.                                 'type' => 'select',
  120.                             ],
  121.                         ],
  122.                         [
  123.                             'name' => 'cio_customer_input_' $i '_title',
  124.                             'type' => CustomFieldTypes::TEXT,
  125.                             'config' => [
  126.                                 'label' => [
  127.                                     'de-DE' => 'Beschriftung oberhalb der Produkteingabe',
  128.                                     'en-GB' => 'label above the product input',
  129.                                 ],
  130.                                 'placeholder' => [
  131.                                     'de-DE' => 'Beschriftung oberhalb der Produkteingabe, bspw. "Texteingabe:"',
  132.                                     'en-GB' => 'label above the product input, e.g. "text input:"',
  133.                                 ],
  134.                                 'componentName' => 'sw-field',
  135.                                 'customFieldType' => 'text',
  136.                                 'customFieldPosition' => 3,
  137.                                 'type' => 'text',
  138.                             ],
  139.                         ],
  140.                         [
  141.                             'name' => 'cio_customer_input_' $i '_placeholder',
  142.                             'type' => CustomFieldTypes::TEXT,
  143.                             'config' => [
  144.                                 'label' => [
  145.                                     'de-DE' => 'Platzhalter der Produkteingabe',
  146.                                     'en-GB' => 'placeholder of the product input',
  147.                                 ],
  148.                                 'placeholder' => [
  149.                                     'de-DE' => 'Platzhalter der Produkteingabe, bspw. "Bitte geben Sie einen Wert ein"',
  150.                                     'en-GB' => 'placeholder of the product input, e.g. "please enter a value"',
  151.                                 ],
  152.                                 'componentName' => 'sw-field',
  153.                                 'customFieldType' => 'text',
  154.                                 'customFieldPosition' => 4,
  155.                                 'type' => 'text',
  156.                             ],
  157.                         ],
  158.                         [
  159.                             'name' => 'cio_customer_input_' $i '_required',
  160.                             'type' => CustomFieldTypes::BOOL,
  161.                             'config' => [
  162.                                 'label' => [
  163.                                     'de-DE' => 'Produkteingabe ist ein Pflichtfeld',
  164.                                     'en-GB' => 'product input is a required field',
  165.                                 ],
  166.                                 'componentName' => 'sw-field',
  167.                                 'customFieldType' => 'checkbox',
  168.                                 'customFieldPosition' => 5,
  169.                                 'type' => 'checkbox',
  170.                             ],
  171.                         ],
  172.                         [
  173.                             'name' => 'cio_customer_input_' $i '_price',
  174.                             'type' => CustomFieldTypes::FLOAT,
  175.                             'config' => [
  176.                                 'label' => [
  177.                                     'de-DE' => 'Preis für Veredelung',
  178.                                     'en-GB' => 'price for finishing',
  179.                                 ],
  180.                                 'placeholder' => [
  181.                                     'de-DE' => 'Preis in € bspw. "45.00"',
  182.                                     'en-GB' => 'price in €, e.g. "45.00"',
  183.                                 ],
  184.                                 'componentName' => 'sw-field',
  185.                                 'customFieldType' => 'text',
  186.                                 'customFieldPosition' => 6,
  187.                                 'type' => 'text',
  188.                             ],
  189.                         ],
  190.                         [
  191.                             'name' => 'cio_customer_input_' $i '_startdate',
  192.                             'type' => CustomFieldTypes::TEXT,
  193.                             'config' => [
  194.                                 'label' => [
  195.                                     'de-DE' => 'Startdatum des Datumsfeldes',
  196.                                     'en-GB' => 'start date of the date field',
  197.                                 ],
  198.                                 'placeholder' => [
  199.                                     'de-DE' => '+2 days',
  200.                                     'en-GB' => '+2 days',
  201.                                 ],
  202.                                 'helpText' => [
  203.                                     '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)',
  204.                                     '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)',
  205.                                 ],
  206.                                 'componentName' => 'sw-field',
  207.                                 'customFieldType' => 'text',
  208.                                 'customFieldPosition' => 7,
  209.                                 'type' => 'text',
  210.                             ],
  211.                         ],
  212.                         [
  213.                             'name' => 'cio_customer_input_' $i '_enddate',
  214.                             'type' => CustomFieldTypes::TEXT,
  215.                             'config' => [
  216.                                 'label' => [
  217.                                     'de-DE' => 'Enddatum des Datumsfeldes',
  218.                                     'en-GB' => 'end date of the date field',
  219.                                 ],
  220.                                 'placeholder' => [
  221.                                     'de-DE' => '+2 days',
  222.                                     'en-GB' => '+2 days',
  223.                                 ],
  224.                                 'helpText' => [
  225.                                     '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)',
  226.                                     '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)',
  227.                                 ],
  228.                                 'componentName' => 'sw-field',
  229.                                 'customFieldType' => 'text',
  230.                                 'customFieldPosition' => 8,
  231.                                 'type' => 'text',
  232.                             ],
  233.                         ],
  234.                         [
  235.                             'name' => 'cio_customer_input_' $i '_disableddates',
  236.                             'type' => CustomFieldTypes::TEXT,
  237.                             'config' => [
  238.                                 'label' => [
  239.                                     'de-DE' => 'Ausgeschlossene Daten für das Datumsfeld',
  240.                                     'en-GB' => 'excluded dates for the date field',
  241.                                 ],
  242.                                 'placeholder' => [
  243.                                     'de-DE' => '"01.04.2021","01.05.2021"',
  244.                                     'en-GB' => '"01.04.2021","01.05.2021"',
  245.                                 ],
  246.                                 'helpText' => [
  247.                                     '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',
  248.                                     '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',
  249.                                 ],
  250.                                 'componentName' => 'sw-field',
  251.                                 'customFieldType' => 'text',
  252.                                 'customFieldPosition' => 9,
  253.                                 'type' => 'text',
  254.                             ],
  255.                         ],
  256.                         [
  257.                             'name' => 'cio_customer_input_' $i '_starttime',
  258.                             'type' => CustomFieldTypes::TEXT,
  259.                             'config' => [
  260.                                 'label' => [
  261.                                     'de-DE' => 'Startzeit des Uhrzeitfeldes',
  262.                                     'en-GB' => 'start time of the time field',
  263.                                 ],
  264.                                 'placeholder' => [
  265.                                     'de-DE' => '08:00',
  266.                                     'en-GB' => '08:00',
  267.                                 ],
  268.                                 'helpText' => [
  269.                                     'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [14:00] (für 14:00 Uhr)',
  270.                                     'en-GB' => 'The following value is supported without square brackets: [14:00] (for 2:00 p.m.)',
  271.                                 ],
  272.                                 'componentName' => 'sw-field',
  273.                                 'customFieldType' => 'text',
  274.                                 'customFieldPosition' => 10,
  275.                                 'type' => 'text',
  276.                             ],
  277.                         ],
  278.                         [
  279.                             'name' => 'cio_customer_input_' $i '_endtime',
  280.                             'type' => CustomFieldTypes::TEXT,
  281.                             'config' => [
  282.                                 'label' => [
  283.                                     'de-DE' => 'Endzeit des Uhrzeitfeldes',
  284.                                     'en-GB' => 'end time of the time field',
  285.                                 ],
  286.                                 'placeholder' => [
  287.                                     'de-DE' => '17:00',
  288.                                     'en-GB' => '17:00',
  289.                                 ],
  290.                                 'helpText' => [
  291.                                     'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [20:00] (für 20:00 Uhr)',
  292.                                     'en-GB' => 'The following value is supported without square brackets: [20:00] (for 8:00 p.m.)',
  293.                                 ],
  294.                                 'componentName' => 'sw-field',
  295.                                 'customFieldType' => 'text',
  296.                                 'customFieldPosition' => 11,
  297.                                 'type' => 'text',
  298.                             ],
  299.                         ],
  300.                         [
  301.                             'name' => 'cio_customer_input_' $i '_daterange',
  302.                             'type' => CustomFieldTypes::BOOL,
  303.                             'config' => [
  304.                                 'label' => [
  305.                                     'de-DE' => 'Zeitraumauswahl beim Datumsfeld möglich',
  306.                                     'en-GB' => 'period selection is possible for the date field',
  307.                                 ],
  308.                                 'componentName' => 'sw-field',
  309.                                 'customFieldType' => 'checkbox',
  310.                                 'customFieldPosition' => 12,
  311.                                 'type' => 'checkbox',
  312.                             ],
  313.                         ],
  314.                         [
  315.                             'name' => 'cio_customer_input_' $i '_selectfieldvalues',
  316.                             'type' => CustomFieldTypes::TEXT,
  317.                             'config' => [
  318.                                 'label' => [
  319.                                     'de-DE' => 'Werte für das Auswahlfeld kommasepariert',
  320.                                     'en-GB' => 'Values for the selection field separated by commas',
  321.                                 ],
  322.                                 'placeholder' => [
  323.                                     'de-DE' => 'rot,gelb,blau',
  324.                                     'en-GB' => 'red,yellow,blue',
  325.                                 ],
  326.                                 'helpText' => [
  327.                                     'de-DE' => 'Folgender Wert wird ohne eckige Klammern unterstützt: [rot,gelb,blau]',
  328.                                     'en-GB' => 'The following value is supported without square brackets: [red,yellow,blue]',
  329.                                 ],
  330.                                 'componentName' => 'sw-field',
  331.                                 'customFieldType' => 'text',
  332.                                 'customFieldPosition' => 13,
  333.                                 'type' => 'text',
  334.                             ],
  335.                         ],
  336.                     ],
  337.                     'relations' => [
  338.                         ['entityName' => 'product'],
  339.                     ],
  340.                 ]], $installContext->getContext());
  341.             }
  342.         }
  343.         $criteria = new Criteria();
  344.         $criteria->addFilter(new EqualsFilter('name''cio_customer_input_general'));
  345.         $result $customFieldSetRepository->searchIds($criteria$installContext->getContext());
  346.         if (!($result->getTotal() > 0)) {
  347.             $data = [
  348.                 'name' => 'cio_customer_input_general',
  349.                 'config' => [
  350.                     'label' => [
  351.                         'de-DE' => 'Produkteingabe Allgemein',
  352.                         'en-GB' => 'product input general',
  353.                     ],
  354.                 ],
  355.                 'position' => 100,
  356.                 'customFields' => [
  357.                     [
  358.                         'name' => 'cio_customer_input_general_group',
  359.                         'type' => CustomFieldTypes::SELECT,
  360.                         'config' => [
  361.                             'label' => [
  362.                                 'de-DE' => 'Produkteingabefelder gruppieren',
  363.                                 'en-GB' => 'group product input fields',
  364.                             ],
  365.                             'options' => [],
  366.                             'componentName' => 'sw-multi-select',
  367.                             'customFieldType' => 'select',
  368.                             'customFieldPosition' => 1,
  369.                             'type' => 'select',
  370.                         ],
  371.                     ],
  372.                     [
  373.                         'name' => 'cio_customer_input_count_rows',
  374.                         'type' => CustomFieldTypes::INT,
  375.                         'config' => [
  376.                             'label' => [
  377.                                 'de-DE' => 'Anzahl der Reihen',
  378.                                 'en-GB' => 'row count',
  379.                             ],
  380.                             'componentName' => 'sw-field',
  381.                             'customFieldType' => 'number',
  382.                             'numberType' => 'int',
  383.                             'customFieldPosition' => 2,
  384.                             'type' => 'number',
  385.                             'step' => 1,
  386.                             'min' => 1
  387.                         ],
  388.                     ],
  389.                     [
  390.                         'name' => 'cio_customer_input_row_label_prefix',
  391.                         'type' => CustomFieldTypes::TEXT,
  392.                         'config' => [
  393.                             'label' => [
  394.                                 'de-DE' => 'Reihen Label Prefix',
  395.                                 'en-GB' => 'row label prefix',
  396.                             ],
  397.                             'placeholder' => [
  398.                                 'de-DE' => 'Reihe %index%',
  399.                                 'en-GB' => 'row %index%',
  400.                             ],
  401.                             'componentName' => 'sw-field',
  402.                             'customFieldType' => 'text',
  403.                             'customFieldPosition' => 3,
  404.                             'type' => 'text',
  405.                         ],
  406.                     ],
  407.                 ],
  408.                 'relations' => [
  409.                     ['entityName' => 'product'],
  410.                 ],
  411.             ];
  412.             for ($i 1$i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT$i++) {
  413.                 $data['customFields'][0]['config']['options'][] = [
  414.                     'label' => [
  415.                         'de-DE' => 'Produkteingabe ' $i,
  416.                         'en-GB' => 'product input ' $i,
  417.                     ],
  418.                     'value' => 'cio_customer_input_' $i,
  419.                 ];
  420.             }
  421.             $customFieldSetRepository->create([
  422.                 $data
  423.             ], $installContext->getContext());
  424.         }
  425.         parent::install($installContext);
  426.     }
  427.     public function postInstall(InstallContext $installContext): void
  428.     {
  429.         parent::postInstall($installContext);
  430.     }
  431.     public function update(UpdateContext $updateContext): void
  432.     {
  433.     }
  434.     public function postUpdate(UpdateContext $updateContext): void
  435.     {
  436.     }
  437.     public function activate(ActivateContext $activateContext): void
  438.     {
  439.         parent::activate($activateContext);
  440.     }
  441.     public function deactivate(DeactivateContext $deactivateContext): void
  442.     {
  443.         parent::deactivate($deactivateContext);
  444.     }
  445.     public function uninstall(UninstallContext $uninstallContext): void
  446.     {
  447.         if ($uninstallContext->keepUserData()) {
  448.             parent::uninstall($uninstallContext);
  449.             return;
  450.         }
  451.         $customFieldSets = [];
  452.         $customFieldSets[] = 'cio_customer_input_general';
  453.         for ($i 1$i <= CioProductCustomerInputs::CUSTOMER_INPUT_COUNT; ++$i) {
  454.             $customFieldSets[] = 'cio_customer_input_' $i;
  455.         }
  456.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  457.         $criteria = new Criteria();
  458.         $criteria->addFilter(new EqualsAnyFilter('name'$customFieldSets));
  459.         $result $customFieldSetRepository->searchIds($criteria$uninstallContext->getContext());
  460.         foreach ($result->getIds() as $id) {
  461.             $data $result->getDataOfId($id);
  462.             $customFieldSetRepository->delete([$data], $uninstallContext->getContext());
  463.         }
  464.         parent::uninstall($uninstallContext);
  465.     }
  466. }