/
JqadmController.php
271 lines (211 loc) · 6.26 KB
/
JqadmController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* @license MIT, http://opensource.org/licenses/MIT
* @copyright Aimeos (aimeos.org), 2015-2016
* @package laravel
* @subpackage Controller
*/
namespace Aimeos\Shop\Controller;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
/**
* Aimeos controller for the JQuery admin interface
*
* @package laravel
* @subpackage Controller
*/
class JqadmController extends AdminController
{
use AuthorizesRequests;
/**
* Returns the JS file content
*
* @return \Illuminate\Http\Response Response object containing the generated output
*/
public function fileAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$contents = '';
$files = array();
$aimeos = app( 'aimeos' )->get();
$type = Route::input( 'type', Request::get( 'type', 'js' ) );
foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
{
foreach( $paths as $path )
{
$jsbAbsPath = $base . '/' . $path;
$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
$files = array_merge( $files, $jsb2->getFiles( $type ) );
}
}
foreach( $files as $file )
{
if( ( $content = file_get_contents( $file ) ) !== false ) {
$contents .= $content;
}
}
$response = response( $contents );
if( $type === 'js' ) {
$response->header( 'Content-Type', 'application/javascript' );
} elseif( $type === 'css' ) {
$response->header( 'Content-Type', 'text/css' );
}
return $response;
}
/**
* Returns the HTML code for a copy of a resource object
*
* @return string Generated output
*/
public function copyAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->copy() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Returns the HTML code for a new resource object
*
* @return string Generated output
*/
public function createAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->create() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Deletes the resource object or a list of resource objects
*
* @return string Generated output
*/
public function deleteAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->delete() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Exports the data for a resource object
*
* @return string Generated output
*/
public function exportAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->export() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Returns the HTML code for the requested resource object
*
* @return string Generated output
*/
public function getAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->get() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Saves a new resource object
*
* @return string Generated output
*/
public function saveAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->save() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Returns the HTML code for a list of resource objects
*
* @return string Generated output
*/
public function searchAction()
{
if( config( 'shop.authorize', true ) ) {
$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
}
$cntl = $this->createAdmin();
if( ( $html = $cntl->search() ) == '' ) {
return $cntl->response();
}
return $this->getHtml( $html );
}
/**
* Returns the resource controller
*
* @return \Aimeos\Admin\JQAdm\Iface JQAdm client
*/
protected function createAdmin() : \Aimeos\Admin\JQAdm\Iface
{
$site = Route::input( 'site', Request::get( 'site', 'default' ) );
$lang = Request::get( 'locale', config( 'app.locale', 'en' ) );
$resource = Route::input( 'resource' );
$aimeos = app( 'aimeos' )->get();
$paths = $aimeos->getTemplatePaths( 'admin/jqadm/templates' );
$context = app( 'aimeos.context' )->get( false, 'backend' );
$context->setI18n( app( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) );
$context->setLocale( app( 'aimeos.locale' )->getBackend( $context, $site ) );
$view = app( 'aimeos.view' )->create( $context, $paths, $lang );
$view->aimeosType = 'Laravel';
$view->aimeosVersion = app( 'aimeos' )->getVersion();
$view->aimeosExtensions = implode( ',', $aimeos->getExtensions() );
$context->setView( $view );
return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource );
}
/**
* Returns the generated HTML code
*
* @param string $content Content from admin client
* @return \Illuminate\Contracts\View\View View for rendering the output
*/
protected function getHtml( string $content )
{
$site = Route::input( 'site', Request::get( 'site', 'default' ) );
$lang = Request::get( 'locale', config( 'app.locale', 'en' ) );
return View::make( 'shop::jqadm.index', [
'content' => $content,
'site' => $site,
'locale' => $lang,
'localeDir' => in_array( $lang, ['ar', 'az', 'dv', 'fa', 'he', 'ku', 'ur'] ) ? 'rtl' : 'ltr',
'theme' => ( $_COOKIE['aimeos_backend_theme'] ?? '' ) == 'dark' ? 'dark' : 'light'
] );
}
}