Standard::getSearchAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 23
rs 10
c 0
b 0
f 0
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-2024
7
 * @package MShop
8
 * @subpackage Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Manager;
13
14
15
/**
16
 * Default locale manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Locale\Manager\Base
23
	implements \Aimeos\MShop\Locale\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	/** mshop/locale/manager/name
26
	 * Class name of the used locale manager implementation
27
	 *
28
	 * Each default manager can be replace by an alternative imlementation.
29
	 * To use this implementation, you have to set the last part of the class
30
	 * name as configuration value so the manager factory knows which class it
31
	 * has to instantiate.
32
	 *
33
	 * For example, if the name of the default class is
34
	 *
35
	 *  \Aimeos\MShop\Locale\Manager\Standard
36
	 *
37
	 * and you want to replace it with your own version named
38
	 *
39
	 *  \Aimeos\MShop\Locale\Manager\Mymanager
40
	 *
41
	 * then you have to set the this configuration option:
42
	 *
43
	 *  mshop/locale/manager/name = Mymanager
44
	 *
45
	 * The value is the last part of your own class name and it's case sensitive,
46
	 * so take care that the configuration value is exactly named like the last
47
	 * part of the class name.
48
	 *
49
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
50
	 * characters are possible! You should always start the last part of the class
51
	 * name with an upper case character and continue only with lower case characters
52
	 * or numbers. Avoid chamel case names like "MyManager"!
53
	 *
54
	 * @param string Last part of the class name
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
59
	/** mshop/locale/manager/decorators/excludes
60
	 * Excludes decorators added by the "common" option from the locale manager
61
	 *
62
	 * Decorators extend the functionality of a class by adding new aspects
63
	 * (e.g. log what is currently done), executing the methods of the underlying
64
	 * class only in certain conditions (e.g. only for logged in users) or
65
	 * modify what is returned to the caller.
66
	 *
67
	 * This option allows you to remove a decorator added via
68
	 * "mshop/common/manager/decorators/default" before they are wrapped
69
	 * around the locale manager.
70
	 *
71
	 *  mshop/locale/manager/decorators/excludes = array( 'decorator1' )
72
	 *
73
	 * This would remove the decorator named "decorator1" from the list of
74
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
75
	 * "mshop/common/manager/decorators/default" for the locale manager.
76
	 *
77
	 * @param array List of decorator names
78
	 * @since 2014.03
79
	 * @category Developer
80
	 * @see mshop/common/manager/decorators/default
81
	 * @see mshop/locale/manager/decorators/global
82
	 * @see mshop/locale/manager/decorators/local
83
	 */
84
85
	/** mshop/locale/manager/decorators/global
86
	 * Adds a list of globally available decorators only to the locale manager
87
	 *
88
	 * Decorators extend the functionality of a class by adding new aspects
89
	 * (e.g. log what is currently done), executing the methods of the underlying
90
	 * class only in certain conditions (e.g. only for logged in users) or
91
	 * modify what is returned to the caller.
92
	 *
93
	 * This option allows you to wrap global decorators
94
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale manager.
95
	 *
96
	 *  mshop/locale/manager/decorators/global = array( 'decorator1' )
97
	 *
98
	 * This would add the decorator named "decorator1" defined by
99
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
100
	 * manager.
101
	 *
102
	 * @param array List of decorator names
103
	 * @since 2014.03
104
	 * @category Developer
105
	 * @see mshop/common/manager/decorators/default
106
	 * @see mshop/locale/manager/decorators/excludes
107
	 * @see mshop/locale/manager/decorators/local
108
	 */
109
110
	/** mshop/locale/manager/decorators/local
111
	 * Adds a list of local decorators only to the locale manager
112
	 *
113
	 * Decorators extend the functionality of a class by adding new aspects
114
	 * (e.g. log what is currently done), executing the methods of the underlying
115
	 * class only in certain conditions (e.g. only for logged in users) or
116
	 * modify what is returned to the caller.
117
	 *
118
	 * This option allows you to wrap local decorators
119
	 * ("\Aimeos\MShop\Locale\Manager\Decorator\*") around the locale manager.
120
	 *
121
	 *  mshop/locale/manager/decorators/local = array( 'decorator2' )
122
	 *
123
	 * This would add the decorator named "decorator2" defined by
124
	 * "\Aimeos\MShop\Locale\Manager\Decorator\Decorator2" only to the locale
125
	 * manager.
126
	 *
127
	 * @param array List of decorator names
128
	 * @since 2014.03
129
	 * @category Developer
130
	 * @see mshop/common/manager/decorators/default
131
	 * @see mshop/locale/manager/decorators/excludes
132
	 * @see mshop/locale/manager/decorators/global
133
	 */
134
135
136
	private array $searchConfig = array(
137
		'locale.id' => array(
138
			'code' => 'locale.id',
139
			'internalcode' => 'mloc."id"',
140
			'label' => 'ID',
141
			'type' => 'int',
142
			'public' => false,
143
		),
144
		'locale.siteid' => array(
145
			'code' => 'locale.siteid',
146
			'internalcode' => 'mloc."siteid"',
147
			'label' => 'Site ID',
148
			'public' => false,
149
		),
150
		'locale.languageid' => array(
151
			'code' => 'locale.languageid',
152
			'internalcode' => 'mloc."langid"',
153
			'label' => 'Language ID',
154
		),
155
		'locale.currencyid' => array(
156
			'code' => 'locale.currencyid',
157
			'internalcode' => 'mloc."currencyid"',
158
			'label' => 'Currency ID',
159
		),
160
		'locale.status' => array(
161
			'code' => 'locale.status',
162
			'internalcode' => 'mloc."status"',
163
			'label' => 'Status',
164
			'type' => 'int',
165
		),
166
		'locale.position' => array(
167
			'code' => 'locale.position',
168
			'internalcode' => 'mloc."pos"',
169
			'label' => 'Position',
170
			'type' => 'int',
171
		),
172
		'locale.ctime' => array(
173
			'code' => 'locale.ctime',
174
			'internalcode' => 'mloc."ctime"',
175
			'label' => 'Create date/time',
176
			'type' => 'datetime',
177
			'public' => false,
178
		),
179
		'locale.mtime' => array(
180
			'code' => 'locale.mtime',
181
			'internalcode' => 'mloc."mtime"',
182
			'label' => 'Modify date/time',
183
			'type' => 'datetime',
184
			'public' => false,
185
		),
186
		'locale.editor' => array(
187
			'code' => 'locale.editor',
188
			'internalcode' => 'mloc."editor"',
189
			'label' => 'Editor',
190
			'public' => false,
191
		),
192
	);
193
194
195
	/**
196
	 * Initializes the object.
197
	 *
198
	 * @param \Aimeos\MShop\ContextIface $context Context object
199
	 */
200
	public function __construct( \Aimeos\MShop\ContextIface $context )
201
	{
202
		parent::__construct( $context );
203
204
		/** mshop/locale/manager/resource
205
		 * Name of the database connection resource to use
206
		 *
207
		 * You can configure a different database connection for each data domain
208
		 * and if no such connection name exists, the "db" connection will be used.
209
		 * It's also possible to use the same database connection for different
210
		 * data domains by configuring the same connection name using this setting.
211
		 *
212
		 * @param string Database connection name
213
		 * @since 2023.04
214
		 */
215
		$this->setResourceName( $context->config()->get( 'mshop/locale/manager/resource', 'db-locale' ) );
216
	}
217
218
219
	/**
220
	 * Returns the locale item for the given site code, language code and currency code.
221
	 *
222
	 * @param string $site Site code
223
	 * @param string $lang Language code (optional)
224
	 * @param string $currency Currency code (optional)
225
	 * @param bool $active Flag to get only active items (optional)
226
	 * @param int|null $level Constant from abstract class which site ID levels should be available (optional),
227
	 * 	based on config or value for SITE_PATH if null
228
	 * @param bool $bare Allow locale items with sites only
229
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for the given parameters
230
	 * @throws \Aimeos\MShop\Locale\Exception If no locale item is found
231
	 */
232
	public function bootstrap( string $site, string $lang = '', string $currency = '', bool $active = true, int $level = null,
233
		bool $bare = false ) : \Aimeos\MShop\Locale\Item\Iface
234
	{
235
		$siteItem = $this->object()->getSubManager( 'site' )->find( $site );
236
237
		// allow enabled sites and sites under review
238
		if( $active && $siteItem->getStatus() < 1 && $siteItem->getStatus() !== -1 ) {
239
			throw new \Aimeos\MShop\Locale\Exception( 'Site not found' );
240
		}
241
242
		$siteId = $siteItem->getSiteId();
243
		$sites = [Base::SITE_ONE => $siteId];
244
245
		return $this->bootstrapBase( $site, $lang, $currency, $active, $siteItem, $siteId, $sites, $bare );
246
	}
247
248
249
	/**
250
	 * Removes old entries from the storage.
251
	 *
252
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
253
	 * @return \Aimeos\MShop\Locale\Manager\Iface Manager object for chaining method calls
254
	 */
255
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
256
	{
257
		return $this->clearBase( $siteids, 'mshop/locale/manager/delete' );
258
	}
259
260
261
	/**
262
	 * Creates a new empty item instance
263
	 *
264
	 * @param array $values Values the item should be initialized with
265
	 * @return \Aimeos\MShop\Locale\Item\Iface New locale item object
266
	 */
267
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
268
	{
269
		try {
270
			$values['locale.siteid'] = $values['locale.siteid'] ?? $this->context()->locale()->getSiteId();
271
		} catch( \Exception $e ) {} // if no locale item is available
272
273
		return $this->createItemBase( $values );
274
	}
275
276
277
	/**
278
	 * Creates a filter object.
279
	 *
280
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
281
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
282
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
283
	 */
284
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
285
	{
286
		return $this->filterBase( 'locale', $default );
287
	}
288
289
290
	/**
291
	 * Returns the item specified by its ID.
292
	 *
293
	 * @param string $id Unique ID of the locale item
294
	 * @param string[] $ref List of domains to fetch list items and referenced items for
295
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
296
	 * @return \Aimeos\MShop\Locale\Item\Iface Returns the locale item of the given id
297
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
298
	 */
299
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
300
	{
301
		return $this->getItemBase( 'locale.id', $id, $ref, $default );
302
	}
303
304
305
	/**
306
	 * Searches for all items matching the given critera.
307
	 *
308
	 * @param \Aimeos\Base\Criteria\Iface $search Criteria object with conditions, sortations, etc.
309
	 * @param string[] $ref List of domains to fetch list items and referenced items for
310
	 * @param int &$total Number of items that are available in total
311
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Locale\Item\Iface with ids as keys
312
	 */
313
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
314
	{
315
		$items = [];
316
//		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
317
//		$search = (clone $search)->add( $this->siteCondition( 'locale.siteid', $level ) );
318
319
		foreach( $this->searchEntries( $search, $ref, $total ) as $row )
320
		{
321
			if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
322
				$items[$row['locale.id']] = $item;
323
			}
324
		}
325
326
		return map( $items );
327
	}
328
329
330
	/**
331
	 * Removes multiple items.
332
	 *
333
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
334
	 * @return \Aimeos\MShop\Locale\Manager\Iface Manager object for chaining method calls
335
	 */
336
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
337
	{
338
		/** mshop/locale/manager/delete/mysql
339
		 * Deletes the items matched by the given IDs from the database
340
		 *
341
		 * @see mshop/locale/manager/delete/ansi
342
		 */
343
344
		/** mshop/locale/manager/delete/ansi
345
		 * Deletes the items matched by the given IDs from the database
346
		 *
347
		 * Removes the records specified by the given IDs from the locale database.
348
		 * The records must be from the site that is configured via the
349
		 * context item.
350
		 *
351
		 * The ":cond" placeholder is replaced by the name of the ID column and
352
		 * the given ID or list of IDs while the site ID is bound to the question
353
		 * mark.
354
		 *
355
		 * The SQL statement should conform to the ANSI standard to be
356
		 * compatible with most relational database systems. This also
357
		 * includes using double quotes for table and column names.
358
		 *
359
		 * @param string SQL statement for deleting items
360
		 * @since 2014.03
361
		 * @category Developer
362
		 * @see mshop/locale/manager/insert/ansi
363
		 * @see mshop/locale/manager/update/ansi
364
		 * @see mshop/locale/manager/newid/ansi
365
		 * @see mshop/locale/manager/search/ansi
366
		 * @see mshop/locale/manager/count/ansi
367
		 */
368
		$path = 'mshop/locale/manager/delete';
369
370
		return $this->deleteItemsBase( $itemIds, $path );
371
	}
372
373
374
	/**
375
	 * Adds or updates an item object.
376
	 *
377
	 * @param \Aimeos\MShop\Locale\Item\Iface $item Item object whose data should be saved
378
	 * @param bool $fetch True if the new ID should be returned in the item
379
	 * @return \Aimeos\MShop\Locale\Item\Iface $item Updated item including the generated ID
380
	 */
381
	protected function saveItem( \Aimeos\MShop\Locale\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Locale\Item\Iface
382
	{
383
		if( !$item->isModified() ) {
384
			return $item;
385
		}
386
387
		$context = $this->context();
388
		$conn = $context->db( $this->getResourceName() );
389
390
		$id = $item->getId();
391
		$columns = $this->object()->getSaveAttributes();
392
393
		if( $id === null )
394
		{
395
			/** mshop/locale/manager/insert/mysql
396
			 * Inserts a new locale record into the database table
397
			 *
398
			 * @see mshop/locale/manager/insert/ansi
399
			 */
400
401
			/** mshop/locale/manager/insert/ansi
402
			 * Inserts a new locale record into the database table
403
			 *
404
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
405
			 * the database and the newly created ID retrieved afterwards
406
			 * using the "newid" SQL statement.
407
			 *
408
			 * The SQL statement must be a string suitable for being used as
409
			 * prepared statement. It must include question marks for binding
410
			 * the values from the locale item to the statement before they are
411
			 * sent to the database server. The number of question marks must
412
			 * be the same as the number of columns listed in the INSERT
413
			 * statement. The order of the columns must correspond to the
414
			 * order in the save() method, so the correct values are
415
			 * bound to the columns.
416
			 *
417
			 * The SQL statement should conform to the ANSI standard to be
418
			 * compatible with most relational database systems. This also
419
			 * includes using double quotes for table and column names.
420
			 *
421
			 * @param string SQL statement for inserting records
422
			 * @since 2014.03
423
			 * @category Developer
424
			 * @see mshop/locale/manager/update/ansi
425
			 * @see mshop/locale/manager/newid/ansi
426
			 * @see mshop/locale/manager/delete/ansi
427
			 * @see mshop/locale/manager/search/ansi
428
			 * @see mshop/locale/manager/count/ansi
429
			 */
430
			$path = 'mshop/locale/manager/insert';
431
			$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

431
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
432
		}
433
		else
434
		{
435
			/** mshop/locale/manager/update/mysql
436
			 * Updates an existing locale record in the database
437
			 *
438
			 * @see mshop/locale/manager/update/ansi
439
			 */
440
441
			/** mshop/locale/manager/update/ansi
442
			 * Updates an existing locale record in the database
443
			 *
444
			 * Items which already have an ID (i.e. the ID is not NULL) will
445
			 * be updated in the database.
446
			 *
447
			 * The SQL statement must be a string suitable for being used as
448
			 * prepared statement. It must include question marks for binding
449
			 * the values from the locale item to the statement before they are
450
			 * sent to the database server. The order of the columns must
451
			 * correspond to the order in the save() method, so the
452
			 * correct values are bound to the columns.
453
			 *
454
			 * The SQL statement should conform to the ANSI standard to be
455
			 * compatible with most relational database systems. This also
456
			 * includes using double quotes for table and column names.
457
			 *
458
			 * @param string SQL statement for updating records
459
			 * @since 2014.03
460
			 * @category Developer
461
			 * @see mshop/locale/manager/insert/ansi
462
			 * @see mshop/locale/manager/newid/ansi
463
			 * @see mshop/locale/manager/delete/ansi
464
			 * @see mshop/locale/manager/search/ansi
465
			 * @see mshop/locale/manager/count/ansi
466
			 */
467
			$path = 'mshop/locale/manager/update';
468
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
469
		}
470
471
		$idx = 1;
472
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
473
		$siteIds = explode( '.', trim( $item->getSiteId(), '.' ) );
474
475
		foreach( $columns as $name => $entry ) {
476
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
477
		}
478
479
		$stmt->bind( $idx++, $item->getLanguageId() );
480
		$stmt->bind( $idx++, $item->getCurrencyId() );
481
		$stmt->bind( $idx++, $item->getPosition(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
482
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
483
		$stmt->bind( $idx++, $context->datetime() ); // mtime
484
		$stmt->bind( $idx++, $context->editor() );
485
		$stmt->bind( $idx++, end( $siteIds ), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
486
		$stmt->bind( $idx++, $item->getSiteId() );
487
488
		if( $id !== null ) {
489
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
490
		} else {
491
			$stmt->bind( $idx++, $context->datetime() ); // ctime
492
		}
493
494
		$stmt->execute()->finish();
495
496
		if( $id === null && $fetch === true )
497
		{
498
			/** mshop/locale/manager/newid/mysql
499
			 * Retrieves the ID generated by the database when inserting a new record
500
			 *
501
			 * @see mshop/locale/manager/newid/ansi
502
			 */
503
504
			/** mshop/locale/manager/newid/ansi
505
			 * Retrieves the ID generated by the database when inserting a new record
506
			 *
507
			 * As soon as a new record is inserted into the database table,
508
			 * the database server generates a new and unique identifier for
509
			 * that record. This ID can be used for retrieving, updating and
510
			 * deleting that specific record from the table again.
511
			 *
512
			 * For MySQL:
513
			 *  SELECT LAST_INSERT_ID()
514
			 * For PostgreSQL:
515
			 *  SELECT currval('seq_mloc_id')
516
			 * For SQL Server:
517
			 *  SELECT SCOPE_IDENTITY()
518
			 * For Oracle:
519
			 *  SELECT "seq_mloc_id".CURRVAL FROM DUAL
520
			 *
521
			 * There's no way to retrive the new ID by a SQL statements that
522
			 * fits for most database servers as they implement their own
523
			 * specific way.
524
			 *
525
			 * @param string SQL statement for retrieving the last inserted record ID
526
			 * @since 2014.03
527
			 * @category Developer
528
			 * @see mshop/locale/manager/insert/ansi
529
			 * @see mshop/locale/manager/update/ansi
530
			 * @see mshop/locale/manager/delete/ansi
531
			 * @see mshop/locale/manager/search/ansi
532
			 * @see mshop/locale/manager/count/ansi
533
			 */
534
			$path = 'mshop/locale/manager/newid';
535
			$id = $this->newId( $conn, $path );
536
		}
537
538
		$item->setId( $id );
539
540
		return $item;
541
	}
542
543
544
	/**
545
	 * Returns a new manager for locale extensions
546
	 *
547
	 * @param string $manager Name of the sub manager type in lower case
548
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
549
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g site, language, currency.
550
	 */
551
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
552
	{
553
		return $this->getSubManagerBase( 'locale', $manager, $name );
554
	}
555
556
557
	/**
558
	 * Returns the available manager types
559
	 *
560
	 * @param bool $withsub Return also the resource type of sub-managers if true
561
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
562
	 */
563
	public function getResourceType( bool $withsub = true ) : array
564
	{
565
		$path = 'mshop/locale/manager/submanagers';
566
		return $this->getResourceTypeBase( 'locale', $path, array( 'currency', 'language', 'site' ), $withsub );
567
	}
568
569
570
	/**
571
	 * Returns the attributes that can be used for searching.
572
	 *
573
	 * @param bool $withsub Return also attributes of sub-managers if true
574
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
575
	 */
576
	public function getSearchAttributes( bool $withsub = true ) : array
577
	{
578
		/** mshop/locale/manager/submanagers
579
		 * List of manager names that can be instantiated by the locale manager
580
		 *
581
		 * Managers provide a generic interface to the underlying storage.
582
		 * Each manager has or can have sub-managers caring about particular
583
		 * aspects. Each of these sub-managers can be instantiated by its
584
		 * parent manager using the getSubManager() method.
585
		 *
586
		 * The search keys from sub-managers can be normally used in the
587
		 * manager as well. It allows you to search for items of the manager
588
		 * using the search keys of the sub-managers to further limit the
589
		 * retrieved list of items.
590
		 *
591
		 * @param array List of sub-manager names
592
		 * @since 2014.03
593
		 * @category Developer
594
		 */
595
		$path = 'mshop/locale/manager/submanagers';
596
		$default = array( 'language', 'currency', 'site' );
597
598
		return $this->getSearchAttributesBase( $this->searchConfig, $path, $default, $withsub );
599
	}
600
601
602
	/**
603
	 * Returns the locale item for the given site code, language code and currency code.
604
	 *
605
	 * If the locale item is inherited from a parent site, the site ID of this locale item
606
	 * is changed to the site ID of the actual site. This ensures that items assigned to
607
	 * the same site as the site item are still used.
608
	 *
609
	 * @param string $site Site code
610
	 * @param string $lang Language code
611
	 * @param string $currency Currency code
612
	 * @param bool $active Flag to get only active items
613
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item
614
	 * @param string $siteId Site ID
615
	 * @param array $sites Associative list of site constant as key and sites as values
616
	 * @param bool $bare Allow locale items with sites only
617
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for the given parameters
618
	 * @throws \Aimeos\MShop\Locale\Exception If no locale item is found
619
	 */
620
	protected function bootstrapBase( string $site, string $lang, string $currency, bool $active,
621
		\Aimeos\MShop\Locale\Item\Site\Iface $siteItem, string $siteId, array $sites, bool $bare ) : \Aimeos\MShop\Locale\Item\Iface
622
	{
623
		if( $result = $this->bootstrapMatch( $siteId, $lang, $currency, $active, $siteItem, $sites ) ) {
624
			return $result;
625
		}
626
627
		if( $result = $this->bootstrapClosest( $siteId, $lang, $active, $siteItem, $sites ) ) {
628
			return $result;
629
		}
630
631
		if( $bare === true ) {
632
			return $this->createItemBase( ['locale.siteid' => $siteId], $siteItem, $sites );
633
		}
634
635
		$msg = $this->context()->translate( 'mshop', 'Locale item for site "%1$s" not found' );
636
		throw new \Aimeos\MShop\Locale\Exception( sprintf( $msg, $site ) );
637
	}
638
639
640
	/**
641
	 * Returns the matching locale item for the given site code, language code and currency code.
642
	 *
643
	 * If the locale item is inherited from a parent site, the site ID of this locale item
644
	 * is changed to the site ID of the actual site. This ensures that items assigned to
645
	 * the same site as the site item are still used.
646
	 *
647
	 * @param string $siteId Site ID
648
	 * @param string $lang Language code
649
	 * @param string $currency Currency code
650
	 * @param bool $active Flag to get only active items
651
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item
652
	 * @param array $sites Associative list of site constant as key and sites as values
653
	 * @return \Aimeos\MShop\Locale\Item\Iface|null Locale item for the given parameters or null if no item was found
654
	 */
655
	private function bootstrapMatch( string $siteId, string $lang, string $currency, bool $active,
656
		\Aimeos\MShop\Locale\Item\Site\Iface $siteItem, array $sites ) : ?\Aimeos\MShop\Locale\Item\Iface
657
	{
658
		// Try to find exact match
659
		$search = $this->object()->filter( $active );
660
661
		$expr = array( $search->compare( '==', 'locale.siteid', $sites[Base::SITE_PATH] ?? $sites[Base::SITE_ONE] ) );
662
663
		if( !empty( $lang ) )
664
		{
665
			$langIds = strlen( $lang ) > 2 ? [$lang, substr( $lang, 0, 2 )] : [$lang];
666
			$expr[] = $search->compare( '==', 'locale.languageid', $langIds );
667
		}
668
669
		if( !empty( $currency ) ) {
670
			$expr[] = $search->compare( '==', 'locale.currencyid', $currency );
671
		}
672
673
		$expr[] = $search->getConditions();
674
675
676
		if( $active === true )
677
		{
678
			$expr[] = $search->compare( '>', 'locale.currency.status', 0 );
679
			$expr[] = $search->compare( '>', 'locale.language.status', 0 );
680
			$expr[] = $search->compare( '>', 'locale.site.status', 0 );
681
		}
682
683
		$search->setConditions( $search->and( $expr ) );
684
		$search->setSortations( array( $search->sort( '+', 'locale.position' ) ) );
685
		$result = $this->searchEntries( $search );
686
687
		// Try to find first item where site matches
688
		foreach( $result as $row )
689
		{
690
			if( $row['locale.siteid'] === $siteId ) {
691
				return $this->createItemBase( $row, $siteItem, $sites );
692
			}
693
		}
694
695
		if( ( $row = reset( $result ) ) !== false )
696
		{
697
			$row['locale.siteid'] = $siteId;
698
			return $this->createItemBase( $row, $siteItem, $sites );
699
		}
700
701
		return null;
702
	}
703
704
705
	/**
706
	 * Returns the locale item for the given site code, language code and currency code.
707
	 *
708
	 * If the locale item is inherited from a parent site, the site ID of this locale item
709
	 * is changed to the site ID of the actual site. This ensures that items assigned to
710
	 * the same site as the site item are still used.
711
	 *
712
	 * @param string $siteId Site ID
713
	 * @param string $lang Language code
714
	 * @param bool $active Flag to get only active items
715
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item
716
	 * @param array $sites Associative list of site constant as key and sites as values
717
	 * @return \Aimeos\MShop\Locale\Item\Iface|null Locale item for the given parameters or null if no item was found
718
	 */
719
	private function bootstrapClosest( string $siteId, string $lang, bool $active,
720
		\Aimeos\MShop\Locale\Item\Site\Iface $siteItem, array $sites ) : ?\Aimeos\MShop\Locale\Item\Iface
721
	{
722
		// Try to find the best matching locale
723
		$search = $this->object()->filter( $active );
724
725
		$expr = array(
726
			$search->compare( '==', 'locale.siteid', $sites[Base::SITE_PATH] ?? $sites[Base::SITE_ONE] ),
727
			$search->getConditions()
728
		);
729
730
		if( $active === true )
731
		{
732
			$expr[] = $search->compare( '>', 'locale.currency.status', 0 );
733
			$expr[] = $search->compare( '>', 'locale.language.status', 0 );
734
			$expr[] = $search->compare( '>', 'locale.site.status', 0 );
735
		}
736
737
		$search->setConditions( $search->and( $expr ) );
738
		$search->setSortations( array( $search->sort( '+', 'locale.position' ) ) );
739
		$result = $this->searchEntries( $search );
740
741
		// Try to find first item where site and language matches
742
		foreach( $result as $row )
743
		{
744
			if( $row['locale.siteid'] === $siteId && $row['locale.languageid'] === $lang ) {
745
				return $this->createItemBase( $row, $siteItem, $sites );
746
			}
747
		}
748
749
		$short = strlen( $lang ) > 2 ? substr( $lang, 0, 2 ) : null;
750
751
		// Try to find first item where site and language without country matches
752
		if( $short )
753
		{
754
			foreach( $result as $row )
755
			{
756
				if( $row['locale.siteid'] === $siteId && $row['locale.languageid'] === $short ) {
757
					return $this->createItemBase( $row, $siteItem, $sites );
758
				}
759
			}
760
		}
761
762
		// Try to find first item where language matches
763
		foreach( $result as $row )
764
		{
765
			if( $row['locale.languageid'] === $lang )
766
			{
767
				$row['locale.siteid'] = $siteId;
768
				return $this->createItemBase( $row, $siteItem, $sites );
769
			}
770
		}
771
772
		// Try to find first item where language without country matches
773
		if( $short )
774
		{
775
			foreach( $result as $row )
776
			{
777
				if( $row['locale.siteid'] === $siteId && $row['locale.languageid'] === $short ) {
778
					return $this->createItemBase( $row, $siteItem, $sites );
779
				}
780
			}
781
		}
782
783
		// Try to find first item where site matches
784
		foreach( $result as $row )
785
		{
786
			if( $row['locale.siteid'] === $siteId ) {
787
				return $this->createItemBase( $row, $siteItem, $sites );
788
			}
789
		}
790
791
		// Return first item (no other match found)
792
		if( ( $row = reset( $result ) ) !== false )
793
		{
794
			$row['locale.siteid'] = $siteId;
795
			return $this->createItemBase( $row, $siteItem, $sites );
796
		}
797
798
		return null;
799
	}
800
801
802
	/**
803
	 * Instances a new locale item object.
804
	 *
805
	 * @param array $values Parameter to initialise the item
806
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface|null $site Site item
807
	 * @param array $sites Associative list of site constant as key and sites as values
808
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item
809
	 */
810
	protected function createItemBase( array $values = [], \Aimeos\MShop\Locale\Item\Site\Iface $site = null,
811
		array $sites = [] ) : \Aimeos\MShop\Locale\Item\Iface
812
	{
813
		return new \Aimeos\MShop\Locale\Item\Standard( $values, $site, $sites );
814
	}
815
816
817
	/**
818
	 * Searches for all items matching the given critera.
819
	 *
820
	 * @param \Aimeos\Base\Criteria\Iface $search Criteria object with conditions, sortations, etc.
821
	 * @param string[] $ref List of domains to fetch list items and referenced items for
822
	 * @param int &$total Number of items that are available in total
823
	 * @return array Associative list of key/value pairs
824
	 */
825
	protected function searchEntries( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : array
826
	{
827
		$map = [];
828
		$context = $this->context();
829
		$conn = $context->db( $this->getResourceName() );
830
831
		$required = ['locale'];
832
833
		/** mshop/locale/manager/search/mysql
834
		 * Retrieves the records matched by the given criteria in the database
835
		 *
836
		 * @see mshop/locale/manager/search/ansi
837
		 */
838
839
		/** mshop/locale/manager/search/ansi
840
		 * Retrieves the records matched by the given criteria in the database
841
		 *
842
		 * Fetches the records matched by the given criteria from the locale
843
		 * database. The records must be from one of the sites that are
844
		 * configured via the context item. If the current site is part of
845
		 * a tree of sites, the SELECT statement can retrieve all records
846
		 * from the current site and the complete sub-tree of sites.
847
		 *
848
		 * To limit the records matched, conditions can be added to the given
849
		 * criteria object. It can contain comparisons like column names that
850
		 * must match specific values which can be combined by AND, OR or NOT
851
		 * operators. The resulting string of SQL conditions replaces the
852
		 * ":cond" placeholder before the statement is sent to the database
853
		 * server.
854
		 *
855
		 * If the records that are retrieved should be ordered by one or more
856
		 * columns, the generated string of column / sort direction pairs
857
		 * replaces the ":order" placeholder. Columns of
858
		 * sub-managers can also be used for ordering the result set but then
859
		 * no index can be used.
860
		 *
861
		 * The number of returned records can be limited and can start at any
862
		 * number between the begining and the end of the result set. For that
863
		 * the ":size" and ":start" placeholders are replaced by the
864
		 * corresponding values from the criteria object. The default values
865
		 * are 0 for the start and 100 for the size value.
866
		 *
867
		 * The SQL statement should conform to the ANSI standard to be
868
		 * compatible with most relational database systems. This also
869
		 * includes using double quotes for table and column names.
870
		 *
871
		 * @param string SQL statement for searching items
872
		 * @since 2014.03
873
		 * @category Developer
874
		 * @see mshop/locale/manager/insert/ansi
875
		 * @see mshop/locale/manager/update/ansi
876
		 * @see mshop/locale/manager/newid/ansi
877
		 * @see mshop/locale/manager/delete/ansi
878
		 * @see mshop/locale/manager/count/ansi
879
		 */
880
		$cfgPathSearch = 'mshop/locale/manager/search';
881
882
		/** mshop/locale/manager/count/mysql
883
		 * Counts the number of records matched by the given criteria in the database
884
		 *
885
		 * @see mshop/locale/manager/count/ansi
886
		 */
887
888
		/** mshop/locale/manager/count/ansi
889
		 * Counts the number of records matched by the given criteria in the database
890
		 *
891
		 * Counts all records matched by the given criteria from the locale
892
		 * database. The records must be from one of the sites that are
893
		 * configured via the context item. If the current site is part of
894
		 * a tree of sites, the statement can count all records from the
895
		 * current site and the complete sub-tree of sites.
896
		 *
897
		 * To limit the records matched, conditions can be added to the given
898
		 * criteria object. It can contain comparisons like column names that
899
		 * must match specific values which can be combined by AND, OR or NOT
900
		 * operators. The resulting string of SQL conditions replaces the
901
		 * ":cond" placeholder before the statement is sent to the database
902
		 * server.
903
		 *
904
		 * Both, the strings for ":joins" and for ":cond" are the same as for
905
		 * the "search" SQL statement.
906
		 *
907
		 * Contrary to the "search" statement, it doesn't return any records
908
		 * but instead the number of records that have been found. As counting
909
		 * thousands of records can be a long running task, the maximum number
910
		 * of counted records is limited for performance reasons.
911
		 *
912
		 * The SQL statement should conform to the ANSI standard to be
913
		 * compatible with most relational database systems. This also
914
		 * includes using double quotes for table and column names.
915
		 *
916
		 * @param string SQL statement for counting items
917
		 * @since 2014.03
918
		 * @category Developer
919
		 * @see mshop/locale/manager/insert/ansi
920
		 * @see mshop/locale/manager/update/ansi
921
		 * @see mshop/locale/manager/newid/ansi
922
		 * @see mshop/locale/manager/delete/ansi
923
		 * @see mshop/locale/manager/search/ansi
924
		 */
925
		$cfgPathCount = 'mshop/locale/manager/count';
926
927
		$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total );
928
929
		while( $row = $results->fetch() ) {
930
			$map[$row['locale.id']] = $row;
931
		}
932
933
		return $map;
934
	}
935
936
937
	/**
938
	 * Returns the site coditions for the search request
939
	 *
940
	 * @param string[] $keys Sorted list of criteria keys
941
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes Associative list of search keys and criteria attribute items as values
942
	 * @param int $sitelevel Site level constant from \Aimeos\MShop\Locale\Manager\Base
943
	 * @return \Aimeos\Base\Criteria\Expression\Iface[] List of search conditions
944
	 */
945
	protected function getSiteConditions( array $keys, array $attributes, int $sitelevel ) : array
946
	{
947
		return [];
948
	}
949
}
950