-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintCatalogExtension.php
231 lines (204 loc) · 6.83 KB
/
PrintCatalogExtension.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Studio1 Kommunikation GmbH
*
* This source file is available under following license:
* - GNU General Public License v3.0 (GNU GPLv3)
*
* @copyright Copyright (c) Studio1 Kommunikation GmbH (http://www.studio1.de)
* @license https://www.gnu.org/licenses/gpl-3.0.txt
*/
namespace Studio1\Web2PrintBricksBundle\Twig\Extension;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\DataObject\ClassDefinition\Data\Hotspotimage;
use Pimcore\Model\DataObject\ClassDefinition\Data\ImageGallery;
use Pimcore\Model\DataObject\ClassDefinition\Data\ManyToManyObjectRelation;
use Pimcore\Model\DataObject\ClassDefinition\Data\ManyToOneRelation;
use Pimcore\Model\DataObject\ClassDefinition\Data\Multiselect;
use Pimcore\Model\DataObject\ClassDefinition\Data\Select;
use Pimcore\Model\Element\AbstractElement;
use Pimcore\Translation\Translator;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Studio1\UtilityBundle\Utility\String\StringUtils;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class PrintCatalogExtension extends AbstractExtension
{
/**
* @var Translator
*/
protected $translator;
/**
* @var Placeholder
*/
protected $placeholderHelper;
/**
* PrintCatalogExtension constructor.
*
* @param Translator $translator
* @param Placeholder $placeholderHelper
*/
public function __construct(Translator $translator, Placeholder $placeholderHelper)
{
$this->translator = $translator;
$this->placeholderHelper = $placeholderHelper;
}
/**
* @return TwigFunction[]
*/
public function getFunctions()
{
return [
new TwigFunction('app_print_output_spec_value', [$this, 'getSpecValue']),
new TwigFunction('app_print_create_register', [$this, 'createRegisterTitleStyling']),
new TwigFunction('app_print_get_register_name', [$this, 'getRegisterName'])
];
}
public function getSpecValue(\stdClass $outputElement, string $thumbnailName = null): string
{
if ($outputElement->value instanceof Image) {
return $this->printImage($outputElement->value, $thumbnailName);
} elseif (($outputElement->def ?? null) instanceof ImageGallery) {
return $this->printImageGallery($outputElement->value, $thumbnailName);
} elseif (($outputElement->def ?? null) instanceof Hotspotimage) {
return $this->printHotspotImage($outputElement->value, $thumbnailName);
} elseif (($outputElement->def ?? null) instanceof Select) {
return $this->printSelectValue($outputElement->value);
} elseif (($outputElement->def ?? null) instanceof Multiselect) {
return $this->printMultiSelectValue($outputElement->value);
} elseif (($outputElement->def ?? null) instanceof ManyToOneRelation) {
return $this->printManyToOne($outputElement->value);
} elseif (($outputElement->def ?? null) instanceof ManyToManyObjectRelation) {
return $this->printManyToManyObjects($outputElement->value);
} else {
return (string)$outputElement->value;
}
}
/**
* @param Image $image
* @param string $thumbnailName
*
* @return string
*/
protected function printImage(Image $image, string $thumbnailName): string
{
$src = $thumbnailName ? $image->getThumbnail($thumbnailName) : $image->getFullPath();
return "<img src='$src' alt='image' />";
}
/**
* @param \Pimcore\Model\DataObject\Data\ImageGallery $imageGallery
* @param string $thumbnailName
*
* @return string
*/
protected function printImageGallery(\Pimcore\Model\DataObject\Data\ImageGallery $imageGallery, string $thumbnailName): string
{
$firstItem = $imageGallery->current();
if ($firstItem && $firstItem->getImage()) {
return $this->printImage($firstItem->getImage(), $thumbnailName);
}
return '';
}
/**
* @param \Pimcore\Model\DataObject\Data\Hotspotimage $hotspotimage
* @param string $thumbnailName
*
* @return string
*/
protected function printHotspotImage(\Pimcore\Model\DataObject\Data\Hotspotimage $hotspotimage, string $thumbnailName): string
{
$image = $hotspotimage->getImage();
if ($image) {
return "<img src='{$hotspotimage->getThumbnail($thumbnailName)}' alt='image' />";
}
}
/**
* @param string $value
*
* @return string
*/
protected function printSelectValue(string $value): string
{
return $this->translator->trans('attribute.' . strtolower($value));
}
/**
* @param array|null $value
*
* @return string
*/
protected function printMultiSelectValue(?array $value = null): string
{
$result = [];
if ($value) {
foreach ($value as $v) {
$result[] = $this->translator->trans('attribute.' . strtolower($v));
}
}
return implode(', ', $result);
}
/**
* @param array $value
*
* @return string
*/
protected function printManyToManyObjects(array $value): string
{
$result = [];
if ($value) {
foreach ($value as $v) {
if (method_exists($v, 'getName')) {
$result[] = $v->getName();
}
}
}
return implode(', ', $result);
}
/**
* @param AbstractElement|null $element
*
* @return string
*/
protected function printManyToOne(?AbstractElement $element = null): string
{
if ($element && method_exists($element, 'getName')) {
return $element->getName();
}
return '';
}
/**
* @param string|null $name
* @param string|null $registerType
*/
public function createRegisterTitleStyling(?string $name, ?string $registerType = '')
{
$key = $this->getRegisterName($name);
if ($name) {
$this->placeholderHelper->__invoke('register-title-definition')->append("
@page $key:right {
@left-top {
content: xhtml(\"<div class='register left register-$registerType'>$name</div>\");
z-index: 2000;
}
}
@page $key:left {
@right-top {
content: xhtml(\"<div class='register register-$registerType'>$name</div>\");
z-index: 2000;
}
}
.page.$key {
page: $key;
}
");
}
}
/**
* @param string|null $name
*
* @return string
*/
public function getRegisterName(?string $name)
{
return StringUtils::toUrl((string)$name);
}
}