Completed
Push — master ( 6114e5...8f259d )
by Aimeos
07:23
created

ExtadmController::getJsonLanguages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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 string $site Unique site code
31
	 * @param string $lang ISO language code
32
	 * @param integer $tab Number of the currently active tab
33
	 * @return \Symfony\Component\HttpFoundation\Response HTML page for the admin interface
34
	 */
35
	public function indexAction( $site = 'default', $lang, $tab )
36
	{
37
		$context = $this->get( 'aimeos_context' )->get( false );
38
		$context = $this->setLocale( $context, $site, $lang );
39
40
		$aimeos = $this->get( 'aimeos' );
41
		$bootstrap = $aimeos->get();
42
43
		$cntlPaths = $bootstrap->getCustomPaths( 'controller/extjs' );
44
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
45
		$cssFiles = array();
46
47
		foreach( $bootstrap->getCustomPaths( 'admin/extjs' ) as $base => $paths )
48
		{
49
			foreach( $paths as $path )
50
			{
51
				$jsbAbsPath = $base . '/' . $path;
52
53
				if( !is_file( $jsbAbsPath ) ) {
54
					throw new \Exception( sprintf( 'JSB2 file "%1$s" not found', $jsbAbsPath ) );
55
				}
56
57
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $path ) );
58
				$cssFiles = array_merge( $cssFiles, $jsb2->getUrls( 'css' ) );
59
			}
60
		}
61
62
		$params = array( 'site' => '{site}', 'lang' => '{lang}', 'tab' => '{tab}' );
63
		$adminUrl = $this->generateUrl( 'aimeos_shop_extadm', $params );
64
65
		$token = $this->get( 'security.csrf.token_manager' )->getToken( 'aimeos_admin_token' )->getValue();
66
		$jsonUrl = $this->generateUrl( 'aimeos_shop_extadm_json', array( '_token' => $token, 'site' => $site ) );
67
68
		$vars = array(
69
			'lang' => $lang,
70
			'cssFiles' => $cssFiles,
71
			'languages' => $this->getJsonLanguages( $context),
72
			'config' => $this->getJsonClientConfig( $context ),
73
			'site' => $this->getJsonSiteItem( $context, $site ),
74
			'i18nContent' => $this->getJsonClientI18n( $bootstrap->getI18nPaths(), $lang ),
75
			'searchSchemas' => $controller->getJsonSearchSchemas(),
76
			'itemSchemas' => $controller->getJsonItemSchemas(),
77
			'smd' => $controller->getJsonSmd( $jsonUrl ),
78
			'urlTemplate' => urldecode( $adminUrl ),
79
			'uploaddir' => $this->container->getParameter( 'aimeos_shop.uploaddir' ),
80
			'version' => $aimeos->getVersion(),
81
			'activeTab' => $tab,
82
		);
83
84
		return $this->render( 'AimeosShopBundle:Extadm:index.html.twig', $vars );
85
	}
86
87
88
	/**
89
	 * Single entry point for all JSON admin requests.
90
	 *
91
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
92
	 * @return \Symfony\Component\HttpFoundation\Response 2.0 RPC message response
93
	 */
94
	public function doAction( Request $request )
95
	{
96
		$csrfProvider = $this->get('security.csrf.token_manager');
97
98
		if( $csrfProvider->isTokenValid( new CsrfToken( 'aimeos_admin_token',  $request->query->get( '_token' ) ) ) !== true ) {
99
			throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException( 'CSRF token is invalid' );
100
		}
101
102
		$cntlPaths = $this->get( 'aimeos' )->get()->getCustomPaths( 'controller/extjs' );
103
		$context = $this->get( 'aimeos_context' )->get( false );
104
		$context = $this->setLocale( $context );
105
106
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
107
108
		$response = $controller->process( $request->request->all(), 'php://input' );
109
		return $this->render( 'AimeosShopBundle:Extadm:do.html.twig', array( 'output' => $response ) );
110
	}
111
112
113
	/**
114
	 * Returns the JS file content
115
	 *
116
	 * @return Response Response object
117
	 */
118
	public function fileAction()
119
	{
120
		$contents = '';
121
		$jsFiles = array();
122
		$aimeos = $this->get( 'aimeos' )->get();
123
124
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
125
		{
126
			foreach( $paths as $path )
127
			{
128
				$jsbAbsPath = $base . '/' . $path;
129
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
130
				$jsFiles = array_merge( $jsFiles, $jsb2->getFiles( 'js' ) );
131
			}
132
		}
133
134
		foreach( $jsFiles as $file )
135
		{
136
			if( ( $content = file_get_contents( $file ) ) !== false ) {
137
				$contents .= $content;
138
			}
139
		}
140
141
		$response = new Response( $contents );
142
		$response->headers->set( 'Content-Type', 'application/javascript' );
143
144
		return $response;
145
	}
146
147
148
	/**
149
	 * Creates a list of all available translations.
150
	 *
151
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
152
	 * @return array List of language IDs with labels
153
	 */
154
	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...
155
	{
156
		$result = array();
157
158
		foreach( $this->get( 'aimeos' )->get()->getI18nList( 'admin' ) as $id ) {
159
			$result[] = array( 'id' => $id, 'label' => $id );
160
		}
161
162
		return json_encode( $result );
163
	}
164
165
166
	/**
167
	 * Returns the JSON encoded configuration for the ExtJS client.
168
	 *
169
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
170
	 * @return string JSON encoded configuration object
171
	 */
172
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
173
	{
174
		$config = $context->getConfig()->get( 'admin/extjs', array() );
175
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
176
	}
177
178
179
	/**
180
	 * Returns the JSON encoded translations for the ExtJS client.
181
	 *
182
	 * @param array $i18nPaths List of file system paths which contain the translation files
183
	 * @param string $lang ISO language code like "en" or "en_GB"
184
	 * @return string JSON encoded translation object
185
	 */
186
	protected function getJsonClientI18n( array $i18nPaths, $lang )
187
	{
188
		$i18n = new \Aimeos\MW\Translation\Zend2( $i18nPaths, 'gettext', $lang, array( 'disableNotices' => true ) );
189
190
		$content = array(
191
			'admin' => $i18n->getAll( 'admin' ),
192
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
193
		);
194
195
		return json_encode( $content, JSON_FORCE_OBJECT );
196
	}
197
198
199
	/**
200
	 * Returns the JSON encoded site item.
201
	 *
202
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
203
	 * @param string $site Unique site code
204
	 * @return string JSON encoded site item object
205
	 * @throws Exception If no site item was found for the code
206
	 */
207
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
208
	{
209
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
210
211
		$criteria = $manager->createSearch();
212
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
213
		$items = $manager->searchItems( $criteria );
214
215
		if( ( $item = reset( $items ) ) === false ) {
216
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
217
		}
218
219
		return json_encode( $item->toArray() );
220
	}
221
222
223
	/**
224
	 * Sets the locale item in the given context
225
	 *
226
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
227
	 * @param string $sitecode Unique site code
228
	 * @param string $locale ISO language code, e.g. "en" or "en_GB"
229
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
230
	 */
231
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $locale = null )
232
	{
233
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
234
235
		try {
236
			$localeItem = $localeManager->bootstrap( $sitecode, $locale, '', false );
237
		} catch( \Aimeos\MShop\Locale\Exception $e ) {
238
			$localeItem = $localeManager->createItem();
239
		}
240
241
		$context->setLocale( $localeItem );
242
243
		return $context;
244
	}
245
}
246