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

Standard   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 705
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 144
dl 0
loc 705
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A __construct() 0 4 1
A create() 0 9 2
A getSearchResults() 0 14 1
A find() 0 4 1
A delete() 0 34 1
A getSearchAttributes() 0 22 1
A get() 0 3 1
A saveItem() 0 105 5
A filter() 0 3 1
B search() 0 107 7
A getResourceType() 0 4 1
A createItemBase() 0 3 1
A getSubManager() 0 115 1
A getTotal() 0 65 2
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\Language;
13
14
15
/**
16
 * Default implementation for managing languages.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Locale\Manager\Language\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	private $searchConfig = array(
26
		'locale.language.id' => array(
27
			'code' => 'locale.language.id',
28
			'internalcode' => 'mlocla."id"',
29
			'internaldeps' => array( 'LEFT JOIN "mshop_locale_language" AS mlocla ON (mloc."langid" = mlocla."id")' ),
30
			'label' => 'Language ID',
31
			'type' => 'string',
32
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
33
			'public' => false,
34
		),
35
		'locale.language.label' => array(
36
			'code' => 'locale.language.label',
37
			'internalcode' => 'mlocla."label"',
38
			'label' => 'Language label',
39
			'type' => 'string',
40
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
41
		),
42
		'locale.language.code' => array(
43
			'code' => 'locale.language.code',
44
			'internalcode' => 'mlocla."id"',
45
			'label' => 'Language code',
46
			'type' => 'string',
47
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
48
		),
49
		'locale.language.status' => array(
50
			'code' => 'locale.language.status',
51
			'internalcode' => 'mlocla."status"',
52
			'label' => 'Language status',
53
			'type' => 'integer',
54
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
55
		),
56
		'locale.language.ctime' => array(
57
			'code' => 'locale.language.ctime',
58
			'internalcode' => 'mlocla."ctime"',
59
			'label' => 'Language create date/time',
60
			'type' => 'datetime',
61
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
62
			'public' => false,
63
		),
64
		'locale.language.mtime' => array(
65
			'code' => 'locale.language.mtime',
66
			'internalcode' => 'mlocla."mtime"',
67
			'label' => 'Language modify date/time',
68
			'type' => 'datetime',
69
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
70
			'public' => false,
71
		),
72
		'locale.language.editor' => array(
73
			'code' => 'locale.language.editor',
74
			'internalcode' => 'mlocla."editor"',
75
			'label' => 'Language editor',
76
			'type' => 'string',
77
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
78
			'public' => false,
79
		),
80
	);
81
82
83
	/**
84
	 * Initializes the object.
85
	 *
86
	 * @param \Aimeos\MShop\ContextIface $context Context object
87
	 */
88
	public function __construct( \Aimeos\MShop\ContextIface $context )
89
	{
90
		parent::__construct( $context );
91
		$this->setResourceName( 'db-locale' );
92
	}
93
94
95
	/**
96
	 * Removes old entries from the storage.
97
	 *
98
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
99
	 * @return \Aimeos\MShop\Locale\Manager\Language\Iface Manager object for chaining method calls
100
	 */
101
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
102
	{
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Creates a new empty item instance
109
	 *
110
	 * @param array $values Values the item should be initialized with
111
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface New locale language item object
112
	 */
113
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
114
	{
115
		try {
116
			$values['locale.language.siteid'] = $this->context()->locale()->getSiteId();
117
		} catch( \Exception $ex ) {
118
			$values['locale.language.siteid'] = null;
119
		}
120
121
		return $this->createItemBase( $values );
122
	}
123
124
125
	/**
126
	 * Saves the language object to the storage.
127
	 *
128
	 * @param \Aimeos\MShop\Locale\Item\Language\Iface $item Language object
129
	 * @param bool $fetch True if the new ID should be returned in the item
130
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface $item Updated item including the generated ID
131
	 */
132
	public function saveItem( \Aimeos\MShop\Locale\Item\Language\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Locale\Item\Language\Iface
133
	{
134
		if( !$item->isModified() ) {
135
			return $item;
136
		}
137
138
		$context = $this->context();
139
		$conn = $context->db( $this->getResourceName() );
140
141
			$id = $item->getId();
142
			$date = date( 'Y-m-d H:i:s' );
143
			$columns = $this->object()->getSaveAttributes();
144
145
			if( $id === null )
146
			{
147
				/** mshop/locale/manager/language/insert/mysql
148
				 * Inserts a new language record into the database table
149
				 *
150
				 * @see mshop/locale/manager/language/insert/ansi
151
				 */
152
153
				/** mshop/locale/manager/language/insert/ansi
154
				 * Inserts a new language record into the database table
155
				 *
156
				 * The SQL statement must be a string suitable for being used as
157
				 * prepared statement. It must include question marks for binding
158
				 * the values from the language item to the statement before they are
159
				 * sent to the database server. The number of question marks must
160
				 * be the same as the number of columns listed in the INSERT
161
				 * statement. The order of the columns must correspond to the
162
				 * order in the save() method, so the correct values are
163
				 * bound to the columns.
164
				 *
165
				 * The SQL statement should conform to the ANSI standard to be
166
				 * compatible with most relational database systems. This also
167
				 * includes using double quotes for table and column names.
168
				 *
169
				 * @param string SQL statement for inserting records
170
				 * @since 2014.03
171
				 * @category Developer
172
				 * @see mshop/locale/manager/language/update/ansi
173
				 * @see mshop/locale/manager/language/delete/ansi
174
				 * @see mshop/locale/manager/language/search/ansi
175
				 * @see mshop/locale/manager/language/count/ansi
176
				 */
177
				$path = 'mshop/locale/manager/language/insert';
178
				$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

178
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
179
			}
180
			else
181
			{
182
				/** mshop/locale/manager/language/update/mysql
183
				 * Updates an existing language record in the database
184
				 *
185
				 * @see mshop/locale/manager/language/update/ansi
186
				 */
187
188
				/** mshop/locale/manager/language/update/ansi
189
				 * Updates an existing language record in the database
190
				 *
191
				 * The SQL statement must be a string suitable for being used as
192
				 * prepared statement. It must include question marks for binding
193
				 * the values from the language item to the statement before they are
194
				 * sent to the database server. The order of the columns must
195
				 * correspond to the order in the save() method, so the
196
				 * correct values are bound to the columns.
197
				 *
198
				 * The SQL statement should conform to the ANSI standard to be
199
				 * compatible with most relational database systems. This also
200
				 * includes using double quotes for table and column names.
201
				 *
202
				 * @param string SQL statement for updating records
203
				 * @since 2014.03
204
				 * @category Developer
205
				 * @see mshop/locale/manager/language/insert/ansi
206
				 * @see mshop/locale/manager/language/delete/ansi
207
				 * @see mshop/locale/manager/language/search/ansi
208
				 * @see mshop/locale/manager/language/count/ansi
209
				 */
210
				$path = 'mshop/locale/manager/language/update';
211
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
212
			}
213
214
			$idx = 1;
215
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
216
217
			foreach( $columns as $name => $entry ) {
218
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
219
			}
220
221
			$stmt->bind( $idx++, $item->getLabel() );
222
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
223
			$stmt->bind( $idx++, $date ); // mtime
224
			$stmt->bind( $idx++, $context->editor() );
225
			// code and ID are identical after saving and ID is the flag to detect updates or inserts
226
			$stmt->bind( $idx++, $item->getCode() );
227
228
			if( $id === null ) {
229
				$stmt->bind( $idx++, $date ); // ctime
230
			}
231
232
			$stmt->execute()->finish();
233
234
			$item->setId( $item->getCode() ); // set modified flag to false
235
236
		return $item;
237
	}
238
239
240
	/**
241
	 * Removes multiple items.
242
	 *
243
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
244
	 * @return \Aimeos\MShop\Locale\Manager\Language\Iface Manager object for chaining method calls
245
	 */
246
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
247
	{
248
		/** mshop/locale/manager/language/delete/mysql
249
		 * Deletes the items matched by the given IDs from the database
250
		 *
251
		 * @see mshop/locale/manager/language/delete/ansi
252
		 */
253
254
		/** mshop/locale/manager/language/delete/ansi
255
		 * Deletes the items matched by the given IDs from the database
256
		 *
257
		 * Removes the language records specified by the given IDs from the
258
		 * locale database. The records must be from the site that is configured
259
		 * via the context item.
260
		 *
261
		 * The ":cond" placeholder is replaced by the name of the ID column and
262
		 * the given ID or list of IDs while the site ID is bound to the question
263
		 * mark.
264
		 *
265
		 * The SQL statement should conform to the ANSI standard to be
266
		 * compatible with most relational database systems. This also
267
		 * includes using double quotes for table and column names.
268
		 *
269
		 * @param string SQL statement for deleting items
270
		 * @since 2014.03
271
		 * @category Developer
272
		 * @see mshop/locale/manager/language/insert/ansi
273
		 * @see mshop/locale/manager/language/update/ansi
274
		 * @see mshop/locale/manager/language/search/ansi
275
		 * @see mshop/locale/manager/language/count/ansi
276
		 */
277
		$path = 'mshop/locale/manager/language/delete';
278
279
		return $this->deleteItemsBase( $itemIds, $path, false );
280
	}
281
282
283
	/**
284
	 * Create a Language object from a given Language ID/Key.
285
	 *
286
	 * @param string $id Language id to create the Language object
287
	 * @param string[] $ref List of domains to fetch list items and referenced items for
288
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Returns the language item of the given id
289
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
290
	 * @throws \Aimeos\Base\DB\Exception If language object couldn't be fetched
291
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
292
	 */
293
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
294
	{
295
		return $this->getItemBase( 'locale.language.id', $id, $ref, $default );
296
	}
297
298
299
	/**
300
	 * Returns the available manager types
301
	 *
302
	 * @param bool $withsub Return also the resource type of sub-managers if true
303
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
304
	 */
305
	public function getResourceType( bool $withsub = true ) : array
306
	{
307
		$path = 'mshop/locale/manager/language/submanagers';
308
		return $this->getResourceTypeBase( 'locale/language', $path, [], $withsub );
309
	}
310
311
312
	/**
313
	 * Returns the attributes that can be used for searching.
314
	 *
315
	 * @param bool $withsub Return also attributes of sub-managers if true
316
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
317
	 */
318
	public function getSearchAttributes( bool $withsub = true ) : array
319
	{
320
		/** mshop/locale/manager/language/submanagers
321
		 * List of manager names that can be instantiated by the locale language manager
322
		 *
323
		 * Managers provide a generic interface to the underlying storage.
324
		 * Each manager has or can have sub-managers caring about particular
325
		 * aspects. Each of these sub-managers can be instantiated by its
326
		 * parent manager using the getSubManager() method.
327
		 *
328
		 * The search keys from sub-managers can be normally used in the
329
		 * manager as well. It allows you to search for items of the manager
330
		 * using the search keys of the sub-managers to further limit the
331
		 * retrieved list of items.
332
		 *
333
		 * @param array List of sub-manager names
334
		 * @since 2014.03
335
		 * @category Developer
336
		 */
337
		$path = 'mshop/locale/manager/language/submanagers';
338
339
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
340
	}
341
342
343
	/**
344
	 * Returns a new sub manager of the given type and name.
345
	 *
346
	 * @param string $manager Name of the sub manager type in lower case
347
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
348
	 * @return \Aimeos\MShop\Common\Manager\Iface manager
349
	 */
350
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
351
	{
352
		/** mshop/locale/manager/language/name
353
		 * Class name of the used locale language manager implementation
354
		 *
355
		 * Each default locale language manager can be replaced by an alternative imlementation.
356
		 * To use this implementation, you have to set the last part of the class
357
		 * name as configuration value so the manager factory knows which class it
358
		 * has to instantiate.
359
		 *
360
		 * For example, if the name of the default class is
361
		 *
362
		 *  \Aimeos\MShop\Locale\Manager\Language\Standard
363
		 *
364
		 * and you want to replace it with your own version named
365
		 *
366
		 *  \Aimeos\MShop\Locale\Manager\Language\Mylanguage
367
		 *
368
		 * then you have to set the this configuration option:
369
		 *
370
		 *  mshop/locale/manager/language/name = Mylanguage
371
		 *
372
		 * The value is the last part of your own class name and it's case sensitive,
373
		 * so take care that the configuration value is exactly named like the last
374
		 * part of the class name.
375
		 *
376
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
377
		 * characters are possible! You should always start the last part of the class
378
		 * name with an upper case character and continue only with lower case characters
379
		 * or numbers. Avoid chamel case names like "MyLanguage"!
380
		 *
381
		 * @param string Last part of the class name
382
		 * @since 2014.03
383
		 * @category Developer
384
		 */
385
386
		/** mshop/locale/manager/language/decorators/excludes
387
		 * Excludes decorators added by the "common" option from the locale language manager
388
		 *
389
		 * Decorators extend the functionality of a class by adding new aspects
390
		 * (e.g. log what is currently done), executing the methods of the underlying
391
		 * class only in certain conditions (e.g. only for logged in users) or
392
		 * modify what is returned to the caller.
393
		 *
394
		 * This option allows you to remove a decorator added via
395
		 * "mshop/common/manager/decorators/default" before they are wrapped
396
		 * around the locale language manager.
397
		 *
398
		 *  mshop/locale/manager/language/decorators/excludes = array( 'decorator1' )
399
		 *
400
		 * This would remove the decorator named "decorator1" from the list of
401
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
402
		 * "mshop/common/manager/decorators/default" for the locale language manager.
403
		 *
404
		 * @param array List of decorator names
405
		 * @since 2014.03
406
		 * @category Developer
407
		 * @see mshop/common/manager/decorators/default
408
		 * @see mshop/locale/manager/language/decorators/global
409
		 * @see mshop/locale/manager/language/decorators/local
410
		 */
411
412
		/** mshop/locale/manager/language/decorators/global
413
		 * Adds a list of globally available decorators only to the locale language manager
414
		 *
415
		 * Decorators extend the functionality of a class by adding new aspects
416
		 * (e.g. log what is currently done), executing the methods of the underlying
417
		 * class only in certain conditions (e.g. only for logged in users) or
418
		 * modify what is returned to the caller.
419
		 *
420
		 * This option allows you to wrap global decorators
421
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale language
422
		 * manager.
423
		 *
424
		 *  mshop/locale/manager/language/decorators/global = array( 'decorator1' )
425
		 *
426
		 * This would add the decorator named "decorator1" defined by
427
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
428
		 * language manager.
429
		 *
430
		 * @param array List of decorator names
431
		 * @since 2014.03
432
		 * @category Developer
433
		 * @see mshop/common/manager/decorators/default
434
		 * @see mshop/locale/manager/language/decorators/excludes
435
		 * @see mshop/locale/manager/language/decorators/local
436
		 */
437
438
		/** mshop/locale/manager/language/decorators/local
439
		 * Adds a list of local decorators only to the locale language manager
440
		 *
441
		 * Decorators extend the functionality of a class by adding new aspects
442
		 * (e.g. log what is currently done), executing the methods of the underlying
443
		 * class only in certain conditions (e.g. only for logged in users) or
444
		 * modify what is returned to the caller.
445
		 *
446
		 * This option allows you to wrap local decorators
447
		 * ("\Aimeos\MShop\Locale\Manager\Language\Decorator\*") around the locale
448
		 * language manager.
449
		 *
450
		 *  mshop/locale/manager/language/decorators/local = array( 'decorator2' )
451
		 *
452
		 * This would add the decorator named "decorator2" defined by
453
		 * "\Aimeos\MShop\Locale\Manager\Language\Decorator\Decorator2" only to the
454
		 * locale language manager.
455
		 *
456
		 * @param array List of decorator names
457
		 * @since 2014.03
458
		 * @category Developer
459
		 * @see mshop/common/manager/decorators/default
460
		 * @see mshop/locale/manager/language/decorators/excludes
461
		 * @see mshop/locale/manager/language/decorators/global
462
		 */
463
464
		return $this->getSubManagerBase( 'locale', 'language/' . $manager, $name );
465
	}
466
467
468
	/**
469
	 * Returns the item specified by its code and domain/type if necessary
470
	 *
471
	 * @param string $code Code of the item
472
	 * @param string[] $ref List of domains to fetch list items and referenced items for
473
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
474
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
475
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
476
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
477
	 */
478
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
479
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
480
	{
481
		return $this->findBase( array( 'locale.language.id' => $code ), $ref, $default );
482
	}
483
484
485
	/**
486
	 * Searches for language items matching the given criteria.
487
	 *
488
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
489
	 * @param string[] $ref List of domains to fetch list items and referenced items for
490
	 * @param int|null &$total Number of items that are available in total
491
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Locale\Item\Language\Iface with ids as keys
492
	 */
493
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
494
	{
495
		$items = [];
496
		$context = $this->context();
497
		$conn = $context->db( $this->getResourceName() );
498
499
			$attributes = $this->object()->getSearchAttributes();
500
			$translations = $this->getSearchTranslations( $attributes );
501
			$types = $this->getSearchTypes( $attributes );
502
			$columns = $this->object()->getSaveAttributes();
503
			$sortcols = $search->translate( $search->getSortations(), $translations );
504
505
			$colstring = '';
506
			foreach( $columns as $name => $entry ) {
507
				$colstring .= $entry->getInternalCode() . ', ';
508
			}
509
510
			$find = array( ':columns', ':cond', ':order', ':start', ':size' );
511
			$replace = array(
512
				$colstring . ( $sortcols ? join( ', ', $sortcols ) . ', ' : '' ),
513
				$search->getConditionSource( $types, $translations ),
514
				$search->getSortationSource( $types, $translations ),
515
				$search->getOffset(),
516
				$search->getLimit(),
517
			);
518
519
			/** mshop/locale/manager/language/search/mysql
520
			 * Retrieves the records matched by the given criteria in the database
521
			 *
522
			 * @see mshop/locale/manager/language/search/ansi
523
			 */
524
525
			/** mshop/locale/manager/language/search/ansi
526
			 * Retrieves the records matched by the given criteria in the database
527
			 *
528
			 * Fetches the records matched by the given criteria from the attribute
529
			 * database. The records must be from one of the sites that are
530
			 * configured via the context item. If the current site is part of
531
			 * a tree of sites, the SELECT statement can retrieve all records
532
			 * from the current site and the complete sub-tree of sites.
533
			 *
534
			 * As the records can normally be limited by criteria from sub-managers,
535
			 * their tables must be joined in the SQL context. This is done by
536
			 * using the "internaldeps" property from the definition of the ID
537
			 * column of the sub-managers. These internal dependencies specify
538
			 * the JOIN between the tables and the used columns for joining. The
539
			 * ":joins" placeholder is then replaced by the JOIN strings from
540
			 * the sub-managers.
541
			 *
542
			 * To limit the records matched, conditions can be added to the given
543
			 * criteria object. It can contain comparisons like column names that
544
			 * must match specific values which can be combined by AND, OR or NOT
545
			 * operators. The resulting string of SQL conditions replaces the
546
			 * ":cond" placeholder before the statement is sent to the database
547
			 * server.
548
			 *
549
			 * If the records that are retrieved should be ordered by one or more
550
			 * columns, the generated string of column / sort direction pairs
551
			 * replaces the ":order" placeholder. In case no ordering is required,
552
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
553
			 * markers is removed to speed up retrieving the records. Columns of
554
			 * sub-managers can also be used for ordering the result set but then
555
			 * no index can be used.
556
			 *
557
			 * The number of returned records can be limited and can start at any
558
			 * number between the begining and the end of the result set. For that
559
			 * the ":size" and ":start" placeholders are replaced by the
560
			 * corresponding values from the criteria object. The default values
561
			 * are 0 for the start and 100 for the size value.
562
			 *
563
			 * The SQL statement should conform to the ANSI standard to be
564
			 * compatible with most relational database systems. This also
565
			 * includes using double quotes for table and column names.
566
			 *
567
			 * @param string SQL statement for searching items
568
			 * @since 2014.03
569
			 * @category Developer
570
			 * @see mshop/locale/manager/language/insert/ansi
571
			 * @see mshop/locale/manager/language/update/ansi
572
			 * @see mshop/locale/manager/language/delete/ansi
573
			 * @see mshop/locale/manager/language/count/ansi
574
			 */
575
			$path = 'mshop/locale/manager/language/search';
576
577
			$sql = $this->getSqlConfig( $path );
578
			$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
579
580
			try
581
			{
582
				while( ( $row = $results->fetch() ) !== null )
583
				{
584
					if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
585
						$items[$row['locale.language.id']] = $item;
586
					}
587
				}
588
			}
589
			catch( \Exception $e )
590
			{
591
				$results->finish();
592
				throw $e;
593
			}
594
595
			if( $total !== null ) {
596
				$total = $this->getTotal( $conn, $find, $replace );
597
			}
598
599
		return map( $items );
600
	}
601
602
603
	/**
604
	 * Creates a filter object.
605
	 *
606
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
607
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
608
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
609
	 */
610
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
611
	{
612
		return $this->filterBase( 'locale.language', $default );
613
	}
614
615
616
	/**
617
	 * Returns the search results for the given SQL statement.
618
	 *
619
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
620
	 * @param string $sql SQL statement
621
	 * @return \Aimeos\Base\DB\Result\Iface Search result object
622
	 */
623
	protected function getSearchResults( \Aimeos\Base\DB\Connection\Iface $conn, string $sql ) : \Aimeos\Base\DB\Result\Iface
624
	{
625
		$time = microtime( true );
626
627
		$stmt = $conn->create( $sql );
628
		$result = $stmt->execute();
629
630
		$msg = 'Time: ' . ( microtime( true ) - $time ) * 1000 . "ms\n"
631
			. 'Class: ' . get_class( $this ) . "\n"
632
			. str_replace( ["\t", "\n\n"], ['', "\n"], trim( (string) $stmt ) );
633
634
		$this->context()->logger()->debug( $msg, 'core/sql' );
635
636
		return $result;
637
	}
638
639
640
	/**
641
	 * Create new item object initialized with given parameters.
642
	 *
643
	 * @param array $data Associative list of item key/value pairs
644
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Language item
645
	 */
646
	protected function createItemBase( array $data = [] ) : \Aimeos\MShop\Locale\Item\Language\Iface
647
	{
648
		return new \Aimeos\MShop\Locale\Item\Language\Standard( $data );
649
	}
650
651
652
	/**
653
	 * Returns the total number of items found for the conditions
654
	 *
655
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection
656
	 * @param string[] $find List of markers that should be replaced in the SQL statement
657
	 * @param string[] $replace List of replacements for the markers in the SQL statement
658
	 * @throws \Aimeos\MShop\Locale\Exception If no total value was found
659
	 * @return integer Total number of found items
660
	 */
661
	protected function getTotal( \Aimeos\Base\DB\Connection\Iface $conn, array $find, array $replace ) : int
662
	{
663
		/** mshop/locale/manager/language/count/mysql
664
		 * Counts the number of records matched by the given criteria in the database
665
		 *
666
		 * @see mshop/locale/manager/language/count/ansi
667
		 */
668
669
		/** mshop/locale/manager/language/count/ansi
670
		 * Counts the number of records matched by the given criteria in the database
671
		 *
672
		 * Counts all records matched by the given criteria from the attribute
673
		 * database. The records must be from one of the sites that are
674
		 * configured via the context item. If the current site is part of
675
		 * a tree of sites, the statement can count all records from the
676
		 * current site and the complete sub-tree of sites.
677
		 *
678
		 * As the records can normally be limited by criteria from sub-managers,
679
		 * their tables must be joined in the SQL context. This is done by
680
		 * using the "internaldeps" property from the definition of the ID
681
		 * column of the sub-managers. These internal dependencies specify
682
		 * the JOIN between the tables and the used columns for joining. The
683
		 * ":joins" placeholder is then replaced by the JOIN strings from
684
		 * the sub-managers.
685
		 *
686
		 * To limit the records matched, conditions can be added to the given
687
		 * criteria object. It can contain comparisons like column names that
688
		 * must match specific values which can be combined by AND, OR or NOT
689
		 * operators. The resulting string of SQL conditions replaces the
690
		 * ":cond" placeholder before the statement is sent to the database
691
		 * server.
692
		 *
693
		 * Both, the strings for ":joins" and for ":cond" are the same as for
694
		 * the "search" SQL statement.
695
		 *
696
		 * Contrary to the "search" statement, it doesn't return any records
697
		 * but instead the number of records that have been found. As counting
698
		 * thousands of records can be a long running task, the maximum number
699
		 * of counted records is limited for performance reasons.
700
		 *
701
		 * The SQL statement should conform to the ANSI standard to be
702
		 * compatible with most relational database systems. This also
703
		 * includes using double quotes for table and column names.
704
		 *
705
		 * @param string SQL statement for counting items
706
		 * @since 2014.03
707
		 * @category Developer
708
		 * @see mshop/locale/manager/language/insert/ansi
709
		 * @see mshop/locale/manager/language/update/ansi
710
		 * @see mshop/locale/manager/language/delete/ansi
711
		 * @see mshop/locale/manager/language/search/ansi
712
		 */
713
		$path = 'mshop/locale/manager/language/count';
714
715
		$sql = $this->getSqlConfig( $path );
716
		$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
717
718
		$row = $results->fetch();
719
		$results->finish();
720
721
		if( $row === null ) {
722
			throw new \Aimeos\MShop\Locale\Exception( 'No total results value found' );
723
		}
724
725
		return $row['count'];
726
	}
727
}
728