Passed
Push — master ( c46287...9f1c00 )
by Aimeos
05:19
created

Standard::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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