Completed
Push — master ( 22edf4...dc114c )
by Aimeos
11:09
created

Standard::searchItems()   B

Complexity

Conditions 8
Paths 31

Size

Total Lines 205
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 36
nc 31
nop 3
dl 0
loc 205
rs 8.0995
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
6
 * @package MShop
7
 * @subpackage Subscription
8
 */
9
10
11
namespace Aimeos\MShop\Subscription\Manager;
12
13
14
/**
15
 * Default subscription manager implementation
16
 *
17
 * @package MShop
18
 * @subpackage Subscription
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Subscription\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private $searchConfig = array(
25
		'subscription.id' => array(
26
			'code' => 'subscription.id',
27
			'internalcode' => 'mord."id"',
28
			'label' => 'Subscription ID',
29
			'type' => 'integer',
30
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
31
		),
32
		'subscription.siteid' => array(
33
			'code' => 'subscription.siteid',
34
			'internalcode' => 'mord."siteid"',
35
			'label' => 'Site ID',
36
			'type' => 'string',
37
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
38
			'public' => false,
39
		),
40
		'subscription.ordbaseid' => array(
41
			'code' => 'subscription.ordbaseid',
42
			'internalcode' => 'mord."baseid"',
43
			'label' => 'Order base ID',
44
			'type' => 'integer',
45
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
46
			'public' => false,
47
		),
48
		'subscription.ordprodid' => array(
49
			'code' => 'subscription.ordprodid',
50
			'internalcode' => 'mord."ordprodid"',
51
			'label' => 'Order product ID',
52
			'type' => 'integer',
53
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
54
			'public' => false,
55
		),
56
		'subscription.datenext' => array(
57
			'code' => 'subscription.datenext',
58
			'internalcode' => 'mord."next"',
59
			'label' => 'Next renewal date/time',
60
			'type' => 'datetime',
61
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
62
		),
63
		'subscription.dateend' => array(
64
			'code' => 'subscription.dateend',
65
			'internalcode' => 'mord."end"',
66
			'label' => 'End of subscription',
67
			'type' => 'datetime',
68
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
69
		),
70
		'subscription.interval' => array(
71
			'code' => 'subscription.interval',
72
			'internalcode' => 'mord."interval"',
73
			'label' => 'Renewal interval',
74
			'type' => 'string',
75
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
76
		),
77
		'subscription.reason' => array(
78
			'code' => 'subscription.reason',
79
			'internalcode' => 'mord."reason"',
80
			'label' => 'Subscription end reason',
81
			'type' => 'integer',
82
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
83
		),
84
		'subscription.period' => array(
85
			'code' => 'subscription.period',
86
			'internalcode' => 'mord."period"',
87
			'label' => 'Subscription period count',
88
			'type' => 'integer',
89
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
90
		),
91
		'subscription.productid' => array(
92
			'code' => 'subscription.productid',
93
			'internalcode' => 'mord."productid"',
94
			'label' => 'Subscription product ID',
95
			'type' => 'string',
96
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
97
		),
98
		'subscription.status' => array(
99
			'code' => 'subscription.status',
100
			'internalcode' => 'mord."status"',
101
			'label' => 'Subscription status',
102
			'type' => 'integer',
103
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
104
		),
105
		'subscription.ctime' => array(
106
			'code' => 'subscription.ctime',
107
			'internalcode' => 'mord."ctime"',
108
			'label' => 'Create date/time',
109
			'type' => 'datetime',
110
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
111
			'public' => false,
112
		),
113
		'subscription.mtime' => array(
114
			'code' => 'subscription.mtime',
115
			'internalcode' => 'mord."mtime"',
116
			'label' => 'Modify date/time',
117
			'type' => 'datetime',
118
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
119
			'public' => false,
120
		),
121
		'subscription.editor' => array(
122
			'code' => 'subscription.editor',
123
			'internalcode' => 'mord."editor"',
124
			'label' => 'Editor',
125
			'type' => 'string',
126
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
127
			'public' => false,
128
		),
129
	);
130
131
132
	/**
133
	 * Creates the manager that will use the given context object.
134
	 *
135
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
136
	 */
137
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
138
	{
139
		parent::__construct( $context );
140
		$this->setResourceName( 'db-order' );
141
	}
142
143
144
	/**
145
	 * Counts the number items that are available for the values of the given key.
146
	 *
147
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria
148
	 * @param string $key Search key to aggregate items for
149
	 * @param string|null $value Search key for aggregating the value column
150
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
151
	 * @return \Aimeos\Map List of the search keys as key and the number of counted items as value
152
	 */
153
	public function aggregate( \Aimeos\MW\Criteria\Iface $search, string $key, string $value = null, string $type = null ) : \Aimeos\Map
154
	{
155
		/** mshop/subscription/manager/standard/aggregate/mysql
156
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
157
		 *
158
		 * @see mshop/subscription/manager/standard/aggregate/ansi
159
		 */
160
161
		/** mshop/subscription/manager/standard/aggregate/ansi
162
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
163
		 *
164
		 * Groups all records by the values in the key column and counts their
165
		 * occurence. The matched records can be limited by the given criteria
166
		 * from the subscription database. The records must be from one of the sites
167
		 * that are configured via the context item. If the current site is part
168
		 * of a tree of sites, the statement can count all records from the
169
		 * current site and the complete sub-tree of sites.
170
		 *
171
		 * As the records can normally be limited by criteria from sub-managers,
172
		 * their tables must be joined in the SQL context. This is done by
173
		 * using the "internaldeps" property from the definition of the ID
174
		 * column of the sub-managers. These internal dependencies specify
175
		 * the JOIN between the tables and the used columns for joining. The
176
		 * ":joins" placeholder is then replaced by the JOIN strings from
177
		 * the sub-managers.
178
		 *
179
		 * To limit the records matched, conditions can be added to the given
180
		 * criteria object. It can contain comparisons like column names that
181
		 * must match specific values which can be combined by AND, OR or NOT
182
		 * operators. The resulting string of SQL conditions replaces the
183
		 * ":cond" placeholder before the statement is sent to the database
184
		 * server.
185
		 *
186
		 * This statement doesn't return any records. Instead, it returns pairs
187
		 * of the different values found in the key column together with the
188
		 * number of records that have been found for that key values.
189
		 *
190
		 * The SQL statement should conform to the ANSI standard to be
191
		 * compatible with most relational database systems. This also
192
		 * includes using double quotes for table and column names.
193
		 *
194
		 * @param string SQL statement for aggregating subscription items
195
		 * @since 2018.04
196
		 * @category Developer
197
		 * @see mshop/subscription/manager/standard/insert/ansi
198
		 * @see mshop/subscription/manager/standard/update/ansi
199
		 * @see mshop/subscription/manager/standard/newid/ansi
200
		 * @see mshop/subscription/manager/standard/delete/ansi
201
		 * @see mshop/subscription/manager/standard/search/ansi
202
		 * @see mshop/subscription/manager/standard/count/ansi
203
		 */
204
205
		$cfgkey = 'mshop/subscription/manager/standard/aggregate' . $type;
206
		return $this->aggregateBase( $search, $key, $cfgkey, array( 'subscription' ), $value );
207
	}
208
209
210
	/**
211
	 * Removes old entries from the storage.
212
	 *
213
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
214
	 * @return \Aimeos\MShop\Subscription\Manager\Iface Manager object for chaining method calls
215
	 */
216
	public function clear( array $siteids ) : \Aimeos\MShop\Common\Manager\Iface
217
	{
218
		$path = 'mshop/subscription/manager/submanagers';
219
		foreach( $this->getContext()->getConfig()->get( $path, [] ) as $domain ) {
220
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
221
		}
222
223
		return $this->clearBase( $siteids, 'mshop/subscription/manager/standard/delete' );
224
	}
225
226
227
	/**
228
	 * Creates a new empty item instance
229
	 *
230
	 * @param array $values Values the item should be initialized with
231
	 * @return \Aimeos\MShop\Subscription\Item\Iface New subscription item object
232
	 */
233
	public function createItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
234
	{
235
		$values['subscription.siteid'] = $this->getContext()->getLocale()->getSiteId();
236
		return $this->createItemBase( $values );
237
	}
238
239
240
	/**
241
	 * Creates a search critera object
242
	 *
243
	 * @param bool $default Add default criteria (optional)
244
	 * @param bool $site TRUE to add site criteria to show orders with available products only
245
	 * @return \Aimeos\MW\Criteria\Iface New search criteria object
246
	 */
247
	public function createSearch( bool $default = false, bool $site = false ) : \Aimeos\MW\Criteria\Iface
248
	{
249
		$search = parent::createSearch();
250
251
		if( $default === true ) {
252
			$search = $this->createSearchBase( 'subscription' );
253
		}
254
255
		if( $site === true )
256
		{
257
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
258
			$search->setConditions( $search->combine( '&&', [
259
				$this->getSiteCondition( $search, 'order.base.product.siteid', $level ),
260
				$search->getConditions()
261
			] ) );
262
		}
263
264
		return $search;
265
	}
266
267
268
	/**
269
	 * Creates a one-time subscription in the storage from the given invoice object.
270
	 *
271
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription item with necessary values
272
	 * @param bool $fetch True if the new ID should be returned in the item
273
	 * @return \Aimeos\MShop\Subscription\Item\Iface Updated item including the generated ID
274
	 */
275
	public function saveItem( \Aimeos\MShop\Subscription\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Subscription\Item\Iface
276
	{
277
		if( $item->getOrderProductId() === null ) {
278
			throw new \Aimeos\MShop\Subscription\Exception( 'Required order product ID is missing' );
279
		}
280
281
		if( !$item->isModified() ) {
282
			return $item;
283
		}
284
285
		$context = $this->getContext();
286
287
		$dbm = $context->getDatabaseManager();
288
		$dbname = $this->getResourceName();
289
		$conn = $dbm->acquire( $dbname );
290
291
		try
292
		{
293
			$id = $item->getId();
294
			$date = date( 'Y-m-d H:i:s' );
295
			$columns = $this->getObject()->getSaveAttributes();
296
297
			if( $id === null )
298
			{
299
				/** mshop/subscription/manager/standard/insert/mysql
300
				 * Inserts a new subscription record into the database table
301
				 *
302
				 * @see mshop/subscription/manager/standard/insert/ansi
303
				 */
304
305
				/** mshop/subscription/manager/standard/insert/ansi
306
				 * Inserts a new subscription record into the database table
307
				 *
308
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
309
				 * the database and the newly created ID retrieved afterwards
310
				 * using the "newid" SQL statement.
311
				 *
312
				 * The SQL statement must be a string suitable for being used as
313
				 * prepared statement. It must include question marks for binding
314
				 * the values from the subscription item to the statement before they are
315
				 * sent to the database server. The number of question marks must
316
				 * be the same as the number of columns listed in the INSERT
317
				 * statement. The subscription of the columns must correspond to the
318
				 * subscription in the saveItems() method, so the correct values are
319
				 * bound to the columns.
320
				 *
321
				 * The SQL statement should conform to the ANSI standard to be
322
				 * compatible with most relational database systems. This also
323
				 * includes using double quotes for table and column names.
324
				 *
325
				 * @param string SQL statement for inserting records
326
				 * @since 2018.04
327
				 * @category Developer
328
				 * @see mshop/subscription/manager/standard/update/ansi
329
				 * @see mshop/subscription/manager/standard/newid/ansi
330
				 * @see mshop/subscription/manager/standard/delete/ansi
331
				 * @see mshop/subscription/manager/standard/search/ansi
332
				 * @see mshop/subscription/manager/standard/count/ansi
333
				 */
334
				$path = 'mshop/subscription/manager/standard/insert';
335
				$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

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