<?php declare(strict_types=1);
namespace Cytrus\Import\Subscriber;
use Psr\Container\ContainerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\Content\Media\File\FileFetcher;
use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Context;
class OrderWrittenSubscriber implements EventSubscriberInterface
{
private $container;
private $fileFetcher;
private $fileSaver;
public function __construct(ContainerInterface $container, FileFetcher $fileFetcher, FileSaver $fileSaver)
{
$this->container = $container;
$this->fileFetcher = $fileFetcher;
$this->fileSaver = $fileSaver;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWrittenEvent',
CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWrittenEvent',
];
}
public function onOrderWrittenEvent(EntityWrittenEvent $event)
{
if(isset($_FILES['customerFile']) && is_array($_FILES['customerFile'])) {
foreach($_FILES['customerFile']['tmp_name'] as $key => $tmpName) {
if($_FILES['customerFile']['error'][$key] == 0) {
$blob = file_get_contents($tmpName);
$ext = pathinfo($_FILES['customerFile']['name'][$key])['extension'];
$mimeType = $_FILES['customerFile']['type'][$key];
$file = $this->fileFetcher->fetchBlob($blob, $ext, $mimeType);
$fileName = $event->getWriteResults()[0]->getPayload()['orderNumber'].'_'.basename($_FILES['customerFile']['name'][$key], '.'.$ext);
$mediaId = Uuid::randomHex();
$media = [
[
'id' => $mediaId,
'fileSize' => $file->getFileSize(),
'fileName' => $fileName,
'mimeType' => $file->getMimeType(),
'fileExtension' => $ext,
'mediaFolderId' => '5f4cd6995dd94405a956de45ade11378',
]
];
$this->container->get('media.repository')->upsert($media, Context::createDefaultContext());
$this->fileSaver->persistFileToMedia(
$file,
$fileName,
$mediaId,
Context::createDefaultContext()
);
}
}
}
if( isset($_POST['customField']) && is_array($_POST['customField']) && !empty($_POST['customField']) ) {
$this->container->get(\Doctrine\DBAL\Connection::class)->executeStatement('UPDATE `order` SET custom_fields=\''.json_encode($_REQUEST['customField']).'\' WHERE id = X\''.$event->getWriteResults()[0]->getPrimaryKey().'\'');
}
}
public function onCustomerWrittenEvent(EntityWrittenEvent $event)
{
if( isset($_POST['customField']) && is_array($_POST['customField']) && !empty($_POST['customField']) ) {
if(isset($_POST['customField']['custom_customers_customernumber']) && trim($_POST['customField']['custom_customers_customernumber']) != '') {
$customerNumberPrefix = 'DI-';
if($_POST['billingAddress']['countryId'] == 'c224f9eb849240a982273f712d9799ca' || substr($_SERVER['REQUEST_URI'],0,4) == '/fr/' ) {
$customerNumberPrefix = 'DE-';
}
$_POST['customField']['custom_customers_customernumber'] = $customerNumberPrefix . str_replace(['DI-','DE-'],['',''],$_POST['customField']['custom_customers_customernumber']);
$this->container->get(\Doctrine\DBAL\Connection::class)->executeStatement('UPDATE `customer` SET custom_fields=\''.json_encode($_POST['customField']).'\' WHERE id = X\''.$event->getWriteResults()[0]->getPrimaryKey().'\'');
}
}
}
}