custom/plugins/CioPodProducts/src/CioPodProducts.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioPodProducts;
  3. use CioPodProducts\StateMachine\PodCustomStates;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  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\InstallContext;
  9. class CioPodProducts extends Plugin
  10. {
  11.     public const MEDIA_UPLOAD_FOLDER_ID '780da71c02cd4cac87abdc26208e602b';
  12.     public const MEDIA_UPLOAD_DEFAULT_FOLDER_ID '03090e6bae934426b1f3281a5ae016e6';
  13.     public const MEDIA_UPLOAD_FOLDER_ENTITY 'form_builder_upload';
  14.     public const POD_PRODUCTS_CUSTOM_FIELD_IS_POD 'custom_pod_products_isPodProduct';
  15.     public function install(InstallContext $installContext): void
  16.     {
  17.         parent::install($installContext);
  18.         $this->installCustomStates($installContext);
  19.         $this->installCustomStateTransitions($installContext);
  20.         $this->installMediaFolder($installContext);
  21.     }
  22.     private function installCustomStates(InstallContext $installContext): void
  23.     {
  24.         $context $installContext->getContext();
  25.         // Hole das Repository der State Machines, um die Order-State Machine anhand des technicalName "order.state" zu finden
  26.         /** @var EntityRepository $stateMachineRepository */
  27.         $stateMachineRepository $this->container->get('state_machine.repository');
  28.         $criteria = new Criteria();
  29.         $criteria->addFilter(new EqualsFilter('technicalName''order.state'));
  30.         $stateMachine $stateMachineRepository->search($criteria$context)->first();
  31.         if (!$stateMachine) {
  32.             throw new \RuntimeException('Order state machine not found');
  33.         }
  34.         $orderStateMachineId $stateMachine->getId();
  35.         // Hole das Repository für die State Machine States
  36.         /** @var EntityRepository $stateMachineStateRepository */
  37.         $stateMachineStateRepository $this->container->get('state_machine_state.repository');
  38.         $states = [
  39.             [
  40.                 'id' => PodCustomStates::STATE_POD_WAITING_FOR_PHOTOGRAPHER_DATA,
  41.                 'technicalName' => 'pod_waiting_for_photographer_data',
  42.                 'name' => 'Warten auf Daten des Fotografen',
  43.                 'stateMachineId' => $orderStateMachineId,
  44.             ],
  45.             [
  46.                 'id' => PodCustomStates::STATE_POD_WAITING_FOR_SUPPLIER_DATA,
  47.                 'technicalName' => 'pod_waiting_for_supplier_data',
  48.                 'name' => 'Warten auf Lieferantendaten',
  49.                 'stateMachineId' => $orderStateMachineId,
  50.             ],
  51.             [
  52.                 'id' => PodCustomStates::STATE_POD_WAITING_FOR_APPROVAL,
  53.                 'technicalName' => 'pod_waiting_for_approval',
  54.                 'name' => 'Warten auf Freigabe',
  55.                 'stateMachineId' => $orderStateMachineId,
  56.             ],
  57.             [
  58.                 'id' => PodCustomStates::STATE_POD_WAITING_FOR_FINAL_APPROVAL,
  59.                 'technicalName' => 'pod_waiting_for_final_approval',
  60.                 'name' => 'Warten auf finale Freigabe',
  61.                 'stateMachineId' => $orderStateMachineId,
  62.             ],
  63.         ];
  64.         // Neue States werden angelegt (upsert fügt hinzu, falls sie noch nicht existieren)
  65.         $stateMachineStateRepository->upsert($states$context);
  66.     }
  67.     private function installMediaFolder(InstallContext $installContext): void
  68.     {
  69.         $context $installContext->getContext();
  70.         /** @var EntityRepository $mediaDefaultFolderRepository */
  71.         $mediaDefaultFolderRepository $this->container->get('media_default_folder.repository');
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(new EqualsFilter('entity'self::MEDIA_UPLOAD_FOLDER_ENTITY));
  74.         $defaultFolderResult $mediaDefaultFolderRepository->search($criteria$context);
  75.         if ($defaultFolderResult->count() === 0) {
  76.             $mediaDefaultFolderRepository->upsert([
  77.                 [
  78.                     'id' => self::MEDIA_UPLOAD_DEFAULT_FOLDER_ID,
  79.                     'mediaFolderId' => self::MEDIA_UPLOAD_FOLDER_ID,
  80.                     'entity' => self::MEDIA_UPLOAD_FOLDER_ENTITY,
  81.                     'associationFields' => ['media']
  82.                 ]
  83.             ], $context);
  84.         }
  85.         /** @var EntityRepository $mediaFolderRepository */
  86.         $mediaFolderRepository $this->container->get('media_folder.repository');
  87.         $criteria = new Criteria([self::MEDIA_UPLOAD_FOLDER_ID]);
  88.         $folderResult $mediaFolderRepository->search($criteria$context);
  89.         if ($folderResult->count() === 0) {
  90.             $mediaFolderRepository->upsert([
  91.                 [
  92.                     'id' => self::MEDIA_UPLOAD_FOLDER_ID,
  93.                     'name' => 'FormBuilder Upload Data',
  94.                     'defaultFolderId' => self::MEDIA_UPLOAD_DEFAULT_FOLDER_ID,
  95.                     'configuration' => [
  96.                         'createThumbnails' => false,
  97.                     ]
  98.                 ]
  99.             ], $context);
  100.         }
  101.     }
  102.     private function installCustomStateTransitions(InstallContext $installContext): void
  103.     {
  104.         $context $installContext->getContext();
  105.         /** @var EntityRepository $stateMachineStateRepository */
  106.         $stateMachineStateRepository $this->container->get('state_machine_state.repository');
  107.         /** @var EntityRepository $stateMachineTransitionRepository */
  108.         $stateMachineTransitionRepository $this->container->get('state_machine_transition.repository');
  109.         // Order-State-Machine holen (du hast die ID bereits oben ermittelt)
  110.         /** @var EntityRepository $stateMachineRepository */
  111.         $stateMachineRepository $this->container->get('state_machine.repository');
  112.         $criteria = (new Criteria())->addFilter(new EqualsFilter('technicalName''order.state'));
  113.         $stateMachine $stateMachineRepository->search($criteria$context)->first();
  114.         if (!$stateMachine) {
  115.             throw new \RuntimeException('Order state machine not found');
  116.         }
  117.         $orderStateMachineId $stateMachine->getId();
  118.         // Zielstate "cancelled" ermitteln
  119.         $cancelledCriteria = (new Criteria())
  120.             ->addFilter(new EqualsFilter('stateMachineId'$orderStateMachineId))
  121.             ->addFilter(new EqualsFilter('technicalName''cancelled'));
  122.         $cancelled $stateMachineStateRepository->search($cancelledCriteria$context)->first();
  123.         if (!$cancelled) {
  124.             throw new \RuntimeException('Cancelled state not found');
  125.         }
  126.         $cancelledId $cancelled->getId();
  127.         // Deine Custom-State-IDs (wie beim Anlegen verwendet)
  128.         $fromStates = [
  129.             PodCustomStates::STATE_POD_WAITING_FOR_PHOTOGRAPHER_DATA,
  130.             PodCustomStates::STATE_POD_WAITING_FOR_SUPPLIER_DATA,
  131.             PodCustomStates::STATE_POD_WAITING_FOR_APPROVAL,
  132.             PodCustomStates::STATE_POD_WAITING_FOR_FINAL_APPROVAL,
  133.         ];
  134.         // Für jeden Custom-State eine Transition -> cancelled mit actionName "cancel"
  135.         $transitions = [];
  136.         foreach ($fromStates as $fromStateId) {
  137.             $transitions[] = [
  138.                 'stateMachineId' => $orderStateMachineId,
  139.                 'fromStateId'    => $fromStateId,
  140.                 'toStateId'      => $cancelledId,
  141.                 'actionName'     => 'cancel',
  142.             ];
  143.         }
  144.         // upsert ist idempotent dank Unique-Constraint (action/from/to/stateMachine)
  145.         $stateMachineTransitionRepository->upsert($transitions$context);
  146.     }
  147. }