custom/plugins/CytrusImport/src/Subscriber/OrderWrittenSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cytrus\Import\Subscriber;
  3. use Psr\Container\ContainerInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Checkout\Order\OrderEvents;
  7. use Shopware\Core\Checkout\Customer\CustomerEvents;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Shopware\Core\Content\Media\File\FileFetcher;
  10. use Shopware\Core\Content\Media\File\FileSaver;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Shopware\Core\Framework\Context;
  13. class OrderWrittenSubscriber implements EventSubscriberInterface
  14. {
  15.     
  16.     private $container;
  17.     private $fileFetcher;
  18.     private $fileSaver;
  19.     
  20.     public function __construct(ContainerInterface $container, FileFetcher $fileFetcher, FileSaver $fileSaver)
  21.     {
  22.         $this->container = $container;
  23.         $this->fileFetcher = $fileFetcher;
  24.         $this->fileSaver = $fileSaver;
  25.     }
  26.     
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  30.         return [
  31.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWrittenEvent',
  32.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWrittenEvent',
  33.         ];
  34.     }
  35.     public function onOrderWrittenEvent(EntityWrittenEvent $event)
  36.     {
  37.         if(isset($_FILES['customerFile']) && is_array($_FILES['customerFile'])) {
  38.             foreach($_FILES['customerFile']['tmp_name'] as $key => $tmpName) {
  39.                 if($_FILES['customerFile']['error'][$key] == 0) {
  40.                     $blob = file_get_contents($tmpName);
  41.                     $ext = pathinfo($_FILES['customerFile']['name'][$key])['extension'];
  42.                     $mimeType = $_FILES['customerFile']['type'][$key];
  43.                     $file = $this->fileFetcher->fetchBlob($blob, $ext, $mimeType);
  44.                     
  45.                     $fileName = $event->getWriteResults()[0]->getPayload()['orderNumber'].'_'.basename($_FILES['customerFile']['name'][$key], '.'.$ext);
  46.                     $mediaId = Uuid::randomHex();
  47.                     
  48.                     $media = [
  49.                         [
  50.                             'id' => $mediaId,
  51.                             'fileSize' => $file->getFileSize(),
  52.                             'fileName' => $fileName,
  53.                             'mimeType' => $file->getMimeType(),
  54.                             'fileExtension' => $ext,
  55.                             'mediaFolderId' => '5f4cd6995dd94405a956de45ade11378',
  56.                         ]
  57.                     ];
  58.                     $this->container->get('media.repository')->upsert($media, Context::createDefaultContext());
  59.                     
  60.                     $this->fileSaver->persistFileToMedia(
  61.                         $file, 
  62.                         $fileName,
  63.                         $mediaId,
  64.                         Context::createDefaultContext()
  65.                     );
  66.                 }
  67.             }
  68.         }
  69.         if( isset($_POST['customField']) && is_array($_POST['customField']) && !empty($_POST['customField']) ) {
  70.             $this->container->get(\Doctrine\DBAL\Connection::class)->executeStatement('UPDATE `order` SET custom_fields=\''.json_encode($_REQUEST['customField']).'\' WHERE id = X\''.$event->getWriteResults()[0]->getPrimaryKey().'\'');
  71.         }
  72.     }
  73.     public function onCustomerWrittenEvent(EntityWrittenEvent $event)
  74.     {
  75.         if( isset($_POST['customField']) && is_array($_POST['customField']) && !empty($_POST['customField']) ) {
  76.             if(isset($_POST['customField']['custom_customers_customernumber']) && trim($_POST['customField']['custom_customers_customernumber']) != '') {
  77.                 $customerNumberPrefix = 'DI-';
  78.                 if($_POST['billingAddress']['countryId'] == 'c224f9eb849240a982273f712d9799ca' || substr($_SERVER['REQUEST_URI'],0,4) == '/fr/' ) {
  79.                     $customerNumberPrefix = 'DE-';
  80.                 }
  81.                 $_POST['customField']['custom_customers_customernumber'] = $customerNumberPrefix . str_replace(['DI-','DE-'],['',''],$_POST['customField']['custom_customers_customernumber']);
  82.                 $this->container->get(\Doctrine\DBAL\Connection::class)->executeStatement('UPDATE `customer` SET custom_fields=\''.json_encode($_POST['customField']).'\' WHERE id = X\''.$event->getWriteResults()[0]->getPrimaryKey().'\'');
  83.             }
  84.         }
  85.     }
  86. }