move pdf download to pdf models, use different (changeable) filenames for every document
Cette révision appartient à :
@ -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;
|
||||
}
|
||||
}
|
@ -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"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Référencer dans un nouveau ticket
Bloquer un utilisateur