|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\TemplatesSystemBundle\Container; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerInterface; |
|
18
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Widget\WidgetHandlerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class ContainerRenderer. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ContainerRenderer implements ContainerRendererInterface |
|
24
|
|
|
{ |
|
25
|
|
|
const WIDGET_CLASS = 'swp_widget'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ContainerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $containerEntity; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Twig_Environment |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $renderer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $widgets = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $cacheDir; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $debug; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* ContainerRenderer constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param ContainerInterface $containerEntity |
|
56
|
|
|
* @param \Twig_Environment|null $renderer |
|
57
|
|
|
* @param bool $debug |
|
58
|
|
|
* @param null $cacheDir |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
ContainerInterface $containerEntity, |
|
62
|
|
|
\Twig_Environment $renderer = null, |
|
63
|
|
|
$debug = false, |
|
64
|
|
|
$cacheDir = null |
|
65
|
|
|
) { |
|
66
|
|
|
$this->containerEntity = $containerEntity; |
|
67
|
|
|
if (null === $renderer) { |
|
68
|
|
|
$renderer = $this->getRenderer(); |
|
69
|
|
|
} |
|
70
|
|
|
$this->renderer = $renderer; |
|
71
|
|
|
$this->debug = $debug; |
|
72
|
|
|
$this->cacheDir = $cacheDir; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set Widgets. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $widgets |
|
79
|
|
|
* |
|
80
|
|
|
* @return self |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setWidgets($widgets) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->widgets = $widgets; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Render open tag for container. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function renderOpenTag() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->renderer->render('open_tag', [ |
|
97
|
|
|
'id' => $this->containerEntity->getId(), |
|
98
|
|
|
'class' => $this->containerEntity->getCssClass(), |
|
99
|
|
|
'styles' => $this->containerEntity->getStyles(), |
|
100
|
|
|
'visible' => $this->containerEntity->getVisible(), |
|
101
|
|
|
'data' => $this->containerEntity->getData(), |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check if container has items. |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function hasWidgets() |
|
111
|
|
|
{ |
|
112
|
|
|
if (count($this->widgets) > 0) { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Go through widgets render them and collect output of rendering. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function renderWidgets() |
|
125
|
|
|
{ |
|
126
|
|
|
$widgetsOutput = []; |
|
127
|
|
|
/** @var WidgetHandlerInterface $widget */ |
|
128
|
|
|
foreach ($this->widgets as $widget) { |
|
129
|
|
|
$widgetsOutput[] = sprintf( |
|
130
|
|
|
'<div id="%s_%s" class="%s">%s</div>', |
|
131
|
|
|
self::WIDGET_CLASS, |
|
132
|
|
|
$widget->getId(), |
|
133
|
|
|
self::WIDGET_CLASS, |
|
134
|
|
|
$widget->render() |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return implode("\n", $widgetsOutput); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Check if container is visible. |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
public function isVisible() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->containerEntity->getVisible(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Render close tag for container. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function renderCloseTag() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->renderer->render('close_tag'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return \Twig_Environment |
|
163
|
|
|
*/ |
|
164
|
|
|
private function getRenderer() |
|
165
|
|
|
{ |
|
166
|
|
|
$options = []; |
|
167
|
|
|
if (false === $this->debug && null !== $this->cacheDir) { |
|
168
|
|
|
$options['cache'] = $this->cacheDir.'/twig'; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->renderer = new \Twig_Environment( |
|
172
|
|
|
new \Twig_Loader_Array([ |
|
173
|
|
|
'open_tag' => ContainerRendererInterface::OPEN_TAG_TEMPLATE, |
|
174
|
|
|
'close_tag' => ContainerRendererInterface::CLOSE_TAG_TEMPLATE, |
|
175
|
|
|
]), |
|
176
|
|
|
$options |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
return $this->renderer; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|