Completed
Push — master ( 1a27eb...ec4895 )
by Aimeos
08:55
created

ExtadmController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 18
c 3
b 1
f 1
lcom 1
cbo 8
dl 0
loc 216
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 47 4
A doAction() 0 10 1
B fileAction() 0 26 5
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 LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015
6
 */
7
8
9
namespace Aimeos\Shop\Controller;
10
11
use TYPO3\Flow\Annotations as Flow;
12
13
14
/**
15
 * Controller for ExtJS adminisration interface.
16
 */
17
class ExtadmController extends \TYPO3\Flow\Mvc\Controller\ActionController
18
{
19
	/**
20
	 * @var \Aimeos\Shop\Base\Aimeos
21
	 * @Flow\Inject
22
	 */
23
	protected $aimeos;
24
25
	/**
26
	 * @var \Aimeos\Shop\Base\Context
27
	 * @Flow\Inject
28
	 */
29
	protected $context;
30
31
32
	/**
33
	 * Creates the initial HTML view for the admin interface.
34
	 *
35
	 * @param string $site Shop site code
36
	 * @param string $lang ISO language code
37
	 * @param integer $tab Number of the current active tab
38
	 */
39
	public function indexAction( $site = 'default', $lang = 'en', $tab = 0 )
40
	{
41
		$context = $this->context->get( $this->request );
42
		$context = $this->setLocale( $context, $site, $lang );
43
44
		$aimeos = $this->aimeos->get();
45
		$cntlPaths = $aimeos->getCustomPaths( 'controller/extjs' );
46
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
47
		$cssFiles = array();
48
49
		foreach( $aimeos->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->uriBuilder->uriFor( 'index', $params, 'admin' );
66
		$jsonUrl = $this->uriBuilder->uriFor( 'do' );
67
68
		$vars = array(
69
			'lang' => $lang,
70
			'cssFiles' => $cssFiles,
71
			'languages' => $this->getJsonLanguages(),
72
			'config' => $this->getJsonClientConfig( $context ),
73
			'site' => $this->getJsonSiteItem( $context, $site ),
74
			'i18nContent' => $this->getJsonClientI18n( $aimeos->getI18nPaths(), $lang ),
75
			'searchSchemas' => $controller->getJsonSearchSchemas(),
76
			'itemSchemas' => $controller->getJsonItemSchemas(),
77
			'smd' => $controller->getJsonSmd( $jsonUrl ),
78
			'urlTemplate' => urldecode( $adminUrl ),
79
			'uploaddir' => $context->getConfig()->get( 'flow/uploaddir', '/.' ),
80
			'version' => $this->aimeos->getVersion(),
81
			'activeTab' => $tab,
82
		);
83
84
		$this->view->assignMultiple( $vars );
85
	}
86
87
88
	/**
89
	 * Single entry point for all JSON admin requests.
90
	 */
91
	public function doAction()
92
	{
93
		$context = $this->context->get( $this->request );
94
		$context = $this->setLocale( $context );
95
		$cntlPaths = $this->aimeos->get()->getCustomPaths( 'controller/extjs' );
96
97
		$controller = new \Aimeos\Controller\ExtJS\JsonRpc( $context, $cntlPaths );
98
99
		return $controller->process( $this->request->getArguments(), 'php://input' );
100
	}
101
102
103
	/**
104
	 * Returns the JS file content
105
	 *
106
	 * @return \TYPO3\Flow\Http\Response Response object
107
	 */
108
	public function fileAction()
109
	{
110
		$jsFiles = array();
111
		$aimeos = $this->aimeos->get();
112
113
		foreach( $aimeos->getCustomPaths( 'admin/extjs' ) as $base => $paths )
114
		{
115
			foreach( $paths as $path )
116
			{
117
				$jsbAbsPath = $base . '/' . $path;
118
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
119
				$jsFiles = array_merge( $jsFiles, $jsb2->getUrls( 'js', '' ) );
120
			}
121
		}
122
123
		foreach( $jsFiles as $file )
124
		{
125
			if( ( $content = file_get_contents( $file ) ) !== false ) {
126
				$this->response->appendContent( $content );
127
			}
128
		}
129
130
		$this->response->setHeader( 'Content-Type', 'application/javascript' );
131
132
		return $this->response;
133
	}
134
135
136
	/**
137
	 * Creates a list of all available translations.
138
	 *
139
	 * @return string JSON encoded list of language IDs with labels
140
	 */
141
	protected function getJsonLanguages()
142
	{
143
		$result = array();
144
145
		foreach( $this->aimeos->get()->getI18nList( 'admin' ) as $id ) {
146
			$result[] = array( 'id' => $id, 'label' => $id );
147
		}
148
149
		return json_encode( $result );
150
	}
151
152
153
	/**
154
	 * Returns the JSON encoded configuration for the ExtJS client.
155
	 *
156
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
157
	 * @return string JSON encoded configuration object
158
	 */
159
	protected function getJsonClientConfig( \Aimeos\MShop\Context\Item\Iface $context )
160
	{
161
		$config = $context->getConfig()->get( 'admin/extjs', array() );
162
		return json_encode( array( 'admin' => array( 'extjs' => $config ) ), JSON_FORCE_OBJECT );
163
	}
164
165
166
	/**
167
	 * Returns the JSON encoded translations for the ExtJS client.
168
	 *
169
	 * @param array $i18nPaths List of file system paths which contain the translation files
170
	 * @param string $lang ISO language code like "en" or "en_GB"
171
	 * @return string JSON encoded translation object
172
	 */
173
	protected function getJsonClientI18n( array $i18nPaths, $lang )
174
	{
175
		$i18n = new \Aimeos\MW\Translation\Zend2( $i18nPaths, 'gettext', $lang, array( 'disableNotices' => true ) );
176
177
		$content = array(
178
			'admin' => $i18n->getAll( 'admin' ),
179
			'admin/ext' => $i18n->getAll( 'admin/ext' ),
180
		);
181
182
		return json_encode( $content, JSON_FORCE_OBJECT );
183
	}
184
185
186
	/**
187
	 * Returns the JSON encoded site item.
188
	 *
189
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
190
	 * @param string $site Unique site code
191
	 * @return string JSON encoded site item object
192
	 * @throws Exception If no site item was found for the code
193
	 */
194
	protected function getJsonSiteItem( \Aimeos\MShop\Context\Item\Iface $context, $site )
195
	{
196
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
197
198
		$criteria = $manager->createSearch();
199
		$criteria->setConditions( $criteria->compare( '==', 'locale.site.code', $site ) );
200
		$items = $manager->searchItems( $criteria );
201
202
		if( ( $item = reset( $items ) ) === false ) {
203
			throw new \Exception( sprintf( 'No site found for code "%1$s"', $site ) );
204
		}
205
206
		return json_encode( $item->toArray() );
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 $sitecode Unique site code
215
	 * @param string $locale ISO language code, e.g. "en" or "en_GB"
216
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
217
	 */
218
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $locale = null )
219
	{
220
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
221
222
		try {
223
			$localeItem = $localeManager->bootstrap( $sitecode, $locale, '', false );
224
		} catch( \Aimeos\MShop\Locale\Exception $e ) {
225
			$localeItem = $localeManager->createItem();
226
		}
227
228
		$context->setLocale( $localeItem );
229
230
		return $context;
231
	}
232
}
233