2020-05-26 16:32:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
*
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
*
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Max Buhe <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\PdfDocuments\Application\Model\AbstractClasses;
|
|
|
|
|
2020-06-04 10:31:38 +02:00
|
|
|
use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract;
|
2020-05-30 01:17:27 +02:00
|
|
|
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface;
|
2020-06-04 10:31:38 +02:00
|
|
|
use OxidEsales\Eshop\Core\Base;
|
2020-05-26 16:32:22 +02:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2020-06-04 10:31:38 +02:00
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2020-05-27 09:46:44 +02:00
|
|
|
use Smarty;
|
2020-06-04 15:18:51 +02:00
|
|
|
use Spipu\Html2Pdf\Exception\Html2PdfException;
|
2020-05-26 16:32:22 +02:00
|
|
|
use Spipu\Html2Pdf\Html2Pdf;
|
|
|
|
|
2020-06-04 10:31:38 +02:00
|
|
|
abstract class pdfdocumentsGeneric extends Base implements genericInterface
|
2020-05-26 16:32:22 +02:00
|
|
|
{
|
2020-06-09 14:56:14 +02:00
|
|
|
const PDF_DESTINATION_DOWNLOAD = 'D'; // force download in browser
|
|
|
|
const PDF_DESTINATION_STDOUT = 'I'; // show in browser plugin if available, otherwise download
|
|
|
|
const PDF_DESTINATION_FILE = 'F'; // save as local file
|
|
|
|
const PDF_DESTINATION_FILEANDSTDOUT = 'FI'; // output as local file and show in browser plugin
|
|
|
|
const PDF_DESTINATION_FILEANDDOWNLOAD = 'FD'; // output as local file and force download in browser
|
|
|
|
const PDF_DESTINATION_STRING = 'S'; // output as string
|
2020-06-04 15:18:51 +02:00
|
|
|
|
|
|
|
const PDF_ORIENTATION_PORTRAIT = 'P';
|
|
|
|
const PDF_ORIENTATION_LANDSCAPE = 'L';
|
2020-06-04 10:31:38 +02:00
|
|
|
|
2020-06-05 10:06:58 +02:00
|
|
|
public $filenameExtension = 'pdf';
|
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
/** @var Smarty */
|
|
|
|
public $oSmarty;
|
|
|
|
|
2020-06-05 09:37:11 +02:00
|
|
|
/** @var string */
|
|
|
|
public $filename;
|
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
/**
|
2020-05-30 01:17:27 +02:00
|
|
|
* pdfDocumentsGeneric constructor.
|
2020-05-28 12:21:11 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-06-04 10:31:38 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
/** @var Smarty $oSmarty */
|
|
|
|
$this->oSmarty = Registry::getUtilsView()->getSmarty();
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:46:44 +02:00
|
|
|
/**
|
|
|
|
* @param $sFilename
|
|
|
|
* @param int $iSelLang
|
|
|
|
* @param string $target
|
2020-06-09 14:56:14 +02:00
|
|
|
*
|
|
|
|
* @return mixed|string
|
2020-06-04 15:18:51 +02:00
|
|
|
* @throws Html2PdfException
|
2020-05-27 09:46:44 +02:00
|
|
|
*/
|
2020-06-04 15:18:51 +02:00
|
|
|
public function genPdf($sFilename, $iSelLang = 0, $target = self::PDF_DESTINATION_STDOUT)
|
2020-05-27 09:46:44 +02:00
|
|
|
{
|
2020-05-28 12:21:11 +02:00
|
|
|
$oPdf = oxNew(Html2Pdf::class, ...$this->getPdfProperties());
|
2020-06-09 22:49:47 +02:00
|
|
|
$oPdf->setTestIsImage(false);
|
2020-05-27 09:46:44 +02:00
|
|
|
$oPdf->writeHTML($this->getHTMLContent($iSelLang));
|
2020-06-09 22:49:47 +02:00
|
|
|
$oPdf->pdf->SetAuthor(Registry::getConfig()->getActiveShop()->getFieldData('oxname'));
|
|
|
|
$oPdf->pdf->SetTitle(Registry::getLang()->translateString($this->getTitleIdent()));
|
|
|
|
$oPdf->pdf->SetCreator('D³ PDF Documents for OXID eShop');
|
|
|
|
$oPdf->pdf->SetSubject(NULL);
|
2020-06-08 16:39:33 +02:00
|
|
|
return $oPdf->output($sFilename, $target);
|
2020-05-27 09:46:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:18:51 +02:00
|
|
|
/**
|
|
|
|
* @param int $iLanguage
|
|
|
|
*/
|
2020-06-04 10:31:38 +02:00
|
|
|
public function downloadPdf($iLanguage = 0)
|
|
|
|
{
|
|
|
|
try {
|
2020-06-04 15:18:51 +02:00
|
|
|
$sFilename = $this->getFilename();
|
|
|
|
$this->genPdf($sFilename, $iLanguage, self::PDF_DESTINATION_DOWNLOAD);
|
|
|
|
Registry::getUtils()->showMessageAndExit('');
|
2020-06-04 10:31:38 +02:00
|
|
|
} catch (pdfGeneratorExceptionAbstract $e) {
|
|
|
|
Registry::get(UtilsView::class)->addErrorToDisplay($e);
|
|
|
|
Registry::getLogger()->error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:56:14 +02:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param int $iLanguage
|
|
|
|
*
|
|
|
|
* @throws Html2PdfException
|
|
|
|
*/
|
|
|
|
public function savePdfFile($path, $iLanguage = 0)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$sFilename = $this->getFilename();
|
|
|
|
$this->genPdf(
|
|
|
|
rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$sFilename,
|
|
|
|
$iLanguage,
|
|
|
|
self::PDF_DESTINATION_FILE
|
|
|
|
);
|
|
|
|
} catch (pdfGeneratorExceptionAbstract $e) {
|
|
|
|
Registry::get(UtilsView::class)->addErrorToDisplay($e);
|
|
|
|
Registry::getLogger()->error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:39:33 +02:00
|
|
|
/**
|
|
|
|
* @param int $iLanguage
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
* @throws Html2PdfException
|
|
|
|
*/
|
|
|
|
public function getPdfContent($iLanguage = 0)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$sFilename = $this->getFilename();
|
2020-06-09 14:56:14 +02:00
|
|
|
return $this->genPdf( $sFilename, $iLanguage, self::PDF_DESTINATION_STRING );
|
2020-06-08 16:39:33 +02:00
|
|
|
} catch (pdfGeneratorExceptionAbstract $e) {
|
|
|
|
Registry::get(UtilsView::class)->addErrorToDisplay($e);
|
|
|
|
Registry::getLogger()->error($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
public function setSmartyVars()
|
2020-05-27 09:46:44 +02:00
|
|
|
{
|
2020-05-28 12:21:11 +02:00
|
|
|
$this->oSmarty->assign('oConfig', Registry::getSession()->getConfig());
|
|
|
|
$this->oSmarty->assign('oViewConf', Registry::getSession()->getConfig()->getActiveView()->getViewConfig());
|
|
|
|
$this->oSmarty->assign('shop', Registry::getSession()->getConfig()->getActiveShop());
|
|
|
|
$this->oSmarty->assign('lang', Registry::getLang());
|
2020-05-27 09:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $iSelLang
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getHTMLContent($iSelLang = 0)
|
|
|
|
{
|
2020-06-09 22:49:47 +02:00
|
|
|
$blCurrentRenderFromAdmin = self::$_blIsAdmin;
|
2020-06-04 10:31:38 +02:00
|
|
|
self::$_blIsAdmin = $this->renderTemplateFromAdmin();
|
|
|
|
|
2020-05-27 09:46:44 +02:00
|
|
|
$lang = Registry::getLang();
|
|
|
|
|
|
|
|
$currTplLang = $lang->getTplLanguage();
|
|
|
|
$lang->setTplLanguage($iSelLang);
|
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
$this->setSmartyVars();
|
2020-05-27 09:46:44 +02:00
|
|
|
|
2020-05-28 12:21:11 +02:00
|
|
|
$content = $this->oSmarty->fetch($this->getTemplate());
|
2020-05-27 09:46:44 +02:00
|
|
|
|
|
|
|
$lang->setTplLanguage($currTplLang);
|
|
|
|
|
2020-06-09 22:49:47 +02:00
|
|
|
self::$_blIsAdmin = $blCurrentRenderFromAdmin;
|
|
|
|
|
2020-05-27 09:46:44 +02:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* arguments for Html2Pdf class constructor
|
|
|
|
* - $orientation = 'P',
|
|
|
|
* - $format = 'A4',
|
|
|
|
* - $lang = 'fr',
|
|
|
|
* - $unicode = true,
|
|
|
|
* - $encoding = 'UTF-8',
|
|
|
|
* - $margins = array(5, 5, 5, 8),
|
|
|
|
* - $pdfa = false
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getPdfProperties()
|
|
|
|
{
|
2020-06-04 15:18:51 +02:00
|
|
|
return [self::PDF_ORIENTATION_PORTRAIT, 'A4', 'de'];
|
2020-05-27 09:46:44 +02:00
|
|
|
}
|
2020-06-04 10:31:38 +02:00
|
|
|
|
2020-06-05 09:37:11 +02:00
|
|
|
/**
|
|
|
|
* @param $filename
|
|
|
|
*/
|
|
|
|
public function setFilename($filename)
|
|
|
|
{
|
|
|
|
$this->filename = $filename;
|
|
|
|
}
|
|
|
|
|
2020-06-05 10:06:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $sFilename
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilename()
|
|
|
|
{
|
|
|
|
// forced filename from setFilename()
|
|
|
|
if ($this->filename) {
|
2020-06-17 09:05:49 +02:00
|
|
|
return $this->addFilenameExtension(
|
|
|
|
$this->makeValidFileName(
|
|
|
|
$this->filename
|
|
|
|
)
|
|
|
|
);
|
2020-06-05 10:06:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:05:49 +02:00
|
|
|
return $this->addFilenameExtension(
|
|
|
|
$this->makeValidFileName(
|
|
|
|
$this->getTypeForFilename()
|
|
|
|
)
|
|
|
|
);
|
2020-06-05 10:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function addFilenameExtension($filename)
|
|
|
|
{
|
|
|
|
$extension = $this->filenameExtension;
|
|
|
|
$extensionLength = (strlen($extension) + 1) * -1;
|
|
|
|
if ((bool) strlen($extension) && substr($filename, $extensionLength) != '.'.$extension) {
|
|
|
|
$filename .= '.'.$extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:38 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2020-05-26 16:32:22 +02:00
|
|
|
}
|