Completed
Push — master ( 519ef0...6cdfe5 )
by Aimeos
15:36
created

JqadmController::deleteAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 14
loc 14
rs 9.4285
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Illuminate\Support\Facades\View;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Input;
16
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
17
18
19
/**
20
 * Aimeos controller for the JQuery admin interface
21
 *
22
 * @package laravel
23
 * @subpackage Controller
24
 */
25
class JqadmController extends AdminController
26
{
27
	use AuthorizesRequests;
28
29
30
	/**
31
	 * Returns the JS file content
32
	 *
33
	 * @return \Illuminate\Http\Response Response object containing the generated output
34
	 */
35
	public function fileAction()
36
	{
37
		if( config( 'shop.authorize', true ) ) {
38
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
39
		}
40
41
		$contents = '';
42
		$files = array();
43
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
44
		$type = Route::input( 'type', Input::get( 'type', 'js' ) );
45
46
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
47
		{
48
			foreach( $paths as $path )
49
			{
50
				$jsbAbsPath = $base . '/' . $path;
51
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
52
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
53
			}
54
		}
55
56
		foreach( $files as $file )
57
		{
58
			if( ( $content = file_get_contents( $file ) ) !== false ) {
59
				$contents .= $content;
60
			}
61
		}
62
63
		$response = response( $contents );
64
65
		if( $type === 'js' ) {
66
			$response->header( 'Content-Type', 'application/javascript' );
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
		} elseif( $type === 'css' ) {
68
			$response->header( 'Content-Type', 'text/css' );
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
69
		}
70
71
		return $response;
72
	}
73
74
75
	/**
76
	 * Returns the HTML code for a copy of a resource object
77
	 *
78
	 * @return string Generated output
79
	 */
80 View Code Duplication
	public function copyAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
	{
82
		if( config( 'shop.authorize', true ) ) {
83
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
84
		}
85
86
		$cntl = $this->createClient();
87
88
		if( ( $html = $cntl->copy() ) == '' ) {
89
			return $cntl->getView()->response();
90
		}
91
92
		return $this->getHtml( $html );
93
	}
94
95
96
	/**
97
	 * Returns the HTML code for a new resource object
98
	 *
99
	 * @return string Generated output
100
	 */
101 View Code Duplication
	public function createAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
	{
103
		if( config( 'shop.authorize', true ) ) {
104
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
105
		}
106
107
		$cntl = $this->createClient();
108
109
		if( ( $html = $cntl->create() ) == '' ) {
110
			return $cntl->getView()->response();
111
		}
112
113
		return $this->getHtml( $html );
114
	}
115
116
117
	/**
118
	 * Deletes the resource object or a list of resource objects
119
	 *
120
	 * @return string Generated output
121
	 */
122 View Code Duplication
	public function deleteAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
	{
124
		if( config( 'shop.authorize', true ) ) {
125
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
126
		}
127
128
		$cntl = $this->createClient();
129
130
		if( ( $html = $cntl->delete() ) == '' ) {
131
			return $cntl->getView()->response();
132
		}
133
134
		return $this->getHtml( $html );
135
	}
136
137
138
	/**
139
	 * Exports the data for a resource object
140
	 *
141
	 * @return string Generated output
142
	 */
143 View Code Duplication
	public function exportAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
	{
145
		if( config( 'shop.authorize', true ) ) {
146
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
147
		}
148
149
		$cntl = $this->createClient();
150
151
		if( ( $html = $cntl->export() ) == '' ) {
152
			return $cntl->getView()->response();
153
		}
154
155
		return $this->getHtml( $html );
156
	}
157
158
159
	/**
160
	 * Returns the HTML code for the requested resource object
161
	 *
162
	 * @return string Generated output
163
	 */
164 View Code Duplication
	public function getAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
	{
166
		if( config( 'shop.authorize', true ) ) {
167
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
168
		}
169
170
		$cntl = $this->createClient();
171
172
		if( ( $html = $cntl->get() ) == '' ) {
173
			return $cntl->getView()->response();
174
		}
175
176
		return $this->getHtml( $html );
177
	}
178
179
180
	/**
181
	 * Saves a new resource object
182
	 *
183
	 * @return string Generated output
184
	 */
185 View Code Duplication
	public function saveAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
	{
187
		if( config( 'shop.authorize', true ) ) {
188
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
189
		}
190
191
		$cntl = $this->createClient();
192
193
		if( ( $html = $cntl->save() ) == '' ) {
194
			return $cntl->getView()->response();
195
		}
196
197
		return $this->getHtml( $html );
198
	}
199
200
201
	/**
202
	 * Returns the HTML code for a list of resource objects
203
	 *
204
	 * @return string Generated output
205
	 */
206 View Code Duplication
	public function searchAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
	{
208
		if( config( 'shop.authorize', true ) ) {
209
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor', 'super']] );
210
		}
211
212
		$cntl = $this->createClient();
213
214
		if( ( $html = $cntl->search() ) == '' ) {
215
			return $cntl->getView()->response();
216
		}
217
218
		return $this->getHtml( $html );
219
	}
220
221
222
	/**
223
	 * Returns the resource controller
224
	 *
225
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
226
	 */
227
	protected function createClient()
228
	{
229
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
230
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
231
		$resource = Route::input( 'resource' );
232
233
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
234
		$templatePaths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
235
236
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false, 'backend' );
237
		$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $lang, 'en' ) ) );
238
		$context->setLocale( app('\Aimeos\Shop\Base\Locale')->getBackend( $context, $site ) );
239
		$context->setView( app( '\Aimeos\Shop\Base\View' )->create( $context, $templatePaths, $lang ) );
240
241
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $aimeos, $resource );
242
	}
243
244
245
	/**
246
	 * Returns the generated HTML code
247
	 *
248
	 * @param string $content Content from admin client
249
	 * @return \Illuminate\Contracts\View\View View for rendering the output
250
	 */
251
	protected function getHtml( $content )
252
	{
253
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' );
254
		$extnames = implode( ',', $aimeos->get()->getExtensions() );
255
		$version = $aimeos->getVersion();
256
257
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
258
		$content = str_replace( ['{type}', '{version}', '{extensions}'], ['Laravel', $version, $extnames], $content );
259
260
		return View::make( 'shop::jqadm.index', array( 'content' => $content, 'site' => $site ) );
261
	}
262
}
263