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

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