Passed
Push — master ( 23d0a5...26f6d6 )
by Aimeos
05:18
created

Standard::saveItem()   B

Complexity

Conditions 6
Paths 72

Size

Total Lines 121
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nc 72
nop 2
dl 0
loc 121
rs 8.7217
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
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\MW\DB\Statement\Base::PARAM_STR,
33
			'public' => false,
34
		),
35
		'locale.language.siteid' => array(
36
			'code' => 'locale.language.siteid',
37
			'internalcode' => 'mlocla."siteid"',
38
			'label' => 'Language site ID',
39
			'type' => 'string',
40
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
41
			'public' => false,
42
		),
43
		'locale.language.label' => array(
44
			'code' => 'locale.language.label',
45
			'internalcode' => 'mlocla."label"',
46
			'label' => 'Language label',
47
			'type' => 'string',
48
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
49
		),
50
		'locale.language.code' => array(
51
			'code' => 'locale.language.code',
52
			'internalcode' => 'mlocla."id"',
53
			'label' => 'Language code',
54
			'type' => 'string',
55
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
56
		),
57
		'locale.language.status' => array(
58
			'code' => 'locale.language.status',
59
			'internalcode' => 'mlocla."status"',
60
			'label' => 'Language status',
61
			'type' => 'integer',
62
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
63
		),
64
		'locale.language.ctime' => array(
65
			'code' => 'locale.language.ctime',
66
			'internalcode' => 'mlocla."ctime"',
67
			'label' => 'Language create date/time',
68
			'type' => 'datetime',
69
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
70
			'public' => false,
71
		),
72
		'locale.language.mtime' => array(
73
			'code' => 'locale.language.mtime',
74
			'internalcode' => 'mlocla."mtime"',
75
			'label' => 'Language modify date/time',
76
			'type' => 'datetime',
77
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
78
			'public' => false,
79
		),
80
		'locale.language.editor' => array(
81
			'code' => 'locale.language.editor',
82
			'internalcode' => 'mlocla."editor"',
83
			'label' => 'Language editor',
84
			'type' => 'string',
85
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
86
			'public' => false,
87
		),
88
	);
89
90
91
	/**
92
	 * Initializes the object.
93
	 *
94
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
95
	 */
96
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
97
	{
98
		parent::__construct( $context );
99
		$this->setResourceName( 'db-locale' );
100
	}
101
102
103
	/**
104
	 * Creates a new empty item instance
105
	 *
106
	 * @param array $values Values the item should be initialized with
107
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface New locale language item object
108
	 */
109
	public function createItem( array $values = [] )
110
	{
111
		try {
112
			$values['locale.language.siteid'] = $this->getContext()->getLocale()->getSiteId();
113
		} catch( \Exception $ex ) {
114
			$values['locale.language.siteid'] = null;
115
		}
116
117
		return $this->createItemBase( $values );
118
	}
119
120
121
	/**
122
	 * Saves the language object to the storage.
123
	 *
124
	 * @param \Aimeos\MShop\Locale\Item\Language\Iface $item Language object
125
	 * @param boolean $fetch True if the new ID should be returned in the item
126
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface $item Updated item including the generated ID
127
	 */
128
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
129
	{
130
		self::checkClass( \Aimeos\MShop\Locale\Item\Language\Iface::class, $item );
131
132
		if( !$item->isModified() ) {
133
			return $item;
134
		}
135
136
		$context = $this->getContext();
137
138
		$dbm = $context->getDatabaseManager();
139
		$dbname = $this->getResourceName();
140
		$conn = $dbm->acquire( $dbname );
141
142
		try
143
		{
144
			$id = $item->getId();
145
			$date = date( 'Y-m-d H:i:s' );
146
			$columns = $this->getObject()->getSaveAttributes();
147
148
			if( $id === null )
149
			{
150
				/** mshop/locale/manager/language/standard/insert/mysql
151
				 * Inserts a new language record into the database table
152
				 *
153
				 * @see mshop/locale/manager/language/standard/insert/ansi
154
				 */
155
156
				/** mshop/locale/manager/language/standard/insert/ansi
157
				 * Inserts a new language record into the database table
158
				 *
159
				 * The SQL statement must be a string suitable for being used as
160
				 * prepared statement. It must include question marks for binding
161
				 * the values from the language item to the statement before they are
162
				 * sent to the database server. The number of question marks must
163
				 * be the same as the number of columns listed in the INSERT
164
				 * statement. The order of the columns must correspond to the
165
				 * order in the saveItems() method, so the correct values are
166
				 * bound to the columns.
167
				 *
168
				 * The SQL statement should conform to the ANSI standard to be
169
				 * compatible with most relational database systems. This also
170
				 * includes using double quotes for table and column names.
171
				 *
172
				 * @param string SQL statement for inserting records
173
				 * @since 2014.03
174
				 * @category Developer
175
				 * @see mshop/locale/manager/language/standard/update/ansi
176
				 * @see mshop/locale/manager/language/standard/delete/ansi
177
				 * @see mshop/locale/manager/language/standard/search/ansi
178
				 * @see mshop/locale/manager/language/standard/count/ansi
179
				 */
180
				$path = 'mshop/locale/manager/language/standard/insert';
181
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
182
			}
183
			else
184
			{
185
				/** mshop/locale/manager/language/standard/update/mysql
186
				 * Updates an existing language record in the database
187
				 *
188
				 * @see mshop/locale/manager/language/standard/update/ansi
189
				 */
190
191
				/** mshop/locale/manager/language/standard/update/ansi
192
				 * Updates an existing language record in the database
193
				 *
194
				 * The SQL statement must be a string suitable for being used as
195
				 * prepared statement. It must include question marks for binding
196
				 * the values from the language item to the statement before they are
197
				 * sent to the database server. The order of the columns must
198
				 * correspond to the order in the saveItems() method, so the
199
				 * correct values are bound to the columns.
200
				 *
201
				 * The SQL statement should conform to the ANSI standard to be
202
				 * compatible with most relational database systems. This also
203
				 * includes using double quotes for table and column names.
204
				 *
205
				 * @param string SQL statement for updating records
206
				 * @since 2014.03
207
				 * @category Developer
208
				 * @see mshop/locale/manager/language/standard/insert/ansi
209
				 * @see mshop/locale/manager/language/standard/delete/ansi
210
				 * @see mshop/locale/manager/language/standard/search/ansi
211
				 * @see mshop/locale/manager/language/standard/count/ansi
212
				 */
213
				$path = 'mshop/locale/manager/language/standard/update';
214
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
215
			}
216
217
			$idx = 1;
218
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
219
220
			foreach( $columns as $name => $entry ) {
221
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
222
			}
223
224
			$stmt->bind( $idx++, $item->getLabel() );
225
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
226
			$stmt->bind( $idx++, $item->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
227
			$stmt->bind( $idx++, $date ); // mtime
228
			$stmt->bind( $idx++, $context->getEditor() );
229
			// code and ID are identical after saving and ID is the flag to detect updates or inserts
230
			$stmt->bind( $idx++, $item->getCode() );
231
232
			if( $id === null ) {
233
				$stmt->bind( $idx++, $date ); // ctime
234
			}
235
236
			$stmt->execute()->finish();
237
238
			$item->setId( $item->getCode() ); // set modified flag to false
239
240
			$dbm->release( $conn, $dbname );
241
		}
242
		catch( \Exception $e )
243
		{
244
			$dbm->release( $conn, $dbname );
245
			throw $e;
246
		}
247
248
		return $item;
249
	}
250
251
252
	/**
253
	 * Removes multiple items specified by ids in the array.
254
	 *
255
	 * @param string[] $ids List of IDs
256
	 * @return \Aimeos\MShop\Locale\Manager\Currency\Iface Manager object for chaining method calls
257
	 */
258
	public function deleteItems( array $ids )
259
	{
260
		/** mshop/locale/manager/language/standard/delete/mysql
261
		 * Deletes the items matched by the given IDs from the database
262
		 *
263
		 * @see mshop/locale/manager/language/standard/delete/ansi
264
		 */
265
266
		/** mshop/locale/manager/language/standard/delete/ansi
267
		 * Deletes the items matched by the given IDs from the database
268
		 *
269
		 * Removes the language records specified by the given IDs from the
270
		 * locale database. The records must be from the site that is configured
271
		 * via the context item.
272
		 *
273
		 * The ":cond" placeholder is replaced by the name of the ID column and
274
		 * the given ID or list of IDs while the site ID is bound to the question
275
		 * mark.
276
		 *
277
		 * The SQL statement should conform to the ANSI standard to be
278
		 * compatible with most relational database systems. This also
279
		 * includes using double quotes for table and column names.
280
		 *
281
		 * @param string SQL statement for deleting items
282
		 * @since 2014.03
283
		 * @category Developer
284
		 * @see mshop/locale/manager/language/standard/insert/ansi
285
		 * @see mshop/locale/manager/language/standard/update/ansi
286
		 * @see mshop/locale/manager/language/standard/search/ansi
287
		 * @see mshop/locale/manager/language/standard/count/ansi
288
		 */
289
		$path = 'mshop/locale/manager/language/standard/delete';
290
291
		return $this->deleteItemsBase( $ids, $path );
292
	}
293
294
295
	/**
296
	 * Create a Language object from a given Language ID/Key.
297
	 *
298
	 * @param string $id Language id to create the Language object
299
	 * @param string[] $ref List of domains to fetch list items and referenced items for
300
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Returns the language item of the given id
301
	 * @param boolean $default Add default criteria
302
	 * @throws \Aimeos\MW\DB\Exception If language object couldn't be fetched
303
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
304
	 */
305
	public function getItem( $id, array $ref = [], $default = false )
306
	{
307
		return $this->getItemBase( 'locale.language.id', $id, $ref, $default );
308
	}
309
310
311
	/**
312
	 * Returns the available manager types
313
	 *
314
	 * @param boolean $withsub Return also the resource type of sub-managers if true
315
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
316
	 */
317
	public function getResourceType( $withsub = true )
318
	{
319
		$path = 'mshop/locale/manager/language/submanagers';
320
		return $this->getResourceTypeBase( 'locale/language', $path, [], $withsub );
321
	}
322
323
324
	/**
325
	 * Returns the attributes that can be used for searching.
326
	 *
327
	 * @param boolean $withsub Return also attributes of sub-managers if true
328
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
329
	 */
330
	public function getSearchAttributes( $withsub = true )
331
	{
332
		/** mshop/locale/manager/language/submanagers
333
		 * List of manager names that can be instantiated by the locale language manager
334
		 *
335
		 * Managers provide a generic interface to the underlying storage.
336
		 * Each manager has or can have sub-managers caring about particular
337
		 * aspects. Each of these sub-managers can be instantiated by its
338
		 * parent manager using the getSubManager() method.
339
		 *
340
		 * The search keys from sub-managers can be normally used in the
341
		 * manager as well. It allows you to search for items of the manager
342
		 * using the search keys of the sub-managers to further limit the
343
		 * retrieved list of items.
344
		 *
345
		 * @param array List of sub-manager names
346
		 * @since 2014.03
347
		 * @category Developer
348
		 */
349
		$path = 'mshop/locale/manager/language/submanagers';
350
351
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
352
	}
353
354
355
	/**
356
	 * Returns a new sub manager of the given type and name.
357
	 *
358
	 * @param string $manager Name of the sub manager type in lower case
359
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
360
	 * @return \Aimeos\MShop\Locale\Manager\Iface manager
361
	 */
362
	public function getSubManager( $manager, $name = null )
363
	{
364
		/** mshop/locale/manager/language/name
365
		 * Class name of the used locale language manager implementation
366
		 *
367
		 * Each default locale language manager can be replaced by an alternative imlementation.
368
		 * To use this implementation, you have to set the last part of the class
369
		 * name as configuration value so the manager factory knows which class it
370
		 * has to instantiate.
371
		 *
372
		 * For example, if the name of the default class is
373
		 *
374
		 *  \Aimeos\MShop\Locale\Manager\Language\Standard
375
		 *
376
		 * and you want to replace it with your own version named
377
		 *
378
		 *  \Aimeos\MShop\Locale\Manager\Language\Mylanguage
379
		 *
380
		 * then you have to set the this configuration option:
381
		 *
382
		 *  mshop/locale/manager/language/name = Mylanguage
383
		 *
384
		 * The value is the last part of your own class name and it's case sensitive,
385
		 * so take care that the configuration value is exactly named like the last
386
		 * part of the class name.
387
		 *
388
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
389
		 * characters are possible! You should always start the last part of the class
390
		 * name with an upper case character and continue only with lower case characters
391
		 * or numbers. Avoid chamel case names like "MyLanguage"!
392
		 *
393
		 * @param string Last part of the class name
394
		 * @since 2014.03
395
		 * @category Developer
396
		 */
397
398
		/** mshop/locale/manager/language/decorators/excludes
399
		 * Excludes decorators added by the "common" option from the locale language manager
400
		 *
401
		 * Decorators extend the functionality of a class by adding new aspects
402
		 * (e.g. log what is currently done), executing the methods of the underlying
403
		 * class only in certain conditions (e.g. only for logged in users) or
404
		 * modify what is returned to the caller.
405
		 *
406
		 * This option allows you to remove a decorator added via
407
		 * "mshop/common/manager/decorators/default" before they are wrapped
408
		 * around the locale language manager.
409
		 *
410
		 *  mshop/locale/manager/language/decorators/excludes = array( 'decorator1' )
411
		 *
412
		 * This would remove the decorator named "decorator1" from the list of
413
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
414
		 * "mshop/common/manager/decorators/default" for the locale language manager.
415
		 *
416
		 * @param array List of decorator names
417
		 * @since 2014.03
418
		 * @category Developer
419
		 * @see mshop/common/manager/decorators/default
420
		 * @see mshop/locale/manager/language/decorators/global
421
		 * @see mshop/locale/manager/language/decorators/local
422
		 */
423
424
		/** mshop/locale/manager/language/decorators/global
425
		 * Adds a list of globally available decorators only to the locale language manager
426
		 *
427
		 * Decorators extend the functionality of a class by adding new aspects
428
		 * (e.g. log what is currently done), executing the methods of the underlying
429
		 * class only in certain conditions (e.g. only for logged in users) or
430
		 * modify what is returned to the caller.
431
		 *
432
		 * This option allows you to wrap global decorators
433
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale language
434
		 * manager.
435
		 *
436
		 *  mshop/locale/manager/language/decorators/global = array( 'decorator1' )
437
		 *
438
		 * This would add the decorator named "decorator1" defined by
439
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
440
		 * language manager.
441
		 *
442
		 * @param array List of decorator names
443
		 * @since 2014.03
444
		 * @category Developer
445
		 * @see mshop/common/manager/decorators/default
446
		 * @see mshop/locale/manager/language/decorators/excludes
447
		 * @see mshop/locale/manager/language/decorators/local
448
		 */
449
450
		/** mshop/locale/manager/language/decorators/local
451
		 * Adds a list of local decorators only to the locale language manager
452
		 *
453
		 * Decorators extend the functionality of a class by adding new aspects
454
		 * (e.g. log what is currently done), executing the methods of the underlying
455
		 * class only in certain conditions (e.g. only for logged in users) or
456
		 * modify what is returned to the caller.
457
		 *
458
		 * This option allows you to wrap local decorators
459
		 * ("\Aimeos\MShop\Locale\Manager\Language\Decorator\*") around the locale
460
		 * language manager.
461
		 *
462
		 *  mshop/locale/manager/language/decorators/local = array( 'decorator2' )
463
		 *
464
		 * This would add the decorator named "decorator2" defined by
465
		 * "\Aimeos\MShop\Locale\Manager\Language\Decorator\Decorator2" only to the
466
		 * locale language manager.
467
		 *
468
		 * @param array List of decorator names
469
		 * @since 2014.03
470
		 * @category Developer
471
		 * @see mshop/common/manager/decorators/default
472
		 * @see mshop/locale/manager/language/decorators/excludes
473
		 * @see mshop/locale/manager/language/decorators/global
474
		 */
475
476
		return $this->getSubManagerBase( 'locale', 'language/' . $manager, $name );
477
	}
478
479
480
	/**
481
	 * Returns the item specified by its code and domain/type if necessary
482
	 *
483
	 * @param string $code Code of the item
484
	 * @param string[] $ref List of domains to fetch list items and referenced items for
485
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
486
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
487
	 * @param boolean $default True to add default criteria
488
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
489
	 */
490
	public function findItem( $code, array $ref = [], $domain = null, $type = null, $default = false )
491
	{
492
		return $this->findItemBase( array( 'locale.language.id' => $code ), $ref, $default );
493
	}
494
495
496
	/**
497
	 * Searches for language items matching the given criteria.
498
	 *
499
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
500
	 * @param string[] $ref List of domains to fetch list items and referenced items for
501
	 * @param integer|null &$total Number of items that are available in total
502
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface[] List of locale language items
503
	 */
504
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
505
	{
506
		$items = [];
507
		$context = $this->getContext();
508
509
		$dbm = $context->getDatabaseManager();
510
		$dbname = $this->getResourceName();
511
		$conn = $dbm->acquire( $dbname );
512
513
		try
514
		{
515
			$attributes = $this->getObject()->getSearchAttributes();
516
			$types = $this->getSearchTypes( $attributes );
517
			$translations = $this->getSearchTranslations( $attributes );
518
			$columns = $search->translate( $search->getSortations(), $translations );
519
520
			$find = array( ':cond', ':order', ':columns', ':start', ':size' );
521
			$replace = array(
522
				$search->getConditionSource( $types, $translations ),
523
				$search->getSortationSource( $types, $translations ),
524
				( $columns ? ', ' . implode( ',', $columns ) : '' ),
525
				$search->getSliceStart(),
526
				$search->getSliceSize(),
527
			);
528
529
			/** mshop/locale/manager/language/standard/search/mysql
530
			 * Retrieves the records matched by the given criteria in the database
531
			 *
532
			 * @see mshop/locale/manager/language/standard/search/ansi
533
			 */
534
535
			/** mshop/locale/manager/language/standard/search/ansi
536
			 * Retrieves the records matched by the given criteria in the database
537
			 *
538
			 * Fetches the records matched by the given criteria from the attribute
539
			 * database. The records must be from one of the sites that are
540
			 * configured via the context item. If the current site is part of
541
			 * a tree of sites, the SELECT statement can retrieve all records
542
			 * from the current site and the complete sub-tree of sites.
543
			 *
544
			 * As the records can normally be limited by criteria from sub-managers,
545
			 * their tables must be joined in the SQL context. This is done by
546
			 * using the "internaldeps" property from the definition of the ID
547
			 * column of the sub-managers. These internal dependencies specify
548
			 * the JOIN between the tables and the used columns for joining. The
549
			 * ":joins" placeholder is then replaced by the JOIN strings from
550
			 * the sub-managers.
551
			 *
552
			 * To limit the records matched, conditions can be added to the given
553
			 * criteria object. It can contain comparisons like column names that
554
			 * must match specific values which can be combined by AND, OR or NOT
555
			 * operators. The resulting string of SQL conditions replaces the
556
			 * ":cond" placeholder before the statement is sent to the database
557
			 * server.
558
			 *
559
			 * If the records that are retrieved should be ordered by one or more
560
			 * columns, the generated string of column / sort direction pairs
561
			 * replaces the ":order" placeholder. In case no ordering is required,
562
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
563
			 * markers is removed to speed up retrieving the records. Columns of
564
			 * sub-managers can also be used for ordering the result set but then
565
			 * no index can be used.
566
			 *
567
			 * The number of returned records can be limited and can start at any
568
			 * number between the begining and the end of the result set. For that
569
			 * the ":size" and ":start" placeholders are replaced by the
570
			 * corresponding values from the criteria object. The default values
571
			 * are 0 for the start and 100 for the size value.
572
			 *
573
			 * The SQL statement should conform to the ANSI standard to be
574
			 * compatible with most relational database systems. This also
575
			 * includes using double quotes for table and column names.
576
			 *
577
			 * @param string SQL statement for searching items
578
			 * @since 2014.03
579
			 * @category Developer
580
			 * @see mshop/locale/manager/language/standard/insert/ansi
581
			 * @see mshop/locale/manager/language/standard/update/ansi
582
			 * @see mshop/locale/manager/language/standard/delete/ansi
583
			 * @see mshop/locale/manager/language/standard/count/ansi
584
			 */
585
			$path = 'mshop/locale/manager/language/standard/search';
586
587
			$sql = $this->getSqlConfig( $path );
588
			$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
589
590
			try
591
			{
592
				while( ( $row = $results->fetch() ) !== false ) {
593
					$items[(string) $row['locale.language.id']] = $this->createItemBase( $row );
594
				}
595
			}
596
			catch( \Exception $e )
597
			{
598
				$results->finish();
599
				throw $e;
600
			}
601
602
			if( $total !== null ) {
603
				$total = $this->getTotal( $conn, $find, $replace );
604
			}
605
606
			$dbm->release( $conn, $dbname );
607
		}
608
		catch( \Exception $e )
609
		{
610
			$dbm->release( $conn, $dbname );
611
			throw $e;
612
		}
613
614
		return $items;
615
	}
616
617
618
	/**
619
	 * Creates a search critera object
620
	 *
621
	 * @param boolean $default Add default criteria (optional)
622
	 * @return \Aimeos\MW\Criteria\Iface New search criteria object
623
	 */
624
	public function createSearch( $default = false )
625
	{
626
		if( $default === true ) {
627
			return $this->createSearchBase( 'locale.language' );
628
		}
629
630
		return parent::createSearch();
631
	}
632
633
634
	/**
635
	 * Returns the search results for the given SQL statement.
636
	 *
637
	 * @param \Aimeos\MW\DB\Connection\Iface $conn Database connection
638
	 * @param string $sql SQL statement
639
	 * @return \Aimeos\MW\DB\Result\Iface Search result object
640
	 */
641
	protected function getSearchResults( \Aimeos\MW\DB\Connection\Iface $conn, $sql )
642
	{
643
		$time = microtime( true );
644
645
		$stmt = $conn->create( $sql );
646
		$result = $stmt->execute();
647
648
		$msg = [
649
			'time' => ( microtime( true ) - $time ) * 1000,
650
			'class' => get_class( $this ),
651
			'stmt' => (string) $stmt,
652
		];
653
		$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::DEBUG, 'core/sql' );
654
655
		return $result;
656
	}
657
658
659
	/**
660
	 * Create new item object initialized with given parameters.
661
	 *
662
	 * @param array $data Associative list of item key/value pairs
663
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Language item
664
	 */
665
	protected function createItemBase( array $data = [] )
666
	{
667
		return new \Aimeos\MShop\Locale\Item\Language\Standard( $data );
668
	}
669
670
671
	/**
672
	 * Returns the total number of items found for the conditions
673
	 *
674
	 * @param \Aimeos\MW\DB\Connection\Iface $conn Database connection
675
	 * @param string[] $find List of markers that should be replaced in the SQL statement
676
	 * @param string[] $replace List of replacements for the markers in the SQL statement
677
	 * @throws \Aimeos\MShop\Locale\Exception If no total value was found
678
	 * @return integer Total number of found items
679
	 */
680
	protected function getTotal( \Aimeos\MW\DB\Connection\Iface $conn, array $find, array $replace )
681
	{
682
		/** mshop/locale/manager/language/standard/count/mysql
683
		 * Counts the number of records matched by the given criteria in the database
684
		 *
685
		 * @see mshop/locale/manager/language/standard/count/ansi
686
		 */
687
688
		/** mshop/locale/manager/language/standard/count/ansi
689
		 * Counts the number of records matched by the given criteria in the database
690
		 *
691
		 * Counts all records matched by the given criteria from the attribute
692
		 * database. The records must be from one of the sites that are
693
		 * configured via the context item. If the current site is part of
694
		 * a tree of sites, the statement can count all records from the
695
		 * current site and the complete sub-tree of sites.
696
		 *
697
		 * As the records can normally be limited by criteria from sub-managers,
698
		 * their tables must be joined in the SQL context. This is done by
699
		 * using the "internaldeps" property from the definition of the ID
700
		 * column of the sub-managers. These internal dependencies specify
701
		 * the JOIN between the tables and the used columns for joining. The
702
		 * ":joins" placeholder is then replaced by the JOIN strings from
703
		 * the sub-managers.
704
		 *
705
		 * To limit the records matched, conditions can be added to the given
706
		 * criteria object. It can contain comparisons like column names that
707
		 * must match specific values which can be combined by AND, OR or NOT
708
		 * operators. The resulting string of SQL conditions replaces the
709
		 * ":cond" placeholder before the statement is sent to the database
710
		 * server.
711
		 *
712
		 * Both, the strings for ":joins" and for ":cond" are the same as for
713
		 * the "search" SQL statement.
714
		 *
715
		 * Contrary to the "search" statement, it doesn't return any records
716
		 * but instead the number of records that have been found. As counting
717
		 * thousands of records can be a long running task, the maximum number
718
		 * of counted records is limited for performance reasons.
719
		 *
720
		 * The SQL statement should conform to the ANSI standard to be
721
		 * compatible with most relational database systems. This also
722
		 * includes using double quotes for table and column names.
723
		 *
724
		 * @param string SQL statement for counting items
725
		 * @since 2014.03
726
		 * @category Developer
727
		 * @see mshop/locale/manager/language/standard/insert/ansi
728
		 * @see mshop/locale/manager/language/standard/update/ansi
729
		 * @see mshop/locale/manager/language/standard/delete/ansi
730
		 * @see mshop/locale/manager/language/standard/search/ansi
731
		 */
732
		$path = 'mshop/locale/manager/language/standard/count';
733
734
		$sql = $this->getSqlConfig( $path );
735
		$results = $this->getSearchResults( $conn, str_replace( $find, $replace, $sql ) );
736
737
		$row = $results->fetch();
738
		$results->finish();
739
740
		if( $row === false ) {
741
			throw new \Aimeos\MShop\Locale\Exception( 'No total results value found' );
742
		}
743
744
		return $row['count'];
745
	}
746
}
747