Passed
Push — master ( 2e9af5...8d773b )
by Aimeos
05:58
created

Standard::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Locale
8
 */
9
10
11
namespace Aimeos\MShop\Locale\Manager\Language;
12
13
14
/**
15
 * Default implementation for managing languages.
16
 *
17
 * @package MShop
18
 * @subpackage Locale
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Locale\Manager\Language\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private array $searchConfig = [
25
		'locale.language.id' => [
26
			'label' => 'Language ID',
27
			'internaldeps' => [ 'LEFT JOIN "mshop_locale_language" AS mlocla ON (mloc."langid" = mlocla."id")' ],
28
			'internalcode' => 'mlocla."id"',
29
			'public' => false,
30
		],
31
		'locale.language.label' => [
32
			'label' => 'Language label',
33
			'internalcode' => 'mlocla."label"',
34
		],
35
		'locale.language.code' => [
36
			'label' => 'Language code',
37
			'internalcode' => 'mlocla."id"',
38
		],
39
		'locale.language.status' => [
40
			'label' => 'Language status',
41
			'internalcode' => 'mlocla."status"',
42
			'type' => 'int',
43
		],
44
		'locale.language.ctime' => [
45
			'label' => 'Language create date/time',
46
			'internalcode' => 'mlocla."ctime"',
47
			'type' => 'datetime',
48
			'public' => false,
49
		],
50
		'locale.language.mtime' => [
51
			'label' => 'Language modify date/time',
52
			'internalcode' => 'mlocla."mtime"',
53
			'type' => 'datetime',
54
			'public' => false,
55
		],
56
		'locale.language.editor' => [
57
			'label' => 'Language editor',
58
			'internalcode' => 'mlocla."editor"',
59
			'public' => false,
60
		],
61
	];
62
63
64
	/**
65
	 * Removes old entries from the storage.
66
	 *
67
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
68
	 * @return \Aimeos\MShop\Locale\Manager\Language\Iface Manager object for chaining method calls
69
	 */
70
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
71
	{
72
		return $this;
73
	}
74
75
76
	/**
77
	 * Creates a new empty item instance
78
	 *
79
	 * @param array $values Values the item should be initialized with
80
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface New locale language item object
81
	 */
82
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
83
	{
84
		return new \Aimeos\MShop\Locale\Item\Language\Standard( 'locale.language.', $values );
85
	}
86
87
88
	/**
89
	 * Removes multiple items.
90
	 *
91
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
92
	 * @return \Aimeos\MShop\Locale\Manager\Language\Iface Manager object for chaining method calls
93
	 */
94
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
95
	{
96
		/** mshop/locale/manager/language/delete/mysql
97
		 * Deletes the items matched by the given IDs from the database
98
		 *
99
		 * @see mshop/locale/manager/language/delete/ansi
100
		 */
101
102
		/** mshop/locale/manager/language/delete/ansi
103
		 * Deletes the items matched by the given IDs from the database
104
		 *
105
		 * Removes the language records specified by the given IDs from the
106
		 * locale database. The records must be from the site that is configured
107
		 * via the context item.
108
		 *
109
		 * The ":cond" placeholder is replaced by the name of the ID column and
110
		 * the given ID or list of IDs while the site ID is bound to the question
111
		 * mark.
112
		 *
113
		 * The SQL statement should conform to the ANSI standard to be
114
		 * compatible with most relational database systems. This also
115
		 * includes using double quotes for table and column names.
116
		 *
117
		 * @param string SQL statement for deleting items
118
		 * @since 2015.10
119
		 * @see mshop/locale/manager/language/insert/ansi
120
		 * @see mshop/locale/manager/language/update/ansi
121
		 * @see mshop/locale/manager/language/search/ansi
122
		 * @see mshop/locale/manager/language/count/ansi
123
		 */
124
		$path = 'mshop/locale/manager/language/delete';
125
126
		return $this->deleteItemsBase( $itemIds, $path, false );
127
	}
128
129
130
	/**
131
	 * Returns the attributes that can be used for searching.
132
	 *
133
	 * @param bool $withsub Return also attributes of sub-managers if true
134
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
135
	 */
136
	public function getSearchAttributes( bool $withsub = true ) : array
137
	{
138
		/** mshop/locale/manager/language/submanagers
139
		 * List of manager names that can be instantiated by the locale language manager
140
		 *
141
		 * Managers provide a generic interface to the underlying storage.
142
		 * Each manager has or can have sub-managers caring about particular
143
		 * aspects. Each of these sub-managers can be instantiated by its
144
		 * parent manager using the getSubManager() method.
145
		 *
146
		 * The search keys from sub-managers can be normally used in the
147
		 * manager as well. It allows you to search for items of the manager
148
		 * using the search keys of the sub-managers to further limit the
149
		 * retrieved list of items.
150
		 *
151
		 * @param array List of sub-manager names
152
		 * @since 2015.10
153
		 */
154
		$path = 'mshop/locale/manager/language/submanagers';
155
156
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
157
	}
158
159
160
	/**
161
	 * Returns the item specified by its code and domain/type if necessary
162
	 *
163
	 * @param string $code Code of the item
164
	 * @param string[] $ref List of domains to fetch list items and referenced items for
165
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
166
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
167
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
168
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
169
	 */
170
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
171
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
172
	{
173
		return $this->findBase( array( 'locale.language.id' => $code ), $ref, $default );
174
	}
175
176
177
	/**
178
	 * Creates a filter object.
179
	 *
180
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
181
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
182
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
183
	 */
184
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
185
	{
186
		return $this->filterBase( 'locale.language', $default );
187
	}
188
189
190
	/**
191
	 * Returns the site coditions for the search request
192
	 *
193
	 * @param string[] $keys Sorted list of criteria keys
194
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes Associative list of search keys and criteria attribute items as values
195
	 * @param int $sitelevel Site level constant from \Aimeos\MShop\Locale\Manager\Base
196
	 * @return \Aimeos\Base\Criteria\Expression\Iface[] List of search conditions
197
	 */
198
	protected function getSiteConditions( array $keys, array $attributes, int $sitelevel ) : array
199
	{
200
		return [];
201
	}
202
203
204
	/**
205
	 * Returns the prefix for the item properties and search keys.
206
	 *
207
	 * @return string Prefix for the item properties and search keys
208
	 */
209
	protected function prefix() : string
210
	{
211
		return 'locale.language.';
212
	}
213
214
215
	/**
216
	 * Saves the language object to the storage.
217
	 *
218
	 * @param \Aimeos\MShop\Locale\Item\Language\Iface $item Language object
219
	 * @param bool $fetch True if the new ID should be returned in the item
220
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface $item Updated item including the generated ID
221
	 */
222
	protected function saveBase( \Aimeos\MShop\Common\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Common\Item\Iface
223
	{
224
		if( !$item->isModified() ) {
225
			return $item;
226
		}
227
228
		$context = $this->context();
229
		$conn = $context->db( $this->getResourceName() );
230
231
		$id = $item->getId();
232
		$columns = $this->object()->getSaveAttributes();
233
234
		if( $id === null )
235
		{
236
			/** mshop/locale/manager/language/insert/mysql
237
			 * Inserts a new language record into the database table
238
			 *
239
			 * @see mshop/locale/manager/language/insert/ansi
240
			 */
241
242
			/** mshop/locale/manager/language/insert/ansi
243
			 * Inserts a new language record into the database table
244
			 *
245
			 * The SQL statement must be a string suitable for being used as
246
			 * prepared statement. It must include question marks for binding
247
			 * the values from the language item to the statement before they are
248
			 * sent to the database server. The number of question marks must
249
			 * be the same as the number of columns listed in the INSERT
250
			 * statement. The order of the columns must correspond to the
251
			 * order in the save() method, so the correct values are
252
			 * bound to the columns.
253
			 *
254
			 * The SQL statement should conform to the ANSI standard to be
255
			 * compatible with most relational database systems. This also
256
			 * includes using double quotes for table and column names.
257
			 *
258
			 * @param string SQL statement for inserting records
259
			 * @since 2015.10
260
			 * @see mshop/locale/manager/language/update/ansi
261
			 * @see mshop/locale/manager/language/delete/ansi
262
			 * @see mshop/locale/manager/language/search/ansi
263
			 * @see mshop/locale/manager/language/count/ansi
264
			 */
265
			$path = 'mshop/locale/manager/language/insert';
266
			$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

266
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
267
		}
268
		else
269
		{
270
			/** mshop/locale/manager/language/update/mysql
271
			 * Updates an existing language record in the database
272
			 *
273
			 * @see mshop/locale/manager/language/update/ansi
274
			 */
275
276
			/** mshop/locale/manager/language/update/ansi
277
			 * Updates an existing language record in the database
278
			 *
279
			 * The SQL statement must be a string suitable for being used as
280
			 * prepared statement. It must include question marks for binding
281
			 * the values from the language item to the statement before they are
282
			 * sent to the database server. The order of the columns must
283
			 * correspond to the order in the save() method, so the
284
			 * correct values are bound to the columns.
285
			 *
286
			 * The SQL statement should conform to the ANSI standard to be
287
			 * compatible with most relational database systems. This also
288
			 * includes using double quotes for table and column names.
289
			 *
290
			 * @param string SQL statement for updating records
291
			 * @since 2015.10
292
			 * @see mshop/locale/manager/language/insert/ansi
293
			 * @see mshop/locale/manager/language/delete/ansi
294
			 * @see mshop/locale/manager/language/search/ansi
295
			 * @see mshop/locale/manager/language/count/ansi
296
			 */
297
			$path = 'mshop/locale/manager/language/update';
298
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
299
		}
300
301
		$idx = 1;
302
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
303
304
		foreach( $columns as $name => $entry ) {
305
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
306
		}
307
308
		$stmt->bind( $idx++, $item->getLabel() );
309
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
310
		$stmt->bind( $idx++, $context->datetime() ); // mtime
311
		$stmt->bind( $idx++, $context->editor() );
312
		// code and ID are identical after saving and ID is the flag to detect updates or inserts
313
		$stmt->bind( $idx++, $item->getCode() );
314
315
		if( $id === null ) {
316
			$stmt->bind( $idx++, $context->datetime() ); // ctime
317
		}
318
319
		$stmt->execute()->finish();
320
321
		$item->setId( $item->getCode() ); // set modified flag to false
322
323
		return $item;
324
	}
325
326
327
	/** mshop/locale/manager/language/name
328
	 * Class name of the used locale language manager implementation
329
	 *
330
	 * Each default locale language manager can be replaced by an alternative imlementation.
331
	 * To use this implementation, you have to set the last part of the class
332
	 * name as configuration value so the manager factory knows which class it
333
	 * has to instantiate.
334
	 *
335
	 * For example, if the name of the default class is
336
	 *
337
	 *  \Aimeos\MShop\Locale\Manager\Language\Standard
338
	 *
339
	 * and you want to replace it with your own version named
340
	 *
341
	 *  \Aimeos\MShop\Locale\Manager\Language\Mylanguage
342
	 *
343
	 * then you have to set the this configuration option:
344
	 *
345
	 *  mshop/locale/manager/language/name = Mylanguage
346
	 *
347
	 * The value is the last part of your own class name and it's case sensitive,
348
	 * so take care that the configuration value is exactly named like the last
349
	 * part of the class name.
350
	 *
351
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
352
	 * characters are possible! You should always start the last part of the class
353
	 * name with an upper case character and continue only with lower case characters
354
	 * or numbers. Avoid chamel case names like "MyLanguage"!
355
	 *
356
	 * @param string Last part of the class name
357
	 * @since 2015.10
358
	 */
359
360
	/** mshop/locale/manager/language/decorators/excludes
361
	 * Excludes decorators added by the "common" option from the locale language manager
362
	 *
363
	 * Decorators extend the functionality of a class by adding new aspects
364
	 * (e.g. log what is currently done), executing the methods of the underlying
365
	 * class only in certain conditions (e.g. only for logged in users) or
366
	 * modify what is returned to the caller.
367
	 *
368
	 * This option allows you to remove a decorator added via
369
	 * "mshop/common/manager/decorators/default" before they are wrapped
370
	 * around the locale language manager.
371
	 *
372
	 *  mshop/locale/manager/language/decorators/excludes = array( 'decorator1' )
373
	 *
374
	 * This would remove the decorator named "decorator1" from the list of
375
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
376
	 * "mshop/common/manager/decorators/default" for the locale language manager.
377
	 *
378
	 * @param array List of decorator names
379
	 * @since 2015.10
380
	 * @see mshop/common/manager/decorators/default
381
	 * @see mshop/locale/manager/language/decorators/global
382
	 * @see mshop/locale/manager/language/decorators/local
383
	 */
384
385
	/** mshop/locale/manager/language/decorators/global
386
	 * Adds a list of globally available decorators only to the locale language manager
387
	 *
388
	 * Decorators extend the functionality of a class by adding new aspects
389
	 * (e.g. log what is currently done), executing the methods of the underlying
390
	 * class only in certain conditions (e.g. only for logged in users) or
391
	 * modify what is returned to the caller.
392
	 *
393
	 * This option allows you to wrap global decorators
394
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the locale language
395
	 * manager.
396
	 *
397
	 *  mshop/locale/manager/language/decorators/global = array( 'decorator1' )
398
	 *
399
	 * This would add the decorator named "decorator1" defined by
400
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the locale
401
	 * language manager.
402
	 *
403
	 * @param array List of decorator names
404
	 * @since 2015.10
405
	 * @see mshop/common/manager/decorators/default
406
	 * @see mshop/locale/manager/language/decorators/excludes
407
	 * @see mshop/locale/manager/language/decorators/local
408
	 */
409
410
	/** mshop/locale/manager/language/decorators/local
411
	 * Adds a list of local decorators only to the locale language manager
412
	 *
413
	 * Decorators extend the functionality of a class by adding new aspects
414
	 * (e.g. log what is currently done), executing the methods of the underlying
415
	 * class only in certain conditions (e.g. only for logged in users) or
416
	 * modify what is returned to the caller.
417
	 *
418
	 * This option allows you to wrap local decorators
419
	 * ("\Aimeos\MShop\Locale\Manager\Language\Decorator\*") around the locale
420
	 * language manager.
421
	 *
422
	 *  mshop/locale/manager/language/decorators/local = array( 'decorator2' )
423
	 *
424
	 * This would add the decorator named "decorator2" defined by
425
	 * "\Aimeos\MShop\Locale\Manager\Language\Decorator\Decorator2" only to the
426
	 * locale language manager.
427
	 *
428
	 * @param array List of decorator names
429
	 * @since 2015.10
430
	 * @see mshop/common/manager/decorators/default
431
	 * @see mshop/locale/manager/language/decorators/excludes
432
	 * @see mshop/locale/manager/language/decorators/global
433
	 */
434
435
	/** mshop/locale/manager/language/search/mysql
436
	 * Retrieves the records matched by the given criteria in the database
437
	 *
438
	 * @see mshop/locale/manager/language/search/ansi
439
	 */
440
441
	/** mshop/locale/manager/language/search/ansi
442
	 * Retrieves the records matched by the given criteria in the database
443
	 *
444
	 * Fetches the records matched by the given criteria from the attribute
445
	 * database. The records must be from one of the sites that are
446
	 * configured via the context item. If the current site is part of
447
	 * a tree of sites, the SELECT statement can retrieve all records
448
	 * from the current site and the complete sub-tree of sites.
449
	 *
450
	 * As the records can normally be limited by criteria from sub-managers,
451
	 * their tables must be joined in the SQL context. This is done by
452
	 * using the "internaldeps" property from the definition of the ID
453
	 * column of the sub-managers. These internal dependencies specify
454
	 * the JOIN between the tables and the used columns for joining. The
455
	 * ":joins" placeholder is then replaced by the JOIN strings from
456
	 * the sub-managers.
457
	 *
458
	 * To limit the records matched, conditions can be added to the given
459
	 * criteria object. It can contain comparisons like column names that
460
	 * must match specific values which can be combined by AND, OR or NOT
461
	 * operators. The resulting string of SQL conditions replaces the
462
	 * ":cond" placeholder before the statement is sent to the database
463
	 * server.
464
	 *
465
	 * If the records that are retrieved should be ordered by one or more
466
	 * columns, the generated string of column / sort direction pairs
467
	 * replaces the ":order" placeholder. Columns of
468
	 * sub-managers can also be used for ordering the result set but then
469
	 * no index can be used.
470
	 *
471
	 * The number of returned records can be limited and can start at any
472
	 * number between the begining and the end of the result set. For that
473
	 * the ":size" and ":start" placeholders are replaced by the
474
	 * corresponding values from the criteria object. The default values
475
	 * are 0 for the start and 100 for the size value.
476
	 *
477
	 * The SQL statement should conform to the ANSI standard to be
478
	 * compatible with most relational database systems. This also
479
	 * includes using double quotes for table and column names.
480
	 *
481
	 * @param string SQL statement for searching items
482
	 * @since 2015.10
483
	 * @see mshop/locale/manager/language/insert/ansi
484
	 * @see mshop/locale/manager/language/update/ansi
485
	 * @see mshop/locale/manager/language/delete/ansi
486
	 * @see mshop/locale/manager/language/count/ansi
487
	 */
488
489
	/** mshop/locale/manager/language/count/mysql
490
	 * Counts the number of records matched by the given criteria in the database
491
	 *
492
	 * @see mshop/locale/manager/language/count/ansi
493
	 */
494
495
	/** mshop/locale/manager/language/count/ansi
496
	 * Counts the number of records matched by the given criteria in the database
497
	 *
498
	 * Counts all records matched by the given criteria from the attribute
499
	 * database. The records must be from one of the sites that are
500
	 * configured via the context item. If the current site is part of
501
	 * a tree of sites, the statement can count all records from the
502
	 * current site and the complete sub-tree of sites.
503
	 *
504
	 * As the records can normally be limited by criteria from sub-managers,
505
	 * their tables must be joined in the SQL context. This is done by
506
	 * using the "internaldeps" property from the definition of the ID
507
	 * column of the sub-managers. These internal dependencies specify
508
	 * the JOIN between the tables and the used columns for joining. The
509
	 * ":joins" placeholder is then replaced by the JOIN strings from
510
	 * the sub-managers.
511
	 *
512
	 * To limit the records matched, conditions can be added to the given
513
	 * criteria object. It can contain comparisons like column names that
514
	 * must match specific values which can be combined by AND, OR or NOT
515
	 * operators. The resulting string of SQL conditions replaces the
516
	 * ":cond" placeholder before the statement is sent to the database
517
	 * server.
518
	 *
519
	 * Both, the strings for ":joins" and for ":cond" are the same as for
520
	 * the "search" SQL statement.
521
	 *
522
	 * Contrary to the "search" statement, it doesn't return any records
523
	 * but instead the number of records that have been found. As counting
524
	 * thousands of records can be a long running task, the maximum number
525
	 * of counted records is limited for performance reasons.
526
	 *
527
	 * The SQL statement should conform to the ANSI standard to be
528
	 * compatible with most relational database systems. This also
529
	 * includes using double quotes for table and column names.
530
	 *
531
	 * @param string SQL statement for counting items
532
	 * @since 2015.10
533
	 * @see mshop/locale/manager/language/insert/ansi
534
	 * @see mshop/locale/manager/language/update/ansi
535
	 * @see mshop/locale/manager/language/delete/ansi
536
	 * @see mshop/locale/manager/language/search/ansi
537
	 */
538
}
539