Issues (66)

src/Controller/Jqadm.php (7 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Slim\Controller;
12
13
use Psr\Container\ContainerInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
18
/**
19
 * Aimeos controller for the JQuery admin interface
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jqadm
25
{
26
	/**
27
	 * Returns the JS file content
28
	 *
29
	 * @param ContainerInterface $container Dependency injection container
30
	 * @param ServerRequestInterface $request Request object
31
	 * @param ResponseInterface $response Response object
32
	 * @param array $args Associative list of route parameters
33
	 * @return ResponseInterface Modified response object with generated output
34
	 */
35
	public static function fileAction( ContainerInterface $container, ServerRequestInterface $request,
36
		ResponseInterface $response, array $args ) : ResponseInterface
37
	{
38
		$contents = '';
39
		$files = array();
40
		$aimeos = $container->get( 'aimeos' );
41
		$type = ( isset( $args['type'] ) ? $args['type'] : 'js' );
42
43
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
44
		{
45
			foreach( $paths as $path )
46
			{
47
				$jsbAbsPath = $base . '/' . $path;
48
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
49
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
50
			}
51
		}
52
53
		foreach( $files as $file )
54
		{
55
			if( ( $content = file_get_contents( $file ) ) !== false ) {
56
				$contents .= $content;
57
			}
58
		}
59
60
		$response->getBody()->write( $contents );
61
62
		if( $type === 'js' ) {
63
			$response = $response->withHeader( 'Content-Type', 'application/javascript' );
64
		} elseif( $type === 'css' ) {
65
			$response = $response->withHeader( 'Content-Type', 'text/css' );
66
		}
67
68
		return $response;
69
	}
70
71
72
	/**
73
	 * Returns the HTML code for a copy of a resource object
74
	 *
75
	 * @param ContainerInterface $container Dependency injection container
76
	 * @param ServerRequestInterface $request Request object
77
	 * @param ResponseInterface $response Response object
78
	 * @param array $args Associative list of route parameters
79
	 * @return ResponseInterface Modified response object with generated output
80
	 */
81
	public static function copyAction( ContainerInterface $container, ServerRequestInterface $request,
82
		ResponseInterface $response, array $args ) : ResponseInterface
83
	{
84
		$cntl = self::createAdmin( $container, $request, $response, $args );
85
86
		if( ( $html = $cntl->copy() ) == '' ) {
87
			return $cntl->getView()->response();
88
		}
89
90
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

90
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
91
	}
92
93
94
	/**
95
	 * Returns the HTML code for a new resource object
96
	 *
97
	 * @param ContainerInterface $container Dependency injection container
98
	 * @param ServerRequestInterface $request Request object
99
	 * @param ResponseInterface $response Response object
100
	 * @param array $args Associative list of route parameters
101
	 * @return ResponseInterface Modified response object with generated output
102
	 */
103
	public static function createAction( ContainerInterface $container, ServerRequestInterface $request,
104
		ResponseInterface $response, array $args ) : ResponseInterface
105
	{
106
		$cntl = self::createAdmin( $container, $request, $response, $args );
107
108
		if( ( $html = $cntl->create() ) == '' ) {
109
			return $cntl->getView()->response();
110
		}
111
112
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

112
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
113
	}
114
115
116
	/**
117
	 * Deletes the resource object or a list of resource objects
118
	 *
119
	 * @param ContainerInterface $container Dependency injection container
120
	 * @param ServerRequestInterface $request Request object
121
	 * @param ResponseInterface $response Response object
122
	 * @param array $args Associative list of route parameters
123
	 * @return ResponseInterface Modified response object with generated output
124
	 */
125
	public static function deleteAction( ContainerInterface $container, ServerRequestInterface $request,
126
		ResponseInterface $response, array $args ) : ResponseInterface
127
	{
128
		$cntl = self::createAdmin( $container, $request, $response, $args );
129
130
		if( ( $html = $cntl->delete() ) == '' ) {
131
			return $cntl->getView()->response();
132
		}
133
134
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

134
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
135
	}
136
137
138
	/**
139
	 * Exports the requested resource object
140
	 *
141
	 * @param ContainerInterface $container Dependency injection container
142
	 * @param ServerRequestInterface $request Request object
143
	 * @param ResponseInterface $response Response object
144
	 * @param array $args Associative list of route parameters
145
	 * @return ResponseInterface Modified response object with generated output
146
	 */
147
	public static function exportAction( ContainerInterface $container, ServerRequestInterface $request,
148
		ResponseInterface $response, array $args ) : ResponseInterface
149
	{
150
		$cntl = self::createAdmin( $container, $request, $response, $args );
151
152
		if( ( $html = $cntl->export() ) == '' ) {
153
			return $cntl->getView()->response();
154
		}
155
156
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

156
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
157
	}
158
159
160
	/**
161
	 * Returns the HTML code for the requested resource object
162
	 *
163
	 * @param ContainerInterface $container Dependency injection container
164
	 * @param ServerRequestInterface $request Request object
165
	 * @param ResponseInterface $response Response object
166
	 * @param array $args Associative list of route parameters
167
	 * @return ResponseInterface Modified response object with generated output
168
	 */
169
	public static function getAction( ContainerInterface $container, ServerRequestInterface $request,
170
		ResponseInterface $response, array $args ) : ResponseInterface
171
	{
172
		$cntl = self::createAdmin( $container, $request, $response, $args );
173
174
		if( ( $html = $cntl->get() ) == '' ) {
175
			return $cntl->getView()->response();
176
		}
177
178
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

178
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
179
	}
180
181
182
	/**
183
	 * Saves a new resource object
184
	 *
185
	 * @param ContainerInterface $container Dependency injection container
186
	 * @param ServerRequestInterface $request Request object
187
	 * @param ResponseInterface $response Response object
188
	 * @param array $args Associative list of route parameters
189
	 * @return ResponseInterface Modified response object with generated output
190
	 */
191
	public static function saveAction( ContainerInterface $container, ServerRequestInterface $request,
192
		ResponseInterface $response, array $args ) : ResponseInterface
193
	{
194
		$cntl = self::createAdmin( $container, $request, $response, $args );
195
196
		if( ( $html = $cntl->save() ) == '' ) {
197
			return $cntl->getView()->response();
198
		}
199
200
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

200
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
201
	}
202
203
204
	/**
205
	 * Returns the HTML code for a list of resource objects
206
	 *
207
	 * @param ContainerInterface $container Dependency injection container
208
	 * @param ServerRequestInterface $request Request object
209
	 * @param ResponseInterface $response Response object
210
	 * @param array $args Associative list of route parameters
211
	 * @return ResponseInterface Modified response object with generated output
212
	 */
213
	public static function searchAction( ContainerInterface $container, ServerRequestInterface $request,
214
		ResponseInterface $response, array $args ) : ResponseInterface
215
	{
216
		$cntl = self::createAdmin( $container, $request, $response, $args );
217
218
		if( ( $html = $cntl->search() ) == '' ) {
219
			return $cntl->getView()->response();
220
		}
221
222
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
It seems like $html can also be of type null; however, parameter $content of Aimeos\Slim\Controller\Jqadm::getHtml() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

222
		return self::getHtml( $container, $response, /** @scrutinizer ignore-type */ $html );
Loading history...
223
	}
224
225
226
	/**
227
	 * Returns the resource controller
228
	 *
229
	 * @param ContainerInterface $container Dependency injection container
230
	 * @param ServerRequestInterface $request Request object
231
	 * @param ResponseInterface $response Response object
232
	 * @param array $args Associative list of route parameters
233
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
234
	 */
235
	protected static function createAdmin( ContainerInterface $container, ServerRequestInterface $request,
236
		ResponseInterface $response, array $args ) : \Aimeos\Admin\JQAdm\Iface
237
	{
238
		$aimeos = $container->get( 'aimeos' );
239
		$templatePaths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
240
		$params = $args + (array) $request->getParsedBody() + (array) $request->getQueryParams();
241
242
		$resource = ( isset( $params['resource'] ) ? $params['resource'] : null );
243
		$site = ( isset( $params['site'] ) ? $params['site'] : 'default' );
244
		$lang = ( isset( $params['lang'] ) ? $params['lang'] : 'en' );
245
246
		$context = $container->get( 'aimeos.context' )->get( false, $args, 'backend' );
247
		$context->setI18n( $container->get( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) );
248
		$context->setLocale( $container->get( 'aimeos.locale' )->getBackend( $context, $site ) );
249
250
		$view = $container->get( 'aimeos.view' )->create( $context, $request, $response, $args, $templatePaths, $lang );
251
252
		$view->aimeosType = 'Slim';
253
		$view->aimeosVersion = \Aimeos\Slim\Bootstrap::getVersion();
254
		$view->aimeosExtensions = implode( ',', $aimeos->getExtensions() );
255
256
		$context->setView( $view );
257
258
		return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource );
259
	}
260
261
262
	/**
263
	 * Returns the generated HTML code
264
	 *
265
	 * @param ContainerInterface $container Dependency injection container
266
	 * @param ResponseInterface $response Response object
267
	 * @param string $content Content from admin client
268
	 * @return ResponseInterface Modified response object with generated output
269
	 */
270
	protected static function getHtml( ContainerInterface $container, ResponseInterface $response,
271
		string $content ) : ResponseInterface
272
	{
273
		return $container->get( 'view' )->render( $response, 'Jqadm/index.html.twig', array( 'content' => $content ) );
274
	}
275
}
276