1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
6
|
|
|
* @package symfony |
7
|
|
|
* @subpackage Service |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Aimeos\ShopBundle\Service; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
use Symfony\Component\DependencyInjection\Container; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Service providing the view objects |
19
|
|
|
* |
20
|
|
|
* @package symfony |
21
|
|
|
* @subpackage Service |
22
|
|
|
*/ |
23
|
|
|
class View |
24
|
|
|
{ |
25
|
|
|
private $requestStack; |
26
|
|
|
private $container; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initializes the context manager object |
31
|
|
|
* |
32
|
|
|
* @param RequestStack $requestStack Current request stack |
33
|
|
|
* @param Container $container Container object to access parameters |
34
|
|
|
*/ |
35
|
|
|
public function __construct( RequestStack $requestStack, Container $container ) |
36
|
|
|
{ |
37
|
|
|
$this->requestStack = $requestStack; |
38
|
|
|
$this->container = $container; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates the view object for the HTML client. |
44
|
|
|
* |
45
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
46
|
|
|
* @param array $templatePaths List of base path names with relative template paths as key/value pairs |
47
|
|
|
* @param string|null $locale Code of the current language or null for no translation |
48
|
|
|
* @return \Aimeos\MW\View\Iface View object |
49
|
|
|
*/ |
50
|
|
|
public function create( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths, $locale = null ) |
51
|
|
|
{ |
52
|
|
|
$twig = $this->container->get( 'twig' ); |
53
|
|
|
$engine = new \Aimeos\MW\View\Engine\Twig( $twig ); |
54
|
|
|
$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.html.twig' => $engine ) ); |
55
|
|
|
|
56
|
|
|
$config = $context->getConfig(); |
57
|
|
|
$session = $context->getSession(); |
58
|
|
|
|
59
|
|
|
$this->addCsrf( $view ); |
60
|
|
|
$this->addAccess( $view, $context ); |
61
|
|
|
$this->addConfig( $view, $config ); |
62
|
|
|
$this->addNumber( $view, $config ); |
63
|
|
|
$this->addParam( $view ); |
64
|
|
|
$this->addRequest( $view ); |
65
|
|
|
$this->addResponse( $view ); |
66
|
|
|
$this->addSession( $view, $session ); |
67
|
|
|
$this->addTranslate( $view, $locale ); |
68
|
|
|
$this->addUrl( $view ); |
69
|
|
|
|
70
|
|
|
$this->initTwig( $view, $twig ); |
71
|
|
|
|
72
|
|
|
return $view; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds the "access" helper to the view object |
78
|
|
|
* |
79
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
80
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
81
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
82
|
|
|
*/ |
83
|
|
|
protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context ) |
84
|
|
|
{ |
85
|
|
|
$container = $this->container; |
86
|
|
|
$token = $this->container->get( 'security.token_storage' )->getToken(); |
87
|
|
|
|
88
|
|
|
if( is_object( $token ) && is_object( $token->getUser() ) |
89
|
|
|
&& in_array( 'ROLE_ADMIN', (array) $token->getUser()->getRoles() ) ) |
90
|
|
|
{ |
91
|
|
|
$helper = new \Aimeos\MW\View\Helper\Access\All( $view ); |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
$fcn = function() use ( $container, $context ) { |
96
|
|
|
return $container->get( 'aimeos_support' )->getGroups( $context ); |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$view->addHelper( 'access', $helper ); |
103
|
|
|
|
104
|
|
|
return $view; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Adds the "config" helper to the view object |
110
|
|
|
* |
111
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
112
|
|
|
* @param \Aimeos\MW\Config\Iface $config Configuration object |
113
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
114
|
|
|
*/ |
115
|
|
|
protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config ) |
116
|
|
|
{ |
117
|
|
|
$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) ); |
118
|
|
|
$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config ); |
119
|
|
|
$view->addHelper( 'config', $helper ); |
120
|
|
|
|
121
|
|
|
return $view; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds the "access" helper to the view object |
127
|
|
|
* |
128
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
129
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
130
|
|
|
*/ |
131
|
|
|
protected function addCsrf( \Aimeos\MW\View\Iface $view ) |
132
|
|
|
{ |
133
|
|
|
$token = $this->container->get( 'security.csrf.token_manager' )->getToken( '_token' ); |
134
|
|
|
$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', $token->getValue() ); |
135
|
|
|
$view->addHelper( 'csrf', $helper ); |
136
|
|
|
|
137
|
|
|
return $view; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Adds the "number" helper to the view object |
143
|
|
|
* |
144
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
145
|
|
|
* @param \Aimeos\MW\Config\Iface $config Configuration object |
146
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
147
|
|
|
*/ |
148
|
|
|
protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config ) |
149
|
|
|
{ |
150
|
|
|
$sepDec = $config->get( 'client/html/common/format/separatorDecimal', '.' ); |
151
|
|
|
$sep1000 = $config->get( 'client/html/common/format/separator1000', ' ' ); |
152
|
|
|
$decimals = $config->get( 'client/html/common/format/decimals', 2 ); |
153
|
|
|
|
154
|
|
|
$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals ); |
155
|
|
|
$view->addHelper( 'number', $helper ); |
156
|
|
|
|
157
|
|
|
return $view; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Adds the "param" helper to the view object |
163
|
|
|
* |
164
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
165
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
166
|
|
|
*/ |
167
|
|
|
protected function addParam( \Aimeos\MW\View\Iface $view ) |
168
|
|
|
{ |
169
|
|
|
$params = array(); |
170
|
|
|
$request = $this->requestStack->getMasterRequest(); |
171
|
|
|
|
172
|
|
|
if( $request !== null ) { |
173
|
|
|
$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params ); |
177
|
|
|
$view->addHelper( 'param', $helper ); |
178
|
|
|
|
179
|
|
|
return $view; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Adds the "request" helper to the view object |
185
|
|
|
* |
186
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
187
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
188
|
|
|
*/ |
189
|
|
|
protected function addRequest( \Aimeos\MW\View\Iface $view ) |
190
|
|
|
{ |
191
|
|
|
$request = $this->requestStack->getMasterRequest(); |
192
|
|
|
|
193
|
|
|
if( $request !== null ) |
194
|
|
|
{ |
195
|
|
|
$helper = new \Aimeos\MW\View\Helper\Request\Symfony2( $view, $request ); |
196
|
|
|
$view->addHelper( 'request', $helper ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $view; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Adds the "response" helper to the view object |
205
|
|
|
* |
206
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
207
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
208
|
|
|
*/ |
209
|
|
|
protected function addResponse( \Aimeos\MW\View\Iface $view ) |
210
|
|
|
{ |
211
|
|
|
$helper = new \Aimeos\MW\View\Helper\Response\Symfony2( $view ); |
212
|
|
|
$view->addHelper( 'response', $helper ); |
213
|
|
|
|
214
|
|
|
return $view; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Adds the "session" helper to the view object |
220
|
|
|
* |
221
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
222
|
|
|
* @param \Aimeos\MW\Session\Iface $session Session object |
223
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
224
|
|
|
*/ |
225
|
|
|
protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session ) |
226
|
|
|
{ |
227
|
|
|
$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session ); |
228
|
|
|
$view->addHelper( 'session', $helper ); |
229
|
|
|
|
230
|
|
|
return $view; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Adds the "translate" helper to the view object |
236
|
|
|
* |
237
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
238
|
|
|
* @param string|null $locale ISO language code, e.g. "de" or "de_CH" |
239
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
240
|
|
|
*/ |
241
|
|
|
protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale ) |
242
|
|
|
{ |
243
|
|
|
if( $locale !== null ) |
244
|
|
|
{ |
245
|
|
|
$i18n = $this->container->get( 'aimeos_i18n' )->get( array( $locale ) ); |
246
|
|
|
$translation = $i18n[$locale]; |
247
|
|
|
} |
248
|
|
|
else |
249
|
|
|
{ |
250
|
|
|
$translation = new \Aimeos\MW\Translation\None( 'en' ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation ); |
254
|
|
|
$view->addHelper( 'translate', $helper ); |
255
|
|
|
|
256
|
|
|
return $view; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Adds the "url" helper to the view object |
262
|
|
|
* |
263
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
264
|
|
|
* @return \Aimeos\MW\View\Iface Modified view object |
265
|
|
|
*/ |
266
|
|
|
protected function addUrl( \Aimeos\MW\View\Iface $view ) |
267
|
|
|
{ |
268
|
|
|
$fixed = array(); |
269
|
|
|
$request = $this->requestStack->getMasterRequest(); |
270
|
|
|
|
271
|
|
|
if( $request !== null ) |
272
|
|
|
{ |
273
|
|
|
$attr = $request->attributes; |
274
|
|
|
|
275
|
|
|
if( ( $site = $attr->get( 'site' ) ) !== null ) { |
276
|
|
|
$fixed['site'] = $site; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if( ( $lang = $attr->get( 'locale' ) ) !== null ) { |
280
|
|
|
$fixed['locale'] = $lang; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if( ( $currency = $attr->get( 'currency' ) ) !== null ) { |
284
|
|
|
$fixed['currency'] = $currency; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, $this->container->get( 'router' ), $fixed ); |
289
|
|
|
$view->addHelper( 'url', $helper ); |
290
|
|
|
|
291
|
|
|
return $view; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Adds the Aimeos template functions for Twig |
297
|
|
|
* |
298
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
299
|
|
|
* @param \Twig_Environment $twig Twig environment object |
300
|
|
|
*/ |
301
|
|
|
protected function initTwig( \Aimeos\MW\View\Iface $view, \Twig_Environment $twig ) |
302
|
|
|
{ |
303
|
|
|
$fcn = function( $key, $default = null ) use ( $view ) { |
304
|
|
|
return $view->config( $key, $default ); |
305
|
|
|
}; |
306
|
|
|
$twig->addFunction( new \Twig_SimpleFunction( 'aiconfig', $fcn ) ); |
307
|
|
|
|
308
|
|
|
$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) { |
309
|
|
|
return vsprintf( $view->translate( $domain, $singular ), $values ); |
310
|
|
|
}; |
311
|
|
|
$twig->addFunction( new \Twig_SimpleFunction( 'aitrans', $fcn ) ); |
312
|
|
|
|
313
|
|
|
$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) { |
314
|
|
|
return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values ); |
315
|
|
|
}; |
316
|
|
|
$twig->addFunction( new \Twig_SimpleFunction( 'aitransplural', $fcn ) ); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|