/
Service.php
138 lines (117 loc) · 4.48 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 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\Tools\Pdf;
use Espo\Core\Acl;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Record\ServiceContainer;
use Espo\Core\Utils\Config;
use Espo\Entities\Template as TemplateEntity;
use Espo\ORM\EntityManager;
use Espo\Tools\Pdf\Data\DataLoaderManager;
class Service
{
private const DEFAULT_ENGINE = 'Tcpdf';
private EntityManager $entityManager;
private Acl $acl;
private ServiceContainer $serviceContainer;
private DataLoaderManager $dataLoaderManager;
private Config $config;
private Builder $builder;
public function __construct(
EntityManager $entityManager,
Acl $acl,
ServiceContainer $serviceContainer,
DataLoaderManager $dataLoaderManager,
Config $config,
Builder $builder
) {
$this->entityManager = $entityManager;
$this->acl = $acl;
$this->serviceContainer = $serviceContainer;
$this->dataLoaderManager = $dataLoaderManager;
$this->config = $config;
$this->builder = $builder;
}
/**
* Generate a PDF.
*
* @param string $entityType An entity type.
* @param string $id A record ID.
* @param string $templateId A template ID.
* @param ?Params $params Params. If null, a params with the apply-acl will be used.
* @params ?Data $data Data.
*
* @throws Error
* @throws NotFound
* @throws Forbidden
*/
public function generate(
string $entityType,
string $id,
string $templateId,
?Params $params = null,
?Data $data = null
): Contents {
$params = $params ?? Params::create()->withAcl(true);
$applyAcl = $params->applyAcl();
$entity = $this->entityManager->getEntityById($entityType, $id);
if (!$entity) {
throw new NotFound("Record not found.");
}
/** @var ?TemplateEntity $template */
$template = $this->entityManager->getEntityById(TemplateEntity::ENTITY_TYPE, $templateId);
if (!$template) {
throw new NotFound("Template not found.");
}
if ($applyAcl && !$this->acl->checkEntityRead($entity)) {
throw new Forbidden("No access to record.");
}
if ($applyAcl && !$this->acl->checkEntityRead($template)) {
throw new Forbidden("No access to template.");
}
$service = $this->serviceContainer->get($entityType);
$service->loadAdditionalFields($entity);
if (method_exists($service, 'loadAdditionalFieldsForPdf')) {
// For bc.
$service->loadAdditionalFieldsForPdf($entity);
}
if ($template->getTargetEntityType() !== $entityType) {
throw new Error("Not matching entity types.");
}
$data = $this->dataLoaderManager->load($entity, $params, $data);
$engine = $this->config->get('pdfEngine') ?? self::DEFAULT_ENGINE;
$printer = $this->builder
->setTemplate(new TemplateWrapper($template))
->setEngine($engine)
->build();
return $printer->printEntity($entity, $params, $data);
}
}