src/Controller/ProductController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Tools\StaticImportMethods;
  4. use Carbon\Carbon;
  5. use Cocur\Slugify\Slugify;
  6. use Exception;
  7. use Pimcore\Event\Model\ElementEventInterface;
  8. use Pimcore\Model\Asset;
  9. use Pimcore\Model\Asset\Image;
  10. use Pimcore\Model\DataObject;
  11. use Pimcore\Model\DataObject\Family;
  12. use Pimcore\Model\DataObject\Folder;
  13. use Pimcore\Model\DataObject\Product;
  14. use Pimcore\Model\DataObject\Product\Listing;
  15. use Pimcore\Model\DataObject\Sector;
  16. use Pimcore\Model\DataObject\SubFamily;
  17. use Pimcore\Model\Element\ValidationException;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class ProductController
  23. {
  24.     /**
  25.      * @throws ValidationException
  26.      */
  27.     public function onProductPreUpdate(\Pimcore\Event\Model\DataObjectEvent $event): void
  28.     {
  29.         $object $event->getObject();
  30.         if ($object instanceof Product) {
  31.             $this->setFragment($object);
  32.             $this->checkImageShopifyExport($object);
  33.         }
  34.     }
  35.     public function onProductPostUpdate(\Pimcore\Event\Model\DataObjectEvent $event)
  36.     {
  37.         $object $event->getObject();
  38.         if ($object instanceof Product) {
  39.             $this->saveAssetOnWebdav($object);
  40.         }
  41.     }
  42.     private function setFragment(Product $product): void
  43.     {
  44.         $slugify = new Slugify();
  45.         $valueToSlug $product->getName();
  46.         $fragment $product->getFragment();
  47.         if (empty($fragment) && !empty($valueToSlug)) {
  48.             $product->setFragment($slugify->slugify($valueToSlug));
  49.         }
  50.     }
  51.     private function checkImageShopifyExport(Product $product): void
  52.     {
  53.         if ($product->getAvailableInEcommerce() && empty($product->getImage())) {
  54.             throw new ValidationException("Immagine Mancante! Impossibile pubblicare il prodotto.");
  55.         }
  56.     }
  57.     private function saveAssetOnWebdav(Product $object): void
  58.     {
  59.         $fs = new Filesystem();
  60.         $versions $object->getVersions();
  61.         if (count($versions) > 1) {
  62.             $currentVersion $object?->getVersions()[count($versions) - 1];
  63.             $previousVersion $object->getVersions()[count($versions) - 2];
  64.             // check if image is changed
  65.             $currentImage $currentVersion?->getData()?->getImage();
  66.             $previousImage $previousVersion?->getData()?->getImage();
  67.             if (!empty($currentImage?->getId()) && $currentImage?->getId() != $previousImage?->getId()) {
  68.                 // check if folders exist
  69.                 $fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'images'0777);
  70.                 $fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'thumbnails1200'0777);
  71.                 file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "images/{$currentImage->getFilename()}"$currentImage->getData());
  72.                 file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "thumbnails1200/{$currentImage->getFilename()}"$currentImage->getThumbnail("thumbnail400")->getStream());
  73.             }
  74.             // check if gallery is changed
  75.             $currentGallery $currentVersion?->getData()?->getGallery()->getItems();
  76.             $previousGallery $previousVersion?->getData()?->getGallery()->getItems();
  77.             if (!empty(array_diff($currentGallery$previousGallery))) {
  78.                 foreach ($currentGallery as $singleImage) {
  79.                     if (!empty($singleImage)) {
  80.                         // check if folders exist
  81.                         //$fs->mkdir('../../../../hfarm/shared/webdav/onSales/catalog', 0777);
  82.                         $fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'images'0777);
  83.                         $fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'thumbnails400'0777);
  84.                         file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "images/{$currentImage->getFilename()}"$currentImage->getData());
  85.                         file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "thumbnails400/{$currentImage->getFilename()}"$currentImage->getThumbnail("thumbnail400")->getStream());
  86.                     }
  87.                 }
  88.             }
  89.             // check if technical sheet is changed
  90.             $currentSheet $currentVersion?->getData()?->getTechnicalSheet();
  91.             $previousSheet $previousVersion?->getData()?->getTechnicalSheet();
  92.             if (!empty($currentSheet?->getId()) && $currentSheet?->getId() != $previousSheet?->getId()) {
  93.                 // check if folders exist
  94.                 $fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'docs'0777);
  95.                 file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "docs/{$currentSheet->getKey()}"$currentSheet->getData());
  96.             }
  97.         }
  98.     }
  99. }