Completed
Push — master ( 844367...ab725b )
by Aimeos
02:57
created

ExtadmController::setLocale()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 4
nop 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', [['admin']] );
41
		}
42
43
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
44
		$lang = Route::input( 'lang', Input::get( 'lang', config( 'app.locale', 'en' ) ) );
45
		$tab = Route::input( 'tab', Input::get( 'tab', 0 ) );
46
47
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' );
48
49
		$bootstrap = $aimeos->get();
50
		$cntlPaths = $bootstrap->getCustomPaths( 'controller/extjs' );
51
52
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false, 'backend' );
53
		$context->setLocale( app('\Aimeos\Shop\Base\Locale')->getBackend( $context, $site ) );
54
55
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
56
		$cssFiles = array();
57
58
		foreach( $bootstrap->getCustomPaths( 'admin/extjs' ) as $base => $paths )
59
		{
60
			foreach( $paths as $path )
61
			{
62
				$jsbAbsPath = $base . '/' . $path;
63
64
				if( !is_file( $jsbAbsPath ) ) {
65
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
66
				}
67
68
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
69
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
70
			}
71
		}
72
73
		$jqadmUrl = route( 'aimeos_shop_jqadm_search', array( 'site' => $site, 'resource' => 'product' ) );
74
		$jsonUrl = route( 'aimeos_shop_extadm_json', array( 'site' => $site, '_token' => csrf_token() ) );
75
		$adminUrl = route( 'aimeos_shop_extadm', array( 'site' => '<site>', 'lang' => '<lang>', 'tab' => '<tab>' ) );
76
77
		$vars = array(
78
			'site' => $site,
79
			'lang' => $lang,
80
			'cssFiles' => $cssFiles,
81
			'languages' => $this->getJsonLanguages( $context),
82
			'config' => $this->getJsonClientConfig( $context ),
83
			'siteitem' => $this->getJsonSiteItem( $context, $site ),
84
			'i18nContent' => $this->getJsonClientI18n( $bootstrap->getI18nPaths(), $lang ),
85
			'searchSchemas' => $controller->getJsonSearchSchemas(),
86
			'itemSchemas' => $controller->getJsonItemSchemas(),
87
			'smd' => $controller->getJsonSmd( $jsonUrl ),
88
			'urlTemplate' => str_replace( ['<', '>'], ['{', '}'], urldecode( $adminUrl ) ),
89
			'uploaddir' => config( 'shop::uploaddir' ),
90
			'version' => $aimeos->getVersion(),
91
			'jqadmurl' => $jqadmUrl,
92
			'activeTab' => $tab,
93
		);
94
95
		return View::make( 'shop::extadm.index', $vars );
96
	}
97
98
99
	/**
100
	 * Single entry point for all JSON admin requests.
101
	 *
102
	 * @param \Illuminate\Http\Request $request Laravel request object
103
	 * @return \Illuminate\Contracts\View\View View for rendering the output
104
	 */
105
	public function doAction( Request $request )
106
	{
107
		if( config( 'shop.authorize', true ) ) {
108
			$this->authorize( 'admin', [['admin']] );
109
		}
110
111
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
112
113
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
114
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
115
116
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false, 'backend' );
117
		$context->setView( app( '\Aimeos\Shop\Base\View' )->create( $context->getConfig(), array() ) );
118
		$context->setLocale( app('\Aimeos\Shop\Base\Locale')->getBackend( $context, $site ) );
119
120
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
121
122
		$response = $controller->process( Input::instance()->request->all(), $request->getContent() );
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...
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, Aimeos\Controller\ExtJS\JsonRpc::process() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
123
		return View::make( 'shop::extadm.do', array( 'output' => $response ) );
124
	}
125
126
127
	/**
128
	 * Returns the JS file content
129
	 *
130
	 * @return \Illuminate\Http\Response Response object containing the generated output
131
	 */
132
	public function fileAction()
133
	{
134
		if( config( 'shop.authorize', true ) ) {
135
			$this->authorize( 'admin', [['admin']] );
136
		}
137
138
		$contents = '';
139
		$jsFiles = array();
140
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
141
142 View Code Duplication
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) 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...
143
		{
144
			foreach( $paths as $path )
145
			{
146
				$jsbAbsPath = $base . '/' . $path;
147
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
148
				$jsFiles = array_merge( $jsFiles, $jsb2->getFiles( 'js' ) );
149
			}
150
		}
151
152
		foreach( $jsFiles as $file )
153
		{
154
			if( ( $content = file_get_contents( $file ) ) !== false ) {
155
				$contents .= $content;
156
			}
157
		}
158
159
		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...
160
	}
161
162
163
	/**
164
	 * Creates a list of all available translations.
165
	 *
166
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
167
	 * @return array List of language IDs with labels
168
	 */
169
	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...
170
	{
171
		$result = array();
172
173
		foreach( app( '\Aimeos\Shop\Base\Aimeos' )->get()->getI18nList( 'admin' ) as $id ) {
174
			$result[] = array( 'id' => $id, 'label' => $id );
175
		}
176
177
		return json_encode( $result );
178
	}
179
180
181
	/**
182
	 * Returns the JSON encoded configuration for the ExtJS client.
183
	 *
184
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
185
	 * @return string JSON encoded configuration object
186
	 */
187
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
188
	{
189
		$config = $context->getConfig()->get( 'admin/extjs', array() );
190
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
191
	}
192
193
194
	/**
195
	 * Returns the JSON encoded translations for the ExtJS client.
196
	 *
197
	 * @param array $i18nPaths List of file system paths which contain the translation files
198
	 * @param string $lang ISO language code like "en" or "en_GB"
199
	 * @return string JSON encoded translation object
200
	 */
201
	protected function getJsonClientI18n( array $i18nPaths, $lang )
202
	{
203
		$i18n = new \Aimeos\MW\Translation\Gettext( $i18nPaths, $lang );
204
205
		$content = array(
206
			'admin' => $i18n->getAll( 'admin' ),
207
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
208
		);
209
210
		return json_encode( $content, JSON_FORCE_OBJECT );
211
	}
212
213
214
	/**
215
	 * Returns the JSON encoded site item.
216
	 *
217
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
218
	 * @param string $site Unique site code
219
	 * @return string JSON encoded site item object
220
	 * @throws Exception If no site item was found for the code
221
	 */
222
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
223
	{
224
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
225
226
		$criteria = $manager->createSearch();
227
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
228
		$items = $manager->searchItems( $criteria );
229
230
		if( ( $item = reset( $items ) ) === false ) {
231
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
232
		}
233
234
		return json_encode( $item->toArray() );
235
	}
236
}