<?php
namespace WabsPIM\Subscriber;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\System\CustomField\CustomFieldEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use WabsPIM\Core\Content\PIM\Event\ProductDraftStateChangedEvent;
class EntityEventSubscriber implements EventSubscriberInterface
{
protected EntityRepositoryInterface $productRepository;
protected EntityRepositoryInterface $customFieldRepository;
protected EventDispatcherInterface $eventDispatcher;
public function __construct(
EntityRepositoryInterface $productRepository,
EntityRepositoryInterface $customFieldRepository,
EventDispatcherInterface $eventDispatcher
) {
$this->productRepository = $productRepository;
$this->customFieldRepository = $customFieldRepository;
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents()
{
return [
'wabs_product_draft.written' => ['onProductDraftWritten', 1000],
];
}
public function onProductDraftWritten( EntityWrittenEvent $event) {
$results = $event->getWriteResults();
foreach( $results as $result ) {
if( $result->hasPayload('state') ) {
$productId = $result->getProperty('productId');
$productVersionId = $result->getProperty('productVersionId');
$criteria = new Criteria([$productId]);
$criteria->addAssociation('productDraft');
$criteria->addAssociation('productDraft.customer');
$criteria->addAssociation('visibilities');
$versionContext = $event->getContext()->createWithVersionId($productVersionId);
/** @var ProductEntity $product */
$product = $this->productRepository->search($criteria, $versionContext)->getEntities()->first();
$visibility = $product->getVisibilities()->first();
$productCustomFields = $product->getCustomFields();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'igm_mbs_verantwortlicher_gruppe'));
/** @var CustomFieldEntity $customField */
$customField = $this->customFieldRepository->search($criteria, $event->getContext())->getEntities()->first();
$customFieldConfig = $customField->getConfig();
$verantwortlicherMailAddress = null;
foreach( $customFieldConfig['options'] as $customFieldOption ) {
if( $customFieldOption['value'] == $productCustomFields['igm_mbs_verantwortlicher_gruppe']) {
$verantwortlicherMailAddress = $customFieldOption['label']['de-DE'];
}
}
if( $verantwortlicherMailAddress ) {
$event = new ProductDraftStateChangedEvent(
$versionContext,
$product,
$product->getExtension('productDraft')->getCustomer(),
$visibility->getSalesChannelId(),
new MailRecipientStruct([
$verantwortlicherMailAddress => $verantwortlicherMailAddress
])
);
$this->eventDispatcher->dispatch($event);
}
}
}
}
}