Completed
Push — master ( a6e7a8...8e6870 )
by Aimeos
02:29
created

ExtadmController::setLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package symfony
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\ShopBundle\Controller;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Security\Csrf\CsrfToken;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
18
19
/**
20
 * Controller providing the administration interface.
21
 *
22
 * @package symfony
23
 * @subpackage Controller
24
 */
25
class ExtadmController extends Controller
26
{
27
	/**
28
	 * Returns the initial HTML view for the admin interface.
29
	 *
30
	 * @param Request $request Symfony request object
31
	 * @return Response Generated output for the admin interface
32
	 */
33
	public function indexAction( Request $request )
34
	{
35
		$site = $request->attributes->get( 'site', $request->query->get( 'site', 'default' ) );
36
		$lang = $request->attributes->get( 'lang', $request->query->get( 'lang', 'en' ) );
37
		$tab = $request->attributes->get( 'tab', $request->query->get( 'tab', 0 ) );
38
39
		$context = $this->get( 'aimeos_context' )->get( false, 'backend' );
40
		$context->setLocale( $this->get( 'aimeos_locale' )->getBackend( $context, $site ) );
41
42
		$aimeos = $this->get( 'aimeos' );
43
		$bootstrap = $aimeos->get();
44
45
		$cntlPaths = $bootstrap->getCustomPaths( 'controller/extjs' );
46
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
47
		$cssFiles = array();
48
49
		foreach( $bootstrap->getCustomPaths( 'admin/extjs' ) as $base => $paths )
50
		{
51
			foreach( $paths as $path )
52
			{
53
				$jsbAbsPath = $base . '/' . $path;
54
55
				if( !is_file( $jsbAbsPath ) ) {
56
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
57
				}
58
59
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
60
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
61
			}
62
		}
63
64
		$params = array( 'site' => '{site}', 'lang' => '{lang}', 'tab' => '{tab}' );
65
		$adminUrl = $this->generateUrl( 'aimeos_shop_extadm', $params );
66
67
		$token = $this->get( 'security.csrf.token_manager' )->getToken( 'aimeos_admin_token' )->getValue();
68
		$jsonUrl = $this->generateUrl( 'aimeos_shop_extadm_json', array( '_token' => $token, 'site' => $site ) );
69
70
		$jqadmUrl = $this->generateUrl( 'aimeos_shop_jqadm_search', array( 'site' => $site, 'resource' => 'product' ) );
71
72
		$vars = array(
73
			'lang' => $lang,
74
			'cssFiles' => $cssFiles,
75
			'languages' => $this->getJsonLanguages(),
76
			'config' => $this->getJsonClientConfig( $context ),
77
			'site' => $this->getJsonSiteItem( $context, $site ),
78
			'i18nContent' => $this->getJsonClientI18n( $bootstrap->getI18nPaths(), $lang ),
79
			'searchSchemas' => $controller->getJsonSearchSchemas(),
80
			'itemSchemas' => $controller->getJsonItemSchemas(),
81
			'smd' => $controller->getJsonSmd( $jsonUrl ),
82
			'urlTemplate' => urldecode( $adminUrl ),
83
			'uploaddir' => $this->container->getParameter( 'aimeos_shop.uploaddir' ),
84
			'version' => $aimeos->getVersion(),
85
			'jqadmurl' => $jqadmUrl,
86
			'activeTab' => $tab,
87
		);
88
89
		return $this->render( 'AimeosShopBundle:Extadm:index.html.twig', $vars );
90
	}
91
92
93
	/**
94
	 * Single entry point for all JSON admin requests.
95
	 *
96
	 * @param Request $request Symfony request object
97
	 * @return Response JSON RPC message response
98
	 */
99
	public function doAction( Request $request )
100
	{
101
		$csrfProvider = $this->get('security.csrf.token_manager');
102
103
		if( $csrfProvider->isTokenValid( new CsrfToken( 'aimeos_admin_token',  $request->query->get( '_token' ) ) ) !== true ) {
104
			throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException( 'CSRF token is invalid' );
105
		}
106
107
		$cntlPaths = $this->get( 'aimeos' )->get()->getCustomPaths( 'controller/extjs' );
108
		$context = $this->get( 'aimeos_context' )->get( false, 'backend' );
109
		$context->setView( $this->get( 'aimeos_view' )->create( $context, array() ) );
110
		$context->setLocale( $this->get( 'aimeos_locale' )->getBackend( $context, 'default' ) );
111
112
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
113
114
		$response = $controller->process( $request->request->all(), $request->getContent() );
0 ignored issues
show
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...
115
		return $this->render( 'AimeosShopBundle:Extadm:do.html.twig', array( 'output' => $response ) );
116
	}
117
118
119
	/**
120
	 * Returns the JS file content
121
	 *
122
	 * @return Response Response object
123
	 */
124
	public function fileAction()
125
	{
126
		$contents = '';
127
		$jsFiles = array();
128
		$aimeos = $this->get( 'aimeos' )->get();
129
130
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
131
		{
132
			foreach( $paths as $path )
133
			{
134
				$jsbAbsPath = $base . '/' . $path;
135
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
136
				$jsFiles = array_merge( $jsFiles, $jsb2->getFiles( 'js' ) );
137
			}
138
		}
139
140
		foreach( $jsFiles as $file )
141
		{
142
			if( ( $content = file_get_contents( $file ) ) !== false ) {
143
				$contents .= $content;
144
			}
145
		}
146
147
		$response = new Response( $contents );
148
		$response->headers->set( 'Content-Type', 'application/javascript' );
149
150
		return $response;
151
	}
152
153
154
	/**
155
	 * Creates a list of all available translations.
156
	 *
157
	 * @return array List of language IDs with labels
158
	 */
159
	protected function getJsonLanguages()
160
	{
161
		$result = array();
162
163
		foreach( $this->get( 'aimeos' )->get()->getI18nList( 'admin' ) as $id ) {
164
			$result[] = array( 'id' => $id, 'label' => $id );
165
		}
166
167
		return json_encode( $result );
168
	}
169
170
171
	/**
172
	 * Returns the JSON encoded configuration for the ExtJS client.
173
	 *
174
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
175
	 * @return string JSON encoded configuration object
176
	 */
177
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
178
	{
179
		$config = $context->getConfig()->get( 'admin/extjs', array() );
180
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
181
	}
182
183
184
	/**
185
	 * Returns the JSON encoded translations for the ExtJS client.
186
	 *
187
	 * @param array $i18nPaths List of file system paths which contain the translation files
188
	 * @param string $lang ISO language code like "en" or "en_GB"
189
	 * @return string JSON encoded translation object
190
	 */
191
	protected function getJsonClientI18n( array $i18nPaths, $lang )
192
	{
193
		$i18n = new \Aimeos\MW\Translation\Gettext( $i18nPaths, $lang );
194
195
		$content = array(
196
			'admin' => $i18n->getAll( 'admin' ),
197
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
198
		);
199
200
		return json_encode( $content, JSON_FORCE_OBJECT );
201
	}
202
203
204
	/**
205
	 * Returns the JSON encoded site item.
206
	 *
207
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
208
	 * @param string $site Unique site code
209
	 * @return string JSON encoded site item object
210
	 * @throws Exception If no site item was found for the code
211
	 */
212
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
213
	{
214
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
215
216
		$criteria = $manager->createSearch();
217
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
218
		$items = $manager->searchItems( $criteria );
219
220
		if( ( $item = reset( $items ) ) === false ) {
221
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
222
		}
223
224
		return json_encode( $item->toArray() );
225
	}
226
}
227