-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TagHandler.php
398 lines (337 loc) · 12 KB
/
TagHandler.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Document\Tag;
use Pimcore\Extension\Document\Areabrick\AreabrickInterface;
use Pimcore\Extension\Document\Areabrick\AreabrickManagerInterface;
use Pimcore\Extension\Document\Areabrick\Exception\ConfigurationException;
use Pimcore\Extension\Document\Areabrick\TemplateAreabrickInterface;
use Pimcore\Http\RequestHelper;
use Pimcore\Http\ResponseStack;
use Pimcore\HttpKernel\BundleLocator\BundleLocatorInterface;
use Pimcore\HttpKernel\WebPathResolver;
use Pimcore\Model\Document\PageSnippet;
use Pimcore\Model\Document\Tag;
use Pimcore\Model\Document\Tag\Area\Info;
use Pimcore\Templating\Model\ViewModel;
use Pimcore\Templating\Model\ViewModelInterface;
use Pimcore\Templating\Renderer\ActionRenderer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
class TagHandler implements TagHandlerInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var AreabrickManagerInterface
*/
protected $brickManager;
/**
* @var EngineInterface
*/
protected $templating;
/**
* @var BundleLocatorInterface
*/
protected $bundleLocator;
/**
* @var WebPathResolver
*/
protected $webPathResolver;
/**
* @var ActionRenderer
*/
protected $actionRenderer;
/**
* @var RequestHelper
*/
protected $requestHelper;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var ResponseStack
*/
private $responseStack;
/**
* @var array
*/
protected $brickTemplateCache = [];
/**
* @param AreabrickManagerInterface $brickManager
* @param EngineInterface $templating
* @param BundleLocatorInterface $bundleLocator
* @param WebPathResolver $webPathResolver
* @param ActionRenderer $actionRenderer
* @param RequestHelper $requestHelper
* @param TranslatorInterface $translator
*/
public function __construct(
AreabrickManagerInterface $brickManager,
EngineInterface $templating,
BundleLocatorInterface $bundleLocator,
WebPathResolver $webPathResolver,
ActionRenderer $actionRenderer,
RequestHelper $requestHelper,
TranslatorInterface $translator
) {
$this->brickManager = $brickManager;
$this->templating = $templating;
$this->bundleLocator = $bundleLocator;
$this->webPathResolver = $webPathResolver;
$this->actionRenderer = $actionRenderer;
$this->requestHelper = $requestHelper;
$this->translator = $translator;
}
/**
* @required
*
* @deprecated This was added as setter for BC reasons. TODO Pimcore 6: add as constructor dependency
*/
public function setResponseStack(ResponseStack $responseStack)
{
$this->responseStack = $responseStack;
}
/**
* {@inheritdoc}
*/
public function supports($view)
{
return $view instanceof ViewModelInterface;
}
/**
* @inheritDoc
*/
public function isBrickEnabled(Tag $tag, $brick)
{
if ($brick instanceof AreabrickInterface) {
$brick = $brick->getId();
}
return $this->brickManager->isEnabled($brick);
}
/**
* {@inheritdoc}
*/
public function getAvailableAreablockAreas(Tag\Areablock $tag, array $options)
{
/** @var ViewModel $view */
$view = $tag->getView();
$areas = [];
foreach ($this->brickManager->getBricks() as $brick) {
// don't show disabled bricks
if (!isset($options['dontCheckEnabled']) || !$options['dontCheckEnabled']) {
if (!$this->isBrickEnabled($tag, $brick)) {
continue;
}
}
if (!(empty($options['allowed']) || in_array($brick->getId(), $options['allowed']))) {
continue;
}
$name = $brick->getName();
$desc = $brick->getDescription();
$icon = $brick->getIcon();
// autoresolve icon as <bundleName>/Resources/public/areas/<id>/icon.png
if (null === $icon) {
$bundle = null;
try {
$bundle = $this->bundleLocator->getBundle($brick);
// check if file exists
$iconPath = sprintf('%s/Resources/public/areas/%s/icon.png', $bundle->getPath(), $brick->getId());
if (file_exists($iconPath)) {
// build URL to icon
$icon = $this->webPathResolver->getPath($bundle, 'areas/' . $brick->getId(), 'icon.png');
}
} catch (\Exception $e) {
$iconPath = '';
$icon = '';
}
}
if ($view->editmode) {
$name = $this->translator->trans($name);
$desc = $this->translator->trans($desc);
}
$areas[$brick->getId()] = [
'name' => $name,
'description' => $desc,
'type' => $brick->getId(),
'icon' => $icon,
];
}
return $areas;
}
/**
* {@inheritdoc}
*/
public function renderAreaFrontend(Info $info)
{
$tag = $info->getTag();
$params = $info->getParams();
/** @var ViewModelInterface $view */
$view = $tag->getView();
$brick = $this->brickManager->getBrick($info->getId());
$info->setView($view);
$info->setRequest($this->requestHelper->getCurrentRequest());
// assign parameters to view
$view->getParameters()->add($params);
$view->getParameters()->add([
'brick' => $info, // alias of `info` for compatibility reasons
'info' => $info,
'instance' => $brick,
]);
// call action
$this->handleBrickActionResult($brick->action($info));
if (!$brick->hasViewTemplate()) {
return;
}
// check if view template exists and throw error before open tag is rendered
$viewTemplate = $this->resolveBrickTemplate($brick, 'view');
if (!$this->templating->exists($viewTemplate)) {
$e = new ConfigurationException(sprintf(
'The view template "%s" for areabrick %s does not exist',
$viewTemplate,
$brick->getId()
));
$this->logger->error($e->getMessage());
throw $e;
}
$editmode = $view->editmode;
echo $brick->getHtmlTagOpen($info);
if ($brick->hasEditTemplate() && $editmode) {
echo '<div class="pimcore_area_edit_button" data-name="' . $tag->getName() . '" data-real-name="' . $tag->getRealName() . '"></div>';
// forces the editmode in view independent if there's an edit or not
if (!array_key_exists('forceEditInView', $params) || !$params['forceEditInView']) {
$view->editmode = false;
}
}
// render view template
echo $this->templating->render(
$viewTemplate,
$view->getParameters()->all()
);
if ($brick->hasEditTemplate() && $editmode) {
$view->editmode = true;
echo '<div class="pimcore_area_editmode pimcore_area_editmode_hidden" data-name="' . $tag->getName() . '" data-real-name="' . $tag->getRealName() . '">';
$editTemplate = $this->resolveBrickTemplate($brick, 'edit');
// render edit template
echo $this->templating->render(
$editTemplate,
$view->getParameters()->all()
);
echo '</div>';
}
echo $brick->getHtmlTagClose($info);
// call post render
$this->handleBrickActionResult($brick->postRenderAction($info));
}
private function handleBrickActionResult($result)
{
// TODO Pimcore 6 rely on responseStack being set as constructor dependency
if (null === $this->responseStack) {
return;
}
// if the action result is a response object, push it onto the
// response stack. this response will be used by the ResponseStackListener
// and sent back to the client
if ($result instanceof Response) {
$this->responseStack->push($result);
}
}
/**
* Try to get the brick template from get*Template method. If method returns null and brick implements
* TemplateAreabrickInterface fall back to auto-resolving the template reference. See interface for examples.
*
* @param AreabrickInterface $brick
* @param $type
*
* @return mixed|null|string
*/
protected function resolveBrickTemplate(AreabrickInterface $brick, $type)
{
$cacheKey = sprintf('%s.%s', $brick->getId(), $type);
if (isset($this->brickTemplateCache[$cacheKey])) {
return $this->brickTemplateCache[$cacheKey];
}
$template = null;
if ($type === 'view') {
$template = $brick->getViewTemplate();
} elseif ($type === 'edit') {
$template = $brick->getEditTemplate();
}
if (null === $template) {
if ($brick instanceof TemplateAreabrickInterface) {
$template = $this->buildBrickTemplateReference($brick, $type);
} else {
$e = new ConfigurationException(sprintf(
'Brick %s is configured to have a %s template but does not return a template path and does not implement %s',
$brick->getId(),
$type,
TemplateAreabrickInterface::class
));
$this->logger->error($e->getMessage());
throw $e;
}
}
$this->brickTemplateCache[$cacheKey] = $template;
return $template;
}
/**
* Return either bundle or global (= app/Resources) template reference
*
* @param TemplateAreabrickInterface $brick
* @param string $type
*
* @return string
*/
protected function buildBrickTemplateReference(TemplateAreabrickInterface $brick, $type)
{
if ($brick->getTemplateLocation() === TemplateAreabrickInterface::TEMPLATE_LOCATION_BUNDLE) {
$bundle = $this->bundleLocator->getBundle($brick);
return sprintf(
'%s:Areas/%s:%s.%s',
$bundle->getName(),
$brick->getId(),
$type,
$brick->getTemplateSuffix()
);
} else {
return sprintf(
'Areas/%s/%s.%s',
$brick->getId(),
$type,
$brick->getTemplateSuffix()
);
}
}
/**
* {@inheritdoc}
*/
public function renderAction($view, $controller, $action, $parent = null, array $attributes = [], array $query = [], array $options = [])
{
$document = $attributes['document'] ?? null;
if ($document && $document instanceof PageSnippet) {
unset($attributes['document']);
$attributes = $this->actionRenderer->addDocumentAttributes($document, $attributes);
}
$uri = $this->actionRenderer->createControllerReference(
$parent,
$controller,
$action,
$attributes,
$query
);
return $this->actionRenderer->render($uri, $options);
}
}