1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license MIT, http://opensource.org/licenses/MIT |
||
5 | * @copyright Aimeos (aimeos.org), 2015-2016 |
||
6 | * @package laravel |
||
7 | * @subpackage Base |
||
8 | */ |
||
9 | |||
10 | namespace Aimeos\Shop\Base; |
||
11 | |||
12 | |||
13 | use Illuminate\Support\Facades\Route; |
||
14 | use Illuminate\Support\Facades\Request; |
||
15 | use Illuminate\Support\Facades\Response; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Service providing the view objects |
||
20 | * |
||
21 | * @package laravel |
||
22 | * @subpackage Base |
||
23 | */ |
||
24 | class View |
||
25 | { |
||
26 | /** |
||
27 | * @var \Illuminate\Contracts\Config\Repository |
||
28 | */ |
||
29 | private $config; |
||
30 | |||
31 | /** |
||
32 | * @var \Aimeos\Shop\Base\I18n |
||
33 | */ |
||
34 | private $i18n; |
||
35 | |||
36 | /** |
||
37 | * @var \Aimeos\Shop\Base\Support |
||
38 | */ |
||
39 | private $support; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Initializes the object |
||
44 | * |
||
45 | * @param \Illuminate\Contracts\Config\Repository $config Configuration object |
||
46 | * @param \Aimeos\Shop\Base\I18n $i18n I18n object |
||
47 | * @param \Aimeos\Shop\Base\Support $support Support object |
||
48 | */ |
||
49 | public function __construct( \Illuminate\Contracts\Config\Repository $config, |
||
50 | \Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support ) |
||
51 | { |
||
52 | $this->i18n = $i18n; |
||
53 | $this->config = $config; |
||
54 | $this->support = $support; |
||
55 | } |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Creates the view object for the HTML client. |
||
60 | * |
||
61 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
62 | * @param array $templatePaths List of base path names with relative template paths as key/value pairs |
||
63 | * @param string|null $locale Code of the current language or null for no translation |
||
64 | * @return \Aimeos\Base\View\Iface View object |
||
65 | */ |
||
66 | public function create( \Aimeos\MShop\ContextIface $context, array $templatePaths, |
||
67 | string $locale = null ) : \Aimeos\Base\View\Iface |
||
68 | { |
||
69 | $engine = new \Aimeos\Base\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) ); |
||
70 | $view = new \Aimeos\Base\View\Standard( $templatePaths, array( '.blade.php' => $engine ) ); |
||
71 | |||
72 | $config = $context->config(); |
||
73 | $session = $context->session(); |
||
74 | |||
75 | $this->addCsrf( $view ); |
||
76 | $this->addAccess( $view, $context ); |
||
77 | $this->addConfig( $view, $config ); |
||
78 | $this->addNumber( $view, $config, $locale ); |
||
79 | $this->addParam( $view ); |
||
80 | $this->addRequest( $view ); |
||
81 | $this->addResponse( $view ); |
||
82 | $this->addSession( $view, $session ); |
||
83 | $this->addTranslate( $view, $locale ); |
||
84 | $this->addUrl( $view ); |
||
85 | |||
86 | return $view; |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Adds the "access" helper to the view object |
||
92 | * |
||
93 | * @param \Aimeos\Base\View\Iface $view View object |
||
94 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
95 | * @return \Aimeos\Base\View\Iface Modified view object |
||
96 | */ |
||
97 | protected function addAccess( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\ContextIface $context ) : \Aimeos\Base\View\Iface |
||
98 | { |
||
99 | if( $this->config->get( 'shop.accessControl', true ) === false |
||
100 | || ( ( $user = \Illuminate\Support\Facades\Auth::user() ) !== null && $user->superuser ) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
101 | ) { |
||
102 | $helper = new \Aimeos\Base\View\Helper\Access\All( $view ); |
||
103 | } |
||
104 | else |
||
105 | { |
||
106 | $support = $this->support; |
||
107 | |||
108 | $fcn = function() use ( $support, $context ) { |
||
109 | return $support->getGroups( $context ); |
||
110 | }; |
||
111 | |||
112 | $helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, $fcn ); |
||
113 | } |
||
114 | |||
115 | $view->addHelper( 'access', $helper ); |
||
116 | |||
117 | return $view; |
||
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Adds the "config" helper to the view object |
||
123 | * |
||
124 | * @param \Aimeos\Base\View\Iface $view View object |
||
125 | * @param \Aimeos\Base\Config\Iface $config Configuration object |
||
126 | * @return \Aimeos\Base\View\Iface Modified view object |
||
127 | */ |
||
128 | protected function addConfig( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config ) : \Aimeos\Base\View\Iface |
||
129 | { |
||
130 | $prefixes = ['version', 'admin', 'client', 'common', 'resource/fs/baseurl', 'resource/fs-media/baseurl', 'resource/fs-theme/baseurl']; |
||
131 | $config = new \Aimeos\Base\Config\Decorator\Protect( clone $config, $prefixes ); |
||
132 | $helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $config ); |
||
133 | $view->addHelper( 'config', $helper ); |
||
134 | |||
135 | return $view; |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Adds the "access" helper to the view object |
||
141 | * |
||
142 | * @param \Aimeos\Base\View\Iface $view View object |
||
143 | * @return \Aimeos\Base\View\Iface Modified view object |
||
144 | */ |
||
145 | protected function addCsrf( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
146 | { |
||
147 | $helper = new \Aimeos\Base\View\Helper\Csrf\Standard( $view, '_token', csrf_token() ); |
||
148 | $view->addHelper( 'csrf', $helper ); |
||
149 | |||
150 | return $view; |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Adds the "number" helper to the view object |
||
156 | * |
||
157 | * @param \Aimeos\Base\View\Iface $view View object |
||
158 | * @param \Aimeos\Base\Config\Iface $config Configuration object |
||
159 | * @param string|null $locale Code of the current language or null for no translation |
||
160 | * @return \Aimeos\Base\View\Iface Modified view object |
||
161 | */ |
||
162 | protected function addNumber( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config, |
||
163 | string $locale = null ) : \Aimeos\Base\View\Iface |
||
164 | { |
||
165 | if( config( 'shop.num_formatter', 'Locale' ) === 'Locale' ) |
||
166 | { |
||
167 | $pattern = $config->get( 'client/html/common/format/pattern' ); |
||
168 | $helper = new \Aimeos\Base\View\Helper\Number\Locale( $view, $locale, $pattern ); |
||
169 | } |
||
170 | else |
||
171 | { |
||
172 | $sep1000 = $config->get( 'client/html/common/format/separator1000', '' ); |
||
173 | $decsep = $config->get( 'client/html/common/format/separatorDecimal', '.' ); |
||
174 | $helper = new \Aimeos\Base\View\Helper\Number\Standard( $view, $decsep, $sep1000 ); |
||
175 | } |
||
176 | |||
177 | return $view->addHelper( 'number', $helper ); |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Adds the "param" helper to the view object |
||
183 | * |
||
184 | * @param \Aimeos\Base\View\Iface $view View object |
||
185 | * @return \Aimeos\Base\View\Iface Modified view object |
||
186 | */ |
||
187 | protected function addParam( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
188 | { |
||
189 | $params = ( Route::current() ? Route::current()->parameters() : array() ) + Request::all(); |
||
190 | $helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params ); |
||
191 | $view->addHelper( 'param', $helper ); |
||
192 | |||
193 | return $view; |
||
194 | } |
||
195 | |||
196 | |||
197 | /** |
||
198 | * Adds the "request" helper to the view object |
||
199 | * |
||
200 | * @param \Aimeos\Base\View\Iface $view View object |
||
201 | * @return \Aimeos\Base\View\Iface Modified view object |
||
202 | */ |
||
203 | protected function addRequest( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
204 | { |
||
205 | $helper = new \Aimeos\Base\View\Helper\Request\Laravel( $view, Request::instance() ); |
||
206 | $view->addHelper( 'request', $helper ); |
||
207 | |||
208 | return $view; |
||
209 | } |
||
210 | |||
211 | |||
212 | /** |
||
213 | * Adds the "response" helper to the view object |
||
214 | * |
||
215 | * @param \Aimeos\Base\View\Iface $view View object |
||
216 | * @return \Aimeos\Base\View\Iface Modified view object |
||
217 | */ |
||
218 | protected function addResponse( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
219 | { |
||
220 | $helper = new \Aimeos\Base\View\Helper\Response\Laravel( $view ); |
||
221 | $view->addHelper( 'response', $helper ); |
||
222 | |||
223 | return $view; |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Adds the "session" helper to the view object |
||
229 | * |
||
230 | * @param \Aimeos\Base\View\Iface $view View object |
||
231 | * @param \Aimeos\Base\Session\Iface $session Session object |
||
232 | * @return \Aimeos\Base\View\Iface Modified view object |
||
233 | */ |
||
234 | protected function addSession( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Session\Iface $session ) : \Aimeos\Base\View\Iface |
||
235 | { |
||
236 | $helper = new \Aimeos\Base\View\Helper\Session\Standard( $view, $session ); |
||
237 | $view->addHelper( 'session', $helper ); |
||
238 | |||
239 | return $view; |
||
240 | } |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Adds the "translate" helper to the view object |
||
245 | * |
||
246 | * @param \Aimeos\Base\View\Iface $view View object |
||
247 | * @param string|null $locale ISO language code, e.g. "de" or "de_CH" |
||
248 | * @return \Aimeos\Base\View\Iface Modified view object |
||
249 | */ |
||
250 | protected function addTranslate( \Aimeos\Base\View\Iface $view, string $locale = null ) : \Aimeos\Base\View\Iface |
||
251 | { |
||
252 | if( $locale !== null ) |
||
253 | { |
||
254 | $i18n = $this->i18n->get( array( $locale ) ); |
||
255 | $translation = $i18n[$locale]; |
||
256 | } |
||
257 | else |
||
258 | { |
||
259 | $translation = new \Aimeos\Base\Translation\None( 'en' ); |
||
260 | } |
||
261 | |||
262 | $helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $translation ); |
||
263 | $view->addHelper( 'translate', $helper ); |
||
264 | |||
265 | return $view; |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Adds the "url" helper to the view object |
||
271 | * |
||
272 | * @param \Aimeos\Base\View\Iface $view View object |
||
273 | * @return \Aimeos\Base\View\Iface Modified view object |
||
274 | */ |
||
275 | protected function addUrl( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
276 | { |
||
277 | $fixed = [ |
||
278 | 'site' => Request::input( 'site', env( 'SHOP_MULTISHOP' ) ? config( 'shop.mshop.locale.site', 'default' ) : null ), |
||
279 | 'locale' => Request::input( 'locale' ), |
||
280 | 'currency' => Request::input( 'currency' ) |
||
281 | ]; |
||
282 | |||
283 | if( Route::current() ) |
||
284 | { |
||
285 | $fixed['site'] = Request::route( 'site', $fixed['site'] ); |
||
286 | $fixed['locale'] = Request::route( 'locale', $fixed['locale'] ); |
||
287 | $fixed['currency'] = Request::route( 'currency', $fixed['currency'] ); |
||
288 | } |
||
289 | |||
290 | $helper = new \Aimeos\Base\View\Helper\Url\Laravel( $view, app( 'url' ), array_filter( $fixed ) ); |
||
291 | $view->addHelper( 'url', $helper ); |
||
292 | |||
293 | return $view; |
||
294 | } |
||
295 | } |