title as pdf name

This commit is contained in:
aynsix
2022-02-11 12:15:31 +03:00
parent df9380d515
commit 6d3510b8f2
4 changed files with 53 additions and 2 deletions

View File

@@ -11,10 +11,12 @@ namespace Alchemy\Phrasea\Controller\Prod;
use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware; use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware;
use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Controller\RecordsRequest;
use Alchemy\Phrasea\Helper\Record as RecordHelper; use Alchemy\Phrasea\Helper\Record as RecordHelper;
use Alchemy\Phrasea\Out\Module\PDFRecords; use Alchemy\Phrasea\Out\Module\PDFRecords;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class PrinterController extends Controller class PrinterController extends Controller
{ {
@@ -31,9 +33,25 @@ class PrinterController extends Controller
} }
} }
$pdfTitle = '';
$storyId = null;
if ($printer->is_basket()) {
$pdfTitle = $printer->get_original_basket()->getName();
}
$r = RecordsRequest::fromRequest($this->app, $request, false);
if ($r->isSingleStory()) {
$pdfTitle = $r->singleStory()->get_title();
$storyId = $r->singleStory()->getId();
}
return $this->render('prod/actions/printer_default.html.twig', [ return $this->render('prod/actions/printer_default.html.twig', [
'printer' => $printer, 'printer' => $printer,
'message' => '', 'message' => '',
'storyId' => $storyId,
'pdfTitle'=> $pdfTitle,
'basketFeedbackId' => $basketFeedbackId, 'basketFeedbackId' => $basketFeedbackId,
]); ]);
} }
@@ -66,10 +84,23 @@ class PrinterController extends Controller
$PDF = new PDFRecords($this->app, $printer, $layout, $title, $description, $userPassword, $canDownload, $downloadSubdef); $PDF = new PDFRecords($this->app, $printer, $layout, $title, $description, $userPassword, $canDownload, $downloadSubdef);
$pdfName = '';
if (!empty($title)) {
$pdfName = $printer->normalizeString($title);
$pdfName .= '.pdf';
}
$response = new Response($PDF->render(), 200, array('Content-Type' => 'application/pdf')); $response = new Response($PDF->render(), 200, array('Content-Type' => 'application/pdf'));
$response->headers->set('Pragma', 'public', true); $response->headers->set('Pragma', 'public', true);
$response->setMaxAge(0); $response->setMaxAge(0);
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$pdfName
);
$response->headers->set('Content-Disposition', $disposition);
return $response; return $response;
} }

View File

@@ -14,6 +14,8 @@ namespace Alchemy\Phrasea\Helper\Record;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Helper\Record\Helper as RecordHelper; use Alchemy\Phrasea\Helper\Record\Helper as RecordHelper;
use Alchemy\Phrasea\Media\Subdef\Subdef; use Alchemy\Phrasea\Media\Subdef\Subdef;
use Alchemy\Phrasea\Out\Module\PDFRecords;
use Cocur\Slugify\Slugify;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Printer extends RecordHelper class Printer extends RecordHelper
@@ -23,6 +25,7 @@ class Printer extends RecordHelper
private $previewName = 'preview'; private $previewName = 'preview';
private $urlTtl = null; private $urlTtl = null;
private $titleAsDownloadName = true; private $titleAsDownloadName = true;
private $slugify;
/** /**
* @var \ACL * @var \ACL
@@ -211,4 +214,20 @@ class Printer extends RecordHelper
return $this->titleAsDownloadName; return $this->titleAsDownloadName;
} }
public function sanitizeString($string)
{
return str_replace(['/', '\\'], '', $string);
}
public function normalizeString($string)
{
if (!isset($this->slugify)) {
$this->slugify = new Slugify();
}
$string = $this->sanitizeString($string);
return mb_substr($this->slugify->slugify($string, '-'), 0, PDFRecords::$maxFilenameLength);
}
} }

View File

@@ -20,7 +20,7 @@ use \IntlDateFormatter as DateFormatter;
class PDFRecords extends PDF class PDFRecords extends PDF
{ {
private static $maxFilenameLength = 256; public static $maxFilenameLength = 256;
/** @var Printer */ /** @var Printer */
private $printer; private $printer;

View File

@@ -59,7 +59,7 @@
<h3 style="margin-left: 5px;">{{ 'print:: some options' | trans }}</h3> <h3 style="margin-left: 5px;">{{ 'print:: some options' | trans }}</h3>
<div style="margin: 20px 5px;"> <div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf title' | trans }}</h4> <h4 style="margin-left: 0px;">{{ 'print:: pdf title' | trans }}</h4>
<input type="text" value="" name="print-pdf-title" class="print-pdf-title" style="margin-left: 0px;width: 530px;padding-left: 0px;padding-right: 0px;margin-right: 0px;"/> <input type="text" value="{{ pdfTitle }}" name="print-pdf-title" class="print-pdf-title" style="margin-left: 0px;width: 530px;padding-left: 0px;padding-right: 0px;margin-right: 0px;"/>
</div> </div>
<div style="margin: 20px 5px;"> <div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf description' | trans }}</h4> <h4 style="margin-left: 0px;">{{ 'print:: pdf description' | trans }}</h4>
@@ -158,6 +158,7 @@
{% if printer.get_count_thumbnail() > 0 or printer.get_count_preview() > 0 %} {% if printer.get_count_thumbnail() > 0 or printer.get_count_preview() > 0 %}
<div style="margin-bottom: 10px; text-align: center;"> <div style="margin-bottom: 10px; text-align: center;">
<input type="hidden" name="lst" value="{{ printer.get_serialize_list }}" /> <input type="hidden" name="lst" value="{{ printer.get_serialize_list }}" />
<input type="hidden" name="storyId" value="{{ storyId }}" />
<button type="submit" class="btn btn-inverse">{{ 'boutton::imprimer' | trans }}</button> <button type="submit" class="btn btn-inverse">{{ 'boutton::imprimer' | trans }}</button>
</div> </div>
{% endif %} {% endif %}