1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the C33s\StaticPageContentBundle. |
4
|
|
|
* |
5
|
|
|
* (c) consistency <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace C33s\StaticPageContentBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* BaseStaticPageController which should be extended from. |
17
|
|
|
* |
18
|
|
|
* Usage: MyPageController extends BaseStaticPageController |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @author consistency <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class BaseStaticPageController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Returns the name of the Bundle, where the templates, which are |
27
|
|
|
* containing the static content, are stored |
28
|
|
|
* |
29
|
|
|
* @return string Name of the Content Bundle |
30
|
|
|
*/ |
31
|
|
|
protected function getContentBundleName() |
32
|
|
|
{ |
33
|
|
|
return $this->container->getParameter('c33s_static_page_content.content_bundle'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the name of the folder where the content templates are stored. |
38
|
|
|
* |
39
|
|
|
* This folder has to be located in %YourBundleName%/Resources/views |
40
|
|
|
* The default path is C33s/StaticPageContentBundle/Resources/views/Content |
41
|
|
|
* so the default return value is "Content". |
42
|
|
|
* |
43
|
|
|
* @return string Name of the folder containing the Content |
44
|
|
|
*/ |
45
|
|
|
protected function getContentFolderName() |
46
|
|
|
{ |
47
|
|
|
return $this->container->getParameter('c33s_static_page_content.content_dir'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Should the Static content be sandboxed? |
52
|
|
|
* |
53
|
|
|
* Only works for the default Content Container |
54
|
|
|
* |
55
|
|
|
* http://twig.sensiolabs.org/doc/api.html#sandbox-extension |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
protected function isSandboxed() |
60
|
|
|
{ |
61
|
|
|
return $this->container->getParameter('c33s_static_page_content.use_template_sandbox'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the full template "path" expression for a given content name. |
66
|
|
|
* Currently only twig is implemented. The Expression includes the ":" |
67
|
|
|
* seperators. |
68
|
|
|
* It's not the Filesystem path it's a twig path. |
69
|
|
|
* |
70
|
|
|
* @param string $contentName The name of the content file which should be loaded |
71
|
|
|
* @param boolean $useTranslations Whether to add translation component to file path or not |
72
|
|
|
* |
73
|
|
|
* @return string Full path expression for the template |
74
|
|
|
*/ |
75
|
|
|
protected function getContentLocation($contentName, $useTranslations) |
76
|
|
|
{ |
77
|
|
|
return sprintf |
78
|
|
|
( |
79
|
|
|
'%s:%s:%s%s%s', |
80
|
|
|
$this->getContentBundleName(), |
81
|
|
|
$this->getContentFolderName(), |
82
|
|
|
$contentName, |
83
|
|
|
$useTranslations ? $this->getTranslationFilePath() : '', |
84
|
|
|
$this->getTemplateExtension() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns template extension. Default is ".html.twig". |
90
|
|
|
* |
91
|
|
|
* @return string Template Extension (dual extension) like ".html.twig" |
92
|
|
|
*/ |
93
|
|
|
protected function getTemplateExtension() |
94
|
|
|
{ |
95
|
|
|
return $this->container->getParameter('c33s_static_page_content.template_extension'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the full "path" expression for the Container Template |
100
|
|
|
* |
101
|
|
|
* @return string container template "path" expression |
102
|
|
|
*/ |
103
|
|
|
protected function getContainerLocation() |
104
|
|
|
{ |
105
|
|
|
return $this->container->getParameter('c33s_static_page_content.wrapper_template'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the Base Template Location which should be used for extending. |
110
|
|
|
* |
111
|
|
|
* @return string Base Template which should be used to extend from |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
|
|
protected function getBaseTemplateLocation() |
115
|
|
|
{ |
116
|
|
|
return $this->container->getParameter('c33s_static_page_content.base_template'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* The Core Show Controller of this Bundle, renders the container templates, |
121
|
|
|
* which have to include the static page content. |
122
|
|
|
* |
123
|
|
|
* @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Not found Exception is thrown if no template with the given name exists. |
124
|
|
|
* |
125
|
|
|
* @param string $name The Name of the Static Page which should be loaded |
126
|
|
|
* |
127
|
|
|
* @return Response A Response instance |
128
|
|
|
*/ |
129
|
|
|
public function showAction($name) |
130
|
|
|
{ |
131
|
|
|
$contentLocation = $this->getContentLocation($name, $this->isUsingTranslations()); |
132
|
|
|
|
133
|
|
|
if (!$this->container->get('templating')->exists($contentLocation) && $this->isUsingTranslations()) { |
134
|
|
|
// fallback: if translations are enabled, try fetching the same template without translation path |
135
|
|
|
$contentLocation = $this->getContentLocation($name, false); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!$this->container->get('templating')->exists($contentLocation)) { |
139
|
|
|
throw $this->createNotFoundException(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->render($this->getContainerLocation(), array( |
143
|
|
|
'baseTemplate' => $this->getBaseTemplateLocation(), |
144
|
|
|
'isSandboxed' => $this->isSandboxed(), |
145
|
|
|
'contentLocation'=> $contentLocation, |
146
|
|
|
'contentName' => $name, |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Define if translated files should be used or not. Translated files use an additional substring in the template path. |
153
|
|
|
* e.g.: views/Content/foo.de.html.twig instead of views/Content/foo.html.twig |
154
|
|
|
* |
155
|
|
|
* Override this and return true to enable translations support |
156
|
|
|
* |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
|
|
protected function isUsingTranslations() |
160
|
|
|
{ |
161
|
|
|
return $this->container->getParameter('c33s_static_page_content.prefer_locale_templates'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the intermediate file path used for translated templates. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function getTranslationFilePath() |
170
|
|
|
{ |
171
|
|
|
return '.' . $this->get('request')->getLocale(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|