Standard::saveItem()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 159
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 2
b 0
f 0
nc 17
nop 2
dl 0
loc 159
rs 8.7537

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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2023
6
 * @package MShop
7
 * @subpackage Cms
8
 */
9
10
11
namespace Aimeos\MShop\Cms\Manager;
12
13
14
/**
15
 * Default cms manager implementation
16
 *
17
 * @package MShop
18
 * @subpackage Cms
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Cms\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	/** mshop/cms/manager/name
25
	 * Class name of the used cms manager implementation
26
	 *
27
	 * Each default manager can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the manager factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\MShop\Cms\Manager\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\MShop\Cms\Manager\Mymanager
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  mshop/cms/manager/name = Mymanager
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyManager"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2020.10
55
	 * @category Developer
56
	 */
57
58
	/** mshop/cms/manager/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the cms manager
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "mshop/common/manager/decorators/default" before they are wrapped
68
	 * around the cms manager.
69
	 *
70
	 *  mshop/cms/manager/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
74
	 * "mshop/common/manager/decorators/default" for the cms manager.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2020.10
78
	 * @category Developer
79
	 * @see mshop/common/manager/decorators/default
80
	 * @see mshop/cms/manager/decorators/global
81
	 * @see mshop/cms/manager/decorators/local
82
	 */
83
84
	/** mshop/cms/manager/decorators/global
85
	 * Adds a list of globally available decorators only to the cms manager
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the cms manager.
94
	 *
95
	 *  mshop/cms/manager/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the cms
99
	 * manager.
100
	 *
101
	 * @param array List of decorator names
102
	 * @since 2020.10
103
	 * @category Developer
104
	 * @see mshop/common/manager/decorators/default
105
	 * @see mshop/cms/manager/decorators/excludes
106
	 * @see mshop/cms/manager/decorators/local
107
	 */
108
109
	/** mshop/cms/manager/decorators/local
110
	 * Adds a list of local decorators only to the cms manager
111
	 *
112
	 * Decorators extend the functionality of a class by adding new aspects
113
	 * (e.g. log what is currently done), executing the methods of the underlying
114
	 * class only in certain conditions (e.g. only for logged in users) or
115
	 * modify what is returned to the caller.
116
	 *
117
	 * This option allows you to wrap local decorators
118
	 * ("\Aimeos\MShop\Cms\Manager\Decorator\*") around the cms manager.
119
	 *
120
	 *  mshop/cms/manager/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\MShop\Cms\Manager\Decorator\Decorator2" only to the cms
124
	 * manager.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2020.10
128
	 * @category Developer
129
	 * @see mshop/common/manager/decorators/default
130
	 * @see mshop/cms/manager/decorators/excludes
131
	 * @see mshop/cms/manager/decorators/global
132
	 */
133
134
135
	use \Aimeos\MShop\Common\Manager\ListsRef\Traits;
136
137
138
	private array $searchConfig = array(
139
		'cms.id' => array(
140
			'code' => 'cms.id',
141
			'internalcode' => 'mcms."id"',
142
			'label' => 'ID',
143
			'type' => 'int',
144
			'public' => false,
145
		),
146
		'cms.siteid' => array(
147
			'code' => 'cms.siteid',
148
			'internalcode' => 'mcms."siteid"',
149
			'label' => 'Site ID',
150
			'type' => 'string',
151
			'public' => false,
152
		),
153
		'cms.url' => array(
154
			'code' => 'cms.url',
155
			'internalcode' => 'mcms."url"',
156
			'label' => 'Type',
157
			'type' => 'string',
158
		),
159
		'cms.label' => array(
160
			'code' => 'cms.label',
161
			'internalcode' => 'mcms."label"',
162
			'label' => 'Label',
163
			'type' => 'string',
164
		),
165
		'cms.status' => array(
166
			'code' => 'cms.status',
167
			'internalcode' => 'mcms."status"',
168
			'label' => 'Status',
169
			'type' => 'int',
170
		),
171
		'cms.ctime' => array(
172
			'code' => 'cms.ctime',
173
			'internalcode' => 'mcms."ctime"',
174
			'label' => 'create date/time',
175
			'type' => 'datetime',
176
			'public' => false,
177
		),
178
		'cms.mtime' => array(
179
			'code' => 'cms.mtime',
180
			'internalcode' => 'mcms."mtime"',
181
			'label' => 'modify date/time',
182
			'type' => 'datetime',
183
			'public' => false,
184
		),
185
		'cms.editor' => array(
186
			'code' => 'cms.editor',
187
			'internalcode' => 'mcms."editor"',
188
			'label' => 'editor',
189
			'type' => 'string',
190
			'public' => false,
191
		),
192
		'cms:has' => array(
193
			'code' => 'cms:has()',
194
			'internalcode' => ':site AND :key AND mcmsli."id"',
195
			'internaldeps' => ['LEFT JOIN "mshop_cms_list" AS mcmsli ON ( mcmsli."parentid" = mcms."id" )'],
196
			'label' => 'Cms has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
197
			'type' => 'null',
198
			'public' => false,
199
		),
200
	);
201
202
203
	/**
204
	 * Initializes the object.
205
	 *
206
	 * @param \Aimeos\MShop\ContextIface $context Context object
207
	 */
208
	public function __construct( \Aimeos\MShop\ContextIface $context )
209
	{
210
		parent::__construct( $context );
211
212
		/** madmin/cms/manager/resource
213
		 * Name of the database connection resource to use
214
		 *
215
		 * You can configure a different database connection for each data domain
216
		 * and if no such connection name exists, the "db" connection will be used.
217
		 * It's also possible to use the same database connection for different
218
		 * data domains by configuring the same connection name using this setting.
219
		 *
220
		 * @param string Database connection name
221
		 * @since 2023.04
222
		 */
223
		$this->setResourceName( $context->config()->get( 'mshop/cms/manager/resource', 'db-cms' ) );
224
225
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ONE;
226
		$level = $context->config()->get( 'mshop/cms/manager/sitemode', $level );
227
228
229
		$this->searchConfig['cms:has']['function'] = function( &$source, array $params ) use ( $level ) {
230
231
			$keys = [];
232
233
			foreach( (array) ( $params[1] ?? '' ) as $type ) {
234
				foreach( (array) ( $params[2] ?? '' ) as $id ) {
235
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
236
				}
237
			}
238
239
			$sitestr = $this->siteString( 'mcmsli."siteid"', $level );
240
			$keystr = $this->toExpression( 'mcmsli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
241
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
242
243
			return $params;
244
		};
245
	}
246
247
248
	/**
249
	 * Removes old entries from the storage.
250
	 *
251
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
252
	 * @return \Aimeos\MShop\Cms\Manager\Iface Manager object for chaining method calls
253
	 */
254
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
255
	{
256
		$path = 'mshop/cms/manager/submanagers';
257
		foreach( $this->context()->config()->get( $path, ['lists'] ) as $domain ) {
258
			$this->object()->getSubManager( $domain )->clear( $siteids );
259
		}
260
261
		return $this->clearBase( $siteids, 'mshop/cms/manager/delete' );
262
	}
263
264
265
	/**
266
	 * Creates a new empty item instance
267
	 *
268
	 * @param array $values Values the item should be initialized with
269
	 * @return \Aimeos\MShop\Cms\Item\Iface New cms item object
270
	 */
271
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
272
	{
273
		$values['cms.siteid'] = $this->context()->locale()->getSiteId();
274
		return $this->createItemBase( $values );
275
	}
276
277
278
	/**
279
	 * Adds or updates an item object or a list of them.
280
	 *
281
	 * @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
282
	 * @param bool $fetch True if the new ID should be returned in the item
283
	 * @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items
284
	 */
285
	public function save( $items, bool $fetch = true )
286
	{
287
		$items = parent::save( $items, $fetch );
288
289
		$this->context()->cache()->deleteByTags( map( $items )->getId()->prefix( 'cms-' ) );
290
291
		return $items;
292
	}
293
294
295
	/**
296
	 * Updates or adds a cms item object.
297
	 * This method doesn't update the type string that belongs to the type ID
298
	 *
299
	 * @param \Aimeos\MShop\Cms\Item\Iface $item Cms item which should be saved
300
	 * @param bool $fetch True if the new ID should be returned in the item
301
	 * @return \Aimeos\MShop\Cms\Item\Iface Updated item including the generated ID
302
	 */
303
	protected function saveItem( \Aimeos\MShop\Cms\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Cms\Item\Iface
304
	{
305
		if( !$item->isModified() ) {
306
			return $this->saveListItems( $item, 'cms', $fetch );
307
		}
308
309
		$context = $this->context();
310
		$conn = $context->db( $this->getResourceName() );
311
312
		$id = $item->getId();
313
		$date = date( 'Y-m-d H:i:s' );
314
		$columns = $this->object()->getSaveAttributes();
315
316
		if( $id === null )
317
		{
318
			/** mshop/cms/manager/insert/mysql
319
			 * Inserts a new cms record into the database table
320
			 *
321
			 * @see mshop/cms/manager/insert/ansi
322
			 */
323
324
			/** mshop/cms/manager/insert/ansi
325
			 * Inserts a new cms record into the database table
326
			 *
327
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
328
			 * the database and the newly created ID retrieved afterwards
329
			 * using the "newid" SQL statement.
330
			 *
331
			 * The SQL statement must be a string suitable for being used as
332
			 * prepared statement. It must include question marks for binding
333
			 * the values from the cms item to the statement before they are
334
			 * sent to the database server. The number of question marks must
335
			 * be the same as the number of columns listed in the INSERT
336
			 * statement. The order of the columns must correspond to the
337
			 * order in the save() method, so the correct values are
338
			 * bound to the columns.
339
			 *
340
			 * The SQL statement should conform to the ANSI standard to be
341
			 * compatible with most relational database systems. This also
342
			 * includes using double quotes for table and column names.
343
			 *
344
			 * @param string SQL statement for inserting records
345
			 * @since 2020.10
346
			 * @category Developer
347
			 * @see mshop/cms/manager/update/ansi
348
			 * @see mshop/cms/manager/newid/ansi
349
			 * @see mshop/cms/manager/delete/ansi
350
			 * @see mshop/cms/manager/search/ansi
351
			 * @see mshop/cms/manager/count/ansi
352
			 */
353
			$path = 'mshop/cms/manager/insert';
354
			$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

354
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
355
		}
356
		else
357
		{
358
			/** mshop/cms/manager/update/mysql
359
			 * Updates an existing cms record in the database
360
			 *
361
			 * @see mshop/cms/manager/update/ansi
362
			 */
363
364
			/** mshop/cms/manager/update/ansi
365
			 * Updates an existing cms record in the database
366
			 *
367
			 * Items which already have an ID (i.e. the ID is not NULL) will
368
			 * be updated in the database.
369
			 *
370
			 * The SQL statement must be a string suitable for being used as
371
			 * prepared statement. It must include question marks for binding
372
			 * the values from the cms item to the statement before they are
373
			 * sent to the database server. The order of the columns must
374
			 * correspond to the order in the save() method, so the
375
			 * correct values are bound to the columns.
376
			 *
377
			 * The SQL statement should conform to the ANSI standard to be
378
			 * compatible with most relational database systems. This also
379
			 * includes using double quotes for table and column names.
380
			 *
381
			 * @param string SQL statement for updating records
382
			 * @since 2020.10
383
			 * @category Developer
384
			 * @see mshop/cms/manager/insert/ansi
385
			 * @see mshop/cms/manager/newid/ansi
386
			 * @see mshop/cms/manager/delete/ansi
387
			 * @see mshop/cms/manager/search/ansi
388
			 * @see mshop/cms/manager/count/ansi
389
			 */
390
			$path = 'mshop/cms/manager/update';
391
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
392
		}
393
394
		$idx = 1;
395
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
396
397
		foreach( $columns as $name => $entry ) {
398
			$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) );
399
		}
400
401
		$stmt->bind( $idx++, $item->getUrl() );
402
		$stmt->bind( $idx++, $item->getLabel() );
403
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
404
		$stmt->bind( $idx++, $date ); // mtime
405
		$stmt->bind( $idx++, $context->editor() );
406
407
		if( $id !== null ) {
408
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
409
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
410
		} else {
411
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
412
			$stmt->bind( $idx++, $date ); // ctime
413
		}
414
415
		$stmt->execute()->finish();
416
417
		if( $id === null )
418
		{
419
			/** mshop/cms/manager/newid/mysql
420
			 * Retrieves the ID generated by the database when inserting a new record
421
			 *
422
			 * @see mshop/cms/manager/newid/ansi
423
			 */
424
425
			/** mshop/cms/manager/newid/ansi
426
			 * Retrieves the ID generated by the database when inserting a new record
427
			 *
428
			 * As soon as a new record is inserted into the database table,
429
			 * the database server generates a new and unique identifier for
430
			 * that record. This ID can be used for retrieving, updating and
431
			 * deleting that specific record from the table again.
432
			 *
433
			 * For MySQL:
434
			 *  SELECT LAST_INSERT_ID()
435
			 * For PostgreSQL:
436
			 *  SELECT currval('seq_mcms_id')
437
			 * For SQL Server:
438
			 *  SELECT SCOPE_IDENTITY()
439
			 * For Oracle:
440
			 *  SELECT "seq_mcms_id".CURRVAL FROM DUAL
441
			 *
442
			 * There's no way to retrive the new ID by a SQL statements that
443
			 * fits for most database servers as they implement their own
444
			 * specific way.
445
			 *
446
			 * @param string SQL statement for retrieving the last inserted record ID
447
			 * @since 2020.10
448
			 * @category Developer
449
			 * @see mshop/cms/manager/insert/ansi
450
			 * @see mshop/cms/manager/update/ansi
451
			 * @see mshop/cms/manager/delete/ansi
452
			 * @see mshop/cms/manager/search/ansi
453
			 * @see mshop/cms/manager/count/ansi
454
			 */
455
			$path = 'mshop/cms/manager/newid';
456
			$id = $this->newId( $conn, $path );
457
		}
458
459
		$item->setId( $id );
460
461
		return $this->saveListItems( $item, 'cms', $fetch );
462
	}
463
464
465
	/**
466
	 * Removes multiple items.
467
	 *
468
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
469
	 * @return \Aimeos\MShop\Cms\Manager\Iface Manager object for chaining method calls
470
	 */
471
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
472
	{
473
		/** mshop/cms/manager/delete/mysql
474
		 * Deletes the items matched by the given IDs from the database
475
		 *
476
		 * @see mshop/cms/manager/delete/ansi
477
		 */
478
479
		/** mshop/cms/manager/delete/ansi
480
		 * Deletes the items matched by the given IDs from the database
481
		 *
482
		 * Removes the records specified by the given IDs from the cms database.
483
		 * The records must be from the site that is configured via the
484
		 * context item.
485
		 *
486
		 * The ":cond" placeholder is replaced by the name of the ID column and
487
		 * the given ID or list of IDs while the site ID is bound to the question
488
		 * mark.
489
		 *
490
		 * The SQL statement should conform to the ANSI standard to be
491
		 * compatible with most relational database systems. This also
492
		 * includes using double quotes for table and column names.
493
		 *
494
		 * @param string SQL statement for deleting items
495
		 * @since 2020.10
496
		 * @category Developer
497
		 * @see mshop/cms/manager/insert/ansi
498
		 * @see mshop/cms/manager/update/ansi
499
		 * @see mshop/cms/manager/newid/ansi
500
		 * @see mshop/cms/manager/search/ansi
501
		 * @see mshop/cms/manager/count/ansi
502
		 */
503
		$path = 'mshop/cms/manager/delete';
504
505
		return $this->deleteItemsBase( $itemIds, $path )->deleteRefItems( $itemIds );
506
	}
507
508
509
	/**
510
	 * Returns the item specified by its URL
511
	 *
512
	 * @param string $code URL of the item
513
	 * @param string[] $ref List of domains to fetch list items and referenced items for
514
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
515
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
516
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
517
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
518
	 */
519
	public function find( string $code, array $ref = [], ?string $domain = null, ?string $type = null,
520
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
521
	{
522
		return $this->findBase( array( 'cms.url' => $code ), $ref, $default );
523
	}
524
525
526
	/**
527
	 * Returns the cms item object specified by the given ID.
528
	 *
529
	 * @param string $id Id of the cms item
530
	 * @param string[] $ref List of domains to fetch list items and referenced items for
531
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
532
	 * @return \Aimeos\MShop\Cms\Item\Iface Returns the cms item of the given id
533
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
534
	 */
535
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
536
	{
537
		return $this->getItemBase( 'cms.id', $id, $ref, $default );
538
	}
539
540
541
	/**
542
	 * Returns the available manager types
543
	 *
544
	 * @param bool $withsub Return also the resource type of sub-managers if true
545
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
546
	 */
547
	public function getResourceType( bool $withsub = true ) : array
548
	{
549
		$path = 'mshop/cms/manager/submanagers';
550
		return $this->getResourceTypeBase( 'cms', $path, ['lists'], $withsub );
551
	}
552
553
554
	/**
555
	 * Returns the attributes that can be used for searching.
556
	 *
557
	 * @param bool $withsub Return also attributes of sub-managers if true
558
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
559
	 */
560
	public function getSearchAttributes( bool $withsub = true ) : array
561
	{
562
		/** mshop/cms/manager/submanagers
563
		 * List of manager names that can be instantiated by the cms manager
564
		 *
565
		 * Managers provide a generic interface to the underlying storage.
566
		 * Each manager has or can have sub-managers caring about particular
567
		 * aspects. Each of these sub-managers can be instantiated by its
568
		 * parent manager using the getSubManager() method.
569
		 *
570
		 * The search keys from sub-managers can be normally used in the
571
		 * manager as well. It allows you to search for items of the manager
572
		 * using the search keys of the sub-managers to further limit the
573
		 * retrieved list of items.
574
		 *
575
		 * @param array List of sub-manager names
576
		 * @since 2020.10
577
		 * @category Developer
578
		 */
579
		$path = 'mshop/cms/manager/submanagers';
580
581
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
582
	}
583
584
585
	/**
586
	 * Searches for all cms items matching the given critera.
587
	 *
588
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
589
	 * @param string[] $ref List of domains to fetch list items and referenced items for
590
	 * @param int|null &$total Number of items that are available in total
591
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Cms\Item\Iface with ids as keys
592
	 */
593
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], ?int &$total = null ) : \Aimeos\Map
594
	{
595
		$map = [];
596
		$context = $this->context();
597
		$conn = $context->db( $this->getResourceName() );
598
599
			$required = array( 'cms' );
600
601
			/** mshop/cms/manager/sitemode
602
			 * Mode how items from levels below or above in the site tree are handled
603
			 *
604
			 * By default, only items from the current site are fetched from the
605
			 * storage. If the ai-sites extension is installed, you can create a
606
			 * tree of sites. Then, this setting allows you to define for the
607
			 * whole cms domain if items from parent sites are inherited,
608
			 * sites from child sites are aggregated or both.
609
			 *
610
			 * Available constants for the site mode are:
611
			 * * 0 = only items from the current site
612
			 * * 1 = inherit items from parent sites
613
			 * * 2 = aggregate items from child sites
614
			 * * 3 = inherit and aggregate items at the same time
615
			 *
616
			 * You also need to set the mode in the locale manager
617
			 * (mshop/locale/manager/sitelevel) to one of the constants.
618
			 * If you set it to the same value, it will work as described but you
619
			 * can also use different modes. For example, if inheritance and
620
			 * aggregation is configured the locale manager but only inheritance
621
			 * in the domain manager because aggregating items makes no sense in
622
			 * this domain, then items wil be only inherited. Thus, you have full
623
			 * control over inheritance and aggregation in each domain.
624
			 *
625
			 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
626
			 * @category Developer
627
			 * @since 2020.10
628
			 * @see mshop/locale/manager/sitelevel
629
			 */
630
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ONE;
631
			$level = $context->config()->get( 'mshop/cms/manager/sitemode', $level );
632
633
			/** mshop/cms/manager/search/mysql
634
			 * Retrieves the records matched by the given criteria in the database
635
			 *
636
			 * @see mshop/cms/manager/search/ansi
637
			 */
638
639
			/** mshop/cms/manager/search/ansi
640
			 * Retrieves the records matched by the given criteria in the database
641
			 *
642
			 * Fetches the records matched by the given criteria from the cms
643
			 * database. The records must be from one of the sites that are
644
			 * configured via the context item. If the current site is part of
645
			 * a tree of sites, the SELECT statement can retrieve all records
646
			 * from the current site and the complete sub-tree of sites.
647
			 *
648
			 * As the records can normally be limited by criteria from sub-managers,
649
			 * their tables must be joined in the SQL context. This is done by
650
			 * using the "internaldeps" property from the definition of the ID
651
			 * column of the sub-managers. These internal dependencies specify
652
			 * the JOIN between the tables and the used columns for joining. The
653
			 * ":joins" placeholder is then replaced by the JOIN strings from
654
			 * the sub-managers.
655
			 *
656
			 * To limit the records matched, conditions can be added to the given
657
			 * criteria object. It can contain comparisons like column names that
658
			 * must match specific values which can be combined by AND, OR or NOT
659
			 * operators. The resulting string of SQL conditions replaces the
660
			 * ":cond" placeholder before the statement is sent to the database
661
			 * server.
662
			 *
663
			 * If the records that are retrieved should be ordered by one or more
664
			 * columns, the generated string of column / sort direction pairs
665
			 * replaces the ":order" placeholder. In case no ordering is required,
666
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
667
			 * markers is removed to speed up retrieving the records. Columns of
668
			 * sub-managers can also be used for ordering the result set but then
669
			 * no index can be used.
670
			 *
671
			 * The number of returned records can be limited and can start at any
672
			 * number between the begining and the end of the result set. For that
673
			 * the ":size" and ":start" placeholders are replaced by the
674
			 * corresponding values from the criteria object. The default values
675
			 * are 0 for the start and 100 for the size value.
676
			 *
677
			 * The SQL statement should conform to the ANSI standard to be
678
			 * compatible with most relational database systems. This also
679
			 * includes using double quotes for table and column names.
680
			 *
681
			 * @param string SQL statement for searching items
682
			 * @since 2020.10
683
			 * @category Developer
684
			 * @see mshop/cms/manager/insert/ansi
685
			 * @see mshop/cms/manager/update/ansi
686
			 * @see mshop/cms/manager/newid/ansi
687
			 * @see mshop/cms/manager/delete/ansi
688
			 * @see mshop/cms/manager/count/ansi
689
			 */
690
			$cfgPathSearch = 'mshop/cms/manager/search';
691
692
			/** mshop/cms/manager/count/mysql
693
			 * Counts the number of records matched by the given criteria in the database
694
			 *
695
			 * @see mshop/cms/manager/count/ansi
696
			 */
697
698
			/** mshop/cms/manager/count/ansi
699
			 * Counts the number of records matched by the given criteria in the database
700
			 *
701
			 * Counts all records matched by the given criteria from the cms
702
			 * database. The records must be from one of the sites that are
703
			 * configured via the context item. If the current site is part of
704
			 * a tree of sites, the statement can count all records from the
705
			 * current site and the complete sub-tree of sites.
706
			 *
707
			 * As the records can normally be limited by criteria from sub-managers,
708
			 * their tables must be joined in the SQL context. This is done by
709
			 * using the "internaldeps" property from the definition of the ID
710
			 * column of the sub-managers. These internal dependencies specify
711
			 * the JOIN between the tables and the used columns for joining. The
712
			 * ":joins" placeholder is then replaced by the JOIN strings from
713
			 * the sub-managers.
714
			 *
715
			 * To limit the records matched, conditions can be added to the given
716
			 * criteria object. It can contain comparisons like column names that
717
			 * must match specific values which can be combined by AND, OR or NOT
718
			 * operators. The resulting string of SQL conditions replaces the
719
			 * ":cond" placeholder before the statement is sent to the database
720
			 * server.
721
			 *
722
			 * Both, the strings for ":joins" and for ":cond" are the same as for
723
			 * the "search" SQL statement.
724
			 *
725
			 * Contrary to the "search" statement, it doesn't return any records
726
			 * but instead the number of records that have been found. As counting
727
			 * thousands of records can be a long running task, the maximum number
728
			 * of counted records is limited for performance reasons.
729
			 *
730
			 * The SQL statement should conform to the ANSI standard to be
731
			 * compatible with most relational database systems. This also
732
			 * includes using double quotes for table and column names.
733
			 *
734
			 * @param string SQL statement for counting items
735
			 * @since 2020.10
736
			 * @category Developer
737
			 * @see mshop/cms/manager/insert/ansi
738
			 * @see mshop/cms/manager/update/ansi
739
			 * @see mshop/cms/manager/newid/ansi
740
			 * @see mshop/cms/manager/delete/ansi
741
			 * @see mshop/cms/manager/search/ansi
742
			 */
743
			$cfgPathCount = 'mshop/cms/manager/count';
744
745
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
746
747
			while( ( $row = $results->fetch() ) !== null ) {
748
				$map[$row['cms.id']] = $row;
749
			}
750
751
		return $this->buildItems( $map, $ref, 'cms' );
752
	}
753
754
755
	/**
756
	 * Returns a new manager for cms extensions
757
	 *
758
	 * @param string $manager Name of the sub manager type in lower case
759
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
760
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g types, lists etc.
761
	 */
762
	public function getSubManager( string $manager, ?string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
763
	{
764
		return $this->getSubManagerBase( 'cms', $manager, $name );
765
	}
766
767
768
	/**
769
	 * Creates a filter object.
770
	 *
771
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
772
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
773
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
774
	 */
775
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
776
	{
777
		return $this->filterBase( 'cms', $default );
778
	}
779
780
781
	/**
782
	 * Creates a new cms item instance.
783
	 *
784
	 * @param array $values Associative list of key/value pairs
785
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
786
	 * @param \Aimeos\MShop\Common\Item\Iface $refItems List of referenced items
787
	 * @return \Aimeos\MShop\Cms\Item\Iface New cms item
788
	 */
789
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [] ) : \Aimeos\MShop\Common\Item\Iface
790
	{
791
		return new \Aimeos\MShop\Cms\Item\Standard( $values, $listItems, $refItems );
792
	}
793
}
794