move pdf download to pdf models, use different (changeable) filenames for every document

This commit is contained in:
Daniel Seifert 2020-06-04 10:31:38 +02:00
parent 0f03cdfe84
commit 68171b86de
11 changed files with 122 additions and 44 deletions

View File

@ -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);
}
/**

View File

@ -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;
}
}

View File

@ -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"
]
);
}
/**

View File

@ -37,6 +37,14 @@ class deliverynotePdf extends pdfdocumentsOrder
return "ORDER_OVERVIEW_PDF_DNOTE";
}
/**
* @return string
*/
public function getTypeForFilename()
{
return 'delnote';
}
/**
* @return string
*/

View File

@ -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
);
}
}

View File

@ -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();
}

View File

@ -30,4 +30,9 @@ interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface
* @return Order
*/
public function getOrder();
/**
* @return string
*/
public function getTypeForFilename();
}

View File

@ -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;

View File

@ -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);
}
$generator = oxNew( orderOverviewPdfGenerator::class );
$generator->generatePdf($oOrder, Registry::getRequest()->getRequestEscapedParameter("pdflanguage"));
}
}
}

View File

@ -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

View File

@ -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;