custom/plugins/ExpertXmlOrderExport-master/src/ExpertXmlOrderExport.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ExpertXmlOrderExport;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. class ExpertXmlOrderExport extends Plugin
  11. {
  12.     /**
  13.      * set create mode name
  14.      */
  15.     private const FIELD_SET_HELPER_MODE_CREATE 'create';
  16.     /**
  17.      * set delete mode name
  18.      */
  19.     private const FIELD_SET_HELPER_MODE_DELETE 'delete';
  20.     /**
  21.      * @param ActivateContext $activateContext
  22.      */
  23.     public function activate(ActivateContext $activateContext): void
  24.     {
  25.         parent::activate($activateContext);
  26.         $this->customFieldSetHelper($activateContext->getContext(), nullself::FIELD_SET_HELPER_MODE_CREATE);
  27.     }
  28.     /**
  29.      * @param DeactivateContext $deactivateContext
  30.      */
  31.     public function deactivate(DeactivateContext $deactivateContext): void
  32.     {
  33.         parent::deactivate($deactivateContext);
  34.         $this->customFieldSetHelper(
  35.             $deactivateContext->getContext(),
  36.             ['expert_order_export_states'],
  37.             self::FIELD_SET_HELPER_MODE_DELETE
  38.         );
  39.     }
  40.     /**
  41.      * @param Context $context
  42.      * @param array   $fieldNames
  43.      * @param string  $mode
  44.      */
  45.     private function customFieldSetHelper(
  46.         Context $context,
  47.         $fieldNames = [],
  48.         $mode self::FIELD_SET_HELPER_MODE_CREATE
  49.     ): void {
  50.         // get custom fieldset repository
  51.         $customFieldsetRepository $this->container->get('custom_field_set.repository');
  52.         if ($mode == self::FIELD_SET_HELPER_MODE_CREATE) {
  53.             $customFieldsetRepository->create($this->getCustomFieldsContainer(), $context);
  54.         } elseif ($mode == self::FIELD_SET_HELPER_MODE_DELETE && !empty($fieldNames)) {
  55.             // get Criteria
  56.             $criteria = new Criteria();
  57.             foreach ($fieldNames as $fieldName) {
  58.                 // filter criteria by name
  59.                 $criteria->addFilter(new EqualsFilter('name'$fieldName));
  60.                 $ids $customFieldsetRepository->searchIds($criteria$context);
  61.                 if ($ids) {
  62.                     $customFieldsetRepository->delete([['id' => $ids->getIds()[0]]], $context);
  63.                 }
  64.             }
  65.         } else {
  66.             new \Exception();
  67.         }
  68.     }
  69.     /**
  70.      * @return array|array[]
  71.      */
  72.     private function getCustomFieldsContainer(): array
  73.     {
  74.         return [
  75.             [
  76.                 'name' => 'expert_order_export_states',
  77.                 'config' => [
  78.                     'label' => [
  79.                         'en-GB' => 'XML orderexport status',
  80.                         'de-DE' => 'XML Exportstatus',
  81.                     ],
  82.                     'translated' => false,
  83.                 ],
  84.                 'customFields' => [
  85.                     [
  86.                         'name' => 'expert_order_export_state',
  87.                         'type' => CustomFieldTypes::BOOL,
  88.                         'config' => [
  89.                             'componentName' => 'sw-field',
  90.                             'customFieldType' => 'checkbox',
  91.                             'customFieldPosition' => 1,
  92.                             'label' => [
  93.                                 'en-GB' => 'Status',
  94.                                 'de-DE' => 'Status',
  95.                             ],
  96.                             'helpText' => [
  97.                                 'en-GB' => 'Checked: Already exported | Unchecked: Not exported yet',
  98.                                 'de-DE' => 'Wenn Checked: Bestellung bereits exportiert | Wenn Unchecked: Bestellung noch nicht exportiert',
  99.                             ],
  100.                         ]
  101.                     ]
  102.                 ],
  103.                 'relations' => [
  104.                     [
  105.                         'entityName' => 'order'
  106.                     ]
  107.                 ]
  108.             ], // end
  109.         ];
  110.     }
  111. }