Passed
Push — master ( 5d05b9...5af5d7 )
by Aimeos
05:15
created

Standard::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package MShop
7
 * @subpackage Rule
8
 */
9
10
11
namespace Aimeos\MShop\Rule\Manager;
12
13
14
/**
15
 * Default rule manager implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Rule
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Rule\Manager\Base
22
	implements \Aimeos\MShop\Rule\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private $searchConfig = array(
25
		'rule.id' => array(
26
			'label' => 'ID',
27
			'code' => 'rule.id',
28
			'internalcode' => 'mrul."id"',
29
			'type' => 'integer',
30
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
31
			'public' => false,
32
		),
33
		'rule.siteid' => array(
34
			'label' => 'Site ID',
35
			'code' => 'rule.siteid',
36
			'internalcode' => 'mrul."siteid"',
37
			'type' => 'string',
38
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
39
			'public' => false,
40
		),
41
		'rule.type' => array(
42
			'label' => 'Type ID',
43
			'code' => 'rule.type',
44
			'internalcode' => 'mrul."type"',
45
			'type' => 'string',
46
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
47
			'public' => false,
48
		),
49
		'rule.label' => array(
50
			'label' => 'Label',
51
			'code' => 'rule.label',
52
			'internalcode' => 'mrul."label"',
53
			'type' => 'string',
54
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
55
		),
56
		'rule.provider' => array(
57
			'label' => 'Provider',
58
			'code' => 'rule.provider',
59
			'internalcode' => 'mrul."provider"',
60
			'type' => 'string',
61
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
62
		),
63
		'rule.position' => array(
64
			'label' => 'Position',
65
			'code' => 'rule.position',
66
			'internalcode' => 'mrul."pos"',
67
			'type' => 'integer',
68
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
69
		),
70
		'rule.status' => array(
71
			'label' => 'Status',
72
			'code' => 'rule.status',
73
			'internalcode' => 'mrul."status"',
74
			'type' => 'integer',
75
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
76
		),
77
		'rule.config' => array(
78
			'label' => 'Config',
79
			'code' => 'rule.config',
80
			'internalcode' => 'mrul."config"',
81
			'type' => 'string',
82
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
83
			'public' => false,
84
		),
85
		'rule.datestart' => array(
86
			'code' => 'rule.datestart',
87
			'internalcode' => 'mrul."start"',
88
			'label' => 'Start date/time',
89
			'type' => 'datetime',
90
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
91
		),
92
		'rule.dateend' => array(
93
			'code' => 'rule.dateend',
94
			'internalcode' => 'mrul."end"',
95
			'label' => 'End date/time',
96
			'type' => 'datetime',
97
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
98
		),
99
		'rule.ctime' => array(
100
			'code' => 'rule.ctime',
101
			'internalcode' => 'mrul."ctime"',
102
			'label' => 'Create date/time',
103
			'type' => 'datetime',
104
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
105
			'public' => false,
106
		),
107
		'rule.mtime' => array(
108
			'code' => 'rule.mtime',
109
			'internalcode' => 'mrul."mtime"',
110
			'label' => 'Modify date/time',
111
			'type' => 'datetime',
112
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
113
			'public' => false,
114
		),
115
		'rule.editor' => array(
116
			'code' => 'rule.editor',
117
			'internalcode' => 'mrul."editor"',
118
			'label' => 'Editor',
119
			'type' => 'string',
120
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
121
			'public' => false,
122
		),
123
	);
124
125
	private $date;
126
127
128
	/**
129
	 * Initializes the object.
130
	 *
131
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
132
	 */
133
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
134
	{
135
		parent::__construct( $context );
136
137
		$this->setResourceName( 'db-rule' );
138
		$this->date = $context->getDateTime();
139
	}
140
141
142
	/**
143
	 * Removes old entries from the storage.
144
	 *
145
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
146
	 * @return \Aimeos\MShop\Rule\Manager\Iface Manager object for chaining method calls
147
	 */
148
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
149
	{
150
		$path = 'mshop/rule/manager/submanagers';
151
		foreach( $this->getContext()->getConfig()->get( $path, ['type'] ) as $domain ) {
152
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
153
		}
154
155
		return $this->clearBase( $siteids, 'mshop/rule/manager/delete' );
156
	}
157
158
159
	/**
160
	 * Creates a new empty item instance
161
	 *
162
	 * @param array $values Values the item should be initialized with
163
	 * @return \Aimeos\MShop\Rule\Item\Iface New rule item object
164
	 */
165
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
166
	{
167
		$values['rule.siteid'] = $this->getContext()->getLocale()->getSiteId();
168
		return $this->createItemBase( $values );
169
	}
170
171
172
	/**
173
	 * Creates a filter object.
174
	 *
175
	 * @param bool $default Add default criteria
176
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
177
	 * @return \Aimeos\MW\Criteria\Iface Returns the filter object
178
	 */
179
	public function filter( bool $default = false, bool $site = false ) : \Aimeos\MW\Criteria\Iface
180
	{
181
		if( $default === true )
182
		{
183
			$filter = $this->filterBase( 'rule' );
184
185
			return $filter->add( $filter->and( [
186
				$filter->or( [
187
					$filter->is( 'rule.datestart', '==', null ),
188
					$filter->is( 'rule.datestart', '<=', $this->date ),
189
				] ),
190
				$filter->or( [
191
					$filter->is( 'rule.dateend', '==', null ),
192
					$filter->is( 'rule.dateend', '>=', $this->date ),
193
				] ),
194
			] ) );
195
		}
196
197
		return parent::filter();
198
	}
199
200
201
	/**
202
	 * Removes multiple items.
203
	 *
204
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
205
	 * @return \Aimeos\MShop\Rule\Manager\Iface Manager object for chaining method calls
206
	 */
207
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
208
	{
209
		/** mshop/rule/manager/delete/mysql
210
		 * Deletes the items matched by the given IDs from the database
211
		 *
212
		 * @see mshop/rule/manager/delete/ansi
213
		 */
214
215
		/** mshop/rule/manager/delete/ansi
216
		 * Deletes the items matched by the given IDs from the database
217
		 *
218
		 * Removes the records specified by the given IDs from the rule database.
219
		 * The records must be from the site that is configured via the
220
		 * context item.
221
		 *
222
		 * The ":cond" placeholder is replaced by the name of the ID column and
223
		 * the given ID or list of IDs while the site ID is bound to the question
224
		 * mark.
225
		 *
226
		 * The SQL statement should conform to the ANSI standard to be
227
		 * compatible with most relational database systems. This also
228
		 * includes using double quotes for table and column names.
229
		 *
230
		 * @param string SQL statement for deleting items
231
		 * @since 2014.03
232
		 * @category Developer
233
		 * @see mshop/rule/manager/insert/ansi
234
		 * @see mshop/rule/manager/update/ansi
235
		 * @see mshop/rule/manager/newid/ansi
236
		 * @see mshop/rule/manager/search/ansi
237
		 * @see mshop/rule/manager/count/ansi
238
		 */
239
		$path = 'mshop/rule/manager/delete';
240
241
		return $this->deleteItemsBase( $itemIds, $path );
242
	}
243
244
245
	/**
246
	 * Returns the available manager types
247
	 *
248
	 * @param bool $withsub Return also the resource type of sub-managers if true
249
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
250
	 */
251
	public function getResourceType( bool $withsub = true ) : array
252
	{
253
		$path = 'mshop/rule/manager/submanagers';
254
		return $this->getResourceTypeBase( 'rule', $path, [], $withsub );
255
	}
256
257
258
	/**
259
	 * Returns the attributes that can be used for searching.
260
	 *
261
	 * @param bool $withsub Return also attributes of sub-managers if true
262
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
263
	 */
264
	public function getSearchAttributes( bool $withsub = true ) : array
265
	{
266
		/** mshop/rule/manager/submanagers
267
		 * List of manager names that can be instantiated by the rule manager
268
		 *
269
		 * Managers provide a generic interface to the underlying storage.
270
		 * Each manager has or can have sub-managers caring about particular
271
		 * aspects. Each of these sub-managers can be instantiated by its
272
		 * parent manager using the getSubManager() method.
273
		 *
274
		 * The search keys from sub-managers can be normally used in the
275
		 * manager as well. It allows you to search for items of the manager
276
		 * using the search keys of the sub-managers to further limit the
277
		 * retrieved list of items.
278
		 *
279
		 * @param array List of sub-manager names
280
		 * @since 2014.03
281
		 * @category Developer
282
		 */
283
		$path = 'mshop/rule/manager/submanagers';
284
285
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
286
	}
287
288
289
	/**
290
	 * Returns a new manager for rule extensions
291
	 *
292
	 * @param string $manager Name of the sub manager type in lower case
293
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
294
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g types, lists etc.
295
	 */
296
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
297
	{
298
		return $this->getSubManagerBase( 'rule', $manager, $name );
299
	}
300
301
302
	/**
303
	 * Returns rule item specified by the given ID.
304
	 *
305
	 * @param string $id Unique ID of the rule item
306
	 * @param string[] $ref List of domains to fetch list items and referenced items for
307
	 * @param bool $default Add default criteria
308
	 * @return \Aimeos\MShop\Rule\Item\Iface Returns the rule item of the given id
309
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
310
	 */
311
	public function get( string $id, array $ref = [], bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
312
	{
313
		return $this->getItemBase( 'rule.id', $id, $ref, $default );
314
	}
315
316
317
	/**
318
	 * Saves a new or modified rule to the storage.
319
	 *
320
	 * @param \Aimeos\MShop\Rule\Item\Iface $item Rule item
321
	 * @param bool $fetch True if the new ID should be returned in the item
322
	 * @return \Aimeos\MShop\Rule\Item\Iface $item Updated item including the generated ID
323
	 */
324
	public function saveItem( \Aimeos\MShop\Rule\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Rule\Item\Iface
325
	{
326
		if( !$item->isModified() ) {
327
			return $item;
328
		}
329
330
		$context = $this->getContext();
331
332
		$dbm = $context->getDatabaseManager();
333
		$dbname = $this->getResourceName();
334
		$conn = $dbm->acquire( $dbname );
335
336
		try
337
		{
338
			$id = $item->getId();
339
			$date = date( 'Y-m-d H:i:s' );
340
			$columns = $this->getObject()->getSaveAttributes();
341
342
			if( $id === null )
343
			{
344
				/** mshop/rule/manager/insert/mysql
345
				 * Inserts a new rule record into the database table
346
				 *
347
				 * @see mshop/rule/manager/insert/ansi
348
				 */
349
350
				/** mshop/rule/manager/insert/ansi
351
				 * Inserts a new rule record into the database table
352
				 *
353
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
354
				 * the database and the newly created ID retrieved afterwards
355
				 * using the "newid" SQL statement.
356
				 *
357
				 * The SQL statement must be a string suitable for being used as
358
				 * prepared statement. It must include question marks for binding
359
				 * the values from the rule item to the statement before they are
360
				 * sent to the database server. The number of question marks must
361
				 * be the same as the number of columns listed in the INSERT
362
				 * statement. The catalog of the columns must correspond to the
363
				 * catalog in the save() method, so the correct values are
364
				 * bound to the columns.
365
				 *
366
				 * The SQL statement should conform to the ANSI standard to be
367
				 * compatible with most relational database systems. This also
368
				 * includes using double quotes for table and column names.
369
				 *
370
				 * @param string SQL statement for inserting records
371
				 * @since 2014.03
372
				 * @category Developer
373
				 * @see mshop/rule/manager/update/ansi
374
				 * @see mshop/rule/manager/newid/ansi
375
				 * @see mshop/rule/manager/delete/ansi
376
				 * @see mshop/rule/manager/search/ansi
377
				 * @see mshop/rule/manager/count/ansi
378
				 */
379
				$path = 'mshop/rule/manager/insert';
380
				$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

380
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
381
			}
382
			else
383
			{
384
				/** mshop/rule/manager/update/mysql
385
				 * Updates an existing rule record in the database
386
				 *
387
				 * @see mshop/rule/manager/update/ansi
388
				 */
389
390
				/** mshop/rule/manager/update/ansi
391
				 * Updates an existing rule record in the database
392
				 *
393
				 * Items which already have an ID (i.e. the ID is not NULL) will
394
				 * be updated in the database.
395
				 *
396
				 * The SQL statement must be a string suitable for being used as
397
				 * prepared statement. It must include question marks for binding
398
				 * the values from the rule item to the statement before they are
399
				 * sent to the database server. The catalog of the columns must
400
				 * correspond to the catalog in the save() method, so the
401
				 * correct values are bound to the columns.
402
				 *
403
				 * The SQL statement should conform to the ANSI standard to be
404
				 * compatible with most relational database systems. This also
405
				 * includes using double quotes for table and column names.
406
				 *
407
				 * @param string SQL statement for updating records
408
				 * @since 2014.03
409
				 * @category Developer
410
				 * @see mshop/rule/manager/insert/ansi
411
				 * @see mshop/rule/manager/newid/ansi
412
				 * @see mshop/rule/manager/delete/ansi
413
				 * @see mshop/rule/manager/search/ansi
414
				 * @see mshop/rule/manager/count/ansi
415
				 */
416
				$path = 'mshop/rule/manager/update';
417
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
418
			}
419
420
			$idx = 1;
421
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
422
423
			foreach( $columns as $name => $entry ) {
424
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
425
			}
426
427
			$stmt->bind( $idx++, $item->getType() );
428
			$stmt->bind( $idx++, $item->getLabel() );
429
			$stmt->bind( $idx++, $item->getProvider() );
430
			$stmt->bind( $idx++, json_encode( $item->getConfig() ) );
431
			$stmt->bind( $idx++, $item->getDateStart() );
432
			$stmt->bind( $idx++, $item->getDateEnd() );
433
			$stmt->bind( $idx++, $item->getPosition(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
434
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
435
			$stmt->bind( $idx++, $date ); //mtime
436
			$stmt->bind( $idx++, $context->getEditor() );
437
			$stmt->bind( $idx++, $context->getLocale()->getSiteId() );
438
439
			if( $id !== null ) {
440
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
441
			} else {
442
				$stmt->bind( $idx++, $date ); //ctime
443
			}
444
445
			$stmt->execute()->finish();
446
447
			if( $id === null && $fetch === true )
448
			{
449
				/** mshop/rule/manager/newid/mysql
450
				 * Retrieves the ID generated by the database when inserting a new record
451
				 *
452
				 * @see mshop/rule/manager/newid/ansi
453
				 */
454
455
				/** mshop/rule/manager/newid/ansi
456
				 * Retrieves the ID generated by the database when inserting a new record
457
				 *
458
				 * As soon as a new record is inserted into the database table,
459
				 * the database server generates a new and unique identifier for
460
				 * that record. This ID can be used for retrieving, updating and
461
				 * deleting that specific record from the table again.
462
				 *
463
				 * For MySQL:
464
				 *  SELECT LAST_INSERT_ID()
465
				 * For PostgreSQL:
466
				 *  SELECT currval('seq_mrul_id')
467
				 * For SQL Server:
468
				 *  SELECT SCOPE_IDENTITY()
469
				 * For Oracle:
470
				 *  SELECT "seq_mrul_id".CURRVAL FROM DUAL
471
				 *
472
				 * There's no way to retrive the new ID by a SQL statements that
473
				 * fits for most database servers as they implement their own
474
				 * specific way.
475
				 *
476
				 * @param string SQL statement for retrieving the last inserted record ID
477
				 * @since 2014.03
478
				 * @category Developer
479
				 * @see mshop/rule/manager/insert/ansi
480
				 * @see mshop/rule/manager/update/ansi
481
				 * @see mshop/rule/manager/delete/ansi
482
				 * @see mshop/rule/manager/search/ansi
483
				 * @see mshop/rule/manager/count/ansi
484
				 */
485
				$path = 'mshop/rule/manager/newid';
486
				$id = $this->newId( $conn, $path );
487
			}
488
489
			$item->setId( $id );
490
491
			$dbm->release( $conn, $dbname );
492
		}
493
		catch( \Exception $e )
494
		{
495
			$dbm->release( $conn, $dbname );
496
			throw $e;
497
		}
498
499
		return $item;
500
	}
501
502
503
	/**
504
	 * Searches for rule items matching the given criteria.
505
	 *
506
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
507
	 * @param string[] $ref List of domains to fetch list items and referenced items for
508
	 * @param int|null &$total Number of items that are available in total
509
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Rule\Item\Iface with ids as keys
510
	 */
511
	public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
512
	{
513
		$items = [];
514
		$context = $this->getContext();
515
516
		$dbm = $context->getDatabaseManager();
517
		$dbname = $this->getResourceName();
518
		$conn = $dbm->acquire( $dbname );
519
520
		try
521
		{
522
			$required = array( 'rule' );
523
524
			/** mshop/rule/manager/sitemode
525
			 * Mode how items from levels below or above in the site tree are handled
526
			 *
527
			 * By default, only items from the current site are fetched from the
528
			 * storage. If the ai-sites extension is installed, you can create a
529
			 * tree of sites. Then, this setting allows you to define for the
530
			 * whole rule domain if items from parent sites are inherited,
531
			 * sites from child sites are aggregated or both.
532
			 *
533
			 * Available constants for the site mode are:
534
			 * * 0 = only items from the current site
535
			 * * 1 = inherit items from parent sites
536
			 * * 2 = aggregate items from child sites
537
			 * * 3 = inherit and aggregate items at the same time
538
			 *
539
			 * You also need to set the mode in the locale manager
540
			 * (mshop/locale/manager/sitelevel) to one of the constants.
541
			 * If you set it to the same value, it will work as described but you
542
			 * can also use different modes. For example, if inheritance and
543
			 * aggregation is configured the locale manager but only inheritance
544
			 * in the domain manager because aggregating items makes no sense in
545
			 * this domain, then items wil be only inherited. Thus, you have full
546
			 * control over inheritance and aggregation in each domain.
547
			 *
548
			 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
549
			 * @category Developer
550
			 * @since 2018.01
551
			 * @see mshop/locale/manager/sitelevel
552
			 */
553
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
554
			$level = $context->getConfig()->get( 'mshop/rule/manager/sitemode', $level );
555
556
			/** mshop/rule/manager/search/mysql
557
			 * Retrieves the records matched by the given criteria in the database
558
			 *
559
			 * @see mshop/rule/manager/search/ansi
560
			 */
561
562
			/** mshop/rule/manager/search/ansi
563
			 * Retrieves the records matched by the given criteria in the database
564
			 *
565
			 * Fetches the records matched by the given criteria from the rule
566
			 * database. The records must be from one of the sites that are
567
			 * configured via the context item. If the current site is part of
568
			 * a tree of sites, the SELECT statement can retrieve all records
569
			 * from the current site and the complete sub-tree of sites.
570
			 *
571
			 * As the records can normally be limited by criteria from sub-managers,
572
			 * their tables must be joined in the SQL context. This is done by
573
			 * using the "internaldeps" property from the definition of the ID
574
			 * column of the sub-managers. These internal dependencies specify
575
			 * the JOIN between the tables and the used columns for joining. The
576
			 * ":joins" placeholder is then replaced by the JOIN strings from
577
			 * the sub-managers.
578
			 *
579
			 * To limit the records matched, conditions can be added to the given
580
			 * criteria object. It can contain comparisons like column names that
581
			 * must match specific values which can be combined by AND, OR or NOT
582
			 * operators. The resulting string of SQL conditions replaces the
583
			 * ":cond" placeholder before the statement is sent to the database
584
			 * server.
585
			 *
586
			 * If the records that are retrieved should be cataloged by one or more
587
			 * columns, the generated string of column / sort direction pairs
588
			 * replaces the ":catalog" placeholder. In case no cataloging is required,
589
			 * the complete ORDER BY part including the "\/*-catalogby*\/...\/*catalogby-*\/"
590
			 * markers is removed to speed up retrieving the records. Columns of
591
			 * sub-managers can also be used for cataloging the result set but then
592
			 * no index can be used.
593
			 *
594
			 * The number of returned records can be limited and can start at any
595
			 * number between the begining and the end of the result set. For that
596
			 * the ":size" and ":start" placeholders are replaced by the
597
			 * corresponding values from the criteria object. The default values
598
			 * are 0 for the start and 100 for the size value.
599
			 *
600
			 * The SQL statement should conform to the ANSI standard to be
601
			 * compatible with most relational database systems. This also
602
			 * includes using double quotes for table and column names.
603
			 *
604
			 * @param string SQL statement for searching items
605
			 * @since 2014.03
606
			 * @category Developer
607
			 * @see mshop/rule/manager/insert/ansi
608
			 * @see mshop/rule/manager/update/ansi
609
			 * @see mshop/rule/manager/newid/ansi
610
			 * @see mshop/rule/manager/delete/ansi
611
			 * @see mshop/rule/manager/count/ansi
612
			 */
613
			$cfgPathSearch = 'mshop/rule/manager/search';
614
615
			/** mshop/rule/manager/count/mysql
616
			 * Counts the number of records matched by the given criteria in the database
617
			 *
618
			 * @see mshop/rule/manager/count/ansi
619
			 */
620
621
			/** mshop/rule/manager/count/ansi
622
			 * Counts the number of records matched by the given criteria in the database
623
			 *
624
			 * Counts all records matched by the given criteria from the rule
625
			 * database. The records must be from one of the sites that are
626
			 * configured via the context item. If the current site is part of
627
			 * a tree of sites, the statement can count all records from the
628
			 * current site and the complete sub-tree of sites.
629
			 *
630
			 * As the records can normally be limited by criteria from sub-managers,
631
			 * their tables must be joined in the SQL context. This is done by
632
			 * using the "internaldeps" property from the definition of the ID
633
			 * column of the sub-managers. These internal dependencies specify
634
			 * the JOIN between the tables and the used columns for joining. The
635
			 * ":joins" placeholder is then replaced by the JOIN strings from
636
			 * the sub-managers.
637
			 *
638
			 * To limit the records matched, conditions can be added to the given
639
			 * criteria object. It can contain comparisons like column names that
640
			 * must match specific values which can be combined by AND, OR or NOT
641
			 * operators. The resulting string of SQL conditions replaces the
642
			 * ":cond" placeholder before the statement is sent to the database
643
			 * server.
644
			 *
645
			 * Both, the strings for ":joins" and for ":cond" are the same as for
646
			 * the "search" SQL statement.
647
			 *
648
			 * Contrary to the "search" statement, it doesn't return any records
649
			 * but instead the number of records that have been found. As counting
650
			 * thousands of records can be a long running task, the maximum number
651
			 * of counted records is limited for performance reasons.
652
			 *
653
			 * The SQL statement should conform to the ANSI standard to be
654
			 * compatible with most relational database systems. This also
655
			 * includes using double quotes for table and column names.
656
			 *
657
			 * @param string SQL statement for counting items
658
			 * @since 2014.03
659
			 * @category Developer
660
			 * @see mshop/rule/manager/insert/ansi
661
			 * @see mshop/rule/manager/update/ansi
662
			 * @see mshop/rule/manager/newid/ansi
663
			 * @see mshop/rule/manager/delete/ansi
664
			 * @see mshop/rule/manager/search/ansi
665
			 */
666
			$cfgPathCount = 'mshop/rule/manager/count';
667
668
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
669
670
			while( ( $row = $results->fetch() ) !== null )
671
			{
672
				if( ( $row['rule.config'] = json_decode( $config = $row['rule.config'], true ) ) === null )
673
				{
674
					$msg = sprintf( 'Invalid JSON as result of search for ID "%2$s" in "%1$s": %3$s', 'rule.config', $row['rule.id'], $config );
675
					$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN );
676
				}
677
678
				if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) {
679
					$items[$row['rule.id']] = $item;
680
				}
681
			}
682
683
			$dbm->release( $conn, $dbname );
684
		}
685
		catch( \Exception $e )
686
		{
687
			$dbm->release( $conn, $dbname );
688
			throw $e;
689
		}
690
691
		return map( $items );
692
	}
693
694
695
	/**
696
	 * Creates a new rule object.
697
	 *
698
	 * @param array $values Associative list of item key/value pairs
699
	 * @return \Aimeos\MShop\Rule\Item\Iface New rule object
700
	 */
701
	protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Rule\Item\Iface
702
	{
703
		return new \Aimeos\MShop\Rule\Item\Standard( $values );
704
	}
705
}
706