Passed
Push — master ( 0b8510...665276 )
by Aimeos
05:27
created

Standard::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2024
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 array $searchConfig = [
25
		'type' => [
26
			'label' => 'Type ID',
27
			'code' => 'rule.type',
28
			'internalcode' => 'mrul."type"',
29
		],
30
		'label' => [
31
			'label' => 'Label',
32
			'code' => 'rule.label',
33
			'internalcode' => 'mrul."label"',
34
		],
35
		'provider' => [
36
			'label' => 'Provider',
37
			'code' => 'rule.provider',
38
			'internalcode' => 'mrul."provider"',
39
		],
40
		'pos' => [
41
			'label' => 'Position',
42
			'code' => 'rule.position',
43
			'internalcode' => 'mrul."pos"',
44
			'type' => 'int',
45
		],
46
		'status' => [
47
			'label' => 'Status',
48
			'code' => 'rule.status',
49
			'internalcode' => 'mrul."status"',
50
			'type' => 'int',
51
		],
52
		'config' => [
53
			'label' => 'Configuration',
54
			'code' => 'rule.config',
55
			'internalcode' => 'mrul."config"',
56
			'type' => 'json',
57
			'public' => false,
58
		],
59
		'start' => [
60
			'label' => 'Start date/time',
61
			'code' => 'rule.datestart',
62
			'internalcode' => 'mrul."start"',
63
			'type' => 'datetime',
64
		],
65
		'end' => [
66
			'label' => 'End date/time',
67
			'code' => 'rule.dateend',
68
			'internalcode' => 'mrul."end"',
69
			'type' => 'datetime',
70
		],
71
	];
72
73
74
	/**
75
	 * Creates a new empty item instance
76
	 *
77
	 * @param array $values Values the item should be initialized with
78
	 * @return \Aimeos\MShop\Review\Item\Iface New review item object
79
	 */
80
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
81
	{
82
		$values['rule.siteid'] = $values['rule.siteid'] ?? $this->context()->locale()->getSiteId();
83
		return new \Aimeos\MShop\Rule\Item\Standard( 'rule.', $values );
84
	}
85
86
87
	/**
88
	 * Creates a filter object.
89
	 *
90
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
91
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
92
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
93
	 */
94
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
95
	{
96
		if( $default !== false )
97
		{
98
			$filter = $this->filterBase( 'rule', $default );
99
			$date = $this->context()->datetime();
100
101
			return $filter->add( $filter->and( [
102
				$filter->or( [
103
					$filter->is( 'rule.datestart', '==', null ),
104
					$filter->is( 'rule.datestart', '<=', $date ),
105
				] ),
106
				$filter->or( [
107
					$filter->is( 'rule.dateend', '==', null ),
108
					$filter->is( 'rule.dateend', '>=', $date ),
109
				] ),
110
			] ) );
111
		}
112
113
		return parent::filter();
114
	}
115
116
117
	/**
118
	 * Returns the additional column/search definitions
119
	 *
120
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
121
	 */
122
	public function getSaveAttributes() : array
123
	{
124
		return $this->createAttributes( $this->searchConfig );
125
	}
126
127
128
	/**
129
	 * Returns the prefix for the item properties and search keys.
130
	 *
131
	 * @return string Prefix for the item properties and search keys
132
	 */
133
	protected function getPrefix() : string
134
	{
135
		return 'rule.';
136
	}
137
138
139
	/** mshop/rule/manager/name
140
	 * Class name of the used rule manager implementation
141
	 *
142
	 * Each default manager can be replace by an alternative imlementation.
143
	 * To use this implementation, you have to set the last part of the class
144
	 * name as configuration value so the manager factory knows which class it
145
	 * has to instantiate.
146
	 *
147
	 * For example, if the name of the default class is
148
	 *
149
	 *  \Aimeos\MShop\Rule\Manager\Standard
150
	 *
151
	 * and you want to replace it with your own version named
152
	 *
153
	 *  \Aimeos\MShop\Rule\Manager\Mymanager
154
	 *
155
	 * then you have to set the this configuration option:
156
	 *
157
	 *  mshop/rule/manager/name = Mymanager
158
	 *
159
	 * The value is the last part of your own class name and it's case sensitive,
160
	 * so take care that the configuration value is exactly named like the last
161
	 * part of the class name.
162
	 *
163
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
164
	 * characters are possible! You should always start the last part of the class
165
	 * name with an upper case character and continue only with lower case characters
166
	 * or numbers. Avoid chamel case names like "MyManager"!
167
	 *
168
	 * @param string Last part of the class name
169
	 * @since 2021.04
170
	 */
171
172
	/** mshop/rule/manager/decorators/excludes
173
	 * Excludes decorators added by the "common" option from the rule manager
174
	 *
175
	 * Decorators extend the functionality of a class by adding new aspects
176
	 * (e.g. log what is currently done), executing the methods of the underlying
177
	 * class only in certain conditions (e.g. only for logged in users) or
178
	 * modify what is returned to the caller.
179
	 *
180
	 * This option allows you to remove a decorator added via
181
	 * "mshop/common/manager/decorators/default" before they are wrapped
182
	 * around the rule manager.
183
	 *
184
	 *  mshop/rule/manager/decorators/excludes = array( 'decorator1' )
185
	 *
186
	 * This would remove the decorator named "decorator1" from the list of
187
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
188
	 * "mshop/common/manager/decorators/default" for the rule manager.
189
	 *
190
	 * @param array List of decorator names
191
	 * @since 2021.04
192
	 * @see mshop/common/manager/decorators/default
193
	 * @see mshop/rule/manager/decorators/global
194
	 * @see mshop/rule/manager/decorators/local
195
	 */
196
197
	/** mshop/rule/manager/decorators/global
198
	 * Adds a list of globally available decorators only to the rule manager
199
	 *
200
	 * Decorators extend the functionality of a class by adding new aspects
201
	 * (e.g. log what is currently done), executing the methods of the underlying
202
	 * class only in certain conditions (e.g. only for logged in users) or
203
	 * modify what is returned to the caller.
204
	 *
205
	 * This option allows you to wrap global decorators
206
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the rule manager.
207
	 *
208
	 *  mshop/rule/manager/decorators/global = array( 'decorator1' )
209
	 *
210
	 * This would add the decorator named "decorator1" defined by
211
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the rule
212
	 * manager.
213
	 *
214
	 * @param array List of decorator names
215
	 * @since 2021.04
216
	 * @see mshop/common/manager/decorators/default
217
	 * @see mshop/rule/manager/decorators/excludes
218
	 * @see mshop/rule/manager/decorators/local
219
	 */
220
221
	/** mshop/rule/manager/decorators/local
222
	 * Adds a list of local decorators only to the rule manager
223
	 *
224
	 * Decorators extend the functionality of a class by adding new aspects
225
	 * (e.g. log what is currently done), executing the methods of the underlying
226
	 * class only in certain conditions (e.g. only for logged in users) or
227
	 * modify what is returned to the caller.
228
	 *
229
	 * This option allows you to wrap local decorators
230
	 * ("\Aimeos\MShop\Rule\Manager\Decorator\*") around the rule manager.
231
	 *
232
	 *  mshop/rule/manager/decorators/local = array( 'decorator2' )
233
	 *
234
	 * This would add the decorator named "decorator2" defined by
235
	 * "\Aimeos\MShop\Rule\Manager\Decorator\Decorator2" only to the rule
236
	 * manager.
237
	 *
238
	 * @param array List of decorator names
239
	 * @since 2021.04
240
	 * @see mshop/common/manager/decorators/default
241
	 * @see mshop/rule/manager/decorators/excludes
242
	 * @see mshop/rule/manager/decorators/global
243
	 */
244
245
	/** mshop/rule/manager/resource
246
	 * Name of the database connection resource to use
247
	 *
248
	 * You can configure a different database connection for each data domain
249
	 * and if no such connection name exists, the "db" connection will be used.
250
	 * It's also possible to use the same database connection for different
251
	 * data domains by configuring the same connection name using this setting.
252
	 *
253
	 * @param string Database connection name
254
	 * @since 2023.04
255
	 */
256
257
258
	/** mshop/rule/manager/delete/mysql
259
	 * Deletes the items matched by the given IDs from the database
260
	 *
261
	 * @see mshop/rule/manager/delete/ansi
262
	 */
263
264
	/** mshop/rule/manager/delete/ansi
265
	 * Deletes the items matched by the given IDs from the database
266
	 *
267
	 * Removes the records specified by the given IDs from the rule database.
268
	 * The records must be from the site that is configured via the
269
	 * context item.
270
	 *
271
	 * The ":cond" placeholder is replaced by the name of the ID column and
272
	 * the given ID or list of IDs while the site ID is bound to the question
273
	 * mark.
274
	 *
275
	 * The SQL statement should conform to the ANSI standard to be
276
	 * compatible with most relational database systems. This also
277
	 * includes using double quotes for table and column names.
278
	 *
279
	 * @param string SQL statement for deleting items
280
	 * @since 2021.04
281
	 * @see mshop/rule/manager/insert/ansi
282
	 * @see mshop/rule/manager/update/ansi
283
	 * @see mshop/rule/manager/newid/ansi
284
	 * @see mshop/rule/manager/search/ansi
285
	 * @see mshop/rule/manager/count/ansi
286
	 */
287
288
	 /** mshop/rule/manager/submanagers
289
	 * List of manager names that can be instantiated by the rule manager
290
	 *
291
	 * Managers provide a generic interface to the underlying storage.
292
	 * Each manager has or can have sub-managers caring about particular
293
	 * aspects. Each of these sub-managers can be instantiated by its
294
	 * parent manager using the getSubManager() method.
295
	 *
296
	 * The search keys from sub-managers can be normally used in the
297
	 * manager as well. It allows you to search for items of the manager
298
	 * using the search keys of the sub-managers to further limit the
299
	 * retrieved list of items.
300
	 *
301
	 * @param array List of sub-manager names
302
	 * @since 2021.04
303
	 */
304
305
	 /** mshop/rule/manager/insert/mysql
306
	 * Inserts a new rule record into the database table
307
	 *
308
	 * @see mshop/rule/manager/insert/ansi
309
	 */
310
311
	/** mshop/rule/manager/insert/ansi
312
	 * Inserts a new rule record into the database table
313
	 *
314
	 * Items with no ID yet (i.e. the ID is NULL) will be created in
315
	 * the database and the newly created ID retrieved afterwards
316
	 * using the "newid" SQL statement.
317
	 *
318
	 * The SQL statement must be a string suitable for being used as
319
	 * prepared statement. It must include question marks for binding
320
	 * the values from the rule item to the statement before they are
321
	 * sent to the database server. The number of question marks must
322
	 * be the same as the number of columns listed in the INSERT
323
	 * statement. The catalog of the columns must correspond to the
324
	 * catalog in the save() method, so the correct values are
325
	 * bound to the columns.
326
	 *
327
	 * The SQL statement should conform to the ANSI standard to be
328
	 * compatible with most relational database systems. This also
329
	 * includes using double quotes for table and column names.
330
	 *
331
	 * @param string SQL statement for inserting records
332
	 * @since 2021.04
333
	 * @see mshop/rule/manager/update/ansi
334
	 * @see mshop/rule/manager/newid/ansi
335
	 * @see mshop/rule/manager/delete/ansi
336
	 * @see mshop/rule/manager/search/ansi
337
	 * @see mshop/rule/manager/count/ansi
338
	 */
339
340
	 /** mshop/rule/manager/update/mysql
341
	 * Updates an existing rule record in the database
342
	 *
343
	 * @see mshop/rule/manager/update/ansi
344
	 */
345
346
	/** mshop/rule/manager/update/ansi
347
	 * Updates an existing rule record in the database
348
	 *
349
	 * Items which already have an ID (i.e. the ID is not NULL) will
350
	 * be updated in the database.
351
	 *
352
	 * The SQL statement must be a string suitable for being used as
353
	 * prepared statement. It must include question marks for binding
354
	 * the values from the rule item to the statement before they are
355
	 * sent to the database server. The catalog of the columns must
356
	 * correspond to the catalog in the save() method, so the
357
	 * correct values are bound to the columns.
358
	 *
359
	 * The SQL statement should conform to the ANSI standard to be
360
	 * compatible with most relational database systems. This also
361
	 * includes using double quotes for table and column names.
362
	 *
363
	 * @param string SQL statement for updating records
364
	 * @since 2021.04
365
	 * @see mshop/rule/manager/insert/ansi
366
	 * @see mshop/rule/manager/newid/ansi
367
	 * @see mshop/rule/manager/delete/ansi
368
	 * @see mshop/rule/manager/search/ansi
369
	 * @see mshop/rule/manager/count/ansi
370
	 */
371
372
	/** mshop/rule/manager/newid/mysql
373
	 * Retrieves the ID generated by the database when inserting a new record
374
	 *
375
	 * @see mshop/rule/manager/newid/ansi
376
	 */
377
378
	/** mshop/rule/manager/newid/ansi
379
	 * Retrieves the ID generated by the database when inserting a new record
380
	 *
381
	 * As soon as a new record is inserted into the database table,
382
	 * the database server generates a new and unique identifier for
383
	 * that record. This ID can be used for retrieving, updating and
384
	 * deleting that specific record from the table again.
385
	 *
386
	 * For MySQL:
387
	 *  SELECT LAST_INSERT_ID()
388
	 * For PostgreSQL:
389
	 *  SELECT currval('seq_mrul_id')
390
	 * For SQL Server:
391
	 *  SELECT SCOPE_IDENTITY()
392
	 * For Oracle:
393
	 *  SELECT "seq_mrul_id".CURRVAL FROM DUAL
394
	 *
395
	 * There's no way to retrive the new ID by a SQL statements that
396
	 * fits for most database servers as they implement their own
397
	 * specific way.
398
	 *
399
	 * @param string SQL statement for retrieving the last inserted record ID
400
	 * @since 2021.04
401
	 * @see mshop/rule/manager/insert/ansi
402
	 * @see mshop/rule/manager/update/ansi
403
	 * @see mshop/rule/manager/delete/ansi
404
	 * @see mshop/rule/manager/search/ansi
405
	 * @see mshop/rule/manager/count/ansi
406
	 */
407
408
	/** mshop/rule/manager/sitemode
409
	 * Mode how items from levels below or above in the site tree are handled
410
	 *
411
	 * By default, only items from the current site are fetched from the
412
	 * storage. If the ai-sites extension is installed, you can create a
413
	 * tree of sites. Then, this setting allows you to define for the
414
	 * whole rule domain if items from parent sites are inherited,
415
	 * sites from child sites are aggregated or both.
416
	 *
417
	 * Available constants for the site mode are:
418
	 * * 0 = only items from the current site
419
	 * * 1 = inherit items from parent sites
420
	 * * 2 = aggregate items from child sites
421
	 * * 3 = inherit and aggregate items at the same time
422
	 *
423
	 * You also need to set the mode in the locale manager
424
	 * (mshop/locale/manager/sitelevel) to one of the constants.
425
	 * If you set it to the same value, it will work as described but you
426
	 * can also use different modes. For example, if inheritance and
427
	 * aggregation is configured the locale manager but only inheritance
428
	 * in the domain manager because aggregating items makes no sense in
429
	 * this domain, then items wil be only inherited. Thus, you have full
430
	 * control over inheritance and aggregation in each domain.
431
	 *
432
	 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
433
	 * @since 2018.01
434
	 * @see mshop/locale/manager/sitelevel
435
	 */
436
437
	/** mshop/rule/manager/search/mysql
438
	 * Retrieves the records matched by the given criteria in the database
439
	 *
440
	 * @see mshop/rule/manager/search/ansi
441
	 */
442
443
	/** mshop/rule/manager/search/ansi
444
	 * Retrieves the records matched by the given criteria in the database
445
	 *
446
	 * Fetches the records matched by the given criteria from the rule
447
	 * database. The records must be from one of the sites that are
448
	 * configured via the context item. If the current site is part of
449
	 * a tree of sites, the SELECT statement can retrieve all records
450
	 * from the current site and the complete sub-tree of sites.
451
	 *
452
	 * As the records can normally be limited by criteria from sub-managers,
453
	 * their tables must be joined in the SQL context. This is done by
454
	 * using the "internaldeps" property from the definition of the ID
455
	 * column of the sub-managers. These internal dependencies specify
456
	 * the JOIN between the tables and the used columns for joining. The
457
	 * ":joins" placeholder is then replaced by the JOIN strings from
458
	 * the sub-managers.
459
	 *
460
	 * To limit the records matched, conditions can be added to the given
461
	 * criteria object. It can contain comparisons like column names that
462
	 * must match specific values which can be combined by AND, OR or NOT
463
	 * operators. The resulting string of SQL conditions replaces the
464
	 * ":cond" placeholder before the statement is sent to the database
465
	 * server.
466
	 *
467
	 * If the records that are retrieved should be cataloged by one or more
468
	 * columns, the generated string of column / sort direction pairs
469
	 * replaces the ":catalog" placeholder. In case no cataloging is required,
470
	 * the complete ORDER BY part including the "\/*-catalogby*\/...\/*catalogby-*\/"
471
	 * markers is removed to speed up retrieving the records. Columns of
472
	 * sub-managers can also be used for cataloging the result set but then
473
	 * no index can be used.
474
	 *
475
	 * The number of returned records can be limited and can start at any
476
	 * number between the begining and the end of the result set. For that
477
	 * the ":size" and ":start" placeholders are replaced by the
478
	 * corresponding values from the criteria object. The default values
479
	 * are 0 for the start and 100 for the size value.
480
	 *
481
	 * The SQL statement should conform to the ANSI standard to be
482
	 * compatible with most relational database systems. This also
483
	 * includes using double quotes for table and column names.
484
	 *
485
	 * @param string SQL statement for searching items
486
	 * @since 2021.04
487
	 * @see mshop/rule/manager/insert/ansi
488
	 * @see mshop/rule/manager/update/ansi
489
	 * @see mshop/rule/manager/newid/ansi
490
	 * @see mshop/rule/manager/delete/ansi
491
	 * @see mshop/rule/manager/count/ansi
492
	 */
493
494
	/** mshop/rule/manager/count/mysql
495
	 * Counts the number of records matched by the given criteria in the database
496
	 *
497
	 * @see mshop/rule/manager/count/ansi
498
	 */
499
500
	/** mshop/rule/manager/count/ansi
501
	 * Counts the number of records matched by the given criteria in the database
502
	 *
503
	 * Counts all records matched by the given criteria from the rule
504
	 * database. The records must be from one of the sites that are
505
	 * configured via the context item. If the current site is part of
506
	 * a tree of sites, the statement can count all records from the
507
	 * current site and the complete sub-tree of sites.
508
	 *
509
	 * As the records can normally be limited by criteria from sub-managers,
510
	 * their tables must be joined in the SQL context. This is done by
511
	 * using the "internaldeps" property from the definition of the ID
512
	 * column of the sub-managers. These internal dependencies specify
513
	 * the JOIN between the tables and the used columns for joining. The
514
	 * ":joins" placeholder is then replaced by the JOIN strings from
515
	 * the sub-managers.
516
	 *
517
	 * To limit the records matched, conditions can be added to the given
518
	 * criteria object. It can contain comparisons like column names that
519
	 * must match specific values which can be combined by AND, OR or NOT
520
	 * operators. The resulting string of SQL conditions replaces the
521
	 * ":cond" placeholder before the statement is sent to the database
522
	 * server.
523
	 *
524
	 * Both, the strings for ":joins" and for ":cond" are the same as for
525
	 * the "search" SQL statement.
526
	 *
527
	 * Contrary to the "search" statement, it doesn't return any records
528
	 * but instead the number of records that have been found. As counting
529
	 * thousands of records can be a long running task, the maximum number
530
	 * of counted records is limited for performance reasons.
531
	 *
532
	 * The SQL statement should conform to the ANSI standard to be
533
	 * compatible with most relational database systems. This also
534
	 * includes using double quotes for table and column names.
535
	 *
536
	 * @param string SQL statement for counting items
537
	 * @since 2021.04
538
	 * @see mshop/rule/manager/insert/ansi
539
	 * @see mshop/rule/manager/update/ansi
540
	 * @see mshop/rule/manager/newid/ansi
541
	 * @see mshop/rule/manager/delete/ansi
542
	 * @see mshop/rule/manager/search/ansi
543
	 */
544
}
545