|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Routes. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Zikula contributors (Zikula) |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
* @author Zikula contributors <[email protected]>. |
|
8
|
|
|
* @link http://www.zikula.org |
|
9
|
|
|
* @link http://zikula.org |
|
10
|
|
|
* @version Generated by ModuleStudio 0.7.1 (http://modulestudio.de). |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Zikula\RoutesModule\Helper\Base; |
|
14
|
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
19
|
|
|
use Zikula\Core\Response\PlainResponse; |
|
20
|
|
|
use Zikula\ExtensionsModule\Api\VariableApi; |
|
21
|
|
|
use Zikula\PermissionsModule\Api\PermissionApi; |
|
22
|
|
|
use Zikula\RoutesModule\Helper\ControllerHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Helper base class for view layer methods. |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractViewHelper |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var EngineInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $templating; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Request |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $request; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var PermissionApi |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $permissionApi; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var VariableApi |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $variableApi; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ControllerHelper |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $controllerHelper; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* ViewHelper constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param EngineInterface $templating EngineInterface service instance |
|
58
|
|
|
* @param RequestStack $requestStack RequestStack service instance |
|
59
|
|
|
* @param PermissionApi $permissionApi PermissionApi service instance |
|
60
|
|
|
* @param VariableApi $variableApi VariableApi service instance |
|
61
|
|
|
* @param ControllerHelper $controllerHelper ControllerHelper service instance |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(EngineInterface $templating, RequestStack $requestStack, PermissionApi $permissionApi, VariableApi $variableApi, ControllerHelper $controllerHelper) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->templating = $templating; |
|
68
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
69
|
|
|
$this->permissionApi = $permissionApi; |
|
70
|
|
|
$this->variableApi = $variableApi; |
|
71
|
|
|
$this->controllerHelper = $controllerHelper; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Determines the view template for a certain method with given parameters. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $type Current controller (name of currently treated entity) |
|
78
|
|
|
* @param string $func Current function (index, view, ...) |
|
79
|
|
|
* |
|
80
|
|
|
* @return string name of template file |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getViewTemplate($type, $func) |
|
83
|
|
|
{ |
|
84
|
|
|
// create the base template name |
|
85
|
|
|
$template = '@ZikulaRoutesModule/' . ucfirst($type) . '/' . $func; |
|
86
|
|
|
|
|
87
|
|
|
// check for template extension |
|
88
|
|
|
$templateExtension = '.' . $this->determineExtension($type, $func); |
|
89
|
|
|
|
|
90
|
|
|
// check whether a special template is used |
|
91
|
|
|
$tpl = $this->request->query->getAlnum('tpl', ''); |
|
92
|
|
|
if (!empty($tpl)) { |
|
93
|
|
|
// check if custom template exists |
|
94
|
|
|
$customTemplate = $template . ucfirst($tpl); |
|
95
|
|
|
if ($this->templating->exists($customTemplate . $templateExtension)) { |
|
96
|
|
|
$template = $customTemplate; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$template .= $templateExtension; |
|
101
|
|
|
|
|
102
|
|
|
return $template; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Helper method for managing view templates. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $type Current controller (name of currently treated entity) |
|
109
|
|
|
* @param string $func Current function (index, view, ...) |
|
110
|
|
|
* @param array $templateParameters Template data |
|
111
|
|
|
* @param string $template Optional assignment of precalculated template file |
|
112
|
|
|
* |
|
113
|
|
|
* @return mixed Output |
|
114
|
|
|
*/ |
|
115
|
|
|
public function processTemplate($type, $func, array $templateParameters = [], $template = '') |
|
116
|
|
|
{ |
|
117
|
|
|
$templateExtension = $this->determineExtension($type, $func); |
|
118
|
|
|
if (empty($template)) { |
|
119
|
|
|
$template = $this->getViewTemplate($type, $func); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($templateExtension == 'pdf.twig') { |
|
123
|
|
|
$template = str_replace('.pdf', '.html', $template); |
|
124
|
|
|
|
|
125
|
|
|
return $this->processPdf($templateParameters, $template); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// look whether we need output with or without the theme |
|
129
|
|
|
$raw = $this->request->query->getBoolean('raw', false); |
|
130
|
|
|
if (!$raw && $templateExtension != 'html.twig') { |
|
131
|
|
|
$raw = true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$output = $this->templating->render($template, $templateParameters); |
|
135
|
|
|
$response = null; |
|
|
|
|
|
|
136
|
|
|
if (true === $raw) { |
|
137
|
|
|
// standalone output |
|
138
|
|
|
$response = new PlainResponse($output); |
|
139
|
|
|
} else { |
|
140
|
|
|
// normal output |
|
141
|
|
|
$response = new Response($output); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// check if we need to set any custom headers |
|
145
|
|
|
switch ($templateExtension) { |
|
146
|
|
|
case 'ics.twig': |
|
147
|
|
|
$response->headers->set('Content-Type', 'text/calendar; charset=utf-8'); |
|
148
|
|
|
break; |
|
149
|
|
|
case 'kml.twig': |
|
150
|
|
|
$response->headers->set('Content-Type', 'application/vnd.google-earth.kml+xml'); |
|
151
|
|
|
break; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $response; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get extension of the currently treated template. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $type Current controller (name of currently treated entity) |
|
161
|
|
|
* @param string $func Current function (index, view, ...) |
|
162
|
|
|
* |
|
163
|
|
|
* @return array List of allowed template extensions |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function determineExtension($type, $func) |
|
166
|
|
|
{ |
|
167
|
|
|
$templateExtension = 'html.twig'; |
|
168
|
|
|
if (!in_array($func, ['view', 'display'])) { |
|
169
|
|
|
return $templateExtension; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$extensions = $this->availableExtensions($type, $func); |
|
173
|
|
|
$format = $this->request->getRequestFormat(); |
|
174
|
|
|
if ($format != 'html' && in_array($format, $extensions)) { |
|
175
|
|
|
$templateExtension = $format . '.twig'; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $templateExtension; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get list of available template extensions. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $type Current controller (name of currently treated entity) |
|
185
|
|
|
* @param string $func Current function (index, view, ...) |
|
186
|
|
|
* |
|
187
|
|
|
* @return array List of allowed template extensions |
|
188
|
|
|
*/ |
|
189
|
|
|
public function availableExtensions($type, $func) |
|
190
|
|
|
{ |
|
191
|
|
|
$extensions = []; |
|
192
|
|
|
$hasAdminAccess = $this->permissionApi->hasPermission('ZikulaRoutesModule:' . ucfirst($type) . ':', '::', ACCESS_ADMIN); |
|
193
|
|
|
if ($func == 'view') { |
|
194
|
|
|
if ($hasAdminAccess) { |
|
195
|
|
|
$extensions = ['kml']; |
|
196
|
|
|
} else { |
|
197
|
|
|
$extensions = []; |
|
198
|
|
|
} |
|
199
|
|
|
} elseif ($func == 'display') { |
|
200
|
|
|
if ($hasAdminAccess) { |
|
201
|
|
|
$extensions = ['kml', 'ics']; |
|
202
|
|
|
} else { |
|
203
|
|
|
$extensions = ['ics']; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $extensions; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Processes a template file using dompdf (LGPL). |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $templateParameters Template data |
|
214
|
|
|
* @param string $template Name of template to use |
|
215
|
|
|
* |
|
216
|
|
|
* @return mixed Output |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function processPdf(array $templateParameters = [], $template) |
|
219
|
|
|
{ |
|
220
|
|
|
// first the content, to set page vars |
|
221
|
|
|
$output = $this->templating->render($template, $templateParameters); |
|
222
|
|
|
|
|
223
|
|
|
// make local images absolute |
|
224
|
|
|
$output = str_replace('img src="/', 'img src="' . $this->request->server->get('DOCUMENT_ROOT') . '/', $output); |
|
225
|
|
|
|
|
226
|
|
|
// see http://codeigniter.com/forums/viewthread/69388/P15/#561214 |
|
227
|
|
|
//$output = utf8_decode($output); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
// then the surrounding |
|
230
|
|
|
$output = $this->templating->render('includePdfHeader.html.twig') . $output . '</body></html>'; |
|
231
|
|
|
|
|
232
|
|
|
$siteName = $this->variableApi->getSystemVar('sitename'); |
|
233
|
|
|
|
|
234
|
|
|
// create name of the pdf output file |
|
235
|
|
|
$fileTitle = $this->controllerHelper->formatPermalink($siteName) |
|
|
|
|
|
|
236
|
|
|
. '-' |
|
237
|
|
|
. $this->controllerHelper->formatPermalink(\PageUtil::getVar('title')) |
|
|
|
|
|
|
238
|
|
|
. '-' . date('Ymd') . '.pdf'; |
|
239
|
|
|
|
|
240
|
|
|
/* |
|
|
|
|
|
|
241
|
|
|
if (true === $this->request->query->getBoolean('dbg', false)) { |
|
242
|
|
|
die($output); |
|
243
|
|
|
} |
|
244
|
|
|
*/ |
|
245
|
|
|
|
|
246
|
|
|
// instantiate pdf object |
|
247
|
|
|
$pdf = new \DOMPDF(); |
|
248
|
|
|
// define page properties |
|
249
|
|
|
$pdf->set_paper('A4'); |
|
250
|
|
|
// load html input data |
|
251
|
|
|
$pdf->load_html($output); |
|
252
|
|
|
// create the actual pdf file |
|
253
|
|
|
$pdf->render(); |
|
254
|
|
|
// stream output to browser |
|
255
|
|
|
$pdf->stream($fileTitle); |
|
256
|
|
|
|
|
257
|
|
|
return new Response(); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.