Skip to content

Commit

Permalink
Task: open pdf in new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
robertSt7 committed Jan 7, 2025
1 parent 01789a3 commit b1273fe
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/Controller/Admin/Asset/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Pimcore\Messenger\AssetUpdateTasksMessage;
use Pimcore\Model;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Enum\PdfScanStatus;
use Pimcore\Model\DataObject\ClassDefinition\Data\ManyToManyRelation;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element;
Expand Down Expand Up @@ -950,10 +951,12 @@ public function showVersionAction(Request $request, Environment $twig): Response
}

if ($asset instanceof Asset\Document && $asset->getMimeType() === self::PDF_MIMETYPE) {
$scanResponse = $this->getResponseByScanStatus($asset, false);
if ($scanResponse) {
return $scanResponse;
}
$previewData = ['thumbnailPath' => ''];
$previewData['assetPath'] = $asset->getRealFullPath();
return $this->render(
'@PimcoreAdmin/admin/asset/get_preview_pdf_open_in_new_tab.html.twig',
$previewData
);
}

Tool\UserTimezone::setUserTimezone($request->query->get('userTimezone'));
Expand Down Expand Up @@ -1450,8 +1453,23 @@ public function getPreviewDocumentAction(
if ($asset->isAllowed('view')) {
if ($asset instanceof Asset\Document && $asset->getMimeType() === self::PDF_MIMETYPE) {
$scanResponse = $this->getResponseByScanStatus($asset);
if ($scanResponse) {
return $scanResponse;
$openPdfConfig = Config::getSystemConfiguration('assets')['document']['open_pdf_in_new_tab'];

if ($openPdfConfig === 'all-pdfs' ||
($openPdfConfig === 'only-unsafe' && $scanResponse === PdfScanStatus::UNSAFE)) {
$thumbnail = $asset->getImageThumbnail(Asset\Image\Thumbnail\Config::getPreviewConfig());
$previewData = ['thumbnailPath' => $thumbnail->getPath()];
$previewData['assetPath'] = $asset->getRealFullPath();
return $this->render(
'@PimcoreAdmin/admin/asset/get_preview_pdf_open_in_new_tab.html.twig',
$previewData
);
}

if ($scanResponse === PdfScanStatus::IN_PROGRESS) {
return $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_in_progress.html.twig');
} elseif ($scanResponse === PdfScanStatus::UNSAFE) {
return $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_unsafe.html.twig');
}
}

Expand All @@ -1470,7 +1488,7 @@ public function getPreviewDocumentAction(
}
}

private function getResponseByScanStatus(Asset\Document $asset, bool $processBackground = true): ?Response
private function getResponseByScanStatus(Asset\Document $asset, bool $processBackground = true): ?PdfScanStatus
{
if (!Config::getSystemConfiguration('assets')['document']['scan_pdf']) {
return null;
Expand All @@ -1486,11 +1504,7 @@ private function getResponseByScanStatus(Asset\Document $asset, bool $processBac
}
}

return match($scanStatus) {
Asset\Enum\PdfScanStatus::IN_PROGRESS => $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_in_progress.html.twig'),
Asset\Enum\PdfScanStatus::UNSAFE => $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_unsafe.html.twig'),
default => null,
};
return $scanStatus;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions templates/admin/asset/get_preview_pdf_open_in_new_tab.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<style>
/* hide from ie on mac \*/
html {
height: 100%;
}
/* end hide */
body {
height: 100%;
margin: 0;
padding: 0;
background: #EEE;
}
#container {
text-align: center;
width: 100%;
}
{% if not thumbnailPath %}
#container {
padding-top: 200px;
}
{% endif %}
img {
height: 600px;
margin: 4px;
}
</style>
</head>

<body>

<div id="container">
{% if thumbnailPath %}
<a href="{{ assetPath }}" target="_blank">
<img alt="preview" src="{{ thumbnailPath }}" />
</a>
{% else %}
<span>{{ 'no_preview_available'|trans([], 'admin') }}</span>
{% endif %}
<div>
<a href="{{ assetPath }}" target="_blank">{{ 'open_in_new_window'|trans([], 'admin') }}</a>
</div>
</div>


</body>
</html>
2 changes: 1 addition & 1 deletion templates/admin/asset/show_version_unknown.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<table id="wrapper" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
Sorry, no preview available<br>
{{ 'no_preview_available'|trans([], 'admin') }}<br>
{% set tempFile = asset.getTemporaryFile() %}
{% set dataUri = pimcore_asset_version_preview(tempFile) %}

Expand Down
3 changes: 2 additions & 1 deletion translations/admin.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1026,4 +1026,5 @@ zip_upload_want_to_overwrite_yes_option: 'Yes'
zip_upload_want_to_overwrite_no_option: 'No'
invalid_option: 'Invalid Option field [ {field} ]: Please choose a valid option for select / multiselect field [ {field} ]. Current value: "{option}"'
respect_timezone: 'Respect timezone'
delete_quantity_value_unit_confirmation: 'Deleting this unit will impact all related elements that reference it. Are you sure you want to proceed?'
delete_quantity_value_unit_confirmation: 'Deleting this unit will impact all related elements that reference it. Are you sure you want to proceed?'
no_preview_available: 'Sorry, no preview available'

0 comments on commit b1273fe

Please sign in to comment.