1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\AdminListBundle\Entity\OverviewNavigationInterface; |
7
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
8
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
9
|
|
|
use Kunstmaan\NodeBundle\Entity\PageInterface; |
10
|
|
|
use Kunstmaan\NodeBundle\Entity\AbstractStructurePage; |
11
|
|
|
use Kunstmaan\NodeBundle\Helper\NodeMenu; |
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
14
|
|
|
use Twig_Extension; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Extension to fetch node / translation by page in Twig templates |
18
|
|
|
*/ |
19
|
|
|
class NodeTwigExtension extends Twig_Extension |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface |
23
|
|
|
*/ |
24
|
|
|
private $em; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var UrlGeneratorInterface |
28
|
|
|
*/ |
29
|
|
|
private $generator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var NodeMenu |
33
|
|
|
*/ |
34
|
|
|
private $nodeMenu; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RequestStack |
38
|
|
|
*/ |
39
|
|
|
private $requestStack; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $em |
43
|
|
|
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator |
44
|
|
|
* @param \Kunstmaan\NodeBundle\Helper\NodeMenu $nodeMenu |
45
|
|
|
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
EntityManagerInterface $em, |
49
|
|
|
UrlGeneratorInterface $generator, |
50
|
|
|
NodeMenu $nodeMenu, |
51
|
|
|
RequestStack $requestStack |
52
|
|
|
) { |
53
|
|
|
$this->em = $em; |
54
|
|
|
$this->generator = $generator; |
55
|
|
|
$this->nodeMenu = $nodeMenu; |
56
|
|
|
$this->requestStack = $requestStack; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a list of functions to add to the existing list. |
61
|
|
|
* |
62
|
|
|
* @return array An array of functions |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function getFunctions() |
65
|
|
|
{ |
66
|
|
|
return array( |
67
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
68
|
|
|
'get_node_for', |
69
|
|
|
array($this, 'getNodeFor') |
70
|
|
|
), |
71
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
72
|
|
|
'get_node_translation_for', |
73
|
|
|
array($this, 'getNodeTranslationFor') |
74
|
|
|
), |
75
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
76
|
|
|
'get_node_by_internal_name', |
77
|
|
|
array($this, 'getNodeByInternalName') |
78
|
|
|
), |
79
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
80
|
|
|
'get_url_by_internal_name', |
81
|
|
|
array($this, 'getUrlByInternalName') |
82
|
|
|
), |
83
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
84
|
|
|
'get_path_by_internal_name', |
85
|
|
|
array($this, 'getPathByInternalName') |
86
|
|
|
), |
87
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
88
|
|
|
'get_page_by_node_translation', |
89
|
|
|
array($this, 'getPageByNodeTranslation') |
90
|
|
|
), |
91
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
92
|
|
|
'get_node_menu', |
93
|
|
|
array($this, 'getNodeMenu') |
94
|
|
|
), |
95
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
96
|
|
|
'is_structure_node', |
97
|
|
|
array($this, 'isStructureNode') |
98
|
|
|
), |
99
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
100
|
|
|
'is_structure_page', |
101
|
|
|
array($this, 'isStructurePage') |
102
|
|
|
), |
103
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
104
|
|
|
'file_exists', |
105
|
|
|
array($this, 'fileExists') |
106
|
|
|
), |
107
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
108
|
|
|
'get_node_trans_by_node_id', |
109
|
|
|
array($this, 'getNodeTranslationByNodeId') |
110
|
|
|
), |
111
|
|
|
new \Twig_SimpleFunction( |
|
|
|
|
112
|
|
|
'getOverviewRoute', |
113
|
|
|
array($this, 'getOverviewRoute') |
114
|
|
|
), |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the node translation object based on node id and language. |
120
|
|
|
* |
121
|
|
|
* @param int $nodeId |
122
|
|
|
* @param string $lang |
123
|
|
|
* |
124
|
|
|
* @return NodeTranslation |
125
|
|
|
*/ |
126
|
|
|
public function getNodeTranslationByNodeId($nodeId, $lang) |
127
|
|
|
{ |
128
|
|
|
$repo = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation'); |
129
|
|
|
|
130
|
|
|
return $repo->getNodeTranslationByNodeIdQueryBuilder($nodeId, $lang); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param NodeTranslation $nodeTranslation |
135
|
|
|
* |
136
|
|
|
* @return null|object |
137
|
|
|
*/ |
138
|
|
|
public function getPageByNodeTranslation(NodeTranslation $nodeTranslation) |
139
|
|
|
{ |
140
|
|
|
return $nodeTranslation->getRef($this->em); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param PageInterface $page |
145
|
|
|
* |
146
|
|
|
* @return Node |
147
|
|
|
*/ |
148
|
|
|
public function getNodeFor(PageInterface $page) |
149
|
|
|
{ |
150
|
|
|
return $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param PageInterface $page |
155
|
|
|
* |
156
|
|
|
* @return NodeTranslation |
157
|
|
|
*/ |
158
|
|
|
public function getNodeTranslationFor(PageInterface $page) |
159
|
|
|
{ |
160
|
|
|
return $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationFor($page); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $internalName |
165
|
|
|
* @param string $locale |
166
|
|
|
* |
167
|
|
|
* @return Node|null |
168
|
|
|
*/ |
169
|
|
|
public function getNodeByInternalName($internalName, $locale) |
170
|
|
|
{ |
171
|
|
|
$nodes = $this->em->getRepository('KunstmaanNodeBundle:Node') |
172
|
|
|
->getNodesByInternalName($internalName, $locale); |
173
|
|
|
if (!empty($nodes)) { |
174
|
|
|
return $nodes[0]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $internalName Internal name of the node |
182
|
|
|
* @param string $locale Locale |
183
|
|
|
* @param array $parameters (optional) extra parameters |
184
|
|
|
* @param bool $relative (optional) return relative path? |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function getPathByInternalName($internalName, $locale, $parameters = array(), $relative = false) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters); |
191
|
|
|
|
192
|
|
|
return $this->generator->generate( |
193
|
|
|
'_slug', |
194
|
|
|
$routeParameters, |
195
|
|
|
$relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $internalName Internal name of the node |
201
|
|
|
* @param string $locale Locale |
202
|
|
|
* @param array $parameters (optional) extra parameters |
203
|
|
|
* @param bool $schemeRelative (optional) return relative scheme? |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function getUrlByInternalName($internalName, $locale, $parameters = array(), $schemeRelative = false) |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters); |
210
|
|
|
|
211
|
|
|
return $this->generator->generate( |
212
|
|
|
'_slug', |
213
|
|
|
$routeParameters, |
214
|
|
|
$schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $locale |
220
|
|
|
* @param Node $node |
|
|
|
|
221
|
|
|
* @param bool $includeHiddenFromNav |
222
|
|
|
* |
223
|
|
|
* @return NodeMenu |
224
|
|
|
*/ |
225
|
|
|
public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false) |
226
|
|
|
{ |
227
|
|
|
$request = $this->requestStack->getMasterRequest(); |
228
|
|
|
$isPreview = $request->attributes->has('preview') && $request->attributes->get('preview') === true; |
229
|
|
|
$this->nodeMenu->setLocale($locale); |
230
|
|
|
$this->nodeMenu->setCurrentNode($node); |
231
|
|
|
$this->nodeMenu->setIncludeOffline($isPreview); |
232
|
|
|
$this->nodeMenu->setIncludeHiddenFromNav($includeHiddenFromNav); |
233
|
|
|
|
234
|
|
|
return $this->nodeMenu; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @deprecated Using the isStructureNode method is deprecated in KunstmaanNodeBundle 5.2 and will be removed in KunstmaanNodeBundle 6.0. use isStructurePage. |
239
|
|
|
*/ |
240
|
|
|
public function isStructureNode($page) |
241
|
|
|
{ |
242
|
|
|
return $page instanceof AbstractStructurePage; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function isStructurePage($page) |
246
|
|
|
{ |
247
|
|
|
return $page instanceof AbstractStructurePage; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function fileExists($filename) |
251
|
|
|
{ |
252
|
|
|
return file_exists($filename); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $internalName |
257
|
|
|
* @param string $locale |
258
|
|
|
* @param array $parameters |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
private function getRouteParametersByInternalName($internalName, $locale, $parameters = array()) |
263
|
|
|
{ |
264
|
|
|
$url = ''; |
265
|
|
|
$translation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation') |
266
|
|
|
->getNodeTranslationByLanguageAndInternalName($locale, $internalName); |
267
|
|
|
|
268
|
|
|
if (!is_null($translation)) { |
269
|
|
|
$url = $translation->getUrl(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return array_merge( |
273
|
|
|
array( |
274
|
|
|
'url' => $url, |
275
|
|
|
'_locale' => $locale, |
276
|
|
|
), |
277
|
|
|
$parameters |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function getOverviewRoute($node) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
if ($node instanceof OverviewNavigationInterface) { |
284
|
|
|
return $node->getOverViewRoute(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return null; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.