<?php
namespace KmPitchPrint\Subscribers;
use KmPitchPrint\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageOrderCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Swp\ProductOptionsSix\Core\Checkout\ProductOptions\Cart\ProductOptionsCartDataCollector;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Util\HtmlSanitizer;
use Doctrine\DBAL\Connection;
class CheckoutOrderPlacedSubscriber implements EventSubscriberInterface
{
/**
* @var HtmlSanitizer
*/
private HtmlSanitizer $sanitizer;
/**
* @var EntityRepository
*/
private EntityRepository $orderSkusRepo;
/**
* @var EntityRepository
*/
private EntityRepository $orderRepo;
/**
* @var EntityRepository
*/
private EntityRepository $humanReadableRepo;
/**
* @var Connection
*/
private Connection $connection;
public function __construct(
EntityRepository $orderSkusRepo,
EntityRepository $orderRepo,
//EntityRepository $humanReadableRepo,
Connection $connection,
HtmlSanitizer $sanitizer
)
{
$this->connection = $connection;
$this->orderSkusRepo = $orderSkusRepo;
$this->orderRepo = $orderRepo;
$this->sanitizer = $sanitizer;
//$this->humanReadableRepo = $humanReadableRepo;
}
public static function getSubscribedEvents(): array
{
return [
//CheckoutOrderPlacedEvent::class => 'addReadlbeItemIds',
CheckoutFinishPageOrderCriteriaEvent::class => 'constrainLineItems',
OrderEvents::ORDER_WRITTEN_EVENT => 'orderWritten'
];
}
/**
* @param EntityWrittenEvent $event
* @return void
*/
public function addReadlbeItemIds(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
$i = 1;
#$humanReadable = [];
foreach ($order->getLineItems() as $lineItem) {
if (in_array($lineItem->getType(), [
LineItem::PRODUCT_LINE_ITEM_TYPE,
ProductOptionsCartDataCollector::PRODUCT_OPTIONS_LINE_ITEM_TYPE
]) && $lineItem->getParentId() == null) {
$humanReadable[] = [
'lineItemId' => $lineItem->getId(),
'orderNumber' => $order->getOrderNumber(),
'readableId' => $i,
];
$i++;
}
}
if (isset($humanReadable)) {
$this->humanReadableRepo->upsert($humanReadable, $event->getContext());
}
}
/**
* @param EntityWrittenEvent $event
* @return void
*/
public function orderWritten(EntityWrittenEvent $event): void
{
$context = $event->getContext();
foreach ($event->getWriteResults() as $result) {
if ($result->getProperty('id')) {
$order = $this->orderRepo->search(
(new Criteria())
->addFilter(new EqualsFilter('id', $result->getProperty('id')))
->addAssociation('lineItems'),
$context
)->first();
if ($order) {
$this->writeOrderSkus($order, $context);
//$this->deletePPProjects($order, $context); //don't delete the project, we need it for the "jpg-bridge"
}
}
}
}
private function deletePPProjects(OrderEntity $order, Context $context): void
{
$projects = [];
/*
$lineItems = $order->getLineItems()->filter(function (OrderLineItemEntity $lineItem) {
return $lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE;
});*/
/**
* @var OrderLineItemEntity $lineItem
*/
foreach ($order->getLineItems() as $lineItem) {
$projects[] = $lineItem->getIdentifier();
}
$projects = implode("','", $projects);
$sql = <<<SQL
DELETE FROM km_product_pitchprint WHERE project_id IN ('$projects')
SQL;
$result = $this->connection->executeStatement(
$sql
);
}
private function writeOrderSkus(OrderEntity $order, Context $context)
{
$skus = [];
$swOptions = [];
foreach ($order->getLineItems() as $lineItem) {
if (
$lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE
) {
$previewUrl = '#';
if (isset($lineItem->getPayload('features')['pitch_print'])) {
$previewUrl = $lineItem->getPayloadValue('features')['pitch_print']['previews'][0];
}
if ($lineItem->getPayload()['productNumber'] != '*') {
/*$skus[] = $this->sanitizer->sanitize(
'<a href="' .$previewUrl.'" target="_blank">'.$lineItem->getPayload()['productNumber'].'</a>');*/
#$skus[] = htmlspecialchars('<a href="' .$previewUrl.'" target="_blank">'.$lineItem->getPayload()['productNumber'].'</a>');
$skus[] = '<a href="' . $previewUrl . '" target="_blank">' . $lineItem->getPayload()['productNumber'] . '</a>';
}
}
if ($lineItem->getType() == ProductOptionsCartDataCollector::PRODUCT_OPTIONS_OPTION_LINE_ITEM_TYPE) {
$swOptions[] = $lineItem->getLabel();
}
}
if (!empty($skus)) {
$skus = implode('; ', $skus);
$swOptions = implode('; ', array_unique($swOptions));
$this->orderSkusRepo->create([
[
'orderId' => $order->getId(),
'skus' => $skus,
'swOptions' => $swOptions,
]
], $context);
}
//$this->deletePPProjects($order, $context);
}
/**
* @param CheckoutOrderPlacedEvent $event
* @return void
*/
public function checkoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
$context = $event->getContext();
$this->writeOrderSkus($order, $context);
}
/**
* @param CheckoutFinishPageOrderCriteriaEvent $event
* @return void
*/
public function constrainLineItems(CheckoutFinishPageOrderCriteriaEvent $event): void{
$event->getCriteria()->getAssociation('lineItems')->addFilter(
new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('type', LineItem::PRODUCT_LINE_ITEM_TYPE),
new EqualsFilter('type', ProductOptionsCartDataCollector::PRODUCT_OPTIONS_LINE_ITEM_TYPE),
//new EqualsFilter('type', LineItem::PROMOTION_LINE_ITEM_TYPE)
])
);
$event->getCriteria()->getAssociation('lineItems')->addFilter(new EqualsFilter('parentId', null));
}
}