Passed
Push — master ( fb8735...9c9a31 )
by Aimeos
05:27
created

src/Controller/JqadmController.php (5 issues)

1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 */
7
8
9
namespace Aimeos\Shop\Controller;
10
11
use Illuminate\Support\Facades\View;
12
use Illuminate\Support\Facades\Route;
13
use Illuminate\Support\Facades\Request;
14
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
15
16
17
/**
18
 * Aimeos controller for the JQuery admin interface
19
 */
20
class JqadmController extends AdminController
21
{
22
	use AuthorizesRequests;
23
24
25
	/**
26
	 * Returns the JS file content
27
	 *
28
	 * @return \Illuminate\Http\Response Response object containing the generated output
29
	 */
30
	public function fileAction()
31
	{
32
		if( config( 'shop.authorize', true ) ) {
33
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
34
		}
35
36
		$contents = '';
37
		$files = array();
38
		$aimeos = app( 'aimeos' )->get();
0 ignored issues
show
The call to Psr\Container\ContainerInterface::get() has too few arguments starting with id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
		$aimeos = app( 'aimeos' )->/** @scrutinizer ignore-call */ get();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
39
		$type = Route::input( 'type', Request::get( 'type', 'js' ) );
40
41
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
42
		{
43
			foreach( $paths as $path )
44
			{
45
				$jsbAbsPath = $base . '/' . $path;
46
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
47
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
48
			}
49
		}
50
51
		foreach( $files as $file )
52
		{
53
			if( ( $content = file_get_contents( $file ) ) !== false ) {
54
				$contents .= $content;
55
			}
56
		}
57
58
		$response = response( $contents );
59
60
		if( $type === 'js' ) {
61
			$response->header( 'Content-Type', 'application/javascript' );
62
		} elseif( $type === 'css' ) {
63
			$response->header( 'Content-Type', 'text/css' );
64
		}
65
66
		return $response->header( 'Cache-Control', 'public, max-age=3600' );
67
	}
68
69
70
	/**
71
	 * Returns the HTML code for batch operations on a resource object
72
	 *
73
	 * @return string Generated output
74
	 */
75
	public function batchAction()
76
	{
77
		if( config( 'shop.authorize', true ) ) {
78
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
79
		}
80
81
		$cntl = $this->createAdmin();
82
83
		if( ( $html = $cntl->batch() ) == '' ) {
84
			return $cntl->response();
85
		}
86
87
		return $this->getHtml( $html );
88
	}
89
90
91
	/**
92
	 * Returns the HTML code for a copy of a resource object
93
	 *
94
	 * @return string Generated output
95
	 */
96
	public function copyAction()
97
	{
98
		if( config( 'shop.authorize', true ) ) {
99
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
100
		}
101
102
		$cntl = $this->createAdmin();
103
104
		if( ( $html = $cntl->copy() ) == '' ) {
105
			return $cntl->response();
106
		}
107
108
		return $this->getHtml( $html );
109
	}
110
111
112
	/**
113
	 * Returns the HTML code for a new resource object
114
	 *
115
	 * @return string Generated output
116
	 */
117
	public function createAction()
118
	{
119
		if( config( 'shop.authorize', true ) ) {
120
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
121
		}
122
123
		$cntl = $this->createAdmin();
124
125
		if( ( $html = $cntl->create() ) == '' ) {
126
			return $cntl->response();
127
		}
128
129
		return $this->getHtml( $html );
130
	}
131
132
133
	/**
134
	 * Deletes the resource object or a list of resource objects
135
	 *
136
	 * @return string Generated output
137
	 */
138
	public function deleteAction()
139
	{
140
		if( config( 'shop.authorize', true ) ) {
141
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
142
		}
143
144
		$cntl = $this->createAdmin();
145
146
		if( ( $html = $cntl->delete() ) == '' ) {
147
			return $cntl->response();
148
		}
149
150
		return $this->getHtml( $html );
151
	}
152
153
154
	/**
155
	 * Exports the data for a resource object
156
	 *
157
	 * @return string Generated output
158
	 */
159
	public function exportAction()
160
	{
161
		if( config( 'shop.authorize', true ) ) {
162
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
163
		}
164
165
		$cntl = $this->createAdmin();
166
167
		if( ( $html = $cntl->export() ) == '' ) {
168
			return $cntl->response();
169
		}
170
171
		return $this->getHtml( $html );
172
	}
173
174
175
	/**
176
	 * Returns the HTML code for the requested resource object
177
	 *
178
	 * @return string Generated output
179
	 */
180
	public function getAction()
181
	{
182
		if( config( 'shop.authorize', true ) ) {
183
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
184
		}
185
186
		$cntl = $this->createAdmin();
187
188
		if( ( $html = $cntl->get() ) == '' ) {
189
			return $cntl->response();
190
		}
191
192
		return $this->getHtml( $html );
193
	}
194
195
196
	/**
197
	 * Saves a new resource object
198
	 *
199
	 * @return string Generated output
200
	 */
201
	public function saveAction()
202
	{
203
		if( config( 'shop.authorize', true ) ) {
204
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
205
		}
206
207
		$cntl = $this->createAdmin();
208
209
		if( ( $html = $cntl->save() ) == '' ) {
210
			return $cntl->response();
211
		}
212
213
		return $this->getHtml( $html );
214
	}
215
216
217
	/**
218
	 * Returns the HTML code for a list of resource objects
219
	 *
220
	 * @return string Generated output
221
	 */
222
	public function searchAction()
223
	{
224
		if( config( 'shop.authorize', true ) ) {
225
			$this->authorize( 'admin', [JqadmController::class, config( 'shop.roles', ['admin', 'editor'] )] );
226
		}
227
228
		$cntl = $this->createAdmin();
229
230
		if( ( $html = $cntl->search() ) == '' ) {
231
			return $cntl->response();
232
		}
233
234
		return $this->getHtml( $html );
235
	}
236
237
238
	/**
239
	 * Returns the resource controller
240
	 *
241
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
242
	 */
243
	protected function createAdmin() : \Aimeos\Admin\JQAdm\Iface
244
	{
245
		$site = Route::input( 'site', Request::get( 'site', config( 'shop.mshop.locale.site', 'default' ) ) );
246
		$lang = Request::get( 'locale', config( 'app.locale', 'en' ) );
247
		$resource = Route::input( 'resource' );
248
249
		$aimeos = app( 'aimeos' )->get();
0 ignored issues
show
The call to Psr\Container\ContainerInterface::get() has too few arguments starting with id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
		$aimeos = app( 'aimeos' )->/** @scrutinizer ignore-call */ get();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
250
251
		$context = app( 'aimeos.context' )->get( false, 'backend' );
0 ignored issues
show
false of type false is incompatible with the type string expected by parameter $id of Psr\Container\ContainerInterface::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

251
		$context = app( 'aimeos.context' )->get( /** @scrutinizer ignore-type */ false, 'backend' );
Loading history...
The call to Psr\Container\ContainerInterface::get() has too many arguments starting with 'backend'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
		$context = app( 'aimeos.context' )->/** @scrutinizer ignore-call */ get( false, 'backend' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
252
		$context->setI18n( app( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) );
0 ignored issues
show
array($lang, 'en') of type array<integer,mixed|string> is incompatible with the type string expected by parameter $id of Psr\Container\ContainerInterface::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
		$context->setI18n( app( 'aimeos.i18n' )->get( /** @scrutinizer ignore-type */ array( $lang, 'en' ) ) );
Loading history...
253
		$context->setLocale( app( 'aimeos.locale' )->getBackend( $context, $site ) );
254
255
		$siteManager = \Aimeos\MShop::create( $context, 'locale/site');
256
		$context->config()->apply( $siteManager->find( $site )->getConfig() );
257
258
        	$paths = $aimeos->getTemplatePaths( 'admin/jqadm/templates', $context->locale()->getSiteItem()->getTheme() );
259
		$view = app( 'aimeos.view' )->create( $context, $paths, $lang );
260
261
		$view->aimeosType = 'Laravel';
262
		$view->aimeosVersion = app( 'aimeos' )->getVersion();
263
		$view->aimeosExtensions = implode( ',', $aimeos->getExtensions() );
264
265
		$context->setView( $view );
266
267
		return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource );
268
	}
269
270
271
	/**
272
	 * Returns the generated HTML code
273
	 *
274
	 * @param string $content Content from admin client
275
	 * @return \Illuminate\Contracts\View\View View for rendering the output
276
	 */
277
	protected function getHtml( string $content )
278
	{
279
		$site = Route::input( 'site', Request::get( 'site', config( 'shop.mshop.locale.site', 'default' ) ) );
280
		$lang = Request::get( 'locale', config( 'app.locale', 'en' ) );
281
282
		return View::make( 'shop::jqadm.index', [
283
			'content' => $content,
284
			'site' => $site,
285
			'locale' => $lang,
286
			'localeDir' => in_array( $lang, ['ar', 'az', 'dv', 'fa', 'he', 'ku', 'ur'] ) ? 'rtl' : 'ltr',
287
			'theme' => ( $_COOKIE['aimeos_backend_theme'] ?? '' ) == 'dark' ? 'dark' : 'light'
288
		] );
289
	}
290
}
291