Completed
Push — master ( d5d314...f81167 )
by Aimeos
01:21
created

JqadmController::createAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Neos\Flow\Annotations as Flow;
14
15
16
/**
17
 * Controller for JQuery based adminisration interface.
18
 * @package flow
19
 * @subpackage Controller
20
 */
21
class JqadmController extends \Neos\Flow\Mvc\Controller\ActionController
22
{
23
	/**
24
	 * @var \Aimeos\Shop\Base\Aimeos
25
	 * @Flow\Inject
26
	 */
27
	protected $aimeos;
28
29
	/**
30
	 * @var \Aimeos\Shop\Base\Context
31
	 * @Flow\Inject
32
	 */
33
	protected $context;
34
35
	/**
36
	 * @var \Aimeos\Shop\Base\I18n
37
	 * @Flow\Inject
38
	 */
39
	protected $i18n;
40
41
	/**
42
	 * @var \Aimeos\Shop\Base\Locale
43
	 * @Flow\Inject
44
	 */
45
	protected $locale;
46
47
	/**
48
	 * @var \Aimeos\Shop\Base\View
49
	 * @Flow\Inject
50
	 */
51
	protected $viewbase;
52
53
54
	/**
55
	 * Returns the JS file content
56
	 *
57
	 * @return \Neos\Flow\Http\Response Response object
58
	 */
59
	public function fileAction()
60
	{
61
		$files = array();
62
		$aimeos = $this->aimeos->get();
63
		$type = $this->request->getArgument( 'type' );
64
65
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
66
		{
67
			foreach( $paths as $path )
68
			{
69
				$jsbAbsPath = $base . '/' . $path;
70
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
71
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
72
			}
73
		}
74
75
		foreach( $files as $file )
76
		{
77
			if( ( $content = file_get_contents( $file ) ) !== false ) {
78
				$this->response->appendContent( $content );
0 ignored issues
show
Deprecated Code introduced by
The method Neos\Flow\Http\Response::appendContent() has been deprecated with message: Since Flow 5.1, without replacement

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
			}
80
		}
81
82
		if( $type === 'js' ) {
83
			$this->response->setHeader( 'Content-Type', 'application/javascript' );
0 ignored issues
show
Deprecated Code introduced by
The method Neos\Flow\Http\AbstractMessage::setHeader() has been deprecated with message: Since Flow 5.1, use withHeader or withAddedHeader instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
		} elseif( $type === 'css' ) {
85
			$this->response->setHeader( 'Content-Type', 'text/css' );
0 ignored issues
show
Deprecated Code introduced by
The method Neos\Flow\Http\AbstractMessage::setHeader() has been deprecated with message: Since Flow 5.1, use withHeader or withAddedHeader instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
		}
87
	}
88
89
90
	/**
91
	 * Returns the HTML code for a copy of a resource object
92
	 *
93
	 * @param string Resource location, e.g. "product"
94
	 * @param string $site Unique site code
95
	 * @return string Generated output
96
	 */
97
	public function copyAction( $resource, $site = 'default' )
98
	{
99
		$cntl = $this->createAdmin( $site, $resource );
100
101
		if( ( $html = $cntl->copy() ) == '' ) {
102
			return $this->setPsrResponse( $cntl->getView()->response() );
103
		}
104
105
		return $this->getHtml( $html );
106
	}
107
108
109
	/**
110
	 * Returns the HTML code for a new resource object
111
	 *
112
	 * @param string Resource location, e.g. "product"
113
	 * @param string $site Unique site code
114
	 * @return string Generated output
115
	 */
116
	public function createAction( $resource, $site = 'default' )
117
	{
118
		$cntl = $this->createAdmin( $site, $resource );
119
120
		if( ( $html = $cntl->create() ) == '' ) {
121
			return $this->setPsrResponse( $cntl->getView()->response() );
122
		}
123
124
		return $this->getHtml( $html );
125
	}
126
127
128
	/**
129
	 * Deletes the resource object or a list of resource objects
130
	 *
131
	 * @param string Resource location, e.g. "product"
132
	 * @param string $site Unique site code
133
	 * @return string Generated output
134
	 */
135
	public function deleteAction( $resource, $site = 'default' )
136
	{
137
		$cntl = $this->createAdmin( $site, $resource );
138
139
		if( ( $html = $cntl->delete() ) == '' ) {
140
			return $this->setPsrResponse( $cntl->getView()->response() );
141
		}
142
143
		return $this->getHtml( $html );
144
	}
145
146
147
	/**
148
	 * Exports the resource object
149
	 *
150
	 * @param string Resource location, e.g. "order"
151
	 * @param string $site Unique site code
152
	 * @return string Generated output
153
	 */
154
	public function exportAction( $resource, $site = 'default' )
155
	{
156
		$cntl = $this->createAdmin( $site, $resource );
157
158
		if( ( $html = $cntl->export() ) == '' ) {
159
			return $this->setPsrResponse( $cntl->getView()->response() );
160
		}
161
162
		return $this->getHtml( $html );
163
	}
164
165
166
	/**
167
	 * Returns the HTML code for the requested resource object
168
	 *
169
	 * @param string Resource location, e.g. "product"
170
	 * @param string $site Unique site code
171
	 * @return string Generated output
172
	 */
173
	public function getAction( $resource, $site = 'default' )
174
	{
175
		$cntl = $this->createAdmin( $site, $resource );
176
177
		if( ( $html = $cntl->get() ) == '' ) {
178
			return $this->setPsrResponse( $cntl->getView()->response() );
179
		}
180
181
		return $this->getHtml( $html );
182
	}
183
184
185
	/**
186
	 * Saves a new resource object
187
	 *
188
	 * @param string Resource location, e.g. "product"
189
	 * @param string $site Unique site code
190
	 * @return string Generated output
191
	 */
192
	public function saveAction( $resource, $site = 'default' )
193
	{
194
		$cntl = $this->createAdmin( $site, $resource );
195
196
		if( ( $html = $cntl->save() ) == '' ) {
197
			return $this->setPsrResponse( $cntl->getView()->response() );
198
		}
199
200
		return $this->getHtml( $html );
201
	}
202
203
204
	/**
205
	 * Returns the HTML code for a list of resource objects
206
	 *
207
	 * @param string Resource location, e.g. "product"
208
	 * @param string $site Unique site code
209
	 * @return string Generated output
210
	 */
211
	public function searchAction( $resource, $site = 'default' )
212
	{
213
		$cntl = $this->createAdmin( $site, $resource );
214
215
		if( ( $html = $cntl->search() ) == '' ) {
216
			return $this->setPsrResponse( $cntl->getView()->response() );
217
		}
218
219
		return $this->getHtml( $html );
220
	}
221
222
223
	/**
224
	 * Returns the resource controller
225
	 *
226
	 * @param string $sitecode Unique site code
227
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client object
228
	 */
229
	protected function createAdmin( $sitecode, $resource )
230
	{
231
		$aimeos = $this->aimeos->get();
232
		$paths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
233
		$lang = ( $this->request->hasArgument( 'lang' ) ? $this->request->getArgument( 'lang' ) : 'en' );
234
235
		$context = $this->context->get( null, 'backend' );
236
		$context->setI18n( $this->i18n->get( array( $lang, 'en' ) ) );
237
		$context->setLocale( $this->locale->getBackend( $context, $sitecode ) );
238
239
		$view = $this->viewbase->create( $context, $this->uriBuilder, $paths, $this->request, $lang );
240
241
		$view->aimeosType = 'Flow';
0 ignored issues
show
Bug introduced by
Accessing aimeosType on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
242
		$view->aimeosVersion = $this->aimeos->getVersion();
0 ignored issues
show
Bug introduced by
Accessing aimeosVersion on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
243
		$view->aimeosExtensions = implode( ',', $aimeos->getExtensions() );
0 ignored issues
show
Bug introduced by
Accessing aimeosExtensions on the interface Aimeos\MW\View\Iface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
244
245
		$context->setView( $view );
246
247
		return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource );
248
	}
249
250
251
	/**
252
	 * Returns the generated view including the HTML code
253
	 *
254
	 * @param string $content Content from admin client
255
	 */
256
	protected function getHtml( $content )
257
	{
258
		$this->view->assign( 'content', $content );
259
		return $this->view->render( 'index' );
0 ignored issues
show
Unused Code introduced by
The call to ViewInterface::render() has too many arguments starting with 'index'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
260
	}
261
262
263
	/**
264
	 * Set the response data from a PSR-7 response object
265
	 *
266
	 * @param \Psr\Http\Message\ResponseInterface $response PSR-7 response object
267
	 * @return string Response message content
268
	 */
269
	protected function setPsrResponse( \Psr\Http\Message\ResponseInterface $response )
270
	{
271
		$this->response->setStatus( $response->getStatusCode() );
0 ignored issues
show
Deprecated Code introduced by
The method Neos\Flow\Http\Response::setStatus() has been deprecated with message: Since Flow 5.1, use withStatus

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
272
273
		foreach( $response->getHeaders() as $key => $value ) {
274
			$this->response->setHeader( $key, $value );
0 ignored issues
show
Deprecated Code introduced by
The method Neos\Flow\Http\AbstractMessage::setHeader() has been deprecated with message: Since Flow 5.1, use withHeader or withAddedHeader instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
275
		}
276
277
		return (string) $response->getBody();
278
	}
279
}
280