From ccb8ffe4033c8d5a85389bb7b9765c9245d0c22a Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Thu, 4 Jun 2020 10:31:38 +0200 Subject: [PATCH] move pdf download to pdf models, use different (changeable) filenames for every document --- .../Controller/orderOverviewPdfGenerator.php | 4 +- .../AbstractClasses/pdfdocumentsGeneric.php | 67 +++++++++++++++---- .../AbstractClasses/pdfdocumentsOrder.php | 16 +++-- .../Model/Documents/deliverynotePdf.php | 8 +++ Application/Model/Documents/invoicePdf.php | 24 +++++++ .../pdfdocumentsGenericInterface.php | 10 +++ .../Interfaces/pdfdocumentsOrderInterface.php | 5 ++ IntelliSenseHelper.php | 2 +- .../d3_overview_controller_pdfdocuments.php | 27 ++------ .../Model/d3_Order_PdfDocuments.php | 1 + metadata.php | 2 +- 11 files changed, 122 insertions(+), 44 deletions(-) rename Modules/Application/{controllers => Controller}/d3_overview_controller_pdfdocuments.php (61%) diff --git a/Application/Controller/orderOverviewPdfGenerator.php b/Application/Controller/orderOverviewPdfGenerator.php index 9749cd8..a529b3b 100644 --- a/Application/Controller/orderOverviewPdfGenerator.php +++ b/Application/Controller/orderOverviewPdfGenerator.php @@ -34,12 +34,12 @@ class orderOverviewPdfGenerator * @throws noBaseObjectSetException * @throws pdfGeneratorExceptionAbstract */ - public function generatePdf(Order $order, $sFilename, $iSelLang = 0, $target = 'I') + public function generatePdf(Order $order, $iSelLang = 0) { $Pdf= $this->getPdfClass(); $Pdf->setOrder($order); - $Pdf->genPdf($sFilename, $iSelLang, $target); + $Pdf->downloadPdf($iSelLang); } /** diff --git a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php index c091b69..bcf5527 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php +++ b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php @@ -18,13 +18,18 @@ namespace D3\PdfDocuments\Application\Model\AbstractClasses; use D3\PdfDocuments\Application\Model\Exceptions\noBaseObjectSetException; +use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface; +use OxidEsales\Eshop\Core\Base; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\UtilsView; use Smarty; use Spipu\Html2Pdf\Html2Pdf; -abstract class pdfdocumentsGeneric implements genericInterface +abstract class pdfdocumentsGeneric extends Base implements genericInterface { + const PDF_DOWNLOAD = 'I'; + /** @var Smarty */ public $oSmarty; @@ -33,6 +38,8 @@ abstract class pdfdocumentsGeneric implements genericInterface */ public function __construct() { + parent::__construct(); + /** @var Smarty $oSmarty */ $this->oSmarty = Registry::getUtilsView()->getSmarty(); } @@ -45,12 +52,33 @@ abstract class pdfdocumentsGeneric implements genericInterface */ public function genPdf($sFilename, $iSelLang = 0, $target = 'I') { - $sFilename = $this->getFilename( $sFilename); + $sFilename = $this->getFilename(); $oPdf = oxNew(Html2Pdf::class, ...$this->getPdfProperties()); $oPdf->writeHTML($this->getHTMLContent($iSelLang)); $oPdf->output($sFilename, $target); } + public function downloadPdf($iLanguage = 0) + { + try { + $oUtils = Registry::getUtils(); + $sFilename = $this->makeValidFileName($this->getFilename()); + ob_start(); + $this->genPdf($sFilename, $iLanguage, self::PDF_DOWNLOAD); + $sPDF = ob_get_contents(); + ob_end_clean(); + $oUtils->setHeader("Pragma: public"); + $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + $oUtils->setHeader("Expires: 0"); + $oUtils->setHeader("Content-type: application/pdf"); + $oUtils->setHeader("Content-Disposition: attachment; filename=" . $sFilename); + Registry::getUtils()->showMessageAndExit($sPDF); + } catch (pdfGeneratorExceptionAbstract $e) { + Registry::get(UtilsView::class)->addErrorToDisplay($e); + Registry::getLogger()->error($e); + } + } + public function setSmartyVars() { $this->oSmarty->assign('oConfig', Registry::getSession()->getConfig()); @@ -59,16 +87,6 @@ abstract class pdfdocumentsGeneric implements genericInterface $this->oSmarty->assign('lang', Registry::getLang()); } - /** - * @param string $sFilename - * - * @return string - */ - public function getFilename($sFilename) - { - return $sFilename; - } - /** * @param int $iSelLang * @@ -76,6 +94,8 @@ abstract class pdfdocumentsGeneric implements genericInterface */ public function getHTMLContent($iSelLang = 0) { + self::$_blIsAdmin = $this->renderTemplateFromAdmin(); + $lang = Registry::getLang(); $currTplLang = $lang->getTplLanguage(); @@ -105,4 +125,27 @@ abstract class pdfdocumentsGeneric implements genericInterface { return ['P', 'A4', 'de']; } + + /** + * Gets proper file name + * + * @param string $sFilename file name + * + * @return string + */ + public function makeValidFileName($sFilename) + { + $sFilename = preg_replace('/[\s]+/', '_', $sFilename); + $sFilename = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $sFilename); + + return str_replace(' ', '_', $sFilename); + } + + /** + * @return bool + */ + public function renderTemplateFromAdmin() + { + return false; + } } \ No newline at end of file diff --git a/Application/Model/AbstractClasses/pdfdocumentsOrder.php b/Application/Model/AbstractClasses/pdfdocumentsOrder.php index 1e0125f..b719dd8 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsOrder.php +++ b/Application/Model/AbstractClasses/pdfdocumentsOrder.php @@ -64,14 +64,18 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt * * @return string */ - public function getFilename($sFilename) + public function getFilename() { - $sFilename = parent::getFilename( $sFilename); + $sTrimmedBillName = trim($this->getOrder()->getFieldData('oxbilllname')); - $ordernr = $this->getOrder()->getFieldData('oxordernr'); - $billnr = $this->getOrder()->getFieldData('oxbillnr');; - - return str_replace($ordernr, $billnr, $sFilename); + return implode( + '_', + [ + $this->getTypeForFilename(), + $this->getOrder()->getFieldData('oxordernr'), + $sTrimmedBillName . ".pdf" + ] + ); } /** diff --git a/Application/Model/Documents/deliverynotePdf.php b/Application/Model/Documents/deliverynotePdf.php index 63b832f..5af3b11 100644 --- a/Application/Model/Documents/deliverynotePdf.php +++ b/Application/Model/Documents/deliverynotePdf.php @@ -37,6 +37,14 @@ class deliverynotePdf extends pdfdocumentsOrder return "ORDER_OVERVIEW_PDF_DNOTE"; } + /** + * @return string + */ + public function getTypeForFilename() + { + return 'delnote'; + } + /** * @return string */ diff --git a/Application/Model/Documents/invoicePdf.php b/Application/Model/Documents/invoicePdf.php index a78c059..7d48e6a 100644 --- a/Application/Model/Documents/invoicePdf.php +++ b/Application/Model/Documents/invoicePdf.php @@ -42,6 +42,14 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn return "ORDER_OVERVIEW_PDF_STANDART"; } + /** + * @return string + */ + public function getTypeForFilename() + { + return 'invoice'; + } + /** * @param $sFilename * @param int $iSelLang @@ -88,4 +96,20 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn public function getTemplate(){ return 'd3invoice_pdf.tpl'; } + + /** + * @param string $sFilename + * + * @return string + */ + public function getFilename() + { + $filename = parent::getFilename(); + + return str_replace( + $this->getOrder()->getFieldData('oxordernr'), + $this->getOrder()->getFieldData('oxbillnr'), + $filename + ); + } } \ No newline at end of file diff --git a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php index 1009d0d..acc0751 100644 --- a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php +++ b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php @@ -41,6 +41,11 @@ interface pdfdocumentsGenericInterface */ public function getHTMLContent(); + /** + * @param int $iLanguage + */ + public function downloadPdf($iLanguage = 0); + /** * @param $sFilename * @param int $iSelLang @@ -50,4 +55,9 @@ interface pdfdocumentsGenericInterface * @throws noBaseObjectSetException */ public function genPdf($sFilename, $iSelLang = 0, $target = 'I'); + + /** + * @return string + */ + public function getFilename(); } \ No newline at end of file diff --git a/Application/Model/Interfaces/pdfdocumentsOrderInterface.php b/Application/Model/Interfaces/pdfdocumentsOrderInterface.php index dfa2fdd..d02fbcf 100644 --- a/Application/Model/Interfaces/pdfdocumentsOrderInterface.php +++ b/Application/Model/Interfaces/pdfdocumentsOrderInterface.php @@ -30,4 +30,9 @@ interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface * @return Order */ public function getOrder(); + + /** + * @return string + */ + public function getTypeForFilename(); } \ No newline at end of file diff --git a/IntelliSenseHelper.php b/IntelliSenseHelper.php index 53b1730..0c5fc51 100644 --- a/IntelliSenseHelper.php +++ b/IntelliSenseHelper.php @@ -13,7 +13,7 @@ * @link http://www.oxidmodule.com */ -namespace D3\PdfDocuments\Modules\Application\controllers { +namespace D3\PdfDocuments\Modules\Application\Controller { use OxidEsales\Eshop\Application\Controller\Admin\OrderOverview; diff --git a/Modules/Application/controllers/d3_overview_controller_pdfdocuments.php b/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php similarity index 61% rename from Modules/Application/controllers/d3_overview_controller_pdfdocuments.php rename to Modules/Application/Controller/d3_overview_controller_pdfdocuments.php index 80662f2..dbdcd29 100644 --- a/Modules/Application/controllers/d3_overview_controller_pdfdocuments.php +++ b/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php @@ -15,8 +15,9 @@ * @link http://www.oxidmodule.com */ -namespace D3\PdfDocuments\Modules\Application\controllers; +namespace D3\PdfDocuments\Modules\Application\Controller; +use D3\PdfDocuments\Application\Controller\orderOverviewPdfGenerator; use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract; use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; use D3\PdfDocuments\Modules\Application\Model\d3_Order_PdfDocuments; @@ -50,27 +51,9 @@ class d3_overview_controller_pdfdocuments extends d3_overview_controller_pdfdocu /** @var d3_Order_PdfDocuments $oOrder */ $oOrder = oxNew(Order::class); if ($oOrder->load($soxId)) { - try { - self::$_blIsAdmin = 0; - $oUtils = Registry::getUtils(); - $sTrimmedBillName = trim($oOrder->oxorder__oxbilllname->getRawValue()); - //oxbillr nicht so eingeschrieben lassen - $sFilename = $oOrder->oxorder__oxbillnr->value . "_" . $sTrimmedBillName . ".pdf"; - $sFilename = $this->makeValidFileName($sFilename); - ob_start(); - $oOrder->genPdf($sFilename, Registry::getConfig()->getRequestParameter("pdflanguage")); - $sPDF = ob_get_contents(); - ob_end_clean(); - $oUtils->setHeader("Pragma: public"); - $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0"); - $oUtils->setHeader("Expires: 0"); - $oUtils->setHeader("Content-type: application/pdf"); - $oUtils->setHeader("Content-Disposition: attachment; filename=" . $sFilename); - Registry::getUtils()->showMessageAndExit($sPDF); - } catch (pdfGeneratorExceptionAbstract $e) { - Registry::get(UtilsView::class)->addErrorToDisplay($e); - Registry::getLogger()->error($e); - } + self::$_blIsAdmin = 0; + $generator = oxNew( orderOverviewPdfGenerator::class ); + $generator->generatePdf($oOrder, Registry::getRequest()->getRequestEscapedParameter("pdflanguage")); } } } diff --git a/Modules/Application/Model/d3_Order_PdfDocuments.php b/Modules/Application/Model/d3_Order_PdfDocuments.php index bbb2ee2..2df74d0 100644 --- a/Modules/Application/Model/d3_Order_PdfDocuments.php +++ b/Modules/Application/Model/d3_Order_PdfDocuments.php @@ -25,6 +25,7 @@ use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract; class d3_Order_PdfDocuments extends d3_Order_PdfDocuments_parent { /** + * compatibility to OXID Invoice PDF module * @param string $sFilename * @param int $iSelLang * @param string $target diff --git a/metadata.php b/metadata.php index 3e60cac..1465531 100644 --- a/metadata.php +++ b/metadata.php @@ -16,7 +16,7 @@ */ use D3\ModCfg\Application\Model\d3utils; -use D3\PdfDocuments\Modules\Application\controllers\d3_overview_controller_pdfdocuments; +use D3\PdfDocuments\Modules\Application\Controller\d3_overview_controller_pdfdocuments; use D3\PdfDocuments\Modules\Application\Model\d3_Order_PdfDocuments as d3_pdfdocs_OrderModel; use OxidEsales\Eshop\Application\Controller\Admin\OrderOverview; use OxidEsales\Eshop\Application\Model as OxidModel;