mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-29 23:46:10 +00:00
template renderer
This commit is contained in:
@@ -105,6 +105,86 @@ class Htmlizer
|
||||
$this->injectableFactory = $injectableFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an HTML for entity by a given template.
|
||||
*
|
||||
* @param $cacheId @deprecated To be skipped..
|
||||
* @param $additionalData Data will be passed to the template.
|
||||
* @param $skipLinks Do not process related records.
|
||||
*/
|
||||
public function render(
|
||||
?Entity $entity,
|
||||
string $template,
|
||||
?string $cacheId = null,
|
||||
?array $additionalData = null,
|
||||
bool $skipLinks = false
|
||||
): string {
|
||||
|
||||
$template = str_replace('<tcpdf ', '', $template);
|
||||
|
||||
$helpers = $this->getHelpers();
|
||||
|
||||
$code = LightnCandy::compile($template, [
|
||||
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION,
|
||||
'helpers' => $this->getHelpers(),
|
||||
]);
|
||||
|
||||
$renderer = LightnCandy::prepare($code);
|
||||
|
||||
if ($additionalData === null) {
|
||||
$additionalData = $additionalData ?? [];
|
||||
}
|
||||
|
||||
$data = $entity ?
|
||||
$this->getDataFromEntity($entity, $skipLinks, 0, $template, $additionalData) :
|
||||
$additionalData;
|
||||
|
||||
if (!array_key_exists('today', $data)) {
|
||||
$data['today'] = $this->dateTime->getTodayString();
|
||||
$data['today_RAW'] = date('Y-m-d');
|
||||
}
|
||||
|
||||
if (!array_key_exists('now', $data)) {
|
||||
$data['now'] = $this->dateTime->getNowString();
|
||||
$data['now_RAW'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$data['__injectableFactory'] = $this->injectableFactory;
|
||||
$data['__config'] = $this->config;
|
||||
$data['__dateTime'] = $this->dateTime;
|
||||
$data['__metadata'] = $this->metadata;
|
||||
$data['__entityManager'] = $this->entityManager;
|
||||
$data['__language'] = $this->language;
|
||||
$data['__serviceFactory'] = $this->serviceFactory;
|
||||
$data['__log'] = $this->log;
|
||||
$data['__entityType'] = $entity->getEntityType();
|
||||
|
||||
$html = $renderer($data);
|
||||
|
||||
$html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html);
|
||||
|
||||
if ($this->entityManager) {
|
||||
$html = preg_replace_callback(
|
||||
'/\?entryPoint=attachment\&id=([A-Za-z0-9]*)/',
|
||||
function ($matches) {
|
||||
$id = $matches[1];
|
||||
$attachment = $this->entityManager->getEntity('Attachment', $id);
|
||||
|
||||
if ($attachment) {
|
||||
$filePath = $this->entityManager
|
||||
->getRepository('Attachment')
|
||||
->getFilePath($attachment);
|
||||
|
||||
return $filePath;
|
||||
}
|
||||
},
|
||||
$html
|
||||
);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function format($value)
|
||||
{
|
||||
if (is_float($value)) {
|
||||
@@ -633,83 +713,6 @@ class Htmlizer
|
||||
return $helpers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an HTML for entity by a given template.
|
||||
*
|
||||
* @param $cacheId @deprecated Skip or specify NULL.
|
||||
* @param $additionalData Data will be passed to the template.
|
||||
* @param $skipLinks Do not process related records.
|
||||
*/
|
||||
public function render(
|
||||
Entity $entity,
|
||||
string $template,
|
||||
?string $cacheId = null,
|
||||
?array $additionalData = null,
|
||||
bool $skipLinks = false
|
||||
): string {
|
||||
|
||||
$template = str_replace('<tcpdf ', '', $template);
|
||||
|
||||
$helpers = $this->getHelpers();
|
||||
|
||||
$code = LightnCandy::compile($template, [
|
||||
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION,
|
||||
'helpers' => $this->getHelpers(),
|
||||
]);
|
||||
|
||||
$renderer = LightnCandy::prepare($code);
|
||||
|
||||
|
||||
$additionalData = $additionalData ?? [];
|
||||
|
||||
$data = $this->getDataFromEntity($entity, $skipLinks, 0, $template, $additionalData);
|
||||
|
||||
if (!array_key_exists('today', $data)) {
|
||||
$data['today'] = $this->dateTime->getTodayString();
|
||||
$data['today_RAW'] = date('Y-m-d');
|
||||
}
|
||||
|
||||
if (!array_key_exists('now', $data)) {
|
||||
$data['now'] = $this->dateTime->getNowString();
|
||||
$data['now_RAW'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$data['__injectableFactory'] = $this->injectableFactory;
|
||||
$data['__config'] = $this->config;
|
||||
$data['__dateTime'] = $this->dateTime;
|
||||
$data['__metadata'] = $this->metadata;
|
||||
$data['__entityManager'] = $this->entityManager;
|
||||
$data['__language'] = $this->language;
|
||||
$data['__serviceFactory'] = $this->serviceFactory;
|
||||
$data['__log'] = $this->log;
|
||||
$data['__entityType'] = $entity->getEntityType();
|
||||
|
||||
$html = $renderer($data);
|
||||
|
||||
$html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html);
|
||||
|
||||
if ($this->entityManager) {
|
||||
$html = preg_replace_callback(
|
||||
'/\?entryPoint=attachment\&id=([A-Za-z0-9]*)/',
|
||||
function ($matches) {
|
||||
$id = $matches[1];
|
||||
$attachment = $this->entityManager->getEntity('Attachment', $id);
|
||||
|
||||
if ($attachment) {
|
||||
$filePath = $this->entityManager
|
||||
->getRepository('Attachment')
|
||||
->getFilePath($attachment);
|
||||
|
||||
return $filePath;
|
||||
}
|
||||
},
|
||||
$html
|
||||
);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function getFieldType(string $entityType, string $field)
|
||||
{
|
||||
if (!$this->metadata) {
|
||||
|
||||
164
application/Espo/Core/Htmlizer/TemplateRenderer.php
Normal file
164
application/Espo/Core/Htmlizer/TemplateRenderer.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Htmlizer;
|
||||
|
||||
use Espo\Core\AclManager;
|
||||
|
||||
use Espo\Entities\User;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use stdClass;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
|
||||
class TemplateRenderer
|
||||
{
|
||||
private $htmlizerFactory;
|
||||
|
||||
private $aclManager;
|
||||
|
||||
private $data;
|
||||
|
||||
private $user = null;
|
||||
|
||||
private $entity = null;
|
||||
|
||||
private $skipRelations = false;
|
||||
|
||||
private $applyAcl = false;
|
||||
|
||||
public function __construct(
|
||||
HtmlizerFactory $htmlizerFactory,
|
||||
AclManager $aclManager
|
||||
) {
|
||||
$this->htmlizerFactory = $htmlizerFactory;
|
||||
$this->aclManager = $aclManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setting a user also sets applyAcl.
|
||||
*/
|
||||
public function setUser(User $user): self
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
$this->setApplyAcl();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEntity(Entity $entity): self
|
||||
{
|
||||
$this->entity = $entity;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stdClass|array $data Additional data.
|
||||
*/
|
||||
public function setData($data): self
|
||||
{
|
||||
if (!is_array($data) && !$data instanceof stdClass) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSkipRelations(bool $skipRelations = true): self
|
||||
{
|
||||
$this->skipRelations = $skipRelations;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setApplyAcl(bool $applyAcl = true): self
|
||||
{
|
||||
$this->applyAcl = $applyAcl;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
if (!$this->template) {
|
||||
throw new LogicException("No template.");
|
||||
}
|
||||
|
||||
return $this->renderTemplate($this->template);
|
||||
}
|
||||
|
||||
public function renderTemplate(string $template): string
|
||||
{
|
||||
return $this->renderTemplateInternal($template, $this->createHtmlizer());
|
||||
}
|
||||
|
||||
private function renderTemplateInternal(string $template, Htmlizer $htmlizer): string
|
||||
{
|
||||
return $htmlizer->render(
|
||||
$this->entity,
|
||||
$template,
|
||||
null,
|
||||
$this->data,
|
||||
$this->skipRelations
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function renderMultipleTemplates(string ...$templateList): array
|
||||
{
|
||||
$htmlizer = $this->createHtmlizer();
|
||||
|
||||
$resultList = [];
|
||||
|
||||
foreach ($templateList as $template) {
|
||||
$resultList[] = $this->renderTemplateInternal($template, $htmlizer);
|
||||
}
|
||||
|
||||
return $resultList;
|
||||
}
|
||||
|
||||
private function createHtmlizer(): Htmlizer
|
||||
{
|
||||
return $this->user ?
|
||||
$this->htmlizerFactory->createForUser($this->user) :
|
||||
$this->htmlizerFactory->create(!$this->applyAcl);
|
||||
}
|
||||
}
|
||||
47
application/Espo/Core/Htmlizer/TemplateRendererFactory.php
Normal file
47
application/Espo/Core/Htmlizer/TemplateRendererFactory.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Htmlizer;
|
||||
|
||||
use Espo\Core\InjectableFactory;
|
||||
|
||||
class TemplateRendererFactory
|
||||
{
|
||||
private $injectableFactory;
|
||||
|
||||
public function __construct(InjectableFactory $injectableFactory)
|
||||
{
|
||||
$this->injectableFactory = $injectableFactory;
|
||||
}
|
||||
|
||||
public function create(): TemplateRenderer
|
||||
{
|
||||
return $this->injectableFactory->create(TemplateRenderer::class);
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,11 @@
|
||||
namespace Espo\Tools\Pdf\Tcpdf;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Htmlizer\Htmlizer as Htmlizer;
|
||||
use Espo\Core\Htmlizer\HtmlizerFactory as HtmlizerFactory;
|
||||
use Espo\Core\Htmlizer\TemplateRendererFactory;
|
||||
use Espo\Core\Htmlizer\TemplateRenderer;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Tools\Pdf\Template;
|
||||
use Espo\Tools\Pdf\Data;
|
||||
use Espo\Tools\Pdf\Params;
|
||||
@@ -46,21 +48,21 @@ class EntityProcessor
|
||||
|
||||
private $config;
|
||||
|
||||
private $htmlizerFactory;
|
||||
private $templateRendererFactory;
|
||||
|
||||
public function __construct(Config $config, HtmlizerFactory $htmlizerFactory)
|
||||
public function __construct(Config $config, TemplateRendererFactory $templateRendererFactory)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->htmlizerFactory = $htmlizerFactory;
|
||||
$this->templateRendererFactory = $templateRendererFactory;
|
||||
}
|
||||
|
||||
public function process(Tcpdf $pdf, Template $template, Entity $entity, Params $params, Data $data): void
|
||||
{
|
||||
$additionalData = get_object_vars($data->getAdditionalTemplateData());
|
||||
|
||||
$htmlizer = $params->applyAcl() ?
|
||||
$this->htmlizerFactory->create() :
|
||||
$this->htmlizerFactory->createNoAcl();
|
||||
$renderer = $this->templateRendererFactory
|
||||
->create()
|
||||
->setApplyAcl($params->applyAcl())
|
||||
->setEntity($entity)
|
||||
->setData($data->getAdditionalTemplateData());
|
||||
|
||||
$fontFace = $this->config->get('pdfFontFace', $this->fontFace);
|
||||
$fontSize = $this->config->get('pdfFontSize', $this->fontSize);
|
||||
@@ -103,7 +105,7 @@ class EntityProcessor
|
||||
}
|
||||
|
||||
if ($template->hasFooter()) {
|
||||
$htmlFooter = $this->render($htmlizer, $entity, $template->getFooter(), $additionalData);
|
||||
$htmlFooter = $this->render($renderer, $template->getFooter());
|
||||
|
||||
$pdf->setFooterFont([$fontFace, '', $this->fontSize]);
|
||||
$pdf->setFooterPosition($template->getFooterPosition());
|
||||
@@ -115,7 +117,7 @@ class EntityProcessor
|
||||
}
|
||||
|
||||
if ($template->hasHeader()) {
|
||||
$htmlHeader = $this->render($htmlizer, $entity, $template->getHeader(), $additionalData);
|
||||
$htmlHeader = $this->render($renderer, $template->getHeader());
|
||||
|
||||
$pdf->setHeaderFont([$fontFace, '', $this->fontSize]);
|
||||
$pdf->setHeaderPosition($template->getHeaderPosition());
|
||||
@@ -128,21 +130,16 @@ class EntityProcessor
|
||||
|
||||
$pdf->addPage($pageOrientationCode, $pageFormat);
|
||||
|
||||
$htmlBody = $this->render($htmlizer, $entity, $template->getBody(), $additionalData);
|
||||
$htmlBody = $this->render($renderer, $template->getBody());
|
||||
|
||||
$pdf->writeHTML($htmlBody, true, false, true, false, '');
|
||||
}
|
||||
|
||||
private function render(Htmlizer $htmlizer, Entity $entity, string $template, array $additionalData): string
|
||||
private function render(TemplateRenderer $renderer, string $template): string
|
||||
{
|
||||
$html = $htmlizer->render(
|
||||
$entity,
|
||||
$template,
|
||||
null,
|
||||
$additionalData
|
||||
);
|
||||
$html = $renderer->renderTemplate($template);
|
||||
|
||||
$html = preg_replace_callback(
|
||||
return preg_replace_callback(
|
||||
'/<barcodeimage data="([^"]+)"\/>/',
|
||||
function ($matches) {
|
||||
$dataString = $matches[1];
|
||||
@@ -153,8 +150,6 @@ class EntityProcessor
|
||||
},
|
||||
$html
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
private function composeBarcodeTag(array $data): string
|
||||
|
||||
Reference in New Issue
Block a user