<?php
namespace App\Controller;
use App\Tools\StaticImportMethods;
use Carbon\Carbon;
use Cocur\Slugify\Slugify;
use Exception;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Family;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\Product\Listing;
use Pimcore\Model\DataObject\Sector;
use Pimcore\Model\DataObject\SubFamily;
use Pimcore\Model\Element\ValidationException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductController
{
/**
* @throws ValidationException
*/
public function onProductPreUpdate(\Pimcore\Event\Model\DataObjectEvent $event): void
{
$object = $event->getObject();
if ($object instanceof Product) {
$this->setFragment($object);
$this->checkImageShopifyExport($object);
}
}
public function onProductPostUpdate(\Pimcore\Event\Model\DataObjectEvent $event)
{
$object = $event->getObject();
if ($object instanceof Product) {
$this->saveAssetOnWebdav($object);
}
}
private function setFragment(Product $product): void
{
$slugify = new Slugify();
$valueToSlug = $product->getName();
$fragment = $product->getFragment();
if (empty($fragment) && !empty($valueToSlug)) {
$product->setFragment($slugify->slugify($valueToSlug));
}
}
private function checkImageShopifyExport(Product $product): void
{
if ($product->getAvailableInEcommerce() && empty($product->getImage())) {
throw new ValidationException("Immagine Mancante! Impossibile pubblicare il prodotto.");
}
}
private function saveAssetOnWebdav(Product $object): void
{
$fs = new Filesystem();
$versions = $object->getVersions();
if (count($versions) > 1) {
$currentVersion = $object?->getVersions()[count($versions) - 1];
$previousVersion = $object->getVersions()[count($versions) - 2];
// check if image is changed
$currentImage = $currentVersion?->getData()?->getImage();
$previousImage = $previousVersion?->getData()?->getImage();
if (!empty($currentImage?->getId()) && $currentImage?->getId() != $previousImage?->getId()) {
// check if folders exist
$fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'images', 0777);
$fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'thumbnails1200', 0777);
file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "images/{$currentImage->getFilename()}", $currentImage->getData());
file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "thumbnails1200/{$currentImage->getFilename()}", $currentImage->getThumbnail("thumbnail400")->getStream());
}
// check if gallery is changed
$currentGallery = $currentVersion?->getData()?->getGallery()->getItems();
$previousGallery = $previousVersion?->getData()?->getGallery()->getItems();
if (!empty(array_diff($currentGallery, $previousGallery))) {
foreach ($currentGallery as $singleImage) {
if (!empty($singleImage)) {
// check if folders exist
//$fs->mkdir('../../../../hfarm/shared/webdav/onSales/catalog', 0777);
$fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'images', 0777);
$fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'thumbnails400', 0777);
file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "images/{$currentImage->getFilename()}", $currentImage->getData());
file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "thumbnails400/{$currentImage->getFilename()}", $currentImage->getThumbnail("thumbnail400")->getStream());
}
}
}
// check if technical sheet is changed
$currentSheet = $currentVersion?->getData()?->getTechnicalSheet();
$previousSheet = $previousVersion?->getData()?->getTechnicalSheet();
if (!empty($currentSheet?->getId()) && $currentSheet?->getId() != $previousSheet?->getId()) {
// check if folders exist
$fs->mkdir($_ENV["PDF_IMAGES_EXPORT_PATH"] . 'docs', 0777);
file_put_contents($_ENV["PDF_IMAGES_EXPORT_PATH"] . "docs/{$currentSheet->getKey()}", $currentSheet->getData());
}
}
}
}