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