Completed
Push — master ( c5b487...c39a64 )
by Aimeos
02:39
created

JqadmController::createAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 9
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
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' );
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 View Code Duplication
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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' );
84
		}
85
86
		$cntl = $this->createClient();
87
		return $this->getHtml( $cntl->copy() );
88
	}
89
90
91
	/**
92
	 * Returns the HTML code for a new resource object
93
	 *
94
	 * @return string Generated output
95
	 */
96 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...
97
	{
98
		if( config( 'shop.authorize', true ) ) {
99
			$this->authorize( 'admin' );
100
		}
101
102
		$cntl = $this->createClient();
103
		return $this->getHtml( $cntl->create() );
104
	}
105
106
107
	/**
108
	 * Deletes the resource object or a list of resource objects
109
	 *
110
	 * @return string Generated output
111
	 */
112 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...
113
	{
114
		if( config( 'shop.authorize', true ) ) {
115
			$this->authorize( 'admin' );
116
		}
117
118
		$cntl = $this->createClient();
119
		return $this->getHtml( $cntl->delete() . $cntl->search() );
120
	}
121
122
123
	/**
124
	 * Returns the HTML code for the requested resource object
125
	 *
126
	 * @return string Generated output
127
	 */
128 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...
129
	{
130
		if( config( 'shop.authorize', true ) ) {
131
			$this->authorize( 'admin' );
132
		}
133
134
		$cntl = $this->createClient();
135
		return $this->getHtml( $cntl->get() );
136
	}
137
138
139
	/**
140
	 * Saves a new resource object
141
	 *
142
	 * @return string Generated output
143
	 */
144 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...
145
	{
146
		if( config( 'shop.authorize', true ) ) {
147
			$this->authorize( 'admin' );
148
		}
149
150
		$cntl = $this->createClient();
151
		return $this->getHtml( ( $cntl->save() ? : $cntl->search() ) );
152
	}
153
154
155
	/**
156
	 * Returns the HTML code for a list of resource objects
157
	 *
158
	 * @return string Generated output
159
	 */
160 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...
161
	{
162
		if( config( 'shop.authorize', true ) ) {
163
			$this->authorize( 'admin' );
164
		}
165
166
		$cntl = $this->createClient();
167
		return $this->getHtml( $cntl->search() );
168
	}
169
170
171
	/**
172
	 * Returns the resource controller
173
	 *
174
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
175
	 */
176
	protected function createClient()
177
	{
178
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
179
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
180
		$resource = Route::input( 'resource' );
181
182
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
183
		$templatePaths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
184
185
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
186
		$context = $this->setLocale( $context, $site, $lang );
187
188
		$view = app( '\Aimeos\Shop\Base\View' )->create( $context->getConfig(), $templatePaths, $lang );
189
		$context->setView( $view );
190
191
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $templatePaths, $resource );
192
	}
193
194
195
	/**
196
	 * Returns the generated HTML code
197
	 *
198
	 * @param string $content Content from admin client
199
	 * @return \Illuminate\Contracts\View\View View for rendering the output
200
	 */
201
	protected function getHtml( $content )
202
	{
203
		$version = app( '\Aimeos\Shop\Base\Aimeos' )->getVersion();
204
		$content = str_replace( ['{type}', '{version}'], ['Laravel', $version], $content );
205
206
		return View::make( 'shop::jqadm.index', array( 'content' => $content ) );
207
	}
208
209
210
	/**
211
	 * Sets the locale item in the given context
212
	 *
213
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
214
	 * @param string $site Unique site code
215
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
216
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
217
	 */
218 View Code Duplication
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $site, $lang )
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...
219
	{
220
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
221
222
		try
223
		{
224
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
225
			$localeItem->setLanguageId( null );
226
			$localeItem->setCurrencyId( null );
227
		}
228
		catch( \Aimeos\MShop\Locale\Exception $e )
229
		{
230
			$localeItem = $localeManager->createItem();
231
		}
232
233
		$context->setLocale( $localeItem );
234
		$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $lang, 'en' ) ) );
235
236
		return $context;
237
	}
238
}
239