<?php
namespace SalesAppBundle\Controller;
use Exception;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\Asset;
use Pimcore\Model\Document\Editable\Pdf;
use Pimcore\Model\Document\Editable\Image;
use SalesAppBundle\Model\Document\Page;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
//use WebAppBundle\Service\Helper\FolderLocatorService;
use Reaze\HeadlessBundle\Service\Helper\FolderLocatorService;
class DefaultController extends FrontendController
{
/**
* @Template()
*
* @param Request $request
* @return void
*/
public function defaultAction() {}
public function homeAction(): RedirectResponse
{
return $this->redirect("/admin");
}
/**
* @Template()
* @param Request $request
*
* @throws Exception
*/
public function DocumentPageAction(Request $request)
{
if ($this->editmode) {
/** @var Page $document */
$document = $request->attributes->get('contentDocument');
$elements = $document->getEditables();
$previewImageTag = (array_key_exists('preview-image', $elements)) ? $elements['preview-image'] : null;
$documentEditable = (array_key_exists('contentElements:1.document', $elements)) ? $elements['contentElements:1.document'] : null;
// Check if the chosen field is an image editable
if (!$previewImageTag instanceof Image) {
return;
}
// Check for the preview image tag (only fill if empty)
$previewImage = ($previewImageTag->isEmpty()) ? $previewImageTag->getData() : null;
if (!$previewImage || !$documentEditable || !$documentEditable instanceof Pdf) {
return;
}
// Check the document editable and get it's document asset
$editableData = (!$documentEditable->isEmpty()) ? $documentEditable->getData(): null;
$assetId = (is_array($editableData)) ? $editableData['id'] : null;
$asset = ($assetId) ? Asset\Document::getById($assetId): null;
if (!$asset) {
return;
}
// Create and get image data
$thumbnail = $asset->getImageThumbnail('SalesAppPdfPreview', 1, false);
$thumbnailData = file_get_contents($this->getParameter('kernel.project_dir').'/public/var/tmp/thumbnails'.$thumbnail->getPath());
// Get thumbnail filename
$thumbnailPathArray = explode('/', $thumbnail->getPath());
$thumbnailName = end($thumbnailPathArray);
// Get pdf preview folder (defined by property)
$folderLocatorService = new FolderLocatorService();
/** @var Asset\Folder $parentFolder */
$parentFolder = $folderLocatorService->findFolderByProperty(FolderLocatorService::FOLDER_TYPE_ASSET, 'pdf_preview_folder', true);
if ($parentFolder === null) {
return;
}
// Construct full preview image path, removing the config folder
$imagePathKeyArray = explode('/', $parentFolder[0]->getPath().$parentFolder[0]->getKey().$thumbnail->getPath());
array_pop($imagePathKeyArray);
array_pop($imagePathKeyArray);
$imagePath = implode('/', $imagePathKeyArray);
// Create the folder and construct a valid key
$parentFolder = Asset\Service::createFolderByPath($imagePath);
$validThumbnailImageKey = Asset\Service::getValidKey($thumbnail->getConfig()->getName().'-'.$thumbnailName, 'asset');
// Check for an existing image
$existingPdfThumbnail = \SalesAppBundle\Model\Asset\Image::getByPath($imagePath.'/'.$validThumbnailImageKey);
if ($existingPdfThumbnail instanceof \SalesAppBundle\Model\Asset\Image) {
$previewImageTag->setImage($existingPdfThumbnail);
$previewImageTag->save();
return;
}
// Create the new pdf thumbnail image
$assetImage = new Asset\Image();
$assetImage
->setData($thumbnailData)
->setParentId($parentFolder->getId())
->setKey($validThumbnailImageKey)
->save()
;
// Set it on the document
$previewImageTag->setImage($assetImage);
$previewImageTag->save();
}
}
/**
* @Template()
*
* @param Request $request
* @return void
*/
public function productPageAction(Request $request)
{
if (!$this->editmode) {
}
}
}