Passed
Push — master ( 8f16ed...3311dc )
by Aimeos
10:04
created

lib/mshoplib/setup/MShopAddLocaleData.php (4 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds locale records to tables.
15
 */
16
class MShopAddLocaleData extends \Aimeos\MW\Setup\Task\Base
17
{
18
	/**
19
	 * Returns the list of task names which this task depends on.
20
	 *
21
	 * @return string[] List of task names
22
	 */
23
	public function getPreDependencies()
24
	{
25
		return array( 'MShopAddLocaleLangCurData', 'TablesCreateMAdmin' );
26
	}
27
28
29
	/**
30
	 * Returns the list of task names which depends on this task.
31
	 *
32
	 * @return array List of task names
33
	 */
34
	public function getPostDependencies()
35
	{
36
		return array( 'MShopSetLocale' );
37
	}
38
39
40
	/**
41
	 * Creates new locale data if necessary
42
	 */
43
	public function migrate()
44
	{
45
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $this->additional );
46
47
		$this->msg( 'Adding locale data if not yet present', 0 );
48
49
50
		// Set editor for further tasks
51
		$this->additional->setEditor( 'core:setup' );
52
53
54
		$code = $this->additional->getConfig()->get( 'setup/site', 'default' );
55
56
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::create( $this->additional, 'Standard' );
57
		$siteManager = $localeManager->getSubManager( 'site' );
58
59
		try
60
		{
61
			$siteItem = $siteManager->createItem();
62
			$siteItem->setLabel( $code );
63
			$siteItem->setCode( $code );
64
			$siteItem->setStatus( 1 );
65
66
			$siteManager->insertItem( $siteItem );
67
		}
68
		catch( \Aimeos\MW\DB\Exception $e ) // already in the database
69
		{
70
			$siteItem = $siteManager->findItem( $code );
71
		}
72
73
		try
74
		{
75
			$localeItem = $localeManager->createItem();
76
			$localeItem->setSiteId( $siteItem->getId() );
0 ignored issues
show
The method setSiteId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
			$localeItem->/** @scrutinizer ignore-call */ 
77
                setSiteId( $siteItem->getId() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
			$localeItem->setLanguageId( 'en' );
0 ignored issues
show
The method setLanguageId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
			$localeItem->/** @scrutinizer ignore-call */ 
78
                setLanguageId( 'en' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
			$localeItem->setCurrencyId( 'EUR' );
0 ignored issues
show
The method setCurrencyId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
			$localeItem->/** @scrutinizer ignore-call */ 
79
                setCurrencyId( 'EUR' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
			$localeItem->setStatus( 1 );
80
81
			$localeManager->saveItem( $localeItem, false );
82
		}
83
		catch( \Aimeos\MW\DB\Exception $e ) // already in the database
84
		{
85
			$this->status( 'OK' );
86
			return;
87
		}
88
89
		$this->status( 'done' );
90
	}
91
92
93
	/**
94
	 * Adds locale site data.
95
	 *
96
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
97
	 * @param array $data Associative list of locale site data
98
	 * @param string $manager Manager implementation name
99
	 * @param string|null $parentId Parent id of the locale item
100
	 * @return array Associative list of keys from the data and generated site ID
101
	 */
102
	protected function addLocaleSiteData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data, $manager = 'Standard', $parentId = null )
103
	{
104
		$this->msg( 'Adding data for MShop locale sites', 1 );
105
106
		$localeSiteManager = $localeManager->getSubManager( 'site', $manager );
107
		$siteItem = $localeSiteManager->createItem();
108
		$siteIds = [];
109
110
		foreach( $data as $key => $dataset )
111
		{
112
			try
113
			{
114
				$siteItem->setId( null );
115
				$siteItem->setCode( $dataset['code'] );
116
				$siteItem->setLabel( $dataset['label'] );
117
				$siteItem->setConfig( $dataset['config'] );
0 ignored issues
show
The method setConfig() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
				$siteItem->/** @scrutinizer ignore-call */ 
118
               setConfig( $dataset['config'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
				$siteItem->setStatus( $dataset['status'] );
119
120
				$localeSiteManager->insertItem( $siteItem, $parentId );
121
				$siteIds[$key] = $siteItem->getId();
122
			}
123
			catch( \Exception $e )
124
			{
125
				$search = $localeSiteManager->createSearch();
126
				$search->setConditions( $search->compare( '==', 'locale.site.code', $dataset['code'] ) );
127
				$result = $localeSiteManager->searchItems( $search );
128
129
				if( ( $item = reset( $result ) ) === false ) {
130
					throw new \RuntimeException( sprintf( 'No site for code "%1$s" available', $dataset['code'] ) );
131
				}
132
133
				$siteIds[$key] = $item->getId();
134
			}
135
		}
136
137
		$this->status( 'done' );
138
139
		return $siteIds;
140
	}
141
142
143
	/**
144
	 * Adds locale currency data.
145
	 *
146
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
147
	 * @param array $data Associative list of locale currency data
148
	 */
149
	protected function addLocaleCurrencyData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data )
150
	{
151
		$this->msg( 'Adding data for MShop locale currencies', 1 );
152
153
		$currencyManager = $localeManager->getSubManager( 'currency', 'Standard' );
154
		$items = $currencyManager->searchItems( $currencyManager->createSearch()->setSlice( 0, 0x7fffffff ) );
155
156
		$num = $total = 0;
157
158
		foreach( $data as $key => $dataset )
159
		{
160
			$total++;
161
162
			if( !isset( $items[$dataset['id']] ) )
163
			{
164
				$currencyItem = $currencyManager->createItem();
165
				$currencyItem->setCode( $dataset['id'] );
166
				$currencyItem->setLabel( $dataset['label'] );
167
				$currencyItem->setStatus( $dataset['status'] );
168
169
				$items[$dataset['id']] = $currencyItem;
170
			}
171
172
			$currencyManager->saveItem( $items[$dataset['id']] );
173
			$num++;
174
		}
175
176
		$this->status( $num > 0 ? $num . '/' . $total : 'OK' );
177
	}
178
179
180
	/**
181
	 * Adds locale language data.
182
	 *
183
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
184
	 * @param array $data Associative list of locale language data
185
	 */
186
	protected function addLocaleLanguageData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data )
187
	{
188
		$this->msg( 'Adding data for MShop locale languages', 1 );
189
190
		$languageManager = $localeManager->getSubManager( 'language', 'Standard' );
191
		$items = $languageManager->searchItems( $languageManager->createSearch()->setSlice( 0, 0x7fffffff ) );
192
193
		$num = $total = 0;
194
195
		foreach( $data as $dataset )
196
		{
197
			$total++;
198
199
			if( !isset( $items[$dataset['id']] ) )
200
			{
201
				$languageItem = $languageManager->createItem();
202
				$languageItem->setCode( $dataset['id'] );
203
				$languageItem->setLabel( $dataset['label'] );
204
				$languageItem->setStatus( $dataset['status'] );
205
206
				$items[$dataset['id']] = $languageItem;
207
			}
208
209
			$languageManager->saveItem( $items[$dataset['id']] );
210
			$num++;
211
		}
212
213
		$this->status( $num > 0 ? $num . '/' . $total : 'OK' );
214
	}
215
216
217
	/**
218
	 * Adds locale data.
219
	 *
220
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeItemManager Locale manager
221
	 * @param array $data Associative list of locale data
222
	 */
223
	protected function addLocaleData( \Aimeos\MShop\Common\Manager\Iface $localeItemManager, array $data, array $siteIds )
224
	{
225
		$this->msg( 'Adding data for MShop locales', 1 );
226
227
		$localeItem = $localeItemManager->createItem();
228
229
		foreach( $data as $key => $dataset )
230
		{
231
			if( !isset( $siteIds[$dataset['siteid']] ) ) {
232
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No ID for site for key "%1$s" found', $dataset['siteid'] ) );
233
			}
234
235
			$localeItem->setId( null );
236
			$localeItem->setSiteId( $siteIds[$dataset['siteid']] );
237
			$localeItem->setLanguageId( $dataset['langid'] );
238
			$localeItem->setCurrencyId( $dataset['currencyid'] );
239
			$localeItem->setPosition( $dataset['pos'] );
240
			$localeItem->setStatus( $dataset['status'] );
241
242
			try {
243
				$localeItemManager->saveItem( $localeItem );
244
			} catch( \Exception $e ) {; } // if locale combination was already available
245
		}
246
247
		$this->status( 'done' );
248
	}
249
}
250