Skip to content

Commit 0d2b48a

Browse files
authored
Task: open pdf in new tab (#807)
1 parent 01789a3 commit 0d2b48a

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

src/Controller/Admin/Asset/AssetController.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Pimcore\Messenger\AssetUpdateTasksMessage;
3838
use Pimcore\Model;
3939
use Pimcore\Model\Asset;
40+
use Pimcore\Model\Asset\Enum\PdfScanStatus;
4041
use Pimcore\Model\DataObject\ClassDefinition\Data\ManyToManyRelation;
4142
use Pimcore\Model\DataObject\Concrete;
4243
use Pimcore\Model\Element;
@@ -950,10 +951,12 @@ public function showVersionAction(Request $request, Environment $twig): Response
950951
}
951952

952953
if ($asset instanceof Asset\Document && $asset->getMimeType() === self::PDF_MIMETYPE) {
953-
$scanResponse = $this->getResponseByScanStatus($asset, false);
954-
if ($scanResponse) {
955-
return $scanResponse;
956-
}
954+
$previewData = ['thumbnailPath' => ''];
955+
$previewData['assetPath'] = $asset->getRealFullPath();
956+
return $this->render(
957+
'@PimcoreAdmin/admin/asset/get_preview_pdf_open_in_new_tab.html.twig',
958+
$previewData
959+
);
957960
}
958961

959962
Tool\UserTimezone::setUserTimezone($request->query->get('userTimezone'));
@@ -1450,8 +1453,23 @@ public function getPreviewDocumentAction(
14501453
if ($asset->isAllowed('view')) {
14511454
if ($asset instanceof Asset\Document && $asset->getMimeType() === self::PDF_MIMETYPE) {
14521455
$scanResponse = $this->getResponseByScanStatus($asset);
1453-
if ($scanResponse) {
1454-
return $scanResponse;
1456+
$openPdfConfig = Config::getSystemConfiguration('assets')['document']['open_pdf_in_new_tab'];
1457+
1458+
if ($openPdfConfig === 'all-pdfs' ||
1459+
($openPdfConfig === 'only-unsafe' && $scanResponse === PdfScanStatus::UNSAFE)) {
1460+
$thumbnail = $asset->getImageThumbnail(Asset\Image\Thumbnail\Config::getPreviewConfig());
1461+
$previewData = ['thumbnailPath' => $thumbnail->getPath()];
1462+
$previewData['assetPath'] = $asset->getRealFullPath();
1463+
return $this->render(
1464+
'@PimcoreAdmin/admin/asset/get_preview_pdf_open_in_new_tab.html.twig',
1465+
$previewData
1466+
);
1467+
}
1468+
1469+
if ($scanResponse === PdfScanStatus::IN_PROGRESS) {
1470+
return $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_in_progress.html.twig');
1471+
} elseif ($scanResponse === PdfScanStatus::UNSAFE) {
1472+
return $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_unsafe.html.twig');
14551473
}
14561474
}
14571475

@@ -1470,7 +1488,7 @@ public function getPreviewDocumentAction(
14701488
}
14711489
}
14721490

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

1489-
return match($scanStatus) {
1490-
Asset\Enum\PdfScanStatus::IN_PROGRESS => $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_in_progress.html.twig'),
1491-
Asset\Enum\PdfScanStatus::UNSAFE => $this->render('@PimcoreAdmin/admin/asset/get_preview_pdf_unsafe.html.twig'),
1492-
default => null,
1493-
};
1507+
return $scanStatus;
14941508
}
14951509

14961510
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
6+
<style>
7+
8+
/* hide from ie on mac \*/
9+
html {
10+
height: 100%;
11+
}
12+
/* end hide */
13+
14+
body {
15+
height: 100%;
16+
margin: 0;
17+
padding: 0;
18+
background: #EEE;
19+
}
20+
21+
#container {
22+
text-align: center;
23+
width: 100%;
24+
}
25+
26+
{% if not thumbnailPath %}
27+
#container {
28+
padding-top: 200px;
29+
}
30+
{% endif %}
31+
32+
33+
img {
34+
height: 600px;
35+
margin: 4px;
36+
}
37+
38+
</style>
39+
</head>
40+
41+
<body>
42+
43+
<div id="container">
44+
{% if thumbnailPath %}
45+
<a href="{{ assetPath }}" target="_blank">
46+
<img alt="preview" src="{{ thumbnailPath }}" />
47+
</a>
48+
{% else %}
49+
<span>{{ 'no_preview_available'|trans([], 'admin') }}</span>
50+
{% endif %}
51+
<div>
52+
<a href="{{ assetPath }}" target="_blank">{{ 'open_in_new_window'|trans([], 'admin') }}</a>
53+
</div>
54+
</div>
55+
56+
57+
</body>
58+
</html>

templates/admin/asset/show_version_unknown.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<table id="wrapper" cellpadding="0" cellspacing="0" border="0">
2727
<tr>
2828
<td>
29-
Sorry, no preview available<br>
29+
{{ 'no_preview_available'|trans([], 'admin') }}<br>
3030
{% set tempFile = asset.getTemporaryFile() %}
3131
{% set dataUri = pimcore_asset_version_preview(tempFile) %}
3232

translations/admin.en.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,4 +1026,5 @@ zip_upload_want_to_overwrite_yes_option: 'Yes'
10261026
zip_upload_want_to_overwrite_no_option: 'No'
10271027
invalid_option: 'Invalid Option field [ {field} ]: Please choose a valid option for select / multiselect field [ {field} ]. Current value: "{option}"'
10281028
respect_timezone: 'Respect timezone'
1029-
delete_quantity_value_unit_confirmation: 'Deleting this unit will impact all related elements that reference it. Are you sure you want to proceed?'
1029+
delete_quantity_value_unit_confirmation: 'Deleting this unit will impact all related elements that reference it. Are you sure you want to proceed?'
1030+
no_preview_available: 'Sorry, no preview available'

0 commit comments

Comments
 (0)