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

lib/mshoplib/setup/MShopAddDataAbstract.php (19 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds records to tables.
15
 */
16
class MShopAddDataAbstract extends \Aimeos\MW\Setup\Task\Base
17
{
18
	private $attributes;
19
20
21
	/**
22
	 * Initializes the object
23
	 *
24
	 * @param \Aimeos\MW\Setup\DBSchema\Iface $schema
25
	 * @param \Aimeos\MW\DB\Connection\Iface $conn
26
	 * @param \Aimeos\MShop\Context\Item\Iface|null $additional
27
	 * @throws \Aimeos\MW\Setup\Exception
28
	 */
29
	public function __construct( \Aimeos\MW\Setup\DBSchema\Iface $schema, \Aimeos\MW\DB\Connection\Iface $conn, $additional = null )
30
	{
31
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $additional );
32
33
		parent::__construct( $schema, $conn, $additional );
34
	}
35
36
37
	/**
38
	 * Returns the list of task names which this task depends on.
39
	 *
40
	 * @return string[] List of task names
41
	 */
42
	public function getPreDependencies()
43
	{
44
		return array( 'TablesCreateMShop' );
45
	}
46
47
48
	/**
49
	 * Returns the list of task names which depends on this task.
50
	 *
51
	 * @return array List of task names
52
	 */
53
	public function getPostDependencies()
54
	{
55
		return [];
56
	}
57
58
59
	/**
60
	 * Executes the task for MySQL databases.
61
	 */
62
	public function migrate()
63
	{
64
		// executed by tasks in sub-directories for specific sites
65
	}
66
67
68
	/**
69
	 * Adds the attributes to the given parent in the database.
70
	 *
71
	 * @param string $parentid ID of the parent item where the media should be associated to
72
	 * @param array $data Two dimensional associative list of attribute data
73
	 * @param string $domain Domain name the texts should be added to, e.g. 'catalog'
74
	 */
75
	protected function addAttributes( $parentid, array $data, $domain )
76
	{
77
		$context = $this->getContext();
78
		$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
79
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
80
81
82
		$item = $attrManager->createItem();
83
		$item->setDomain( $domain );
84
85
		$listItem = $listManager->createItem();
86
		$listItem->setParentId( $parentid );
0 ignored issues
show
The method setParentId() 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

86
		$listItem->/** @scrutinizer ignore-call */ 
87
             setParentId( $parentid );

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...
87
		$listItem->setDomain( 'attribute' );
88
89
90
		foreach( $data as $entry )
91
		{
92
			if( ( $attrItem = $this->getAttributeItem( $domain, $entry['type'], $entry['code'] ) ) === null )
93
			{
94
				$item->setId( null );
95
				$item->setType( $entry['type'] );
96
				$item->setCode( $entry['code'] );
97
				$item->setLabel( $entry['label'] );
98
				$item->setPosition( $entry['position'] );
99
				$item->setStatus( $entry['status'] );
100
101
				$attrManager->saveItem( $item );
102
				$id = $item->getId();
103
			}
104
			else
105
			{
106
				$id = $attrItem->getId();
107
			}
108
109
			$listItem->setId( null );
110
			$listItem->setType( $entry['list-type'] );
111
			$listItem->setDateStart( $entry['list-start'] );
0 ignored issues
show
The method setDateStart() 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

111
			$listItem->/** @scrutinizer ignore-call */ 
112
              setDateStart( $entry['list-start'] );

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...
112
			$listItem->setDateEnd( $entry['list-end'] );
0 ignored issues
show
The method setDateEnd() 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

112
			$listItem->/** @scrutinizer ignore-call */ 
113
              setDateEnd( $entry['list-end'] );

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...
113
			$listItem->setConfig( $entry['list-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

113
			$listItem->/** @scrutinizer ignore-call */ 
114
              setConfig( $entry['list-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...
114
			$listItem->setPosition( $entry['list-position'] );
115
			$listItem->setStatus( $entry['list-status'] );
116
			$listItem->setRefId( $id );
0 ignored issues
show
The method setRefId() does not exist on Aimeos\MShop\Attribute\Item\Iface. Did you maybe mean setId()? ( Ignorable by Annotation )

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

116
			$listItem->/** @scrutinizer ignore-call */ 
117
              setRefId( $id );

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...
117
118
			$listManager->saveItem( $listItem, false );
119
120
121
			if( isset( $entry['attribute'] ) ) {
122
				$this->addAttributes( $id, $entry['attribute'], 'attribute' );
123
			}
124
125
			if( isset( $entry['media'] ) ) {
126
				$this->addMedia( $id, $entry['media'], 'attribute' );
127
			}
128
129
			if( isset( $entry['price'] ) ) {
130
				$this->addPrices( $id, $entry['price'], 'attribute' );
131
			}
132
133
			if( isset( $entry['text'] ) ) {
134
				$this->addTexts( $id, $entry['text'], 'attribute' );
135
			}
136
		}
137
	}
138
139
140
	/**
141
	 * Adds the media to the given parent in the database.
142
	 *
143
	 * @param string $parentid ID of the parent item where the media should be associated to
144
	 * @param array $data Two dimensional associative list of media data
145
	 * @param string $domain Domain name the texts should be added to, e.g. 'catalog'
146
	 */
147
	protected function addMedia( $parentid, array $data, $domain )
148
	{
149
		$context = $this->getContext();
150
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
151
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
152
153
154
		$item = $mediaManager->createItem();
155
		$item->setDomain( $domain );
156
157
		$listItem = $listManager->createItem();
158
		$listItem->setParentId( $parentid );
159
		$listItem->setDomain( 'media' );
160
161
162
		foreach( $data as $entry )
163
		{
164
			$item->setId( null );
165
			$item->setType( $entry['type'] );
166
			$item->setLanguageId( $entry['languageid'] );
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

166
			$item->/** @scrutinizer ignore-call */ 
167
          setLanguageId( $entry['languageid'] );

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...
167
			$item->setMimetype( $entry['mimetype'] );
0 ignored issues
show
The method setMimetype() 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

167
			$item->/** @scrutinizer ignore-call */ 
168
          setMimetype( $entry['mimetype'] );

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...
168
			$item->setPreview( $entry['preview'] );
0 ignored issues
show
The method setPreview() 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

168
			$item->/** @scrutinizer ignore-call */ 
169
          setPreview( $entry['preview'] );

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...
169
			$item->setUrl( $entry['url'] );
0 ignored issues
show
The method setUrl() 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

169
			$item->/** @scrutinizer ignore-call */ 
170
          setUrl( $entry['url'] );

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...
170
			$item->setLabel( $entry['label'] );
171
			$item->setStatus( $entry['status'] );
172
173
			$mediaManager->saveItem( $item );
174
175
			$listItem->setId( null );
176
			$listItem->setType( $entry['list-type'] );
177
			$listItem->setDateStart( $entry['list-start'] );
178
			$listItem->setDateEnd( $entry['list-end'] );
179
			$listItem->setConfig( $entry['list-config'] );
180
			$listItem->setPosition( $entry['list-position'] );
181
			$listItem->setStatus( $entry['list-status'] );
182
			$listItem->setRefId( $item->getId() );
183
184
			$listManager->saveItem( $listItem, false );
185
186
187
			if( isset( $entry['attribute'] ) ) {
188
				$this->addAttributes( $item->getId(), $entry['attribute'], 'media' );
189
			}
190
191
			if( isset( $entry['media'] ) ) {
192
				$this->addMedia( $item->getId(), $entry['media'], 'media' );
193
			}
194
195
			if( isset( $entry['price'] ) ) {
196
				$this->addPrices( $item->getId(), $entry['price'], 'media' );
197
			}
198
199
			if( isset( $entry['text'] ) ) {
200
				$this->addTexts( $item->getId(), $entry['text'], 'media' );
201
			}
202
		}
203
	}
204
205
206
	/**
207
	 * Adds the prices to the given parent in the database.
208
	 *
209
	 * @param string $parentid ID of the parent item where the media should be associated to
210
	 * @param array $data Two dimensional associative list of price data
211
	 * @param string $domain Domain name the texts should be added to, e.g. 'catalog'
212
	 */
213
	protected function addPrices( $parentid, array $data, $domain )
214
	{
215
		$context = $this->getContext();
216
		$mediaManager = \Aimeos\MShop::create( $context, 'price' );
217
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
218
219
220
		$item = $mediaManager->createItem();
221
		$item->setDomain( $domain );
222
223
		$listItem = $listManager->createItem();
224
		$listItem->setParentId( $parentid );
225
		$listItem->setDomain( 'price' );
226
227
228
		foreach( $data as $entry )
229
		{
230
			$item->setId( null );
231
			$item->setLabel( $entry['label'] );
232
			$item->setType( $entry['type'] );
233
			$item->setCurrencyId( $entry['currencyid'] );
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

233
			$item->/** @scrutinizer ignore-call */ 
234
          setCurrencyId( $entry['currencyid'] );

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...
234
			$item->setQuantity( $entry['quantity'] );
0 ignored issues
show
The method setQuantity() 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

234
			$item->/** @scrutinizer ignore-call */ 
235
          setQuantity( $entry['quantity'] );

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...
235
			$item->setValue( $entry['value'] );
0 ignored issues
show
The method setValue() 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

235
			$item->/** @scrutinizer ignore-call */ 
236
          setValue( $entry['value'] );

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...
236
			$item->setCosts( $entry['costs'] );
0 ignored issues
show
The method setCosts() 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

236
			$item->/** @scrutinizer ignore-call */ 
237
          setCosts( $entry['costs'] );

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...
237
			$item->setRebate( $entry['rebate'] );
0 ignored issues
show
The method setRebate() 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

237
			$item->/** @scrutinizer ignore-call */ 
238
          setRebate( $entry['rebate'] );

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...
238
			$item->setTaxRate( $entry['taxrate'] );
0 ignored issues
show
The method setTaxRate() 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

238
			$item->/** @scrutinizer ignore-call */ 
239
          setTaxRate( $entry['taxrate'] );

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...
239
			$item->setStatus( $entry['status'] );
240
241
			$mediaManager->saveItem( $item );
242
243
			$listItem->setId( null );
244
			$listItem->setType( $entry['list-type'] );
245
			$listItem->setDateStart( $entry['list-start'] );
246
			$listItem->setDateEnd( $entry['list-end'] );
247
			$listItem->setConfig( $entry['list-config'] );
248
			$listItem->setPosition( $entry['list-position'] );
249
			$listItem->setStatus( $entry['list-status'] );
250
			$listItem->setRefId( $item->getId() );
251
252
			$listManager->saveItem( $listItem, false );
253
254
255
			if( isset( $entry['attribute'] ) ) {
256
				$this->addAttributes( $item->getId(), $entry['attribute'], 'price' );
257
			}
258
259
			if( isset( $entry['media'] ) ) {
260
				$this->addMedia( $item->getId(), $entry['media'], 'price' );
261
			}
262
263
			if( isset( $entry['price'] ) ) {
264
				$this->addPrices( $item->getId(), $entry['price'], 'price' );
265
			}
266
267
			if( isset( $entry['text'] ) ) {
268
				$this->addTexts( $item->getId(), $entry['text'], 'price' );
269
			}
270
		}
271
	}
272
273
274
	/**
275
	 * Adds the texts to the given parent in the database.
276
	 *
277
	 * @param string $parentid ID of the parent item where the media should be associated to
278
	 * @param array $data Two dimensional associative list text data
279
	 * @param string $domain Domain name the texts should be added to, e.g. 'catalog'
280
	 */
281
	protected function addTexts( $parentid, array $data, $domain )
282
	{
283
		$context = $this->getContext();
284
		$textManager = \Aimeos\MShop::create( $context, 'text' );
285
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
286
287
288
		$item = $textManager->createItem();
289
		$item->setDomain( $domain );
290
291
		$listItem = $listManager->createItem();
292
		$listItem->setParentId( $parentid );
293
		$listItem->setDomain( 'text' );
294
295
296
		foreach( $data as $entry )
297
		{
298
			$item->setId( null );
299
			$item->setType( $entry['type'] );
300
			$item->setLanguageId( $entry['languageid'] );
301
			$item->setContent( $entry['content'] );
0 ignored issues
show
The method setContent() does not exist on Aimeos\MShop\Attribute\Item\Iface. Did you maybe mean setCode()? ( Ignorable by Annotation )

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

301
			$item->/** @scrutinizer ignore-call */ 
302
          setContent( $entry['content'] );

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...
302
			$item->setLabel( $entry['label'] );
303
			$item->setStatus( $entry['status'] );
304
305
			$textManager->saveItem( $item );
306
307
			$listItem->setId( null );
308
			$listItem->setType( $entry['list-type'] );
309
			$listItem->setDateStart( $entry['list-start'] );
310
			$listItem->setDateEnd( $entry['list-end'] );
311
			$listItem->setConfig( $entry['list-config'] );
312
			$listItem->setPosition( $entry['list-position'] );
313
			$listItem->setStatus( $entry['list-status'] );
314
			$listItem->setRefId( $item->getId() );
315
316
			$listManager->saveItem( $listItem, false );
317
318
319
			if( isset( $entry['attribute'] ) ) {
320
				$this->addAttributes( $item->getId(), $entry['attribute'], 'text' );
321
			}
322
323
			if( isset( $entry['media'] ) ) {
324
				$this->addMedia( $item->getId(), $entry['media'], 'text' );
325
			}
326
327
			if( isset( $entry['price'] ) ) {
328
				$this->addPrices( $item->getId(), $entry['price'], 'text' );
329
			}
330
331
			if( isset( $entry['text'] ) ) {
332
				$this->addTexts( $item->getId(), $entry['text'], 'text' );
333
			}
334
		}
335
	}
336
337
338
	/**
339
	 * Adds the products to the given parent in the database.
340
	 *
341
	 * @param string $parentid ID of the parent item where the products should be associated to
342
	 * @param array $data Two dimensional associative list of product data
343
	 * @param string $domain Domain name the products should be added to, e.g. 'catalog'
344
	 */
345
	protected function addProducts( $parentid, array $data, $domain )
346
	{
347
		$context = $this->getContext();
348
		$productManager = \Aimeos\MShop::create( $context, 'product' );
349
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
350
351
352
		$listItem = $listManager->createItem();
353
		$listItem->setParentId( $parentid );
354
		$listItem->setDomain( 'product' );
355
356
357
		$codes = [];
358
359
		foreach( $data as $entry ) {
360
			$codes[$entry['code']] = null;
361
		}
362
363
		$search = $productManager->createSearch();
364
		$search->setConditions( $search->compare( '==', 'product.code', array_keys( $codes ) ) );
365
		$products = $productManager->searchItems( $search );
366
367
		foreach( $products as $product ) {
368
			$codes[$product->getCode()] = $product->getId();
369
		}
370
371
372
		foreach( $data as $entry )
373
		{
374
			if( !isset( $codes[$entry['code']] ) ) {
375
				throw new \RuntimeException( sprintf( 'No product for code "%1$s" found', $entry['code'] ) );
376
			}
377
378
			$listItem->setId( null );
379
			$listItem->setType( $entry['list-type'] );
380
			$listItem->setDateStart( $entry['list-start'] );
381
			$listItem->setDateEnd( $entry['list-end'] );
382
			$listItem->setConfig( $entry['list-config'] );
383
			$listItem->setPosition( $entry['list-position'] );
384
			$listItem->setStatus( $entry['list-status'] );
385
			$listItem->setRefId( $codes[$entry['code']] );
386
387
			$listManager->saveItem( $listItem, false );
388
		}
389
	}
390
391
392
	/**
393
	 * Adds stock levels to the given product in the database.
394
	 *
395
	 * @param string $productcode Code of the product item where the stock levels should be associated to
396
	 * @param array $data Two dimensional associative list of product stock data
397
	 */
398
	protected function addProductStock( $productcode, array $data )
399
	{
400
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock/type' );
401
402
		$types = [];
403
		foreach( $manager->searchItems( $manager->createSearch() ) as $id => $item ) {
404
			$types[$item->getCode()] = $id;
405
		}
406
407
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
408
409
		$item = $manager->createItem();
410
		$item->setProductCode( $productcode );
0 ignored issues
show
The method setProductCode() 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

410
		$item->/** @scrutinizer ignore-call */ 
411
         setProductCode( $productcode );

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...
411
412
		foreach( $data as $entry )
413
		{
414
			$item->setId( null );
415
			$item->setDateBack( $entry['dateback'] );
0 ignored issues
show
The method setDateBack() 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

415
			$item->/** @scrutinizer ignore-call */ 
416
          setDateBack( $entry['dateback'] );

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...
416
			$item->setStockLevel( $entry['stocklevel'] );
0 ignored issues
show
The method setStockLevel() 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

416
			$item->/** @scrutinizer ignore-call */ 
417
          setStockLevel( $entry['stocklevel'] );

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...
417
			$item->setType( $entry['type'] );
418
419
			$manager->saveItem( $item, false );
420
		}
421
	}
422
423
424
	/**
425
	 * Returns the attribute for the given code, type and domain
426
	 *
427
	 * @param string $domain Domain the attribute belongs to
428
	 * @param string $type Attribute type
429
	 * @param string $code Attribute code
430
	 * @return \Aimeos\MShop\Attribute\Item\Iface|null Found attribute item or null if not available
431
	 */
432
	protected function getAttributeItem( $domain, $type, $code )
433
	{
434
		if( $this->attributes === null )
435
		{
436
			$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
437
438
			foreach( $manager->searchItems( $manager->createSearch() ) as $item ) {
439
				$this->attributes[$item->getDomain()][$item->getType()][$item->getCode()] = $item;
440
			}
441
		}
442
443
		if( isset( $this->attributes[$domain][$type][$code] ) ) {
444
			return $this->attributes[$domain][$type][$code];
445
		}
446
	}
447
448
449
	/**
450
	 * Returns the context.
451
	 *
452
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
453
	 */
454
	protected function getContext()
455
	{
456
		return $this->additional;
457
	}
458
459
460
	/**
461
	 * Deletes the demo items from the given parent ID in the database.
462
	 *
463
	 * @param string $parentid ID of the parent item where the associated items should be removed from
464
	 * @param string $name Name of the list manager, e.g. 'catalog/lists'
465
	 * @param string $domain Name of the domain the items are associated to, e.g. 'catalog'
466
	 * @param string $refdomain Name of the domain to remove the items from, e.g. 'text'
467
	 */
468
	protected function removeItems( $parentid, $name, $domain, $refdomain )
469
	{
470
		$context = $this->getContext();
471
		$key = str_replace( '/', '.', $name );
472
473
		$manager = \Aimeos\MShop::create( $context, $refdomain );
474
		$listManager = \Aimeos\MShop::create( $context, $name );
475
476
477
		$search = $manager->createSearch();
478
		$expr = array(
479
			$search->compare( '==', $refdomain . '.domain', $domain ),
480
			$search->compare( '=~', $refdomain . '.label', 'Demo' ),
481
		);
482
		$search->setConditions( $search->combine( '&&', $expr ) );
483
484
		$ids = array_keys( $manager->searchItems( $search ) );
485
		$manager->deleteItems( $ids );
486
487
488
		$search = $listManager->createSearch();
489
		$expr = array(
490
			$search->compare( '==', $key . '.parentid', $parentid ),
491
			$search->compare( '==', $key . '.domain', $refdomain ),
492
			$search->compare( '==', $key . '.refid', $ids ),
493
		);
494
		$search->setConditions( $search->combine( '&&', $expr ) );
495
496
		$listIds = array_keys( $listManager->searchItems( $search ) );
497
		$listManager->deleteItems( $listIds );
498
	}
499
500
501
	/**
502
	 * Deletes the references to non-existent or demo items in the database.
503
	 *
504
	 * @param string $parentid ID of the parent item where the associated items should be removed from
505
	 * @param string $name Name of the list manager, e.g. 'catalog/lists'
506
	 * @param string $refdomain Name of the domain to remove the items from, e.g. 'product'
507
	 */
508
	protected function removeListItems( $parentid, $name, $refdomain )
509
	{
510
		$start = 0;
511
		$context = $this->getContext();
512
		$key = str_replace( '/', '.', $name );
513
514
		$manager = \Aimeos\MShop::create( $context, $refdomain );
515
		$listManager = \Aimeos\MShop::create( $context, $name );
516
517
518
		$search = $listManager->createSearch();
519
		$expr = array(
520
			$search->compare( '==', $key . '.parentid', $parentid ),
521
			$search->compare( '==', $key . '.domain', $refdomain ),
522
		);
523
		$search->setConditions( $search->combine( '&&', $expr ) );
524
525
		do
526
		{
527
			$refIds = $listIds = $map = [];
528
			$result = $listManager->searchItems( $search );
529
530
			foreach( $result as $id => $listItem )
531
			{
532
				$refIds[] = $listItem->getRefId();
533
				$map[$listItem->getRefId()][] = $id;
534
			}
535
536
537
			$search = $manager->createSearch();
538
			$search->setConditions( $search->compare( '==', $refdomain . '.id', $refIds ) );
539
			$ids = array_keys( $manager->searchItems( $search ) );
540
541
			foreach( array_diff( $refIds, $ids ) as $refId ) {
542
				$listIds = array_merge( $listIds, $map[$refId] );
543
			}
544
545
			$listManager->deleteItems( $listIds );
546
547
548
			$count = count( $result );
549
			$start += $count;
550
			$search->setSlice( $start );
551
		}
552
		while( $count == $search->getSliceSize() );
553
554
555
		$search = $manager->createSearch();
556
		$search->setConditions( $search->compare( '=~', $refdomain . '.label', 'Demo' ) );
557
		$ids = array_keys( $manager->searchItems( $search ) );
558
559
		$search = $listManager->createSearch();
560
		$expr = array(
561
			$search->compare( '==', $key . '.parentid', $parentid ),
562
			$search->compare( '==', $key . '.refid', $ids ),
563
			$search->compare( '==', $key . '.domain', $refdomain ),
564
		);
565
		$search->setConditions( $search->combine( '&&', $expr ) );
566
567
		$listManager->deleteItems( array_keys( $listManager->searchItems( $search ) ) );
568
	}
569
570
571
	/**
572
	 * Starts a new transation
573
	 */
574
	protected function txBegin()
575
	{
576
		$dbm = $this->additional->getDatabaseManager();
577
578
		$conn = $dbm->acquire();
579
		$conn->begin();
580
		$dbm->release( $conn );
581
	}
582
583
584
	/**
585
	 * Commits an existing transaction
586
	 */
587
	protected function txCommit()
588
	{
589
		$dbm = $this->additional->getDatabaseManager();
590
591
		$conn = $dbm->acquire();
592
		$conn->commit();
593
		$dbm->release( $conn );
594
	}
595
}
596