295 lines
9.1 KiB
PHP
295 lines
9.1 KiB
PHP
<?php
|
|
/**
|
|
* For the full copyright and license information, please view the
|
|
* docs/licenses/LICENSE.txt file that was distributed with this source code.
|
|
*/
|
|
class PDFCore
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
public $filename;
|
|
|
|
/**
|
|
* @var PDFGenerator
|
|
*/
|
|
public $pdf_renderer;
|
|
|
|
/**
|
|
* @var PrestaShopCollection|ObjectModel|array
|
|
*/
|
|
public $objects;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
public $template;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
public $send_bulk_flag = false;
|
|
|
|
/**
|
|
* @var Smarty
|
|
*/
|
|
protected $smarty;
|
|
|
|
public const TEMPLATE_INVOICE = 'Invoice';
|
|
public const TEMPLATE_ORDER_RETURN = 'OrderReturn';
|
|
public const TEMPLATE_ORDER_SLIP = 'OrderSlip';
|
|
public const TEMPLATE_DELIVERY_SLIP = 'DeliverySlip';
|
|
|
|
/** @deprecated since 9.0 and will be removed in 10.0 **/
|
|
public const TEMPLATE_SUPPLY_ORDER_FORM = 'SupplyOrderForm';
|
|
|
|
/**
|
|
* @param PrestaShopCollection|ObjectModel|array $objects
|
|
* @param string $template
|
|
* @param Smarty $smarty
|
|
* @param string $orientation
|
|
*/
|
|
public function __construct($objects, $template, $smarty, $orientation = 'P')
|
|
{
|
|
$pdfRendererFromModules = $this->getPdfRendererFromModules($template, $orientation);
|
|
|
|
// if no module wants to provide a pdf renderer, then the core feature is used
|
|
if (null === $pdfRendererFromModules) {
|
|
$this->pdf_renderer = new PDFGenerator((bool) Configuration::get('PS_PDF_USE_CACHE'), $orientation);
|
|
} else {
|
|
$this->pdf_renderer = $pdfRendererFromModules;
|
|
}
|
|
|
|
$this->template = $template;
|
|
|
|
/*
|
|
* We need a Smarty instance that does NOT escape HTML.
|
|
* Since in BO Smarty does not autoescape
|
|
* and in FO Smarty does autoescape, we use
|
|
* a new Smarty of which we're sure it does not escape
|
|
* the HTML.
|
|
*/
|
|
$this->smarty = clone $smarty;
|
|
$this->smarty->escape_html = false;
|
|
|
|
/* We need to get the old instance of the LazyRegister
|
|
* because some of the functions are already defined
|
|
* and we need to check in the old one first
|
|
*/
|
|
$original_lazy_register = SmartyLazyRegister::getInstance($smarty);
|
|
|
|
/* For PDF we restore some functions from Smarty
|
|
* they've been removed in PrestaShop 1.7 so
|
|
* new themes don't use them. Although PDF haven't been
|
|
* reworked so every PDF controller must extend this class.
|
|
*/
|
|
smartyRegisterFunction($this->smarty, 'function', 'convertPrice', ['Product', 'convertPrice'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'convertPriceWithCurrency', ['Product', 'convertPriceWithCurrency'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'displayWtPrice', ['Product', 'displayWtPrice'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'displayWtPriceWithCurrency', ['Product', 'displayWtPriceWithCurrency'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'displayPrice', ['Tools', 'displayPriceSmarty'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'modifier', 'convertAndFormatPrice', ['Product', 'convertAndFormatPrice'], true, $original_lazy_register); // used twice
|
|
smartyRegisterFunction($this->smarty, 'function', 'displayAddressDetail', ['AddressFormat', 'generateAddressSmarty'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'getWidthSize', ['Image', 'getWidth'], true, $original_lazy_register);
|
|
smartyRegisterFunction($this->smarty, 'function', 'getHeightSize', ['Image', 'getHeight'], true, $original_lazy_register);
|
|
|
|
$this->objects = $objects;
|
|
if (!($objects instanceof Iterator) && !is_array($objects)) {
|
|
$this->objects = [$objects];
|
|
}
|
|
|
|
if (count($this->objects) > 1) { // when bulk mode only
|
|
$this->send_bulk_flag = true;
|
|
}
|
|
|
|
$this->setFilename();
|
|
}
|
|
|
|
/**
|
|
* Render PDF.
|
|
*
|
|
* @param bool $display
|
|
*
|
|
* @return string|void
|
|
*
|
|
* @throws PrestaShopException
|
|
*/
|
|
public function render($display = true)
|
|
{
|
|
$render = false;
|
|
$this->pdf_renderer->setFontForLang(Context::getContext()->language->iso_code);
|
|
foreach ($this->objects as $object) {
|
|
$this->pdf_renderer->startPageGroup();
|
|
$template = $this->getTemplateObject($object);
|
|
if (!$template) {
|
|
continue;
|
|
}
|
|
|
|
$template->assignHookData($object);
|
|
|
|
$this->pdf_renderer->createHeader($template->getHeader());
|
|
$this->pdf_renderer->createPagination($template->getPagination());
|
|
$this->pdf_renderer->createContent($template->getContent());
|
|
$this->pdf_renderer->writePage();
|
|
// The footer must be added after adding the page, or TCPDF will
|
|
// add the footer for the next page from on the last page of this
|
|
// page group, which could mean the wrong store info is rendered.
|
|
$this->pdf_renderer->createFooter($template->getFooter());
|
|
$render = true;
|
|
|
|
unset($template);
|
|
}
|
|
|
|
if ($render) {
|
|
// clean the output buffer
|
|
if (ob_get_level() && ob_get_length() > 0) {
|
|
ob_clean();
|
|
}
|
|
|
|
return $this->pdf_renderer->render($this->getFilename(), $display);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get correct PDF template classes.
|
|
*
|
|
* @param mixed $object
|
|
*
|
|
* @return HTMLTemplate|false
|
|
*
|
|
* @throws PrestaShopException
|
|
*/
|
|
public function getTemplateObject($object)
|
|
{
|
|
$class = false;
|
|
$class_name = 'HTMLTemplate' . $this->template;
|
|
|
|
$templateObjectFromModule = $this->getTemplateObjectFromModules($object, $this->smarty, $this->send_bulk_flag, $this->template);
|
|
|
|
if (false === $templateObjectFromModule && class_exists($class_name)) {
|
|
// Some HTMLTemplateXYZ implementations won't use the third param but this is not a problem (no warning in PHP),
|
|
// the third param is then ignored if not added to the method signature.
|
|
$class = new $class_name($object, $this->smarty, $this->send_bulk_flag);
|
|
}
|
|
|
|
if (!($class instanceof HTMLTemplate)) {
|
|
throw new PrestaShopException('Invalid class. It should be an instance of HTMLTemplate');
|
|
}
|
|
|
|
return $class;
|
|
}
|
|
|
|
/**
|
|
* Get the PDF filename based on the objects.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFilename(): string
|
|
{
|
|
if (empty($this->filename)) {
|
|
$this->setFilename();
|
|
}
|
|
|
|
return $this->filename;
|
|
}
|
|
|
|
/**
|
|
* Set the PDF filename based on the objects.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function setFilename(): bool
|
|
{
|
|
$bulk = (1 < count($this->objects));
|
|
|
|
foreach ($this->objects as $object) {
|
|
$template = $this->getTemplateObject($object);
|
|
if (!$template) {
|
|
continue;
|
|
}
|
|
|
|
if ($bulk) {
|
|
$this->filename = $template->getBulkFilename();
|
|
} else {
|
|
$this->filename = $template->getFilename();
|
|
}
|
|
|
|
if (!empty($this->filename)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return !empty($this->filename);
|
|
}
|
|
|
|
/**
|
|
* Get the template object from modules.
|
|
*
|
|
* @param mixed $object
|
|
* @param Smarty $smarty
|
|
* @param bool $send_bulk_flag
|
|
* @param string $template
|
|
*
|
|
* @return HTMLTemplate|false
|
|
*/
|
|
private function getTemplateObjectFromModules($object, $smarty, $send_bulk_flag, $template)
|
|
{
|
|
$templateObjects = Hook::exec(
|
|
'actionGetPdfTemplateObject',
|
|
[
|
|
'object' => $object,
|
|
'smarty' => $smarty,
|
|
'send_bulk_flag' => $send_bulk_flag,
|
|
'template' => $template,
|
|
],
|
|
null,
|
|
true
|
|
);
|
|
|
|
if (!is_array($templateObjects)) {
|
|
$templateObjects = [];
|
|
}
|
|
|
|
foreach ($templateObjects as $templateObject) {
|
|
if ($templateObject instanceof HTMLTemplate) {
|
|
return $templateObject;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the PDF renderer from modules.
|
|
*
|
|
* @param string $template
|
|
* @param string $orientation
|
|
*
|
|
* @return PDFGenerator|null
|
|
*/
|
|
private function getPdfRendererFromModules($template, $orientation)
|
|
{
|
|
$renderers = Hook::exec(
|
|
'actionGetPdfRenderer',
|
|
[
|
|
'template' => $template,
|
|
'orientation' => $orientation,
|
|
],
|
|
null,
|
|
true
|
|
);
|
|
|
|
if (!is_array($renderers)) {
|
|
$renderers = [];
|
|
}
|
|
|
|
foreach ($renderers as $renderer) {
|
|
if ($renderer instanceof PDFGenerator) {
|
|
return $renderer;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|