Completed
Push — master ( 0a4763...f5ea6d )
by Aimeos
49:24 queued 18:11
created

ExtadmController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 21
c 5
b 0
f 2
lcom 1
cbo 8
dl 0
loc 229
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 58 5
A doAction() 0 17 2
B fileAction() 0 29 6
A getJsonLanguages() 0 10 2
A getJsonClientConfig() 0 5 1
A getJsonClientI18n() 0 11 1
A getJsonSiteItem() 0 14 2
A setLocale() 0 14 2
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package laravel
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Facades\View;
15
use Illuminate\Support\Facades\Input;
16
use Illuminate\Support\Facades\Route;
17
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
18
19
20
/**
21
 * Controller providing the ExtJS administration interface
22
 *
23
 * @package laravel
24
 * @subpackage Controller
25
 */
26
class ExtadmController extends AdminController
27
{
28
	use AuthorizesRequests;
29
30
31
	/**
32
	 * Returns the initial HTML view for the admin interface.
33
	 *
34
	 * @param \Illuminate\Http\Request $request Laravel request object
35
	 * @return \Illuminate\Contracts\View\View View for rendering the output
36
	 */
37
	public function indexAction( Request $request )
38
	{
39
		if( config( 'shop.authorize', true ) ) {
40
			$this->authorize( 'admin' );
41
		}
42
43
		$site = Route::input( 'site', 'default' );
44
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
45
46
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' );
47
48
		$bootstrap = $aimeos->get();
49
		$cntlPaths = $bootstrap->getCustomPaths( 'controller/extjs' );
50
51
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
52
		$context = $this->setLocale( $context, $site, $lang );
53
54
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
55
		$cssFiles = array();
56
57
		foreach( $bootstrap->getCustomPaths( 'admin/extjs' ) as $base => $paths )
58
		{
59
			foreach( $paths as $path )
60
			{
61
				$jsbAbsPath = $base . '/' . $path;
62
63
				if( !is_file( $jsbAbsPath ) ) {
64
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
65
				}
66
67
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
68
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
69
			}
70
		}
71
72
		$jqadmUrl = route( 'aimeos_shop_jqadm_search', array( 'site' => $site, 'resource' => 'product' ) );
73
		$jsonUrl = route( 'aimeos_shop_extadm_json', array( 'site' => $site, '_token' => csrf_token() ) );
74
		$adminUrl = route( 'aimeos_shop_extadm', array( 'site' => '<site>', 'lang' => '<lang>', 'tab' => '<tab>' ) );
75
76
		$vars = array(
77
			'lang' => $lang,
78
			'cssFiles' => $cssFiles,
79
			'languages' => $this->getJsonLanguages( $context),
80
			'config' => $this->getJsonClientConfig( $context ),
81
			'site' => $this->getJsonSiteItem( $context, $site ),
82
			'i18nContent' => $this->getJsonClientI18n( $bootstrap->getI18nPaths(), $lang ),
83
			'searchSchemas' => $controller->getJsonSearchSchemas(),
84
			'itemSchemas' => $controller->getJsonItemSchemas(),
85
			'smd' => $controller->getJsonSmd( $jsonUrl ),
86
			'urlTemplate' => str_replace( ['<', '>'], ['{', '}'], urldecode( $adminUrl ) ),
87
			'uploaddir' => config( 'shop::uploaddir' ),
88
			'activeTab' => Input::get( 'tab', 0 ),
89
			'version' => $aimeos->getVersion(),
90
			'jqadmurl' => $jqadmUrl,
91
		);
92
93
		return View::make( 'shop::extadm.index', $vars );
94
	}
95
96
97
	/**
98
	 * Single entry point for all JSON admin requests.
99
	 *
100
	 * @return \Illuminate\Contracts\View\View View for rendering the output
101
	 */
102
	public function doAction()
103
	{
104
		if( config( 'shop.authorize', true ) ) {
105
			$this->authorize( 'admin' );
106
		}
107
108
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
109
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
110
111
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false );
112
		$context = $this->setLocale( $context );
113
114
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
115
116
		$response = $controller->process( Input::instance()->request->all(), 'php://input' );
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\Input. Did you maybe mean clearResolvedInstance()?

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...
117
		return View::make( 'shop::extadm.do', array( 'output' => $response ) );
118
	}
119
120
121
	/**
122
	 * Returns the JS file content
123
	 *
124
	 * @return \Illuminate\Http\Response Response object containing the generated output
125
	 */
126
	public function fileAction()
127
	{
128
		if( config( 'shop.authorize', true ) ) {
129
			$this->authorize( 'admin' );
130
		}
131
132
		$contents = '';
133
		$jsFiles = array();
134
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
135
136
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
137
		{
138
			foreach( $paths as $path )
139
			{
140
				$jsbAbsPath = $base . '/' . $path;
141
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
142
				$jsFiles = array_merge( $jsFiles, $jsb2->getFiles( 'js' ) );
143
			}
144
		}
145
146
		foreach( $jsFiles as $file )
147
		{
148
			if( ( $content = file_get_contents( $file ) ) !== false ) {
149
				$contents .= $content;
150
			}
151
		}
152
153
		return response( $contents )->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...
154
	}
155
156
157
	/**
158
	 * Creates a list of all available translations.
159
	 *
160
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
161
	 * @return array List of language IDs with labels
162
	 */
163
	protected function getJsonLanguages( \Aimeos\MShop\Context\Item\Iface $context )
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
	{
165
		$result = array();
166
167
		foreach( app( '\Aimeos\Shop\Base\Aimeos' )->get()->getI18nList( 'admin' ) as $id ) {
168
			$result[] = array( 'id' => $id, 'label' => $id );
169
		}
170
171
		return json_encode( $result );
172
	}
173
174
175
	/**
176
	 * Returns the JSON encoded configuration for the ExtJS client.
177
	 *
178
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
179
	 * @return string JSON encoded configuration object
180
	 */
181
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
182
	{
183
		$config = $context->getConfig()->get( 'admin/extjs', array() );
184
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
185
	}
186
187
188
	/**
189
	 * Returns the JSON encoded translations for the ExtJS client.
190
	 *
191
	 * @param array $i18nPaths List of file system paths which contain the translation files
192
	 * @param string $lang ISO language code like "en" or "en_GB"
193
	 * @return string JSON encoded translation object
194
	 */
195
	protected function getJsonClientI18n( array $i18nPaths, $lang )
196
	{
197
		$i18n = new \Aimeos\MW\Translation\Zend2( $i18nPaths, 'gettext', $lang, array( 'disableNotices' => true ) );
198
199
		$content = array(
200
			'admin' => $i18n->getAll( 'admin' ),
201
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
202
		);
203
204
		return json_encode( $content, JSON_FORCE_OBJECT );
205
	}
206
207
208
	/**
209
	 * Returns the JSON encoded site item.
210
	 *
211
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
212
	 * @param string $site Unique site code
213
	 * @return string JSON encoded site item object
214
	 * @throws Exception If no site item was found for the code
215
	 */
216
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
217
	{
218
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
219
220
		$criteria = $manager->createSearch();
221
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
222
		$items = $manager->searchItems( $criteria );
223
224
		if( ( $item = reset( $items ) ) === false ) {
225
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
226
		}
227
228
		return json_encode( $item->toArray() );
229
	}
230
231
232
	/**
233
	 * Sets the locale item in the given context
234
	 *
235
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
236
	 * @param string $sitecode Unique site code
237
	 * @param string $locale ISO language code, e.g. "en" or "en_GB"
238
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
239
	 */
240
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $locale = null )
241
	{
242
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
243
244
		try {
245
			$localeItem = $localeManager->bootstrap( $sitecode, $locale, '', false );
246
		} catch( \Aimeos\MShop\Locale\Exception $e ) {
247
			$localeItem = $localeManager->createItem();
248
		}
249
250
		$context->setLocale( $localeItem );
251
252
		return $context;
253
	}
254
}