added inline disposition for PDF previews (#3486)
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Export\Base\DispositionInlineInterface;
|
||||
use App\Export\ExportItemInterface;
|
||||
use App\Export\ServiceExport;
|
||||
use App\Export\TooManyItemsExportException;
|
||||
@@ -117,6 +118,11 @@ class ExportController extends AbstractController
|
||||
throw $this->createNotFoundException('Unknown export renderer');
|
||||
}
|
||||
|
||||
// display file inline if supported and `markAsExported` is not set
|
||||
if ($renderer instanceof DispositionInlineInterface && !$query->isMarkAsExported()) {
|
||||
$renderer->setDispositionInline(true);
|
||||
}
|
||||
|
||||
$entries = $this->getEntries($query);
|
||||
$response = $renderer->render($entries, $query);
|
||||
|
||||
|
||||
@@ -168,7 +168,7 @@ final class InvoiceController extends AbstractController
|
||||
$query->setCustomers([$customer]);
|
||||
$model = $this->service->createModel($query);
|
||||
|
||||
return $this->service->renderInvoiceWithModel($model, $this->dispatcher);
|
||||
return $this->service->renderInvoiceWithModel($model, $this->dispatcher, true);
|
||||
} catch (Exception $ex) {
|
||||
$this->logException($ex);
|
||||
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
|
||||
|
||||
18
src/Export/Base/DispositionInlineInterface.php
Normal file
18
src/Export/Base/DispositionInlineInterface.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Export\Base;
|
||||
|
||||
/**
|
||||
* @deprecated remove me in 2.0
|
||||
*/
|
||||
interface DispositionInlineInterface
|
||||
{
|
||||
public function setDispositionInline(bool $useInlineDisposition): void;
|
||||
}
|
||||
34
src/Export/Base/DispositionInlineTrait.php
Normal file
34
src/Export/Base/DispositionInlineTrait.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Export\Base;
|
||||
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
trait DispositionInlineTrait
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
||||
|
||||
public function getDisposition(): string
|
||||
{
|
||||
return $this->disposition;
|
||||
}
|
||||
|
||||
public function setDispositionInline(bool $useInlineDisposition): void
|
||||
{
|
||||
if ($useInlineDisposition) {
|
||||
$this->disposition = ResponseHeaderBag::DISPOSITION_INLINE;
|
||||
} else {
|
||||
$this->disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,12 @@ use App\Repository\Query\TimesheetQuery;
|
||||
use App\Utils\FileHelper;
|
||||
use App\Utils\HtmlToPdfConverter;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Twig\Environment;
|
||||
|
||||
class PDFRenderer
|
||||
class PDFRenderer implements DispositionInlineInterface
|
||||
{
|
||||
use RendererTrait;
|
||||
use DispositionInlineTrait;
|
||||
|
||||
/**
|
||||
* @var Environment
|
||||
@@ -125,7 +125,7 @@ class PDFRenderer
|
||||
|
||||
$filename = FileHelper::convertToAsciiFilename($filename);
|
||||
|
||||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.pdf');
|
||||
$disposition = $response->headers->makeDisposition($this->getDisposition(), $filename . '.pdf');
|
||||
|
||||
$response->headers->set('Content-Type', 'application/pdf');
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
@@ -10,16 +10,19 @@
|
||||
namespace App\Invoice\Renderer;
|
||||
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Export\Base\DispositionInlineInterface;
|
||||
use App\Export\Base\DispositionInlineTrait;
|
||||
use App\Export\ExportContext;
|
||||
use App\Invoice\InvoiceFilename;
|
||||
use App\Invoice\InvoiceModel;
|
||||
use App\Utils\HtmlToPdfConverter;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Twig\Environment;
|
||||
|
||||
final class PdfRenderer extends AbstractTwigRenderer
|
||||
final class PdfRenderer extends AbstractTwigRenderer implements DispositionInlineInterface
|
||||
{
|
||||
use DispositionInlineTrait;
|
||||
|
||||
/**
|
||||
* @var HtmlToPdfConverter
|
||||
*/
|
||||
@@ -58,7 +61,7 @@ final class PdfRenderer extends AbstractTwigRenderer
|
||||
|
||||
$response = new Response($content);
|
||||
|
||||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.pdf');
|
||||
$disposition = $response->headers->makeDisposition($this->getDisposition(), $filename . '.pdf');
|
||||
|
||||
$response->headers->set('Content-Type', 'application/pdf');
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
@@ -17,6 +17,7 @@ use App\Event\InvoiceCreatedEvent;
|
||||
use App\Event\InvoiceDeleteEvent;
|
||||
use App\Event\InvoicePostRenderEvent;
|
||||
use App\Event\InvoicePreRenderEvent;
|
||||
use App\Export\Base\DispositionInlineInterface;
|
||||
use App\Repository\InvoiceDocumentRepository;
|
||||
use App\Repository\InvoiceRepository;
|
||||
use App\Repository\Query\InvoiceQuery;
|
||||
@@ -309,7 +310,7 @@ final class ServiceInvoice
|
||||
}
|
||||
}
|
||||
|
||||
public function renderInvoiceWithModel(InvoiceModel $model, EventDispatcherInterface $dispatcher): Response
|
||||
public function renderInvoiceWithModel(InvoiceModel $model, EventDispatcherInterface $dispatcher, bool $dispositionInline = false): Response
|
||||
{
|
||||
$document = $this->getDocumentByName($model->getTemplate()->getRenderer());
|
||||
if (null === $document) {
|
||||
@@ -320,6 +321,10 @@ final class ServiceInvoice
|
||||
if ($renderer->supports($document)) {
|
||||
$dispatcher->dispatch(new InvoicePreRenderEvent($model, $document, $renderer));
|
||||
|
||||
if ($renderer instanceof DispositionInlineInterface) {
|
||||
$renderer->setDispositionInline($dispositionInline);
|
||||
}
|
||||
|
||||
$response = $renderer->render($document, $model);
|
||||
|
||||
$dispatcher->dispatch(new InvoicePostRenderEvent($model, $document, $renderer, $response));
|
||||
|
||||
@@ -15,6 +15,7 @@ use App\Tests\Mocks\FileHelperFactory;
|
||||
use App\Utils\HtmlToPdfConverter;
|
||||
use App\Utils\MPdfConverter;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
@@ -25,6 +26,22 @@ use Twig\Environment;
|
||||
*/
|
||||
class PdfRendererTest extends AbstractRendererTest
|
||||
{
|
||||
public function testDisposition()
|
||||
{
|
||||
$sut = new PDFRenderer(
|
||||
$this->createMock(Environment::class),
|
||||
$this->createMock(HtmlToPdfConverter::class),
|
||||
$this->createMock(ProjectStatisticService::class)
|
||||
);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
$sut->setDispositionInline(false);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
$sut->setDispositionInline(true);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_INLINE, $sut->getDisposition());
|
||||
$sut->setDispositionInline(false);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
}
|
||||
|
||||
public function testConfiguration()
|
||||
{
|
||||
$sut = new PDFRenderer(
|
||||
|
||||
@@ -14,7 +14,9 @@ use App\Tests\Mocks\FileHelperFactory;
|
||||
use App\Utils\MPdfConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
/**
|
||||
@@ -26,10 +28,22 @@ class PdfRendererTest extends KernelTestCase
|
||||
{
|
||||
use RendererTestTrait;
|
||||
|
||||
public function testDisposition()
|
||||
{
|
||||
$env = new Environment(new ArrayLoader([]));
|
||||
$sut = new PdfRenderer($env, $this->createMock(MPdfConverter::class));
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
$sut->setDispositionInline(false);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
$sut->setDispositionInline(true);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_INLINE, $sut->getDisposition());
|
||||
$sut->setDispositionInline(false);
|
||||
$this->assertEquals(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sut->getDisposition());
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$loader = new FilesystemLoader();
|
||||
$env = new Environment($loader);
|
||||
$env = new Environment(new ArrayLoader([]));
|
||||
$sut = new PdfRenderer($env, $this->createMock(MPdfConverter::class));
|
||||
$this->assertTrue($sut->supports($this->getInvoiceDocument('default.pdf.twig', true)));
|
||||
$this->assertTrue($sut->supports($this->getInvoiceDocument('freelancer.pdf.twig')));
|
||||
|
||||
Reference in New Issue
Block a user