<?php declare(strict_types=1);
namespace CioPodProducts;
use CioPodProducts\StateMachine\PodCustomStates;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
class CioPodProducts extends Plugin
{
public const MEDIA_UPLOAD_FOLDER_ID = '780da71c02cd4cac87abdc26208e602b';
public const MEDIA_UPLOAD_DEFAULT_FOLDER_ID = '03090e6bae934426b1f3281a5ae016e6';
public const MEDIA_UPLOAD_FOLDER_ENTITY = 'form_builder_upload';
public const POD_PRODUCTS_CUSTOM_FIELD_IS_POD = 'custom_pod_products_isPodProduct';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->installCustomStates($installContext);
$this->installCustomStateTransitions($installContext);
$this->installMediaFolder($installContext);
}
private function installCustomStates(InstallContext $installContext): void
{
$context = $installContext->getContext();
// Hole das Repository der State Machines, um die Order-State Machine anhand des technicalName "order.state" zu finden
/** @var EntityRepository $stateMachineRepository */
$stateMachineRepository = $this->container->get('state_machine.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('technicalName', 'order.state'));
$stateMachine = $stateMachineRepository->search($criteria, $context)->first();
if (!$stateMachine) {
throw new \RuntimeException('Order state machine not found');
}
$orderStateMachineId = $stateMachine->getId();
// Hole das Repository für die State Machine States
/** @var EntityRepository $stateMachineStateRepository */
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
$states = [
[
'id' => PodCustomStates::STATE_POD_WAITING_FOR_PHOTOGRAPHER_DATA,
'technicalName' => 'pod_waiting_for_photographer_data',
'name' => 'Warten auf Daten des Fotografen',
'stateMachineId' => $orderStateMachineId,
],
[
'id' => PodCustomStates::STATE_POD_WAITING_FOR_SUPPLIER_DATA,
'technicalName' => 'pod_waiting_for_supplier_data',
'name' => 'Warten auf Lieferantendaten',
'stateMachineId' => $orderStateMachineId,
],
[
'id' => PodCustomStates::STATE_POD_WAITING_FOR_APPROVAL,
'technicalName' => 'pod_waiting_for_approval',
'name' => 'Warten auf Freigabe',
'stateMachineId' => $orderStateMachineId,
],
[
'id' => PodCustomStates::STATE_POD_WAITING_FOR_FINAL_APPROVAL,
'technicalName' => 'pod_waiting_for_final_approval',
'name' => 'Warten auf finale Freigabe',
'stateMachineId' => $orderStateMachineId,
],
];
// Neue States werden angelegt (upsert fügt hinzu, falls sie noch nicht existieren)
$stateMachineStateRepository->upsert($states, $context);
}
private function installMediaFolder(InstallContext $installContext): void
{
$context = $installContext->getContext();
/** @var EntityRepository $mediaDefaultFolderRepository */
$mediaDefaultFolderRepository = $this->container->get('media_default_folder.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('entity', self::MEDIA_UPLOAD_FOLDER_ENTITY));
$defaultFolderResult = $mediaDefaultFolderRepository->search($criteria, $context);
if ($defaultFolderResult->count() === 0) {
$mediaDefaultFolderRepository->upsert([
[
'id' => self::MEDIA_UPLOAD_DEFAULT_FOLDER_ID,
'mediaFolderId' => self::MEDIA_UPLOAD_FOLDER_ID,
'entity' => self::MEDIA_UPLOAD_FOLDER_ENTITY,
'associationFields' => ['media']
]
], $context);
}
/** @var EntityRepository $mediaFolderRepository */
$mediaFolderRepository = $this->container->get('media_folder.repository');
$criteria = new Criteria([self::MEDIA_UPLOAD_FOLDER_ID]);
$folderResult = $mediaFolderRepository->search($criteria, $context);
if ($folderResult->count() === 0) {
$mediaFolderRepository->upsert([
[
'id' => self::MEDIA_UPLOAD_FOLDER_ID,
'name' => 'FormBuilder Upload Data',
'defaultFolderId' => self::MEDIA_UPLOAD_DEFAULT_FOLDER_ID,
'configuration' => [
'createThumbnails' => false,
]
]
], $context);
}
}
private function installCustomStateTransitions(InstallContext $installContext): void
{
$context = $installContext->getContext();
/** @var EntityRepository $stateMachineStateRepository */
$stateMachineStateRepository = $this->container->get('state_machine_state.repository');
/** @var EntityRepository $stateMachineTransitionRepository */
$stateMachineTransitionRepository = $this->container->get('state_machine_transition.repository');
// Order-State-Machine holen (du hast die ID bereits oben ermittelt)
/** @var EntityRepository $stateMachineRepository */
$stateMachineRepository = $this->container->get('state_machine.repository');
$criteria = (new Criteria())->addFilter(new EqualsFilter('technicalName', 'order.state'));
$stateMachine = $stateMachineRepository->search($criteria, $context)->first();
if (!$stateMachine) {
throw new \RuntimeException('Order state machine not found');
}
$orderStateMachineId = $stateMachine->getId();
// Zielstate "cancelled" ermitteln
$cancelledCriteria = (new Criteria())
->addFilter(new EqualsFilter('stateMachineId', $orderStateMachineId))
->addFilter(new EqualsFilter('technicalName', 'cancelled'));
$cancelled = $stateMachineStateRepository->search($cancelledCriteria, $context)->first();
if (!$cancelled) {
throw new \RuntimeException('Cancelled state not found');
}
$cancelledId = $cancelled->getId();
// Deine Custom-State-IDs (wie beim Anlegen verwendet)
$fromStates = [
PodCustomStates::STATE_POD_WAITING_FOR_PHOTOGRAPHER_DATA,
PodCustomStates::STATE_POD_WAITING_FOR_SUPPLIER_DATA,
PodCustomStates::STATE_POD_WAITING_FOR_APPROVAL,
PodCustomStates::STATE_POD_WAITING_FOR_FINAL_APPROVAL,
];
// Für jeden Custom-State eine Transition -> cancelled mit actionName "cancel"
$transitions = [];
foreach ($fromStates as $fromStateId) {
$transitions[] = [
'stateMachineId' => $orderStateMachineId,
'fromStateId' => $fromStateId,
'toStateId' => $cancelledId,
'actionName' => 'cancel',
];
}
// upsert ist idempotent dank Unique-Constraint (action/from/to/stateMachine)
$stateMachineTransitionRepository->upsert($transitions, $context);
}
}