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