Passed
Push — master ( 6aab01...0294bc )
by Aimeos
09:44
created

Standard   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 1046
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 45
eloc 278
dl 0
loc 1046
rs 8.8
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 3 1
A clear() 0 60 4
A getSearchResults() 0 14 1
A get() 0 3 1
A getSearchConfig() 0 3 1
A insert() 0 107 2
A saveItem() 0 74 4
A createItemBase() 0 3 1
A getSearchAttributes() 0 22 1
A move() 0 5 1
A find() 0 4 1
A getTree() 0 28 6
A getPath() 0 4 1
A getTotal() 0 66 2
A filter() 0 12 1
A getResourceType() 0 5 1
A registerItemFilter() 0 3 1
A getSubManager() 0 115 1
A delete() 0 48 4
B search() 0 124 9

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 Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Manager\Site;
13
14
15
/**
16
 * Default implementation for managing sites.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Locale\Manager\Site\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	private $cache = [];
26
27
	private $searchConfig = array(
28
		'locale.site.id' => array(
29
			'code' => 'locale.site.id',
30
			'internalcode' => 'mlocsi."id"',
31
			'internaldeps' => ['LEFT JOIN "mshop_locale_site" AS mlocsi ON (mloc."siteid" = mlocsi."siteid")'],
32
			'label' => 'Site ID',
33
			'type' => 'string',
34
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
35
			'public' => false,
36
		),
37
		'locale.site.siteid' => array(
38
			'code' => 'locale.site.siteid',
39
			'internalcode' => 'mlocsi."siteid"',
40
			'label' => 'Unique site ID',
41
			'type' => 'string',
42
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
43
			'public' => false,
44
		),
45
		'locale.site.label' => array(
46
			'code' => 'locale.site.label',
47
			'internalcode' => 'mlocsi."label"',
48
			'label' => 'Site label',
49
			'type' => 'string',
50
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
51
		),
52
		'locale.site.code' => array(
53
			'code' => 'locale.site.code',
54
			'internalcode' => 'mlocsi."code"',
55
			'label' => 'Site code',
56
			'type' => 'string',
57
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
58
		),
59
		'locale.site.status' => array(
60
			'code' => 'locale.site.status',
61
			'internalcode' => 'mlocsi."status"',
62
			'label' => 'Site status',
63
			'type' => 'integer',
64
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
65
		),
66
		'locale.site.level' => array(
67
			'code' => 'locale.site.level',
68
			'internalcode' => 'mlocsi."level"',
69
			'label' => 'Site tree level',
70
			'type' => 'integer',
71
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
72
			'public' => false,
73
		),
74
		'locale.site.config' => array(
75
			'code' => 'locale.site.config',
76
			'internalcode' => 'mlocsi."config"',
77
			'label' => 'Site config',
78
			'type' => 'string',
79
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
80
			'public' => false,
81
		),
82
		'locale.site.icon' => array(
83
			'code' => 'locale.site.icon',
84
			'internalcode' => 'mlocsi."icon"',
85
			'label' => 'Site icon',
86
			'type' => 'string',
87
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
88
			'public' => false,
89
		),
90
		'locale.site.logo' => array(
91
			'code' => 'locale.site.logo',
92
			'internalcode' => 'mlocsi."logo"',
93
			'label' => 'Site logo',
94
			'type' => 'string',
95
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
96
			'public' => false,
97
		),
98
		'locale.site.refid' => array(
99
			'code' => 'locale.site.refid',
100
			'internalcode' => 'mlocsi."refid"',
101
			'label' => 'Site-related supplier ID',
102
			'type' => 'string',
103
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
104
			'public' => false,
105
		),
106
		'locale.site.theme' => array(
107
			'code' => 'locale.site.theme',
108
			'internalcode' => 'mlocsi."theme"',
109
			'label' => 'Site theme',
110
			'type' => 'string',
111
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
112
			'public' => false,
113
		),
114
		'locale.site.ctime' => array(
115
			'code' => 'locale.site.ctime',
116
			'internalcode' => 'mlocsi."ctime"',
117
			'label' => 'Site create date/time',
118
			'type' => 'datetime',
119
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
120
			'public' => false,
121
		),
122
		'locale.site.mtime' => array(
123
			'code' => 'locale.site.mtime',
124
			'internalcode' => 'mlocsi."mtime"',
125
			'label' => 'Site modify date/time',
126
			'type' => 'datetime',
127
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
128
			'public' => false,
129
		),
130
		'locale.site.editor' => array(
131
			'code' => 'locale.site.editor',
132
			'internalcode' => 'mlocsi."editor"',
133
			'label' => 'Site editor',
134
			'type' => 'string',
135
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
136
			'public' => false,
137
		),
138
		'parentid' => array(
139
			'code' => 'locale.site.parentid',
140
			'internalcode' => 'mlocsi."parentid"',
141
			'label' => 'Parent site ID',
142
			'type' => 'string',
143
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
144
			'public' => false,
145
		),
146
	);
147
148
149
	/**
150
	 * Initializes the object.
151
	 *
152
	 * @param \Aimeos\MShop\ContextIface $context Context object
153
	 */
154
	public function __construct( \Aimeos\MShop\ContextIface $context )
155
	{
156
		parent::__construct( $context );
157
		$this->setResourceName( 'db-locale' );
158
	}
159
160
161
	/**
162
	 * Removes old entries from the storage.
163
	 *
164
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
165
	 * @return \Aimeos\MShop\Locale\Manager\Site\Iface Manager object for chaining method calls
166
	 */
167
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
168
	{
169
		if( empty( $siteids ) ) {
170
			return $this;
171
		}
172
173
		$context = $this->context();
174
		$config = $context->config();
175
176
		/** mshop/locale/manager/site/cleanup/shop/domains
177
		 * List of madmin domains names whose items referring to the same site should be deleted as well
178
		 *
179
		 * As items for each domain can be stored in a separate database, the
180
		 * site manager needs a list of domain names used to connect to the
181
		 * correct database and to remove all items that belong the the deleted
182
		 * site.
183
		 *
184
		 * For each domain the cleanup will be done by the corresponding MShop
185
		 * manager. To keep records for old sites in the database even if the
186
		 * site was already deleted, you can configure a new list with the
187
		 * domains removed you would like to keep, e.g. the "order" domain to
188
		 * keep all orders ever placed.
189
		 *
190
		 * @param array List of domain names in lower case
191
		 * @since 2014.03
192
		 * @category Developer
193
		 * @see mshop/locale/manager/site/cleanup/admin/domains
194
		 */
195
		$path = 'mshop/locale/manager/site/cleanup/shop/domains';
196
197
		foreach( $config->get( $path, [] ) as $domain ) {
198
			\Aimeos\MShop::create( $context, $domain )->clear( $siteids );
199
		}
200
201
		/** mshop/locale/manager/site/cleanup/admin/domains
202
		 * List of mshop domains names whose items referring to the same site should be deleted as well
203
		 *
204
		 * As items for each domain can be stored in a separate database, the
205
		 * site manager needs a list of domain names used to connect to the
206
		 * correct database and to remove all items that belong the the deleted
207
		 * site.
208
		 *
209
		 * For each domain the cleanup will be done by the corresponding MAdmin
210
		 * manager. To keep records for old sites in the database even if the
211
		 * site was already deleted, you can configure a new list with the
212
		 * domains removed you would like to keep, e.g. the "log" domain to
213
		 * keep all log entries ever written.
214
		 *
215
		 * @param array List of domain names in lower case
216
		 * @since 2014.03
217
		 * @category Developer
218
		 * @see mshop/locale/manager/site/cleanup/shop/domains
219
		 */
220
		$path = 'mshop/locale/manager/site/cleanup/admin/domains';
221
222
		foreach( $config->get( $path, [] ) as $domain ) {
223
			\Aimeos\MAdmin::create( $context, $domain )->clear( $siteids );
224
		}
225
226
		return $this;
227
	}
228
229
230
	/**
231
	 * Creates a new empty item instance
232
	 *
233
	 * @param array $values Values the item should be initialized with
234
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New locale site item object
235
	 */
236
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
237
	{
238
		return $this->createItemBase( $values );
239
	}
240
241
242
	/**
243
	 * Adds a new site to the storage or updates an existing one.
244
	 *
245
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item New site item for saving to the storage
246
	 * @param bool $fetch True if the new ID should be returned in the item
247
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface $item Updated item including the generated ID
248
	 */
249
	public function saveItem( \Aimeos\MShop\Locale\Item\Site\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Locale\Item\Site\Iface
250
	{
251
		if( $item->getId() === null )
252
		{
253
			$msg = $this->context()->translate( 'mshop', 'Newly created item can not be saved using method "save()", use "insert()" instead' );
254
			throw new \Aimeos\MShop\Locale\Exception( $msg );
255
		}
256
257
		if( !$item->isModified() ) {
258
			return $item;
259
		}
260
261
		$context = $this->context();
262
		$conn = $context->db( $this->getResourceName() );
263
264
			$id = $item->getId();
265
			$columns = $this->object()->getSaveAttributes();
266
267
			/** mshop/locale/manager/site/update/mysql
268
			 * Updates an existing site record in the database
269
			 *
270
			 * @see mshop/locale/manager/site/update/ansi
271
			 */
272
273
			/** mshop/locale/manager/site/update/ansi
274
			 * Updates an existing site record in the database
275
			 *
276
			 * The SQL statement must be a string suitable for being used as
277
			 * prepared statement. It must include question marks for binding
278
			 * the values from the site item to the statement before they are
279
			 * sent to the database server. The order of the columns must
280
			 * correspond to the order in the save() method, so the
281
			 * correct values are bound to the columns.
282
			 *
283
			 * The SQL statement should conform to the ANSI standard to be
284
			 * compatible with most relational database systems. This also
285
			 * includes using double quotes for table and column names.
286
			 *
287
			 * @param string SQL statement for updating records
288
			 * @since 2014.03
289
			 * @category Developer
290
			 * @see mshop/locale/manager/site/insert/ansi
291
			 * @see mshop/locale/manager/site/delete/ansi
292
			 * @see mshop/locale/manager/site/search/ansi
293
			 * @see mshop/locale/manager/site/count/ansi
294
			 * @see mshop/locale/manager/site/newid/ansi
295
			 */
296
			$path = 'mshop/locale/manager/site/update';
297
			$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

297
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ), false );
Loading history...
298
299
			$idx = 1;
300
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
301
302
			foreach( $columns as $name => $entry ) {
303
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
304
			}
305
306
			$stmt->bind( $idx++, $item->getSiteId() );
307
			$stmt->bind( $idx++, $item->getCode() );
308
			$stmt->bind( $idx++, $item->getLabel() );
309
			$stmt->bind( $idx++, json_encode( $item->getConfig(), JSON_FORCE_OBJECT ) );
310
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
311
			$stmt->bind( $idx++, $item->getIcon() );
312
			$stmt->bind( $idx++, json_encode( $item->getLogos(), JSON_FORCE_OBJECT ) );
313
			$stmt->bind( $idx++, $item->getRefId() );
314
			$stmt->bind( $idx++, $item->getTheme() );
315
			$stmt->bind( $idx++, $context->editor() );
316
			$stmt->bind( $idx++, date( 'Y-m-d H:i:s' ) ); // mtime
317
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
318
319
			$stmt->execute()->finish();
320
			$item->setId( $id ); // set Modified false
321
322
		return $item;
323
	}
324
325
326
	/**
327
	 * Removes multiple items.
328
	 *
329
	 * @param \Aimeos\MShop\Common\Item\Iface|array|string $items List of item objects or IDs of the items
330
	 * @return \Aimeos\MShop\Locale\Manager\Site\Iface Manager object for chaining method calls
331
	 */
332
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
333
	{
334
		if( is_map( $items ) ) { $items = $items->toArray(); }
335
		if( !is_array( $items ) ) { $items = [$items]; }
336
		if( empty( $items ) ) { return $this; }
337
338
339
		$filter = $this->object()->filter()
340
			->add( ['locale.site.id' => $items] )
341
			->slice( 0, count( $items ) );
342
343
		$siteIds = $this->object()->search( $filter )->getSiteId()->toArray();
344
		$this->object()->clear( $siteIds );
345
346
347
		/** mshop/locale/manager/site/delete/mysql
348
		 * Deletes the items matched by the given IDs from the database
349
		 *
350
		 * @see mshop/locale/manager/site/delete/ansi
351
		 */
352
353
		/** mshop/locale/manager/site/delete/ansi
354
		 * Deletes the items matched by the given IDs from the database
355
		 *
356
		 * Removes the site records specified by the given IDs from the
357
		 * locale database. The records must be from the site that is configured
358
		 * via the context item.
359
		 *
360
		 * The ":cond" placeholder is replaced by the name of the ID column and
361
		 * the given ID or list of IDs while the site ID is bound to the question
362
		 * mark.
363
		 *
364
		 * The SQL statement should conform to the ANSI standard to be
365
		 * compatible with most relational database systems. This also
366
		 * includes using double quotes for table and column names.
367
		 *
368
		 * @param string SQL statement for deleting items
369
		 * @since 2014.03
370
		 * @category Developer
371
		 * @see mshop/locale/manager/site/insert/ansi
372
		 * @see mshop/locale/manager/site/update/ansi
373
		 * @see mshop/locale/manager/site/search/ansi
374
		 * @see mshop/locale/manager/site/count/ansi
375
		 * @see mshop/locale/manager/site/newid/ansi
376
		 */
377
		$path = 'mshop/locale/manager/site/delete';
378
379
		return $this->deleteItemsBase( $items, $path, false );
380
	}
381
382
383
	/**
384
	 * Returns the item specified by its code and domain/type if necessary
385
	 *
386
	 * @param string $code Code of the item
387
	 * @param string[] $ref List of domains to fetch list items and referenced items for
388
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
389
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
390
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
391
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
392
	 */
393
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
394
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
395
	{
396
		return $this->findBase( array( 'locale.site.code' => $code ), $ref, $default );
397
	}
398
399
400
	/**
401
	 * Returns the site item specified by its ID.
402
	 *
403
	 * @param string $id Unique ID of the site data in the storage
404
	 * @param string[] $ref List of domains to fetch list items and referenced items for
405
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
406
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Returns the site item of the given id
407
	 * @throws \Aimeos\MShop\Exception If the item couldn't be found
408
	 */
409
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
410
	{
411
		return $this->getItemBase( 'locale.site.id', $id, $ref, $default );
412
	}
413
414
415
	/**
416
	 * Returns the available manager types
417
	 *
418
	 * @param bool $withsub Return also the resource type of sub-managers if true
419
	 * @return string Type of the manager and submanagers, subtypes are separated by slashes
420
	 */
421
	public function getResourceType( bool $withsub = true ) : array
422
	{
423
		$path = 'mshop/locale/manager/site/submanagers';
424
425
		return $this->getResourceTypeBase( 'locale/site', $path, [], $withsub );
426
	}
427
428
429
	/**
430
	 * Returns the attributes that can be used for searching.
431
	 *
432
	 * @param bool $withsub Return also attributes of sub-managers if true
433
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
434
	 */
435
	public function getSearchAttributes( bool $withsub = true ) : array
436
	{
437
		/** mshop/locale/manager/site/submanagers
438
		 * List of manager names that can be instantiated by the locale site manager
439
		 *
440
		 * Managers provide a generic interface to the underlying storage.
441
		 * Each manager has or can have sub-managers caring about particular
442
		 * aspects. Each of these sub-managers can be instantiated by its
443
		 * parent manager using the getSubManager() method.
444
		 *
445
		 * The search keys from sub-managers can be normally used in the
446
		 * manager as well. It allows you to search for items of the manager
447
		 * using the search keys of the sub-managers to further limit the
448
		 * retrieved list of items.
449
		 *
450
		 * @param array List of sub-manager names
451
		 * @since 2014.03
452
		 * @category Developer
453
		 */
454
		$path = 'mshop/locale/manager/site/submanagers';
455
456
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
457
	}
458
459
460
	/**
461
	 * Returns a new sub manager of the given type and name.
462
	 *
463
	 * @param string $manager Name of the sub manager type in lower case
464
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
465
	 * @return \Aimeos\MShop\Common\Manager\Iface Sub manager
466
	 */
467
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
468
	{
469
		/** mshop/locale/manager/site/name
470
		 * Class name of the used locale site manager implementation
471
		 *
472
		 * Each default locale site manager can be replaced by an alternative imlementation.
473
		 * To use this implementation, you have to set the last part of the class
474
		 * name as configuration value so the manager factory knows which class it
475
		 * has to instantiate.
476
		 *
477
		 * For example, if the name of the default class is
478
		 *
479
		 *  \Aimeos\MShop\Locale\Manager\Site\Standard
480
		 *
481
		 * and you want to replace it with your own version named
482
		 *
483
		 *  \Aimeos\MShop\Locale\Manager\Site\Mysite
484
		 *
485
		 * then you have to set the this configuration option:
486
		 *
487
		 *  mshop/locale/manager/site/name = Mysite
488
		 *
489
		 * The value is the last part of your own class name and it's case sensitive,
490
		 * so take care that the configuration value is exactly named like the last
491
		 * part of the class name.
492
		 *
493
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
494
		 * characters are possible! You should always start the last part of the class
495
		 * name with an upper case character and continue only with lower case characters
496
		 * or numbers. Avoid chamel case names like "MySite"!
497
		 *
498
		 * @param string Last part of the class name
499
		 * @since 2014.03
500
		 * @category Developer
501
		 */
502
503
		/** mshop/locale/manager/site/decorators/excludes
504
		 * Excludes decorators added by the "common" option from the locale site manager
505
		 *
506
		 * Decorators extend the functionality of a class by adding new aspects
507
		 * (e.g. log what is currently done), executing the methods of the underlying
508
		 * class only in certain conditions (e.g. only for logged in users) or
509
		 * modify what is returned to the caller.
510
		 *
511
		 * This option allows you to remove a decorator added via
512
		 * "mshop/common/manager/decorators/default" before they are wrapped
513
		 * around the locale site manager.
514
		 *
515
		 *  mshop/locale/manager/site/decorators/excludes = array( 'decorator1' )
516
		 *
517
		 * This would remove the decorator named "decorator1" from the list of
518
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
519
		 * "mshop/common/manager/decorators/default" for the locale site manager.
520
		 *
521
		 * @param array List of decorator names
522
		 * @since 2014.03
523
		 * @category Developer
524
		 * @see mshop/common/manager/decorators/default
525
		 * @see mshop/locale/manager/site/decorators/global
526
		 * @see mshop/locale/manager/site/decorators/local
527
		 */
528
529
		/** mshop/locale/manager/site/decorators/global
530
		 * Adds a list of globally available decorators only to the locale site manager
531
		 *
532
		 * Decorators extend the functionality of a class by adding new aspects
533
		 * (e.g. log what is currently done), executing the methods of the underlying
534
		 * class only in certain conditions (e.g. only for logged in users) or
535
		 * modify what is returned to the caller.
536
		 *
537
		 * This option allows you to wrap global decorators
538
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale site
539
		 * manager.
540
		 *
541
		 *  mshop/locale/manager/site/decorators/global = array( 'decorator1' )
542
		 *
543
		 * This would add the decorator named "decorator1" defined by
544
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
545
		 * site manager.
546
		 *
547
		 * @param array List of decorator names
548
		 * @since 2014.03
549
		 * @category Developer
550
		 * @see mshop/common/manager/decorators/default
551
		 * @see mshop/locale/manager/site/decorators/excludes
552
		 * @see mshop/locale/manager/site/decorators/local
553
		 */
554
555
		/** mshop/locale/manager/site/decorators/local
556
		 * Adds a list of local decorators only to the locale site manager
557
		 *
558
		 * Decorators extend the functionality of a class by adding new aspects
559
		 * (e.g. log what is currently done), executing the methods of the underlying
560
		 * class only in certain conditions (e.g. only for logged in users) or
561
		 * modify what is returned to the caller.
562
		 *
563
		 * This option allows you to wrap local decorators
564
		 * ("\Aimeos\MShop\Locale\Manager\Site\Decorator\*") around the locale site
565
		 * manager.
566
		 *
567
		 *  mshop/locale/manager/site/decorators/local = array( 'decorator2' )
568
		 *
569
		 * This would add the decorator named "decorator2" defined by
570
		 * "\Aimeos\MShop\Locale\Manager\Site\Decorator\Decorator2" only to the
571
		 * locale site manager.
572
		 *
573
		 * @param array List of decorator names
574
		 * @since 2014.03
575
		 * @category Developer
576
		 * @see mshop/common/manager/decorators/default
577
		 * @see mshop/locale/manager/site/decorators/excludes
578
		 * @see mshop/locale/manager/site/decorators/global
579
		 */
580
581
		return $this->getSubManagerBase( 'locale', 'site/' . $manager, $name );
582
	}
583
584
585
	/**
586
	 * Searches for site items matching the given criteria.
587
	 *
588
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
589
	 * @param string[] $ref List of domains to fetch list items and referenced items for
590
	 * @param int|null &$total Number of items that are available in total
591
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Locale\Item\Site\Iface with ids as keys
592
	 */
593
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
594
	{
595
		$items = [];
596
		$context = $this->context();
597
		$conn = $context->db( $this->getResourceName() );
598
599
			$attributes = $this->object()->getSearchAttributes();
600
			$translations = $this->getSearchTranslations( $attributes );
601
			$types = $this->getSearchTypes( $attributes );
602
			$columns = $this->object()->getSaveAttributes();
603
			$sortcols = $search->translate( $search->getSortations(), $translations );
604
605
			$colstring = '';
606
			foreach( $columns as $name => $entry ) {
607
				$colstring .= $entry->getInternalCode() . ', ';
608
			}
609
610
			$find = array( ':columns', ':cond', ':order', ':start', ':size' );
611
			$replace = array(
612
				$colstring . ( $sortcols ? join( ', ', $sortcols ) . ', ' : '' ),
613
				$search->getConditionSource( $types, $translations ),
614
				$search->getSortationSource( $types, $translations ),
615
				$search->getOffset(),
616
				$search->getLimit(),
617
			);
618
619
			/** mshop/locale/manager/site/search/mysql
620
			 * Retrieves the records matched by the given criteria in the database
621
			 *
622
			 * @see mshop/locale/manager/site/search/ansi
623
			 */
624
625
			/** mshop/locale/manager/site/search/ansi
626
			 * Retrieves the records matched by the given criteria in the database
627
			 *
628
			 * Fetches the records matched by the given criteria from the attribute
629
			 * database. The records must be from one of the sites that are
630
			 * configured via the context item. If the current site is part of
631
			 * a tree of sites, the SELECT statement can retrieve all records
632
			 * from the current site and the complete sub-tree of sites.
633
			 *
634
			 * As the records can normally be limited by criteria from sub-managers,
635
			 * their tables must be joined in the SQL context. This is done by
636
			 * using the "internaldeps" property from the definition of the ID
637
			 * column of the sub-managers. These internal dependencies specify
638
			 * the JOIN between the tables and the used columns for joining. The
639
			 * ":joins" placeholder is then replaced by the JOIN strings from
640
			 * the sub-managers.
641
			 *
642
			 * To limit the records matched, conditions can be added to the given
643
			 * criteria object. It can contain comparisons like column names that
644
			 * must match specific values which can be combined by AND, OR or NOT
645
			 * operators. The resulting string of SQL conditions replaces the
646
			 * ":cond" placeholder before the statement is sent to the database
647
			 * server.
648
			 *
649
			 * If the records that are retrieved should be ordered by one or more
650
			 * columns, the generated string of column / sort direction pairs
651
			 * replaces the ":order" placeholder. In case no ordering is required,
652
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
653
			 * markers is removed to speed up retrieving the records. Columns of
654
			 * sub-managers can also be used for ordering the result set but then
655
			 * no index can be used.
656
			 *
657
			 * The number of returned records can be limited and can start at any
658
			 * number between the begining and the end of the result set. For that
659
			 * the ":size" and ":start" placeholders are replaced by the
660
			 * corresponding values from the criteria object. The default values
661
			 * are 0 for the start and 100 for the size value.
662
			 *
663
			 * The SQL statement should conform to the ANSI standard to be
664
			 * compatible with most relational database systems. This also
665
			 * includes using double quotes for table and column names.
666
			 *
667
			 * @param string SQL statement for searching items
668
			 * @since 2014.03
669
			 * @category Developer
670
			 * @see mshop/locale/manager/site/insert/ansi
671
			 * @see mshop/locale/manager/site/update/ansi
672
			 * @see mshop/locale/manager/site/delete/ansi
673
			 * @see mshop/locale/manager/site/count/ansi
674
			 * @see mshop/locale/manager/site/newid/ansi
675
			 */
676
			$path = 'mshop/locale/manager/site/search';
677
678
			$sql = $this->getSqlConfig( $path );
679
			$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
680
681
			try
682
			{
683
				while( ( $row = $results->fetch() ) !== null )
684
				{
685
					$id = $row['locale.site.id'];
686
					$logos = $row['locale.site.logo'] ?? '{}';
687
					$config = $row['locale.site.config'] ?? '{}';
688
689
					if( ( $row['locale.site.logo'] = json_decode( $logos, true ) ) === null )
690
					{
691
						$msg = sprintf( 'Invalid JSON as result of search for ID "%2$s" in "%1$s": %3$s', 'mshop_locale_site.logo', $id, $logos );
692
						$this->context()->logger()->warning( $msg, 'core/locale/site' );
693
					}
694
695
					if( ( $row['locale.site.config'] = json_decode( $config, true ) ) === null )
696
					{
697
						$msg = sprintf( 'Invalid JSON as result of search for ID "%2$s" in "%1$s": %3$s', 'mshop_locale_site.config', $id, $config );
698
						$this->context()->logger()->warning( $msg, 'core/locale/site' );
699
					}
700
701
					if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
702
						$items[$row['locale.site.id']] = $item;
703
					}
704
				}
705
			}
706
			catch( \Exception $e )
707
			{
708
				$results->finish();
709
				throw $e;
710
			}
711
712
			if( $total !== null ) {
713
				$total = $this->getTotal( $conn, $find, $replace );
714
			}
715
716
		return map( $items );
717
	}
718
719
720
	/**
721
	 * Creates a filter object.
722
	 *
723
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
724
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
725
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
726
	 */
727
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
728
	{
729
		$search = $this->filterBase( 'locale.site', $default );
730
731
		$expr = array(
732
			$search->compare( '==', 'locale.site.level', 0 ),
733
			$search->getConditions(),
734
		);
735
736
		$search->setConditions( $search->and( $expr ) );
737
738
		return $search;
739
	}
740
741
742
	/**
743
	 * Returns a list of item IDs, that are in the path of given item ID.
744
	 *
745
	 * @param string $id ID of item to get the path for
746
	 * @param string[] $ref List of domains to fetch list items and referenced items for
747
	 * @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Locale\Item\Site\Iface
748
	 */
749
	public function getPath( string $id, array $ref = [] ) : \Aimeos\Map
750
	{
751
		$item = $this->getTree( $id, $ref, \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
752
		return map( [$item->getId() => $item] );
753
	}
754
755
756
	/**
757
	 * Returns a node and its descendants depending on the given resource.
758
	 *
759
	 * @param string|null $id Retrieve nodes starting from the given ID
760
	 * @param string[] $ref List of domains (e.g. text, media, etc.) whose referenced items should be attached to the objects
761
	 * @param int $level One of the level constants from \Aimeos\MW\Tree\Manager\Base
762
	 * @param \Aimeos\Base\Criteria\Iface|null $criteria Optional criteria object with conditions
763
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site node, maybe with subnodes
764
	 */
765
	public function getTree( string $id = null, array $ref = [], int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE,
766
		\Aimeos\Base\Criteria\Iface $criteria = null ) : \Aimeos\MShop\Locale\Item\Site\Iface
767
	{
768
		if( $id !== null )
769
		{
770
			if( count( $ref ) > 0 ) {
771
				return $this->object()->get( $id, $ref );
772
			}
773
774
			if( !isset( $this->cache[$id] ) ) {
775
				$this->cache[$id] = $this->object()->get( $id, $ref );
776
			}
777
778
			return $this->cache[$id];
779
		}
780
781
		$criteria = $criteria ? clone $criteria : $this->object()->filter();
782
		$criteria->add( ['locale.site.code' => 'default'] )->slice( 0, 1 );
783
784
		if( ( $item = $this->object()->search( $criteria, $ref )->first() ) === null )
785
		{
786
			$msg = $this->context()->translate( 'mshop', 'Tree root with code "%1$s" in "%2$s" not found' );
787
			throw new \Aimeos\MShop\Locale\Exception( sprintf( $msg, 'default', 'locale.site.code' ) );
788
		}
789
790
		$this->cache[$item->getId()] = $item;
791
792
		return $item;
793
	}
794
795
796
	/**
797
	 * Adds a new item object.
798
	 *
799
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item Item which should be inserted
800
	 * @param string|null $parentId ID of the parent item where the item should be inserted into
801
	 * @param string|null $refId ID of the item where the item should be inserted before (null to append)
802
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface $item Updated item including the generated ID
803
	 */
804
	public function insert( \Aimeos\MShop\Locale\Item\Site\Iface $item, string $parentId = null, string $refId = null ) : \Aimeos\MShop\Locale\Item\Site\Iface
805
	{
806
		$context = $this->context();
807
		$conn = $context->db( $this->getResourceName() );
808
809
			$date = date( 'Y-m-d H:i:s' );
810
			$columns = $this->object()->getSaveAttributes();
811
812
			/** mshop/locale/manager/site/insert/mysql
813
			 * Inserts a new currency record into the database table
814
			 *
815
			 * @see mshop/locale/manager/site/insert/ansi
816
			 */
817
818
			/** mshop/locale/manager/site/insert/ansi
819
			 * Inserts a new currency record into the database table
820
			 *
821
			 * The SQL statement must be a string suitable for being used as
822
			 * prepared statement. It must include question marks for binding
823
			 * the values from the site item to the statement before they are
824
			 * sent to the database server. The number of question marks must
825
			 * be the same as the number of columns listed in the INSERT
826
			 * statement. The order of the columns must correspond to the
827
			 * order in the save() method, so the correct values are
828
			 * bound to the columns.
829
			 *
830
			 * The SQL statement should conform to the ANSI standard to be
831
			 * compatible with most relational database systems. This also
832
			 * includes using double quotes for table and column names.
833
			 *
834
			 * @param string SQL statement for inserting records
835
			 * @since 2014.03
836
			 * @category Developer
837
			 * @see mshop/locale/manager/site/update/ansi
838
			 * @see mshop/locale/manager/site/delete/ansi
839
			 * @see mshop/locale/manager/site/search/ansi
840
			 * @see mshop/locale/manager/site/count/ansi
841
			 * @see mshop/locale/manager/site/newid/ansi
842
			 */
843
			$path = 'mshop/locale/manager/site/insert';
844
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
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

844
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
845
846
			$idx = 1;
847
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
848
849
			foreach( $columns as $name => $entry ) {
850
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
851
			}
852
853
			$stmt->bind( $idx++, '' ); // site ID
854
			$stmt->bind( $idx++, $item->getCode() );
855
			$stmt->bind( $idx++, $item->getLabel() );
856
			$stmt->bind( $idx++, json_encode( $item->getConfig(), JSON_FORCE_OBJECT ) );
857
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
858
			$stmt->bind( $idx++, $item->getIcon() );
859
			$stmt->bind( $idx++, json_encode( $item->getLogos(), JSON_FORCE_OBJECT ) );
860
			$stmt->bind( $idx++, $item->getRefId() );
861
			$stmt->bind( $idx++, $item->getTheme() );
862
			$stmt->bind( $idx++, $context->editor() );
863
			$stmt->bind( $idx++, $date ); // mtime
864
			$stmt->bind( $idx++, $date ); // ctime
865
866
			$stmt->execute()->finish();
867
868
			/** mshop/locale/manager/site/newid/mysql
869
			 * Retrieves the ID generated by the database when inserting a new record
870
			 *
871
			 * @see mshop/locale/manager/site/newid/ansi
872
			 */
873
874
			/** mshop/locale/manager/site/newid/ansi
875
			 * Retrieves the ID generated by the database when inserting a new record
876
			 *
877
			 * As soon as a new record is inserted into the database table,
878
			 * the database server generates a new and unique identifier for
879
			 * that record. This ID can be used for retrieving, updating and
880
			 * deleting that specific record from the table again.
881
			 *
882
			 * For MySQL:
883
			 *  SELECT LAST_INSERT_ID()
884
			 * For PostgreSQL:
885
			 *  SELECT currval('seq_matt_id')
886
			 * For SQL Server:
887
			 *  SELECT SCOPE_IDENTITY()
888
			 * For Oracle:
889
			 *  SELECT "seq_matt_id".CURRVAL FROM DUAL
890
			 *
891
			 * There's no way to retrive the new ID by a SQL statements that
892
			 * fits for most database servers as they implement their own
893
			 * specific way.
894
			 *
895
			 * @param string SQL statement for retrieving the last inserted record ID
896
			 * @since 2014.03
897
			 * @category Developer
898
			 * @see mshop/locale/manager/site/insert/ansi
899
			 * @see mshop/locale/manager/site/update/ansi
900
			 * @see mshop/locale/manager/site/delete/ansi
901
			 * @see mshop/locale/manager/site/search/ansi
902
			 * @see mshop/locale/manager/site/count/ansi
903
			 */
904
			$path = 'mshop/locale/manager/newid';
905
			$item->setId( $this->newId( $conn, $this->getSqlConfig( $path ) ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig($path) can also be of type array; however, parameter $cfgpath of Aimeos\MShop\Common\Manager\Base::newId() 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

905
			$item->setId( $this->newId( $conn, /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) ) );
Loading history...
906
907
			// Add unique site identifier
908
			$item = $this->object()->save( $item->setSiteId( $item->getId() . '.' ) );
909
910
		return $item;
911
	}
912
913
914
	/**
915
	 * Moves an existing item to the new parent in the storage.
916
	 *
917
	 * @param string $id ID of the item that should be moved
918
	 * @param string|null $oldParentId ID of the old parent item which currently contains the item that should be removed
919
	 * @param string|null $newParentId ID of the new parent item where the item should be moved to
920
	 * @param string|null $refId ID of the item where the item should be inserted before (null to append)
921
	 * @return \Aimeos\MShop\Locale\Manager\Site\Iface Manager object for chaining method calls
922
	 */
923
	public function move( string $id, string $oldParentId = null, string $newParentId = null,
924
		string $refId = null ) : \Aimeos\MShop\Locale\Manager\Site\Iface
925
	{
926
		$msg = $this->context()->translate( 'mshop', 'Method "%1$s" for locale site manager not available' );
927
		throw new \Aimeos\MShop\Locale\Exception( sprintf( $msg, 'move()' ) );
928
	}
929
930
931
	/**
932
	 * Registers a new item filter for the given name
933
	 *
934
	 * Not used for site items but required for trees
935
	 *
936
	 * @param string $name Filter name
937
	 * @param \Closure $fcn Callback function
938
	 */
939
	public function registerItemFilter( string $name, \Closure $fcn ) : \Aimeos\MShop\Locale\Manager\Site\Iface
940
	{
941
		return $this;
942
	}
943
944
945
	/**
946
	 * Returns the search results for the given SQL statement.
947
	 *
948
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
949
	 * @param string $sql SQL statement
950
	 * @return \Aimeos\Base\DB\Result\Iface Search result object
951
	 */
952
	protected function getSearchResults( \Aimeos\Base\DB\Connection\Iface $conn, string $sql ) : \Aimeos\Base\DB\Result\Iface
953
	{
954
		$time = microtime( true );
955
956
		$stmt = $conn->create( $sql );
957
		$result = $stmt->execute();
958
959
		$msg = 'Time: ' . ( microtime( true ) - $time ) * 1000 . "ms\n"
960
			. 'Class: ' . get_class( $this ) . "\n"
961
			. str_replace( ["\t", "\n\n"], ['', "\n"], trim( (string) $stmt ) );
962
963
		$this->context()->logger()->debug( $msg, 'core/sql' );
964
965
		return $result;
966
	}
967
968
969
	/**
970
	 * Create new item object initialized with given parameters.
971
	 *
972
	 * @param array $values Associative list of item key/value pairs
973
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item object
974
	 */
975
	protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Locale\Item\Site\Iface
976
	{
977
		return new \Aimeos\MShop\Locale\Item\Site\Standard( $values );
978
	}
979
980
981
	/**
982
	 * Returns the raw search config array.
983
	 *
984
	 * @return array List of search config arrays
985
	 */
986
	protected function getSearchConfig() : array
987
	{
988
		return $this->searchConfig;
989
	}
990
991
992
	/**
993
	 * Returns the total number of items found for the conditions
994
	 *
995
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
996
	 * @param string[] $find List of markers that should be replaced in the SQL statement
997
	 * @param string[] $replace List of replacements for the markers in the SQL statement
998
	 * @throws \Aimeos\MShop\Locale\Exception If no total value was found
999
	 * @return int Total number of found items
1000
	 */
1001
	protected function getTotal( \Aimeos\Base\DB\Connection\Iface $conn, array $find, array $replace ) : int
1002
	{
1003
		/** mshop/locale/manager/site/count/mysql
1004
		 * Counts the number of records matched by the given criteria in the database
1005
		 *
1006
		 * @see mshop/locale/manager/site/count/ansi
1007
		 */
1008
1009
		/** mshop/locale/manager/site/count/ansi
1010
		 * Counts the number of records matched by the given criteria in the database
1011
		 *
1012
		 * Counts all records matched by the given criteria from the attribute
1013
		 * database. The records must be from one of the sites that are
1014
		 * configured via the context item. If the current site is part of
1015
		 * a tree of sites, the statement can count all records from the
1016
		 * current site and the complete sub-tree of sites.
1017
		 *
1018
		 * As the records can normally be limited by criteria from sub-managers,
1019
		 * their tables must be joined in the SQL context. This is done by
1020
		 * using the "internaldeps" property from the definition of the ID
1021
		 * column of the sub-managers. These internal dependencies specify
1022
		 * the JOIN between the tables and the used columns for joining. The
1023
		 * ":joins" placeholder is then replaced by the JOIN strings from
1024
		 * the sub-managers.
1025
		 *
1026
		 * To limit the records matched, conditions can be added to the given
1027
		 * criteria object. It can contain comparisons like column names that
1028
		 * must match specific values which can be combined by AND, OR or NOT
1029
		 * operators. The resulting string of SQL conditions replaces the
1030
		 * ":cond" placeholder before the statement is sent to the database
1031
		 * server.
1032
		 *
1033
		 * Both, the strings for ":joins" and for ":cond" are the same as for
1034
		 * the "search" SQL statement.
1035
		 *
1036
		 * Contrary to the "search" statement, it doesn't return any records
1037
		 * but instead the number of records that have been found. As counting
1038
		 * thousands of records can be a long running task, the maximum number
1039
		 * of counted records is limited for performance reasons.
1040
		 *
1041
		 * The SQL statement should conform to the ANSI standard to be
1042
		 * compatible with most relational database systems. This also
1043
		 * includes using double quotes for table and column names.
1044
		 *
1045
		 * @param string SQL statement for counting items
1046
		 * @since 2014.03
1047
		 * @category Developer
1048
		 * @see mshop/locale/manager/site/insert/ansi
1049
		 * @see mshop/locale/manager/site/update/ansi
1050
		 * @see mshop/locale/manager/site/delete/ansi
1051
		 * @see mshop/locale/manager/site/search/ansi
1052
		 * @see mshop/locale/manager/site/newid/ansi
1053
		 */
1054
		$path = 'mshop/locale/manager/site/count';
1055
1056
		$sql = $this->getSqlConfig( $path );
1057
		$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
1058
1059
		$row = $results->fetch();
1060
		$results->finish();
1061
1062
		if( $row === null ) {
1063
			throw new \Aimeos\MShop\Locale\Exception( 'No total results value found' );
1064
		}
1065
1066
		return $row['count'];
1067
	}
1068
}
1069