Passed
Push — master ( 5a52ba...103d3f )
by Aimeos
04:09
created

Standard   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 1023
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 57
eloc 321
c 3
b 0
f 0
dl 0
loc 1023
rs 5.04

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 5
A delete() 0 28 6
A move() 0 24 2
A get() 0 3 1
A saveItem() 0 16 2
A filter() 0 3 1
A insert() 0 25 2
A find() 0 4 1
A clear() 0 56 2
A getSearchAttributes() 0 22 1
A getResourceType() 0 4 1
A create() 0 4 1
A saveChildren() 0 26 5
A updateUsage() 0 131 4
A getSubManager() 0 3 1
A search() 0 172 3
A lock() 0 28 2
A getPath() 0 33 6
B getTree() 0 58 9
A unlock() 0 29 2

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Catalog
9
 */
10
11
12
namespace Aimeos\MShop\Catalog\Manager;
13
14
15
/**
16
 * Catalog manager with methods for managing categories products, text, media.
17
 *
18
 * @package MShop
19
 * @subpackage Catalog
20
 */
21
class Standard extends Base
22
	implements \Aimeos\MShop\Catalog\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private $searchConfig = array(
25
		'id' => array(
26
			'code' => 'catalog.id',
27
			'internalcode' => 'mcat."id"',
28
			'label' => 'ID',
29
			'type' => 'integer',
30
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
31
			'public' => false,
32
		),
33
		'catalog.siteid' => array(
34
			'code' => 'catalog.siteid',
35
			'internalcode' => 'mcat."siteid"',
36
			'label' => 'Site ID',
37
			'type' => 'string',
38
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
39
			'public' => false,
40
		),
41
		'parentid' => array(
42
			'code' => 'catalog.parentid',
43
			'internalcode' => 'mcat."parentid"',
44
			'label' => 'Parent ID',
45
			'type' => 'integer',
46
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
47
			'public' => false,
48
		),
49
		'level' => array(
50
			'code' => 'catalog.level',
51
			'internalcode' => 'mcat."level"',
52
			'label' => 'Tree level',
53
			'type' => 'integer',
54
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
55
			'public' => false,
56
		),
57
		'left' => array(
58
			'code' => 'catalog.left',
59
			'internalcode' => 'mcat."nleft"',
60
			'label' => 'Left value',
61
			'type' => 'integer',
62
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
63
			'public' => false,
64
		),
65
		'right' => array(
66
			'code' => 'catalog.right',
67
			'internalcode' => 'mcat."nright"',
68
			'label' => 'Right value',
69
			'type' => 'integer',
70
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
71
			'public' => false,
72
		),
73
		'label' => array(
74
			'code' => 'catalog.label',
75
			'internalcode' => 'mcat."label"',
76
			'label' => 'Label',
77
			'type' => 'string',
78
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
79
		),
80
		'code' => array(
81
			'code' => 'catalog.code',
82
			'internalcode' => 'mcat."code"',
83
			'label' => 'Code',
84
			'type' => 'string',
85
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
86
		),
87
		'status' => array(
88
			'code' => 'catalog.status',
89
			'internalcode' => 'mcat."status"',
90
			'label' => 'Status',
91
			'type' => 'integer',
92
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
93
		),
94
		'catalog.url' => array(
95
			'code' => 'catalog.url',
96
			'internalcode' => 'mcat."url"',
97
			'label' => 'URL segment',
98
			'type' => 'string',
99
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
100
		),
101
		'catalog.target' => array(
102
			'code' => 'catalog.target',
103
			'internalcode' => 'mcat."target"',
104
			'label' => 'URL target',
105
			'type' => 'string',
106
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
107
		),
108
		'catalog.config' => array(
109
			'code' => 'catalog.config',
110
			'internalcode' => 'mcat."config"',
111
			'label' => 'Config',
112
			'type' => 'string',
113
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
114
			'public' => false,
115
		),
116
		'catalog.ctime' => array(
117
			'label' => 'Create date/time',
118
			'code' => 'catalog.ctime',
119
			'internalcode' => 'mcat."ctime"',
120
			'type' => 'datetime',
121
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
122
			'public' => false,
123
		),
124
		'catalog.mtime' => array(
125
			'label' => 'Modify date/time',
126
			'code' => 'catalog.mtime',
127
			'internalcode' => 'mcat."mtime"',
128
			'type' => 'datetime',
129
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
130
			'public' => false,
131
		),
132
		'catalog.editor' => array(
133
			'code' => 'catalog.editor',
134
			'internalcode' => 'mcat."editor"',
135
			'label' => 'Editor',
136
			'type' => 'string',
137
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
138
			'public' => false,
139
		),
140
		'catalog:has' => array(
141
			'code' => 'catalog:has()',
142
			'internalcode' => ':site AND :key AND mcatli."id"',
143
			'internaldeps' => ['LEFT JOIN "mshop_catalog_list" AS mcatli ON ( mcatli."parentid" = mcat."id" )'],
144
			'label' => 'Catalog has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
145
			'type' => 'null',
146
			'internaltype' => 'null',
147
			'public' => false,
148
		),
149
	);
150
151
152
	/**
153
	 * Initializes the object.
154
	 *
155
	 * @param \Aimeos\MShop\ContextIface $context Context object
156
	 */
157
	public function __construct( \Aimeos\MShop\ContextIface $context )
158
	{
159
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
160
		$level = $context->config()->get( 'mshop/catalog/manager/sitemode', $level );
161
162
163
		$this->searchConfig['catalog:has']['function'] = function( &$source, array $params ) use ( $level ) {
164
165
			$keys = [];
166
167
			foreach( (array) ( $params[1] ?? '' ) as $type ) {
168
				foreach( (array) ( $params[2] ?? '' ) as $id ) {
169
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
170
				}
171
			}
172
173
			$sitestr = $this->siteString( 'mcatli."siteid"', $level );
174
			$keystr = $this->toExpression( 'mcatli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
175
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
176
177
			return $params;
178
		};
179
180
181
		parent::__construct( $context, $this->searchConfig );
182
		$this->setResourceName( 'db-catalog' );
183
	}
184
185
186
	/**
187
	 * Removes old entries from the storage.
188
	 *
189
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
190
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
191
	 */
192
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
193
	{
194
		$context = $this->context();
195
		$config = $context->config();
196
		$search = $this->object()->filter();
197
198
		foreach( $config->get( 'mshop/catalog/manager/submanagers', ['lists'] ) as $domain ) {
199
			$this->object()->getSubManager( $domain )->clear( $siteids );
200
		}
201
202
		$conn = $context->db( $this->getResourceName() );
203
204
			/** mshop/catalog/manager/cleanup/mysql
205
			 * Deletes the categories for the given site from the database
206
			 *
207
			 * @see mshop/catalog/manager/cleanup/ansi
208
			 */
209
210
			/** mshop/catalog/manager/cleanup/ansi
211
			 * Deletes the categories for the given site from the database
212
			 *
213
			 * Removes the records matched by the given site ID from the catalog
214
			 * database.
215
			 *
216
			 * The ":siteid" placeholder is replaced by the name and value of the
217
			 * site ID column and the given ID or list of IDs.
218
			 *
219
			 * The SQL statement should conform to the ANSI standard to be
220
			 * compatible with most relational database systems. This also
221
			 * includes using double quotes for table and column names.
222
			 *
223
			 * @param string SQL statement for removing the records
224
			 * @since 2014.03
225
			 * @category Developer
226
			 * @see mshop/catalog/manager/delete/ansi
227
			 * @see mshop/catalog/manager/insert/ansi
228
			 * @see mshop/catalog/manager/update/ansi
229
			 * @see mshop/catalog/manager/newid/ansi
230
			 * @see mshop/catalog/manager/search/ansi
231
			 * @see mshop/catalog/manager/count/ansi
232
			 */
233
			$path = 'mshop/catalog/manager/cleanup';
234
			$sql = $this->getSqlConfig( $path );
235
236
			$types = array( 'siteid' => \Aimeos\Base\DB\Statement\Base::PARAM_STR );
237
			$translations = array( 'siteid' => '"siteid"' );
238
239
			$search->setConditions( $search->compare( '==', 'siteid', $siteids ) );
240
			$sql = str_replace( ':siteid', $search->getConditionSource( $types, $translations ), $sql );
241
242
			$stmt = $conn->create( $sql );
243
			$stmt->bind( 1, 0, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
244
			$stmt->bind( 2, 0x7FFFFFFF, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
245
			$stmt->execute()->finish();
246
247
		return $this;
248
	}
249
250
251
	/**
252
	 * Creates a new empty item instance
253
	 *
254
	 * @param array $values Values the item should be initialized with
255
	 * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object
256
	 */
257
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
258
	{
259
		$values['siteid'] = $this->context()->locale()->getSiteId();
260
		return $this->createItemBase( $values );
261
	}
262
263
264
	/**
265
	 * Creates a filter object.
266
	 *
267
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
268
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
269
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
270
	 */
271
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
272
	{
273
		return $this->filterBase( 'catalog', $default );
274
	}
275
276
277
	/**
278
	 * Removes multiple items.
279
	 *
280
	 * @param \Aimeos\MShop\Common\Item\Iface|array|string $items List of item objects or IDs of the items
281
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
282
	 */
283
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
284
	{
285
		if( is_map( $items ) ) { $items = $items->toArray(); }
286
		if( !is_array( $items ) ) { $items = [$items]; }
287
		if( empty( $items ) ) { return $this; }
288
289
		$this->begin();
290
		$this->lock();
291
292
		try
293
		{
294
			$siteid = $this->context()->locale()->getSiteId();
295
296
			foreach( $items as $item ) {
297
				$this->createTreeManager( $siteid )->deleteNode( (string) $item );
298
			}
299
300
			$this->unlock();
301
			$this->commit();
302
		}
303
		catch( \Exception $e )
304
		{
305
			$this->unlock();
306
			$this->rollback();
307
			throw $e;
308
		}
309
310
		return $this->deleteRefItems( $items );
311
	}
312
313
314
	/**
315
	 * Returns the item specified by its code and domain/type if necessary
316
	 *
317
	 * @param string $code Code of the item
318
	 * @param string[] $ref List of domains to fetch list items and referenced items for
319
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
320
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
321
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
322
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item object
323
	 */
324
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
325
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
326
	{
327
		return $this->findBase( array( 'catalog.code' => $code ), $ref, $default );
328
	}
329
330
331
	/**
332
	 * Returns the item specified by its ID.
333
	 *
334
	 * @param string $id Unique ID of the catalog item
335
	 * @param string[] $ref List of domains to fetch list items and referenced items for
336
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
337
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item of the given ID
338
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
339
	 */
340
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
341
	{
342
		return $this->getItemBase( 'catalog.id', $id, $ref, $default );
343
	}
344
345
346
	/**
347
	 * Returns the available manager types
348
	 *
349
	 * @param bool $withsub Return also the resource type of sub-managers if true
350
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
351
	 */
352
	public function getResourceType( bool $withsub = true ) : array
353
	{
354
		$path = 'mshop/catalog/manager/submanagers';
355
		return $this->getResourceTypeBase( 'catalog', $path, array( 'lists' ), $withsub );
356
	}
357
358
359
	/**
360
	 * Returns the attributes that can be used for searching.
361
	 *
362
	 * @param bool $withsub Return also attributes of sub-managers if true
363
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
364
	 */
365
	public function getSearchAttributes( bool $withsub = true ) : array
366
	{
367
		/** mshop/catalog/manager/submanagers
368
		 * List of manager names that can be instantiated by the catalog manager
369
		 *
370
		 * Managers provide a generic interface to the underlying storage.
371
		 * Each manager has or can have sub-managers caring about particular
372
		 * aspects. Each of these sub-managers can be instantiated by its
373
		 * parent manager using the getSubManager() method.
374
		 *
375
		 * The search keys from sub-managers can be normally used in the
376
		 * manager as well. It allows you to search for items of the manager
377
		 * using the search keys of the sub-managers to further limit the
378
		 * retrieved list of items.
379
		 *
380
		 * @param array List of sub-manager names
381
		 * @since 2014.03
382
		 * @category Developer
383
		 */
384
		$path = 'mshop/catalog/manager/submanagers';
385
386
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
387
	}
388
389
390
	/**
391
	 * Adds a new item object.
392
	 *
393
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Item which should be inserted
394
	 * @param string|null $parentId ID of the parent item where the item should be inserted into
395
	 * @param string|null $refId ID of the item where the item should be inserted before (null to append)
396
	 * @return \Aimeos\MShop\Catalog\Item\Iface $item Updated item including the generated ID
397
	 */
398
	public function insert( \Aimeos\MShop\Catalog\Item\Iface $item, string $parentId = null,
399
		string $refId = null ) : \Aimeos\MShop\Catalog\Item\Iface
400
	{
401
		$this->begin();
402
		$this->lock();
403
404
		try
405
		{
406
			$node = $item->getNode();
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Aimeos\MShop\Catalog\Item\Iface. Did you maybe mean getCode()? ( Ignorable by Annotation )

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

406
			/** @scrutinizer ignore-call */ 
407
   $node = $item->getNode();

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...
407
			$siteid = $this->context()->locale()->getSiteId();
408
409
			$this->createTreeManager( $siteid )->insertNode( $node, $parentId, $refId );
410
			$this->updateUsage( $node->getId(), $item, true );
411
			$this->unlock();
412
			$this->commit();
413
		}
414
		catch( \Exception $e )
415
		{
416
			$this->unlock();
417
			$this->rollback();
418
			throw $e;
419
		}
420
421
		$item = $this->saveListItems( $item, 'catalog' );
422
		return $this->saveChildren( $item );
423
	}
424
425
426
	/**
427
	 * Moves an existing item to the new parent in the storage.
428
	 *
429
	 * @param string $id ID of the item that should be moved
430
	 * @param string|null $oldParentId ID of the old parent item which currently contains the item that should be removed
431
	 * @param string|null $newParentId ID of the new parent item where the item should be moved to
432
	 * @param string|null $refId ID of the item where the item should be inserted before (null to append)
433
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
434
	 */
435
	public function move( string $id, string $oldParentId = null, string $newParentId = null,
436
		string $refId = null ) : \Aimeos\MShop\Catalog\Manager\Iface
437
	{
438
		$this->begin();
439
		$this->lock();
440
441
		try
442
		{
443
			$item = $this->object()->get( $id );
444
			$siteid = $this->context()->locale()->getSiteId();
445
446
			$this->createTreeManager( $siteid )->moveNode( $id, $oldParentId, $newParentId, $refId );
447
			$this->updateUsage( $id, $item );
448
			$this->unlock();
449
			$this->commit();
450
		}
451
		catch( \Exception $e )
452
		{
453
			$this->unlock();
454
			$this->rollback();
455
			throw $e;
456
		}
457
458
		return $this;
459
	}
460
461
462
	/**
463
	 * Updates an item object.
464
	 *
465
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Item object whose data should be saved
466
	 * @param bool $fetch True if the new ID should be returned in the item
467
	 * @return \Aimeos\MShop\Catalog\Item\Iface $item Updated item including the generated ID
468
	 */
469
	public function saveItem( \Aimeos\MShop\Catalog\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Catalog\Item\Iface
470
	{
471
		if( !$item->isModified() )
472
		{
473
			$item = $this->saveListItems( $item, 'catalog', $fetch );
474
			return $this->saveChildren( $item );
475
		}
476
477
		$node = $item->getNode();
478
		$siteid = $this->context()->locale()->getSiteId();
479
480
		$this->createTreeManager( $siteid )->saveNode( $node );
481
		$this->updateUsage( $node->getId(), $item );
482
483
		$item = $this->saveListItems( $item, 'catalog', $fetch );
484
		return $this->saveChildren( $item );
485
	}
486
487
488
	/**
489
	 * Searches for all items matching the given critera.
490
	 *
491
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
492
	 * @param string[] $ref List of domains to fetch list items and referenced items for
493
	 * @param int|null &$total Number of items that are available in total
494
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Catalog\Item\Iface with ids as keys
495
	 */
496
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
497
	{
498
		$map = [];
499
		$required = ['catalog'];
500
		$context = $this->context();
501
		$conn = $context->db( $this->getResourceName() );
502
503
		/** mshop/catalog/manager/sitemode
504
		 * Mode how items from levels below or above in the site tree are handled
505
		 *
506
		 * By default, only items from the current site are fetched from the
507
		 * storage. If the ai-sites extension is installed, you can create a
508
		 * tree of sites. Then, this setting allows you to define for the
509
		 * whole catalog domain if items from parent sites are inherited,
510
		 * sites from child sites are aggregated or both.
511
		 *
512
		 * Available constants for the site mode are:
513
		 * * 0 = only items from the current site
514
		 * * 1 = inherit items from parent sites
515
		 * * 2 = aggregate items from child sites
516
		 * * 3 = inherit and aggregate items at the same time
517
		 *
518
		 * You also need to set the mode in the locale manager
519
		 * (mshop/locale/manager/sitelevel) to one of the constants.
520
		 * If you set it to the same value, it will work as described but you
521
		 * can also use different modes. For example, if inheritance and
522
		 * aggregation is configured the locale manager but only inheritance
523
		 * in the domain manager because aggregating items makes no sense in
524
		 * this domain, then items wil be only inherited. Thus, you have full
525
		 * control over inheritance and aggregation in each domain.
526
		 *
527
		 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
528
		 * @category Developer
529
		 * @since 2018.01
530
		 * @see mshop/locale/manager/sitelevel
531
		 */
532
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
533
		$level = $context->config()->get( 'mshop/catalog/manager/sitemode', $level );
534
535
		/** mshop/catalog/manager/search-item/mysql
536
		 * Retrieves the records matched by the given criteria in the database
537
		 *
538
		 * @see mshop/catalog/manager/search-item/ansi
539
		 */
540
541
		/** mshop/catalog/manager/search-item/ansi
542
		 * Retrieves the records matched by the given criteria in the database
543
		 *
544
		 * Fetches the records matched by the given criteria from the catalog
545
		 * database. The records must be from one of the sites that are
546
		 * configured via the context item. If the current site is part of
547
		 * a tree of sites, the SELECT statement can retrieve all records
548
		 * from the current site and the complete sub-tree of sites.
549
		 *
550
		 * As the records can normally be limited by criteria from sub-managers,
551
		 * their tables must be joined in the SQL context. This is done by
552
		 * using the "internaldeps" property from the definition of the ID
553
		 * column of the sub-managers. These internal dependencies specify
554
		 * the JOIN between the tables and the used columns for joining. The
555
		 * ":joins" placeholder is then replaced by the JOIN strings from
556
		 * the sub-managers.
557
		 *
558
		 * To limit the records matched, conditions can be added to the given
559
		 * criteria object. It can contain comparisons like column names that
560
		 * must match specific values which can be combined by AND, OR or NOT
561
		 * operators. The resulting string of SQL conditions replaces the
562
		 * ":cond" placeholder before the statement is sent to the database
563
		 * server.
564
		 *
565
		 * If the records that are retrieved should be ordered by one or more
566
		 * columns, the generated string of column / sort direction pairs
567
		 * replaces the ":order" placeholder. In case no ordering is required,
568
		 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
569
		 * markers is removed to speed up retrieving the records. Columns of
570
		 * sub-managers can also be used for ordering the result set but then
571
		 * no index can be used.
572
		 *
573
		 * The number of returned records can be limited and can start at any
574
		 * number between the begining and the end of the result set. For that
575
		 * the ":size" and ":start" placeholders are replaced by the
576
		 * corresponding values from the criteria object. The default values
577
		 * are 0 for the start and 100 for the size value.
578
		 *
579
		 * The SQL statement should conform to the ANSI standard to be
580
		 * compatible with most relational database systems. This also
581
		 * includes using double quotes for table and column names.
582
		 *
583
		 * @param string SQL statement for searching items
584
		 * @since 2014.03
585
		 * @category Developer
586
		 * @see mshop/catalog/manager/delete/ansi
587
		 * @see mshop/catalog/manager/get/ansi
588
		 * @see mshop/catalog/manager/insert/ansi
589
		 * @see mshop/catalog/manager/update/ansi
590
		 * @see mshop/catalog/manager/newid/ansi
591
		 * @see mshop/catalog/manager/search/ansi
592
		 * @see mshop/catalog/manager/count/ansi
593
		 * @see mshop/catalog/manager/move-left/ansi
594
		 * @see mshop/catalog/manager/move-right/ansi
595
		 * @see mshop/catalog/manager/update-parentid/ansi
596
		 */
597
		$cfgPathSearch = 'mshop/catalog/manager/search-item';
598
599
		/** mshop/catalog/manager/count/mysql
600
		 * Counts the number of records matched by the given criteria in the database
601
		 *
602
		 * @see mshop/catalog/manager/count/ansi
603
		 */
604
605
		/** mshop/catalog/manager/count/ansi
606
		 * Counts the number of records matched by the given criteria in the database
607
		 *
608
		 * Counts all records matched by the given criteria from the catalog
609
		 * database. The records must be from one of the sites that are
610
		 * configured via the context item. If the current site is part of
611
		 * a tree of sites, the statement can count all records from the
612
		 * current site and the complete sub-tree of sites.
613
		 *
614
		 * As the records can normally be limited by criteria from sub-managers,
615
		 * their tables must be joined in the SQL context. This is done by
616
		 * using the "internaldeps" property from the definition of the ID
617
		 * column of the sub-managers. These internal dependencies specify
618
		 * the JOIN between the tables and the used columns for joining. The
619
		 * ":joins" placeholder is then replaced by the JOIN strings from
620
		 * the sub-managers.
621
		 *
622
		 * To limit the records matched, conditions can be added to the given
623
		 * criteria object. It can contain comparisons like column names that
624
		 * must match specific values which can be combined by AND, OR or NOT
625
		 * operators. The resulting string of SQL conditions replaces the
626
		 * ":cond" placeholder before the statement is sent to the database
627
		 * server.
628
		 *
629
		 * Both, the strings for ":joins" and for ":cond" are the same as for
630
		 * the "search" SQL statement.
631
		 *
632
		 * Contrary to the "search" statement, it doesn't return any records
633
		 * but instead the number of records that have been found. As counting
634
		 * thousands of records can be a long running task, the maximum number
635
		 * of counted records is limited for performance reasons.
636
		 *
637
		 * The SQL statement should conform to the ANSI standard to be
638
		 * compatible with most relational database systems. This also
639
		 * includes using double quotes for table and column names.
640
		 *
641
		 * @param string SQL statement for counting items
642
		 * @since 2014.03
643
		 * @category Developer
644
		 * @see mshop/catalog/manager/delete/ansi
645
		 * @see mshop/catalog/manager/get/ansi
646
		 * @see mshop/catalog/manager/insert/ansi
647
		 * @see mshop/catalog/manager/update/ansi
648
		 * @see mshop/catalog/manager/newid/ansi
649
		 * @see mshop/catalog/manager/search/ansi
650
		 * @see mshop/catalog/manager/search-item/ansi
651
		 * @see mshop/catalog/manager/move-left/ansi
652
		 * @see mshop/catalog/manager/move-right/ansi
653
		 * @see mshop/catalog/manager/update-parentid/ansi
654
		 */
655
		$cfgPathCount = 'mshop/catalog/manager/count';
656
657
		if( $search->getSortations() === [] ) {
658
			$search->setSortations( [$search->sort( '+', 'catalog.left' )] );
659
		}
660
661
		$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
662
663
		while( ( $row = $results->fetch() ) !== null ) {
664
			$map[$row['id']] = new \Aimeos\MW\Tree\Node\Standard( $row );
665
		}
666
667
		return $this->buildItems( $map, $ref, 'catalog' );
668
	}
669
670
671
	/**
672
	 * Returns a list of items starting with the given category that are in the path to the root node
673
	 *
674
	 * @param string $id ID of item to get the path for
675
	 * @param string[] $ref List of domains to fetch list items and referenced items for
676
	 * @return \Aimeos\Map Associative list of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface with IDs as keys
677
	 */
678
	public function getPath( string $id, array $ref = [] ) : \Aimeos\Map
679
	{
680
		$mode = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
681
		$mode = $this->context()->config()->get( 'mshop/catalog/manager/sitemode', $mode );
682
683
		if( $mode !== \Aimeos\MShop\Locale\Manager\Base::SITE_ONE ) {
684
			$sitePath = array_reverse( (array) $this->context()->locale()->getSitePath() );
685
		} else {
686
			$sitePath = [$this->context()->locale()->getSiteId()];
687
		}
688
689
		foreach( $sitePath as $siteId )
690
		{
691
			try {
692
				$path = $this->createTreeManager( $siteId )->getPath( $id );
693
			} catch( \Exception $e ) {
694
				continue;
695
			}
696
697
			if( !empty( $path ) )
698
			{
699
				$itemMap = [];
700
701
				foreach( $path as $node ) {
702
					$itemMap[$node->getId()] = $node;
703
				}
704
705
				return $this->buildItems( $itemMap, $ref, 'catalog' );
706
			}
707
		}
708
709
		$msg = $this->context()->translate( 'mshop', 'Catalog path for ID "%1$s" not found' );
710
		throw new \Aimeos\MShop\Catalog\Exception( sprintf( $msg, $id ) );
711
	}
712
713
714
	/**
715
	 * Returns a node and its descendants depending on the given resource.
716
	 *
717
	 * @param string|null $id Retrieve nodes starting from the given ID
718
	 * @param string[] List of domains (e.g. text, media, etc.) whose referenced items should be attached to the objects
719
	 * @param int $level One of the level constants from \Aimeos\MW\Tree\Manager\Base
720
	 * @param \Aimeos\Base\Criteria\Iface|null $criteria Optional criteria object with conditions
721
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item, maybe with subnodes
722
	 */
723
	public function getTree( string $id = null, array $ref = [], int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE,
724
		\Aimeos\Base\Criteria\Iface $criteria = null ) : \Aimeos\MShop\Catalog\Item\Iface
725
	{
726
		$mode = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
727
		$mode = $this->context()->config()->get( 'mshop/catalog/manager/sitemode', $mode );
728
729
		if( $mode !== \Aimeos\MShop\Locale\Manager\Base::SITE_ONE ) {
730
			$sitePath = array_reverse( (array) $this->context()->locale()->getSitePath() );
731
		} else {
732
			$sitePath = [$this->context()->locale()->getSiteId()];
733
		}
734
735
		foreach( $sitePath as $siteId )
736
		{
737
			try {
738
				$node = $this->createTreeManager( $siteId )->getNode( $id, $level, $criteria );
739
			} catch( \Aimeos\MW\Tree\Exception $e ) {
740
				continue;
741
			}
742
743
			$listItems = $listItemMap = $refIdMap = [];
744
			$nodeMap = $this->getNodeMap( $node );
745
746
			if( count( $ref ) > 0 ) {
747
				$listItems = $this->getListItems( array_keys( $nodeMap ), $ref, 'catalog' );
748
			}
749
750
			foreach( $listItems as $listItem )
751
			{
752
				$domain = $listItem->getDomain();
753
				$parentid = $listItem->getParentId();
754
755
				$listItemMap[$parentid][$domain][$listItem->getId()] = $listItem;
756
				$refIdMap[$domain][$listItem->getRefId()][] = $parentid;
757
			}
758
759
			$refItemMap = $this->getRefItems( $refIdMap, $ref );
760
			$nodeid = $node->getId();
761
762
			$listItems = [];
763
			if( array_key_exists( $nodeid, $listItemMap ) ) {
764
				$listItems = $listItemMap[$nodeid];
765
			}
766
767
			$refItems = [];
768
			if( array_key_exists( $nodeid, $refItemMap ) ) {
769
				$refItems = $refItemMap[$nodeid];
770
			}
771
772
			if( $item = $this->applyFilter( $this->createItemBase( [], $listItems, $refItems, [], $node ) ) )
773
			{
774
				$this->createTree( $node, $item, $listItemMap, $refItemMap );
0 ignored issues
show
Bug introduced by
$node of type void is incompatible with the type Aimeos\MW\Tree\Node\Iface expected by parameter $node of Aimeos\MShop\Catalog\Manager\Base::createTree(). ( Ignorable by Annotation )

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

774
				$this->createTree( /** @scrutinizer ignore-type */ $node, $item, $listItemMap, $refItemMap );
Loading history...
775
				return $item;
776
			}
777
		}
778
779
		$msg = $this->context()->translate( 'mshop', 'No catalog node for ID "%1$s"' );
780
		throw new \Aimeos\MShop\Catalog\Exception( sprintf( $msg, $id ) );
781
	}
782
783
784
	/**
785
	 * Creates a new extension manager in the domain.
786
	 *
787
	 * @param string $manager Name of the sub manager type
788
	 * @param string|null $name Name of the implementation, will be from configuration (or Default)
789
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager extending the domain functionality
790
	 */
791
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
792
	{
793
		return $this->getSubManagerBase( 'catalog', $manager, $name );
794
	}
795
796
797
	/**
798
	 * Saves the children of the given node
799
	 *
800
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object incl. child items
801
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item with saved child items
802
	 */
803
	protected function saveChildren( \Aimeos\MShop\Catalog\Item\Iface $item ) : \Aimeos\MShop\Catalog\Item\Iface
804
	{
805
		$rmIds = [];
806
		foreach( $item->getChildrenDeleted() as $child ) {
807
			$rmIds[] = $child->getId();
808
		}
809
810
		$this->delete( $rmIds );
811
812
		foreach( $item->getChildren() as $child )
813
		{
814
			if( $child->getId() !== null )
815
			{
816
				$this->save( $child );
817
818
				if( $child->getParentId() !== $item->getParentId() ) {
819
					$this->move( $child->getId(), $item->getParentId(), $child->getParentId() );
820
				}
821
			}
822
			else
823
			{
824
				$this->insert( $child, $item->getId() );
825
			}
826
		}
827
828
		return $item;
829
	}
830
831
832
	/**
833
	 * Locks the catalog table against modifications from other connections
834
	 *
835
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
836
	 */
837
	protected function lock() : \Aimeos\MShop\Catalog\Manager\Iface
838
	{
839
		/** mshop/catalog/manager/lock/mysql
840
		 * SQL statement for locking the catalog table
841
		 *
842
		 * @see mshop/catalog/manager/lock/ansi
843
		 */
844
845
		/** mshop/catalog/manager/lock/ansi
846
		 * SQL statement for locking the catalog table
847
		 *
848
		 * Updating the nested set of categories in the catalog table requires locking
849
		 * the whole table to avoid data corruption. This statement will be followed by
850
		 * insert or update statements and closed by an unlock statement.
851
		 *
852
		 * @param string Lock SQL statement
853
		 * @since 2019.04
854
		 * @category Developer
855
		 */
856
		$path = 'mshop/catalog/manager/lock';
857
858
		if( ( $sql = $this->getSqlConfig( $path ) ) !== $path )
859
		{
860
			$conn = $this->context()->db( $this->getResourceName() );
861
			$conn->create( $sql )->execute()->finish();
0 ignored issues
show
Bug introduced by
It seems like $sql can also be of type array; however, parameter $sql of Aimeos\Base\DB\Connection\Iface::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

861
			$conn->create( /** @scrutinizer ignore-type */ $sql )->execute()->finish();
Loading history...
862
		}
863
864
		return $this;
865
	}
866
867
868
	/**
869
	 * Unlocks the catalog table for modifications from other connections
870
	 *
871
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
872
	 */
873
	protected function unlock() : \Aimeos\MShop\Catalog\Manager\Iface
874
	{
875
		/** mshop/catalog/manager/unlock/mysql
876
		 * SQL statement for unlocking the catalog table
877
		 *
878
		 * @see mshop/catalog/manager/unlock/ansi
879
		 */
880
881
		/** mshop/catalog/manager/unlock/ansi
882
		 * SQL statement for unlocking the catalog table
883
		 *
884
		 * Updating the nested set of categories in the catalog table requires locking
885
		 * the whole table to avoid data corruption. This statement will be executed
886
		 * after the table is locked and insert or update statements have been sent to
887
		 * the database.
888
		 *
889
		 * @param string Lock SQL statement
890
		 * @since 2019.04
891
		 * @category Developer
892
		 */
893
		 $path = 'mshop/catalog/manager/unlock';
894
895
		if( ( $sql = $this->getSqlConfig( $path ) ) !== $path )
896
		{
897
			$conn = $this->context()->db( $this->getResourceName() );
898
			$conn->create( $sql )->execute()->finish();
0 ignored issues
show
Bug introduced by
It seems like $sql can also be of type array; however, parameter $sql of Aimeos\Base\DB\Connection\Iface::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

898
			$conn->create( /** @scrutinizer ignore-type */ $sql )->execute()->finish();
Loading history...
899
		}
900
901
		return $this;
902
	}
903
904
905
	/**
906
	 * Updates the usage information of a node.
907
	 *
908
	 * @param string $id Id of the record
909
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item
910
	 * @param bool $case True if the record shoud be added or false for an update
911
	 * @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
912
	 */
913
	private function updateUsage( string $id, \Aimeos\MShop\Catalog\Item\Iface $item,
914
		bool $case = false ) : \Aimeos\MShop\Catalog\Manager\Iface
915
	{
916
		$date = date( 'Y-m-d H:i:s' );
917
		$context = $this->context();
918
		$conn = $context->db( $this->getResourceName() );
919
920
			$siteid = $context->locale()->getSiteId();
921
			$columns = $this->object()->getSaveAttributes();
922
923
			if( $case !== true )
924
			{
925
				/** mshop/catalog/manager/update-usage/mysql
926
				 * Updates the config, editor and mtime value of an updated record
927
				 *
928
				 * @see mshop/catalog/manager/update-usage/ansi
929
				 */
930
931
				/** mshop/catalog/manager/update-usage/ansi
932
				 * Updates the config, editor and mtime value of an updated record
933
				 *
934
				 * Each record contains some usage information like when it was
935
				 * created, last modified and by whom. These information are part
936
				 * of the catalog items and the generic tree manager doesn't care
937
				 * about this information. Thus, they are updated after the tree
938
				 * manager saved the basic record information.
939
				 *
940
				 * The SQL statement must be a string suitable for being used as
941
				 * prepared statement. It must include question marks for binding
942
				 * the values from the catalog item to the statement before they are
943
				 * sent to the database server. The order of the columns must
944
				 * correspond to the order in the method using this statement,
945
				 * so the correct values are bound to the columns.
946
				 *
947
				 * The SQL statement should conform to the ANSI standard to be
948
				 * compatible with most relational database systems. This also
949
				 * includes using double quotes for table and column names.
950
				 *
951
				 * @param string SQL statement for updating records
952
				 * @since 2014.03
953
				 * @category Developer
954
				 * @see mshop/catalog/manager/delete/ansi
955
				 * @see mshop/catalog/manager/get/ansi
956
				 * @see mshop/catalog/manager/insert/ansi
957
				 * @see mshop/catalog/manager/newid/ansi
958
				 * @see mshop/catalog/manager/search/ansi
959
				 * @see mshop/catalog/manager/search-item/ansi
960
				 * @see mshop/catalog/manager/count/ansi
961
				 * @see mshop/catalog/manager/move-left/ansi
962
				 * @see mshop/catalog/manager/move-right/ansi
963
				 * @see mshop/catalog/manager/update-parentid/ansi
964
				 * @see mshop/catalog/manager/insert-usage/ansi
965
				 */
966
				$path = 'mshop/catalog/manager/update-usage';
967
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig($path) can also be of type array; however, parameter $sql of Aimeos\MShop\Common\Manager\Base::addSqlColumns() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

967
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ), false );
Loading history...
968
			}
969
			else
970
			{
971
				/** mshop/catalog/manager/insert-usage/mysql
972
				 * Updates the config, editor, ctime and mtime value of an inserted record
973
				 *
974
				 * @see mshop/catalog/manager/insert-usage/ansi
975
				 */
976
977
				/** mshop/catalog/manager/insert-usage/ansi
978
				 * Updates the config, editor, ctime and mtime value of an inserted record
979
				 *
980
				 * Each record contains some usage information like when it was
981
				 * created, last modified and by whom. These information are part
982
				 * of the catalog items and the generic tree manager doesn't care
983
				 * about this information. Thus, they are updated after the tree
984
				 * manager inserted the basic record information.
985
				 *
986
				 * The SQL statement must be a string suitable for being used as
987
				 * prepared statement. It must include question marks for binding
988
				 * the values from the catalog item to the statement before they are
989
				 * sent to the database server. The order of the columns must
990
				 * correspond to the order in the method using this statement,
991
				 * so the correct values are bound to the columns.
992
				 *
993
				 * The SQL statement should conform to the ANSI standard to be
994
				 * compatible with most relational database systems. This also
995
				 * includes using double quotes for table and column names.
996
				 *
997
				 * @param string SQL statement for updating records
998
				 * @since 2014.03
999
				 * @category Developer
1000
				 * @see mshop/catalog/manager/delete/ansi
1001
				 * @see mshop/catalog/manager/get/ansi
1002
				 * @see mshop/catalog/manager/insert/ansi
1003
				 * @see mshop/catalog/manager/newid/ansi
1004
				 * @see mshop/catalog/manager/search/ansi
1005
				 * @see mshop/catalog/manager/search-item/ansi
1006
				 * @see mshop/catalog/manager/count/ansi
1007
				 * @see mshop/catalog/manager/move-left/ansi
1008
				 * @see mshop/catalog/manager/move-right/ansi
1009
				 * @see mshop/catalog/manager/update-parentid/ansi
1010
				 * @see mshop/catalog/manager/update-usage/ansi
1011
				 */
1012
				$path = 'mshop/catalog/manager/insert-usage';
1013
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
1014
			}
1015
1016
			$idx = 1;
1017
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
1018
1019
			foreach( $columns as $name => $entry ) {
1020
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
0 ignored issues
show
Bug introduced by
It seems like $entry->getInternalType() can also be of type string; however, parameter $type of Aimeos\Base\DB\Statement\Iface::bind() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

1020
				$stmt->bind( $idx++, $item->get( $name ), /** @scrutinizer ignore-type */ $entry->getInternalType() );
Loading history...
1021
			}
1022
1023
			$stmt->bind( $idx++, $item->getUrl() );
1024
			$stmt->bind( $idx++, json_encode( $item->getConfig() ) );
1025
			$stmt->bind( $idx++, $date ); // mtime
1026
			$stmt->bind( $idx++, $context->editor() );
1027
			$stmt->bind( $idx++, $item->getTarget() );
1028
1029
			if( $case !== true )
1030
			{
1031
				$stmt->bind( $idx++, $siteid );
1032
				$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
1033
			}
1034
			else
1035
			{
1036
				$stmt->bind( $idx++, $date ); // ctime
1037
				$stmt->bind( $idx++, $siteid );
1038
				$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
1039
			}
1040
1041
			$stmt->execute()->finish();
1042
1043
		return $this;
1044
	}
1045
}
1046