bundles/DownloadPdfBundle/EventListener/DownloadPdfListener.php line 93

Open in your IDE?
  1. <?php
  2. namespace DownloadPdfBundle\EventListener;
  3. use Pimcore\Event\Model\DataObjectEvent;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Model\Asset;
  6. use Pimcore\Model\DataObject;
  7. use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
  8. use Pimcore\Model\DataObject\Service;
  9. use Pimcore\Model\Element\ValidationException;
  10. use Pimcore\Tool;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. class DownloadPdfListener implements EventSubscriberInterface
  14. {
  15.     private $workflowRegistry;
  16.     protected $slugify;
  17.     const DOWNLOADPDF 'download_pdf';
  18.     /**
  19.      * @param GenericEvent $event
  20.      */
  21.     public function checkPermissions(GenericEvent $event): void
  22.     {
  23.         $object $event->getArgument("object");
  24.         //data element that is send to Pimcore backend UI
  25.         $data $event->getArgument("data");
  26.         $data['hasPreview'] = false;
  27.         $currentLayout explode(",""-1,1,2");
  28.         if ($object instanceof DataObject\DownloadPDF && !in_array($data['currentLayoutId'], $currentLayout)) {
  29.             $data $this->f_setLayout($object$data);
  30.             $event->setArgument("data"$data);
  31.         }
  32.     }
  33.     /**
  34.      * @param $object
  35.      * @param $data
  36.      * @return mixed
  37.      */
  38.     public function f_setLayout($object$data): mixed
  39.     {
  40.         DataObject\Product::setGetInheritedValues(true);
  41.         if ('variant' == $object->gettype()) {
  42.             $customLayoutToSelect 9;
  43.         } elseif ($object->getPublished() == false && $object->gettype() != 'variant') {
  44.             $customLayoutToSelect 8;
  45.         } elseif ($object->getPublished() == true && $object->gettype() != 'variant') {
  46.             $customLayoutToSelect 10;
  47.         } else {
  48.             $customLayoutToSelect 10;
  49.         }
  50.         if ($customLayoutToSelect != null) {
  51.             //set current layout to subcategory layout
  52.             $data['currentLayoutId'] = $customLayoutToSelect;
  53.             $customLayout CustomLayout::getById($customLayoutToSelect);
  54.             $data['layout'] = $customLayout->getLayoutDefinitions();
  55.             Service::enrichLayoutDefinition($data["layout"], $object);
  56.         }
  57.         if (!empty($layoutsToRemove)) {
  58.             //remove master layout from valid layouts
  59.             $validLayouts $data["validLayouts"];
  60.             foreach ($validLayouts as $key => $validLayout) {
  61.                 if (in_array($validLayout['id'], $layoutsToRemove)) {
  62.                     unset($validLayouts[$key]);
  63.                 }
  64.             }
  65.             $data["validLayouts"] = array_values($validLayouts);
  66.         }
  67.         return $data;
  68.     }
  69.     /**
  70.      * @return array|\string[][]
  71.      */
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             'workflow.Download_pdf_Workflow.guard' => ['guardPublish'],
  76.             'workflow.Download_pdf_Workflow.leave' => ['onLeave'],
  77.         ];
  78.     }
  79.     /**
  80.      * @param ElementEventInterface $event
  81.      */
  82.     public function onPreUpdate(ElementEventInterface $event): void
  83.     {
  84.         if ($event instanceof DataObjectEvent) {
  85.             $objects $event->getObject();
  86.             $this->checkValidation($objects);
  87.             if ($objects instanceof DataObject\DownloadPDF) {
  88.                 $this->getPdfFiles($objects);
  89.             }
  90.         }
  91.     }
  92.     /**
  93.      * @param $objects
  94.      * @throws ValidationException
  95.      */
  96.     public function checkValidation($objects): void
  97.     {
  98.         $errors =[]; 
  99.         if ($objects instanceof DataObject\DownloadPDF) {
  100.             if ($objects instanceof DataObject\DownloadPDF and $objects->gettype() != 'variant') {
  101.                 $errors = [];
  102.                 if ($objects->getLinkModel() == null || $objects->getLinkModel() == "" || (empty($objects->getLinkModel()))) {
  103.                     $message 'Link Model is required.';
  104.                     array_push($errors$message);
  105.                 } else {
  106.                     if (($objects->getLinkModel()->getModelCode() == null || $objects->getLinkModel()->getModelCode() == "") && ($objects->getLinkModel()->getType() == "variant")) {
  107.                         $message 'Empty Link Model Code Value, Please check Either Product Model or Sku Properly !';
  108.                         array_push($errors$message);
  109.                     } else {
  110.                         $objects->setKey($objects->getLinkModel()->getModelCode());
  111.                         $objects->setUniqueCode($objects->getLinkModel()->getModelCode());
  112.                     }
  113.                 }
  114.                 if ($objects->getPdfType() == null || $objects->getPdfType() == "" || (empty($objects->getPdfType()))) {
  115.                     $message 'Download Pdf Type is required.';
  116.                     array_push($errors$message);
  117.                 }
  118.             } else {
  119. //                $downloadPdfCount = $this->getDownloadPdfMaxCount($objects);
  120.                 $downloadPdfVariantSummaryJson $this->getDownloadPdfSummary($objects);
  121.                 if ($objects->getParent()->getUniqueCode() != null || $objects->getParent()->getUniqueCode() != "") {
  122.                     $this->getDownloadPdfJsonReserve($downloadPdfVariantSummaryJson$objects->getParent()->getId());
  123.                 }
  124.             }
  125.             $this->getExceptionMessage($errors$objects);
  126.         }
  127.     }
  128.     /**
  129.      * @param $skuMessages
  130.      * @param $object
  131.      * @throws ValidationException
  132.      */
  133.     public function getExceptionMessage($skuMessages$object): void
  134.     {
  135.         if (!empty($skuMessages)) {
  136.             $result null;
  137.             foreach ($skuMessages as $skuMessage) {
  138.                 $result .= $skuMessage "<span style='color: red;'>*</span>" '<br>';
  139.             }
  140.             throw new ValidationException($result403);
  141.         }
  142.     }
  143.     /**
  144.      * @param $objects
  145.      */
  146.     public function getDownloadPdfSummary($objects): array
  147.     {
  148.         $downloadPdfSummaryObjects = [];
  149.         foreach (Tool::getValidLanguages() as $language) {
  150.             $downloadPdfAssetsLanguageDocuments $objects->getDownloadPdf($language);
  151.             if (!empty($downloadPdfAssetsLanguageDocuments)) {
  152.                 foreach ($downloadPdfAssetsLanguageDocuments as $downloadPdfAssetsLanguageDocument) {
  153.                     $downloadPdfSummaryObjects[] = [
  154.                         "$language=> $downloadPdfAssetsLanguageDocument->getElement()->getFileName(),
  155.                     ];
  156.                 }
  157.             } else {
  158.                 $downloadPdfSummaryObjects[] = [
  159.                     "$language=> "#############",
  160.                 ];
  161.             }
  162.         }
  163.         $downloadPdfAssetsJsonDetails = [[
  164.             "code" => $objects->getUniqueCode(),
  165.             "languages" => $downloadPdfSummaryObjects
  166.         ]];
  167.         return ($downloadPdfAssetsJsonDetails);
  168.     }
  169.     /**
  170.      * @param $downloadPdfVariantSummaryJson
  171.      * @param $parentUniqueCode
  172.      * @throws \Exception
  173.      */
  174.     public function getDownloadPdfJsonReserve($downloadPdfVariantSummaryJson$parentUniqueId)
  175.     {
  176.         $downloadPdfObjects DataObject\DownloadPDF::getById($parentUniqueId);
  177.         if ($downloadPdfObjects) {
  178.             $productModelObject DataObject\Product::getByModelCode($downloadPdfObjects->getUniqueCode(), 1);
  179.             if ($productModelObject) {
  180.                 if (!empty($productModelObject->getDownloadPdfJson()) || $productModelObject->getDownloadPdfJson() != null || $productModelObject->getDownloadPdfJson() != "") {
  181.                     $downloadPdfMergeJson array_values(array_column(array_merge(json_decode($productModelObject->getDownloadPdfJson()), $downloadPdfVariantSummaryJson), null"code"));
  182.                     $productModelObject->setDownloadPdfJson(json_encode($downloadPdfMergeJson));
  183.                     $productModelObject->setSkipValidation(true);
  184.                     $productModelObject->setOmitMandatoryCheck(true);
  185.                     $productModelObject->setPublished(true);
  186.                     $productModelObject->save();
  187.                 } else {
  188.                     $productModelObject->setDownloadPdfJson(json_encode($downloadPdfVariantSummaryJson));
  189.                     $productModelObject->setSkipValidation(true);
  190.                     $productModelObject->setOmitMandatoryCheck(true);
  191.                     $productModelObject->setPublished(true);
  192.                     $productModelObject->save();
  193.                 }
  194.             }
  195.         }
  196.     }
  197.     /**
  198.      * @param $objects
  199.      * @return int
  200.      */
  201.     public function getDownloadPdfMaxCount($objects)
  202.     {
  203.         $count 0;
  204.         foreach (Tool::getValidLanguages() as $language) {
  205.             if (count($objects->getDownloadPdf($language)) > $count) {
  206.                 $count count($objects->getDownloadPdf($language));
  207.             }
  208.         }
  209.         return $count;
  210.     }
  211.     public function getPdfFiles($objects)
  212.     {
  213.         $error= [];
  214.         if ($objects) {
  215.             foreach (Tool::getValidLanguages() as $language) {
  216.                 if ($objects->getDownloadPdf() != null || !empty($objects->getDownloadPdf())) {
  217.                         $targetPath self::DOWNLOADPDF '/' $objects->getParent()->getUniqueCode() . '/' $objects->getParent()->getUniqueCode() . '_' $objects->getPdfTypeVariant() . '/' 'pdf';
  218.                         $destinationPath '/' self::DOWNLOADPDF '/' $objects->getParent()->getUniqueCode() . '/' $objects->getParent()->getUniqueCode() . '_' $objects->getPdfTypeVariant() . '/' 'pdf/';
  219.                         Asset\Service::createFolderByPath($targetPath);
  220.                         $targetId \Pimcore\Model\Asset::getByPath($destinationPath);
  221.                     foreach ($objects->getDownloadPdf($language) as $obj) {
  222.                         $this->LinkPdf($obj->getElementId(), $targetId->getId());
  223. //                        if ($obj->getMimeType() != 'application/pdf') {
  224. //                            $message = 'please insert pdf file';
  225. //                            array_push($error, $message);
  226. //                            continue;
  227. //                        }
  228.                     }
  229.                 }
  230.                // $this->getExceptionMessage($error, $objects);
  231.             }
  232.         }
  233.     }
  234.     public function LinkPdf($data$id){
  235.         $asset Asset::getById($data);
  236.         $asset->setParentId($id);
  237.         $asset->save();
  238.     }
  239. }