bundles/SalesAppBundle/Controller/DefaultController.php line 27

Open in your IDE?
  1. <?php
  2. namespace SalesAppBundle\Controller;
  3. use Exception;
  4. use Pimcore\Controller\FrontendController;
  5. use Pimcore\Model\Asset;
  6. use Pimcore\Model\Document\Editable\Pdf;
  7. use Pimcore\Model\Document\Editable\Image;
  8. use SalesAppBundle\Model\Document\Page;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. //use WebAppBundle\Service\Helper\FolderLocatorService;
  13. use Reaze\HeadlessBundle\Service\Helper\FolderLocatorService;
  14. class DefaultController extends FrontendController
  15. {
  16.     /**
  17.      * @Template()
  18.      *
  19.      * @param Request $request
  20.      * @return void
  21.      */
  22.     public function defaultAction() {}
  23.     public function homeAction(): RedirectResponse
  24.     {
  25.         return $this->redirect("/admin");
  26.     }
  27.     /**
  28.      * @Template()
  29.      * @param Request $request
  30.      *
  31.      * @throws Exception
  32.      */
  33.     public function DocumentPageAction(Request $request)
  34.     {
  35.         if ($this->editmode) {
  36.             /** @var Page $document */
  37.             $document $request->attributes->get('contentDocument');
  38.             $elements $document->getEditables();
  39.             $previewImageTag = (array_key_exists('preview-image'$elements)) ? $elements['preview-image'] : null;
  40.             $documentEditable = (array_key_exists('contentElements:1.document'$elements)) ? $elements['contentElements:1.document'] : null;
  41.             // Check if the chosen field is an image editable
  42.             if (!$previewImageTag instanceof Image) {
  43.                 return;
  44.             }
  45.             // Check for the preview image tag (only fill if empty)
  46.             $previewImage = ($previewImageTag->isEmpty()) ? $previewImageTag->getData() : null;
  47.             if (!$previewImage || !$documentEditable || !$documentEditable instanceof Pdf) {
  48.                 return;
  49.             }
  50.             // Check the document editable and get it's document asset
  51.             $editableData = (!$documentEditable->isEmpty()) ? $documentEditable->getData(): null;
  52.             $assetId = (is_array($editableData)) ? $editableData['id'] : null;
  53.             $asset = ($assetId) ? Asset\Document::getById($assetId): null;
  54.             if (!$asset) {
  55.                 return;
  56.             }
  57.             // Create and get image data
  58.             $thumbnail $asset->getImageThumbnail('SalesAppPdfPreview'1false);
  59.             $thumbnailData file_get_contents($this->getParameter('kernel.project_dir').'/public/var/tmp/thumbnails'.$thumbnail->getPath());
  60.             // Get thumbnail filename
  61.             $thumbnailPathArray explode('/'$thumbnail->getPath());
  62.             $thumbnailName end($thumbnailPathArray);
  63.             // Get pdf preview folder (defined by property)
  64.             $folderLocatorService = new FolderLocatorService();
  65.             /** @var Asset\Folder $parentFolder */
  66.             $parentFolder $folderLocatorService->findFolderByProperty(FolderLocatorService::FOLDER_TYPE_ASSET'pdf_preview_folder'true);
  67.             if ($parentFolder === null) {
  68.                 return;
  69.             }
  70.             // Construct full preview image path, removing the config folder
  71.             $imagePathKeyArray explode('/'$parentFolder[0]->getPath().$parentFolder[0]->getKey().$thumbnail->getPath());
  72.             array_pop($imagePathKeyArray);
  73.             array_pop($imagePathKeyArray);
  74.             $imagePath implode('/'$imagePathKeyArray);
  75.             // Create the folder and construct a valid key
  76.             $parentFolder Asset\Service::createFolderByPath($imagePath);
  77.             $validThumbnailImageKey Asset\Service::getValidKey($thumbnail->getConfig()->getName().'-'.$thumbnailName'asset');
  78.             // Check for an existing image
  79.             $existingPdfThumbnail \SalesAppBundle\Model\Asset\Image::getByPath($imagePath.'/'.$validThumbnailImageKey);
  80.             if ($existingPdfThumbnail instanceof \SalesAppBundle\Model\Asset\Image) {
  81.                 $previewImageTag->setImage($existingPdfThumbnail);
  82.                 $previewImageTag->save();
  83.                 return;
  84.             }
  85.             // Create the new pdf thumbnail image
  86.             $assetImage = new Asset\Image();
  87.             $assetImage
  88.                 ->setData($thumbnailData)
  89.                 ->setParentId($parentFolder->getId())
  90.                 ->setKey($validThumbnailImageKey)
  91.                 ->save()
  92.             ;
  93.             // Set it on the document
  94.             $previewImageTag->setImage($assetImage);
  95.             $previewImageTag->save();
  96.         }
  97.     }
  98.     /**
  99.      * @Template()
  100.      *
  101.      * @param Request $request
  102.      * @return void
  103.      */
  104.     public function productPageAction(Request $request)
  105.     {
  106.         if (!$this->editmode) {
  107.         }
  108.     }
  109. }