Passed
Push — master ( e21098...29e583 )
by Aimeos
04:47
created

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2023
7
 * @package MShop
8
 * @subpackage Supplier
9
 */
10
11
12
namespace Aimeos\MShop\Supplier\Manager;
13
14
15
/**
16
 * Class \Aimeos\MShop\Supplier\Manager\Standard.
17
 *
18
 * @package MShop
19
 * @subpackage Supplier
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Supplier\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	/** mshop/supplier/manager/name
26
	 * Class name of the used supplier manager implementation
27
	 *
28
	 * Each default manager can be replace by an alternative imlementation.
29
	 * To use this implementation, you have to set the last part of the class
30
	 * name as configuration value so the manager factory knows which class it
31
	 * has to instantiate.
32
	 *
33
	 * For example, if the name of the default class is
34
	 *
35
	 *  \Aimeos\MShop\Supplier\Manager\Standard
36
	 *
37
	 * and you want to replace it with your own version named
38
	 *
39
	 *  \Aimeos\MShop\Supplier\Manager\Mymanager
40
	 *
41
	 * then you have to set the this configuration option:
42
	 *
43
	 *  mshop/supplier/manager/name = Mymanager
44
	 *
45
	 * The value is the last part of your own class name and it's case sensitive,
46
	 * so take care that the configuration value is exactly named like the last
47
	 * part of the class name.
48
	 *
49
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
50
	 * characters are possible! You should always start the last part of the class
51
	 * name with an upper case character and continue only with lower case characters
52
	 * or numbers. Avoid chamel case names like "MyManager"!
53
	 *
54
	 * @param string Last part of the class name
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
59
	/** mshop/supplier/manager/decorators/excludes
60
	 * Excludes decorators added by the "common" option from the supplier manager
61
	 *
62
	 * Decorators extend the functionality of a class by adding new aspects
63
	 * (e.g. log what is currently done), executing the methods of the underlying
64
	 * class only in certain conditions (e.g. only for logged in users) or
65
	 * modify what is returned to the caller.
66
	 *
67
	 * This option allows you to remove a decorator added via
68
	 * "mshop/common/manager/decorators/default" before they are wrapped
69
	 * around the supplier manager.
70
	 *
71
	 *  mshop/supplier/manager/decorators/excludes = array( 'decorator1' )
72
	 *
73
	 * This would remove the decorator named "decorator1" from the list of
74
	 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
75
	 * "mshop/common/manager/decorators/default" for the supplier manager.
76
	 *
77
	 * @param array List of decorator names
78
	 * @since 2014.03
79
	 * @category Developer
80
	 * @see mshop/common/manager/decorators/default
81
	 * @see mshop/supplier/manager/decorators/global
82
	 * @see mshop/supplier/manager/decorators/local
83
	 */
84
85
	/** mshop/supplier/manager/decorators/global
86
	 * Adds a list of globally available decorators only to the supplier manager
87
	 *
88
	 * Decorators extend the functionality of a class by adding new aspects
89
	 * (e.g. log what is currently done), executing the methods of the underlying
90
	 * class only in certain conditions (e.g. only for logged in users) or
91
	 * modify what is returned to the caller.
92
	 *
93
	 * This option allows you to wrap global decorators
94
	 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the supplier manager.
95
	 *
96
	 *  mshop/supplier/manager/decorators/global = array( 'decorator1' )
97
	 *
98
	 * This would add the decorator named "decorator1" defined by
99
	 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the supplier
100
	 * manager.
101
	 *
102
	 * @param array List of decorator names
103
	 * @since 2014.03
104
	 * @category Developer
105
	 * @see mshop/common/manager/decorators/default
106
	 * @see mshop/supplier/manager/decorators/excludes
107
	 * @see mshop/supplier/manager/decorators/local
108
	 */
109
110
	/** mshop/supplier/manager/decorators/local
111
	 * Adds a list of local decorators only to the supplier manager
112
	 *
113
	 * Decorators extend the functionality of a class by adding new aspects
114
	 * (e.g. log what is currently done), executing the methods of the underlying
115
	 * class only in certain conditions (e.g. only for logged in users) or
116
	 * modify what is returned to the caller.
117
	 *
118
	 * This option allows you to wrap local decorators
119
	 * ("\Aimeos\MShop\Supplier\Manager\Decorator\*") around the supplier manager.
120
	 *
121
	 *  mshop/supplier/manager/decorators/local = array( 'decorator2' )
122
	 *
123
	 * This would add the decorator named "decorator2" defined by
124
	 * "\Aimeos\MShop\Supplier\Manager\Decorator\Decorator2" only to the supplier
125
	 * manager.
126
	 *
127
	 * @param array List of decorator names
128
	 * @since 2014.03
129
	 * @category Developer
130
	 * @see mshop/common/manager/decorators/default
131
	 * @see mshop/supplier/manager/decorators/excludes
132
	 * @see mshop/supplier/manager/decorators/global
133
	 */
134
135
136
	use \Aimeos\MShop\Common\Manager\ListsRef\Traits;
137
	use \Aimeos\MShop\Common\Manager\AddressRef\Traits;
138
139
140
	private array $searchConfig = array(
141
		'supplier.id' => array(
142
			'code' => 'supplier.id',
143
			'internalcode' => 'msup."id"',
144
			'label' => 'ID',
145
			'type' => 'integer',
146
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
147
			'public' => false,
148
		),
149
		'supplier.siteid' => array(
150
			'code' => 'supplier.siteid',
151
			'internalcode' => 'msup."siteid"',
152
			'label' => 'Site ID',
153
			'type' => 'string',
154
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
155
			'public' => false,
156
		),
157
		'supplier.label' => array(
158
			'code' => 'supplier.label',
159
			'internalcode' => 'msup."label"',
160
			'label' => 'Label',
161
			'type' => 'string',
162
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
163
		),
164
		'supplier.code' => array(
165
			'code' => 'supplier.code',
166
			'internalcode' => 'msup."code"',
167
			'label' => 'Code',
168
			'type' => 'string',
169
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
170
		),
171
		'supplier.position' => array(
172
			'code' => 'supplier.position',
173
			'internalcode' => 'msup."pos"',
174
			'label' => 'Position',
175
			'type' => 'integer',
176
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
177
		),
178
		'supplier.status' => array(
179
			'code' => 'supplier.status',
180
			'internalcode' => 'msup."status"',
181
			'label' => 'Status',
182
			'type' => 'integer',
183
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
184
		),
185
		'supplier.ctime' => array(
186
			'code' => 'supplier.ctime',
187
			'internalcode' => 'msup."ctime"',
188
			'label' => 'Create date/time',
189
			'type' => 'datetime',
190
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
191
			'public' => false,
192
		),
193
		'supplier.mtime' => array(
194
			'code' => 'supplier.mtime',
195
			'internalcode' => 'msup."mtime"',
196
			'label' => 'Modify date/time',
197
			'type' => 'datetime',
198
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
199
			'public' => false,
200
		),
201
		'supplier.editor' => array(
202
			'code' => 'supplier.editor',
203
			'internalcode' => 'msup."editor"',
204
			'label' => 'Editor',
205
			'type' => 'string',
206
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
207
			'public' => false,
208
		),
209
		'supplier:has' => array(
210
			'code' => 'supplier:has()',
211
			'internalcode' => ':site AND :key AND msupli."id"',
212
			'internaldeps' => ['LEFT JOIN "mshop_supplier_list" AS msupli ON ( msupli."parentid" = msup."id" )'],
213
			'label' => 'Supplier has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
214
			'type' => 'null',
215
			'internaltype' => 'null',
216
			'public' => false,
217
		),
218
	);
219
220
	private array $cacheTags = [];
221
222
223
	/**
224
	 * Initializes the object.
225
	 *
226
	 * @param \Aimeos\MShop\ContextIface $context Context object
227
	 */
228
	public function __construct( \Aimeos\MShop\ContextIface $context )
229
	{
230
		parent::__construct( $context );
231
232
		/** mshop/supplier/manager/resource
233
		 * Name of the database connection resource to use
234
		 *
235
		 * You can configure a different database connection for each data domain
236
		 * and if no such connection name exists, the "db" connection will be used.
237
		 * It's also possible to use the same database connection for different
238
		 * data domains by configuring the same connection name using this setting.
239
		 *
240
		 * @param string Database connection name
241
		 * @since 2023.04
242
		 */
243
		$this->setResourceName( $context->config()->get( 'mshop/supplier/manager/resource', 'db-supplier' ) );
244
245
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
246
		$level = $context->config()->get( 'mshop/supplier/manager/sitemode', $level );
247
248
249
		$this->searchConfig['supplier:has']['function'] = function( &$source, array $params ) use ( $level ) {
250
251
			$keys = [];
252
253
			foreach( (array) ( $params[1] ?? '' ) as $type ) {
254
				foreach( (array) ( $params[2] ?? '' ) as $id ) {
255
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
256
				}
257
			}
258
259
			$sitestr = $this->siteString( 'msupli."siteid"', $level );
260
			$keystr = $this->toExpression( 'msupli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
261
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
262
263
			return $params;
264
		};
265
	}
266
267
268
	/**
269
	 * Removes old entries from the storage.
270
	 *
271
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
272
	 * @return \Aimeos\MShop\Supplier\Manager\Iface Manager object for chaining method calls
273
	 */
274
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
275
	{
276
		$path = 'mshop/supplier/manager/submanagers';
277
		foreach( $this->context()->config()->get( $path, array( 'address' ) ) as $domain ) {
278
			$this->object()->getSubManager( $domain )->clear( $siteids );
279
		}
280
281
		return $this->clearBase( $siteids, 'mshop/supplier/manager/delete' );
282
	}
283
284
285
	/**
286
	 * Commits the running database transaction on the connection identified by the given name
287
	 *
288
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
289
	 */
290
	public function commit() : \Aimeos\MShop\Common\Manager\Iface
291
	{
292
		parent::commit();
293
294
		$this->context()->cache()->deleteByTags( $this->cacheTags );
295
		$this->cacheTags = [];
296
297
		return $this;
298
	}
299
300
301
	/**
302
	 * Creates a new empty item instance
303
	 *
304
	 * @param array $values Values the item should be initialized with
305
	 * @return \Aimeos\MShop\Supplier\Item\Iface New supplier item object
306
	 */
307
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
308
	{
309
		$values['supplier.siteid'] = $values['supplier.siteid'] ?? $this->context()->locale()->getSiteId();
310
		return $this->createItemBase( $values );
311
	}
312
313
314
	/**
315
	 * Returns the available manager types
316
	 *
317
	 * @param bool $withsub Return also the resource type of sub-managers if true
318
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
319
	 */
320
	public function getResourceType( bool $withsub = true ) : array
321
	{
322
		$path = 'mshop/supplier/manager/submanagers';
323
		return $this->getResourceTypeBase( 'supplier', $path, array( 'address', 'lists' ), $withsub );
324
	}
325
326
327
	/**
328
	 * Returns the attributes that can be used for searching.
329
	 *
330
	 * @param bool $withsub Return also attributes of sub-managers if true
331
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items
332
	 */
333
	public function getSearchAttributes( bool $withsub = true ) : array
334
	{
335
		/** mshop/supplier/manager/submanagers
336
		 * List of manager names that can be instantiated by the supplier manager
337
		 *
338
		 * Managers provide a generic interface to the underlying storage.
339
		 * Each manager has or can have sub-managers caring about particular
340
		 * aspects. Each of these sub-managers can be instantiated by its
341
		 * parent manager using the getSubManager() method.
342
		 *
343
		 * The search keys from sub-managers can be normally used in the
344
		 * manager as well. It allows you to search for items of the manager
345
		 * using the search keys of the sub-managers to further limit the
346
		 * retrieved list of items.
347
		 *
348
		 * @param array List of sub-manager names
349
		 * @since 2014.03
350
		 * @category Developer
351
		 */
352
		$path = 'mshop/supplier/manager/submanagers';
353
354
		return $this->getSearchAttributesBase( $this->searchConfig, $path, ['address'], $withsub );
355
	}
356
357
358
	/**
359
	 * Removes multiple items.
360
	 *
361
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $items List of item objects or IDs of the items
362
	 * @return \Aimeos\MShop\Supplier\Manager\Iface Manager object for chaining method calls
363
	 */
364
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
365
	{
366
		/** mshop/supplier/manager/delete/mysql
367
		 * Deletes the items matched by the given IDs from the database
368
		 *
369
		 * @see mshop/supplier/manager/delete/ansi
370
		 */
371
372
		/** mshop/supplier/manager/delete/ansi
373
		 * Deletes the items matched by the given IDs from the database
374
		 *
375
		 * Removes the records specified by the given IDs from the supplier database.
376
		 * The records must be from the site that is configured via the
377
		 * context item.
378
		 *
379
		 * The ":cond" placeholder is replaced by the name of the ID column and
380
		 * the given ID or list of IDs while the site ID is bound to the question
381
		 * mark.
382
		 *
383
		 * The SQL statement should conform to the ANSI standard to be
384
		 * compatible with most relational database systems. This also
385
		 * includes using double quotes for table and column names.
386
		 *
387
		 * @param string SQL statement for deleting items
388
		 * @since 2014.03
389
		 * @category Developer
390
		 * @see mshop/supplier/manager/insert/ansi
391
		 * @see mshop/supplier/manager/update/ansi
392
		 * @see mshop/supplier/manager/newid/ansi
393
		 * @see mshop/supplier/manager/search/ansi
394
		 * @see mshop/supplier/manager/count/ansi
395
		 */
396
		$path = 'mshop/supplier/manager/delete';
397
398
		$this->deleteItemsBase( $items, $path )->deleteRefItems( $items );
399
		$this->cacheTags = array_merge( $this->cacheTags, map( $items )->cast()->prefix( 'supplier-' )->all() );
400
401
		return $this;
402
	}
403
404
405
	/**
406
	 * Returns the item specified by its code and domain/type if necessary
407
	 *
408
	 * @param string $code Code of the item
409
	 * @param string[] $ref List of domains to fetch list items and referenced items for
410
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
411
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
412
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
413
	 * @return \Aimeos\MShop\Supplier\Item\Iface Item object
414
	 */
415
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
416
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
417
	{
418
		return $this->findBase( array( 'supplier.code' => $code ), $ref, $default );
419
	}
420
421
422
	/**
423
	 * Returns the supplier item object specificed by its ID.
424
	 *
425
	 * @param string $id Unique supplier ID referencing an existing supplier
426
	 * @param string[] $ref List of domains to fetch list items and referenced items for
427
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
428
	 * @return \Aimeos\MShop\Supplier\Item\Iface Returns the supplier item of the given id
429
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
430
	 */
431
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
432
	{
433
		return $this->getItemBase( 'supplier.id', $id, $ref, $default );
434
	}
435
436
437
	/**
438
	 * Adds or updates an item object or a list of them.
439
	 *
440
	 * @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
441
	 * @param bool $fetch True if the new ID should be returned in the item
442
	 * @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items
443
	 */
444
	public function save( $items, bool $fetch = true )
445
	{
446
		$items = parent::save( $items, $fetch );
447
448
		if( ( $ids = map( $items )->getId()->filter() )->count() === map( $items )->count() ) {
449
			$this->cacheTags = array_merge( $this->cacheTags, map( $ids )->prefix( 'supplier-' )->all() );
450
		} else {
451
			$this->cacheTags[] = 'supplier';
452
		}
453
454
		return $items;
455
	}
456
457
458
	/**
459
	 * Saves a supplier item object.
460
	 *
461
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object
462
	 * @param bool $fetch True if the new ID should be returned in the item
463
	 * @return \Aimeos\MShop\Supplier\Item\Iface Updated item including the generated ID
464
	 */
465
	protected function saveItem( \Aimeos\MShop\Supplier\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Supplier\Item\Iface
466
	{
467
		if( !$item->isModified() )
468
		{
469
			$item = $this->saveAddressItems( $item, 'supplier', $fetch );
470
			return $this->saveListItems( $item, 'supplier', $fetch );
471
		}
472
473
		$context = $this->context();
474
		$conn = $context->db( $this->getResourceName() );
475
476
		$id = $item->getId();
477
		$date = date( 'Y-m-d H:i:s' );
478
		$columns = $this->object()->getSaveAttributes();
479
480
		if( $id === null )
481
		{
482
			/** mshop/supplier/manager/insert/mysql
483
			 * Inserts a new supplier record into the database table
484
			 *
485
			 * @see mshop/supplier/manager/insert/ansi
486
			 */
487
488
			/** mshop/supplier/manager/insert/ansi
489
			 * Inserts a new supplier record into the database table
490
			 *
491
			 * Items with no ID yet (i.e. the ID is NULL) will be created in
492
			 * the database and the newly created ID retrieved afterwards
493
			 * using the "newid" SQL statement.
494
			 *
495
			 * The SQL statement must be a string suitable for being used as
496
			 * prepared statement. It must include question marks for binding
497
			 * the values from the supplier item to the statement before they are
498
			 * sent to the database server. The number of question marks must
499
			 * be the same as the number of columns listed in the INSERT
500
			 * statement. The order of the columns must correspond to the
501
			 * order in the save() method, so the correct values are
502
			 * bound to the columns.
503
			 *
504
			 * The SQL statement should conform to the ANSI standard to be
505
			 * compatible with most relational database systems. This also
506
			 * includes using double quotes for table and column names.
507
			 *
508
			 * @param string SQL statement for inserting records
509
			 * @since 2014.03
510
			 * @category Developer
511
			 * @see mshop/supplier/manager/update/ansi
512
			 * @see mshop/supplier/manager/newid/ansi
513
			 * @see mshop/supplier/manager/delete/ansi
514
			 * @see mshop/supplier/manager/search/ansi
515
			 * @see mshop/supplier/manager/count/ansi
516
			 */
517
			$path = 'mshop/supplier/manager/insert';
518
			$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

518
			$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
519
		}
520
		else
521
		{
522
			/** mshop/supplier/manager/update/mysql
523
			 * Updates an existing supplier record in the database
524
			 *
525
			 * @see mshop/supplier/manager/update/ansi
526
			 */
527
528
			/** mshop/supplier/manager/update/ansi
529
			 * Updates an existing supplier record in the database
530
			 *
531
			 * Items which already have an ID (i.e. the ID is not NULL) will
532
			 * be updated in the database.
533
			 *
534
			 * The SQL statement must be a string suitable for being used as
535
			 * prepared statement. It must include question marks for binding
536
			 * the values from the supplier item to the statement before they are
537
			 * sent to the database server. The order of the columns must
538
			 * correspond to the order in the save() method, so the
539
			 * correct values are bound to the columns.
540
			 *
541
			 * The SQL statement should conform to the ANSI standard to be
542
			 * compatible with most relational database systems. This also
543
			 * includes using double quotes for table and column names.
544
			 *
545
			 * @param string SQL statement for updating records
546
			 * @since 2014.03
547
			 * @category Developer
548
			 * @see mshop/supplier/manager/insert/ansi
549
			 * @see mshop/supplier/manager/newid/ansi
550
			 * @see mshop/supplier/manager/delete/ansi
551
			 * @see mshop/supplier/manager/search/ansi
552
			 * @see mshop/supplier/manager/count/ansi
553
			 */
554
			$path = 'mshop/supplier/manager/update';
555
			$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
556
		}
557
558
		$idx = 1;
559
		$stmt = $this->getCachedStatement( $conn, $path, $sql );
560
561
		foreach( $columns as $name => $entry ) {
562
			$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
0 ignored issues
show
Bug introduced by
It seems like $entry->getInternalType() can also be of type string; however, parameter $type of Aimeos\Base\DB\Statement\Iface::bind() does only seem to accept integer, 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

562
			$stmt->bind( $idx++, $item->get( $name ), /** @scrutinizer ignore-type */ $entry->getInternalType() );
Loading history...
563
		}
564
565
		$stmt->bind( $idx++, $item->getCode() );
566
		$stmt->bind( $idx++, $item->getLabel() );
567
		$stmt->bind( $idx++, $item->getPosition(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
568
		$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
569
		$stmt->bind( $idx++, $date ); // mtime
570
		$stmt->bind( $idx++, $context->editor() );
571
572
		if( $id !== null ) {
573
			$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' );
574
			$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
575
		} else {
576
			$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) );
577
			$stmt->bind( $idx++, $date ); // ctime
578
		}
579
580
		$stmt->execute()->finish();
581
582
		if( $id === null )
583
		{
584
			/** mshop/supplier/manager/newid/mysql
585
			 * Retrieves the ID generated by the database when inserting a new record
586
			 *
587
			 * @see mshop/supplier/manager/newid/ansi
588
			 */
589
590
			/** mshop/supplier/manager/newid/ansi
591
			 * Retrieves the ID generated by the database when inserting a new record
592
			 *
593
			 * As soon as a new record is inserted into the database table,
594
			 * the database server generates a new and unique identifier for
595
			 * that record. This ID can be used for retrieving, updating and
596
			 * deleting that specific record from the table again.
597
			 *
598
			 * For MySQL:
599
			 *  SELECT LAST_INSERT_ID()
600
			 * For PostgreSQL:
601
			 *  SELECT currval('seq_msup_id')
602
			 * For SQL Server:
603
			 *  SELECT SCOPE_IDENTITY()
604
			 * For Oracle:
605
			 *  SELECT "seq_msup_id".CURRVAL FROM DUAL
606
			 *
607
			 * There's no way to retrive the new ID by a SQL statements that
608
			 * fits for most database servers as they implement their own
609
			 * specific way.
610
			 *
611
			 * @param string SQL statement for retrieving the last inserted record ID
612
			 * @since 2014.03
613
			 * @category Developer
614
			 * @see mshop/supplier/manager/insert/ansi
615
			 * @see mshop/supplier/manager/update/ansi
616
			 * @see mshop/supplier/manager/delete/ansi
617
			 * @see mshop/supplier/manager/search/ansi
618
			 * @see mshop/supplier/manager/count/ansi
619
			 */
620
			$path = 'mshop/supplier/manager/newid';
621
			$id = $this->newId( $conn, $path );
622
		}
623
624
		$item->setId( $id );
625
626
		$item = $this->saveAddressItems( $item, 'supplier', $fetch );
627
		return $this->saveListItems( $item, 'supplier', $fetch );
628
	}
629
630
631
	/**
632
	 * Returns the item objects matched by the given search criteria.
633
	 *
634
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
635
	 * @param string[] $ref List of domains to fetch list items and referenced items for
636
	 * @param int|null &$total Number of items that are available in total
637
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Supplier\Item\Ifac with ids as keys
638
	 */
639
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
640
	{
641
		$map = [];
642
		$context = $this->context();
643
		$conn = $context->db( $this->getResourceName() );
644
645
			$required = array( 'supplier' );
646
647
			/** mshop/supplier/manager/sitemode
648
			 * Mode how items from levels below or above in the site tree are handled
649
			 *
650
			 * By default, only items from the current site are fetched from the
651
			 * storage. If the ai-sites extension is installed, you can create a
652
			 * tree of sites. Then, this setting allows you to define for the
653
			 * whole supplier domain if items from parent sites are inherited,
654
			 * sites from child sites are aggregated or both.
655
			 *
656
			 * Available constants for the site mode are:
657
			 * * 0 = only items from the current site
658
			 * * 1 = inherit items from parent sites
659
			 * * 2 = aggregate items from child sites
660
			 * * 3 = inherit and aggregate items at the same time
661
			 *
662
			 * You also need to set the mode in the locale manager
663
			 * (mshop/locale/manager/sitelevel) to one of the constants.
664
			 * If you set it to the same value, it will work as described but you
665
			 * can also use different modes. For example, if inheritance and
666
			 * aggregation is configured the locale manager but only inheritance
667
			 * in the domain manager because aggregating items makes no sense in
668
			 * this domain, then items wil be only inherited. Thus, you have full
669
			 * control over inheritance and aggregation in each domain.
670
			 *
671
			 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
672
			 * @category Developer
673
			 * @since 2018.01
674
			 * @see mshop/locale/manager/sitelevel
675
			 */
676
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
677
			$level = $context->config()->get( 'mshop/supplier/manager/sitemode', $level );
678
679
			/** mshop/supplier/manager/search/mysql
680
			 * Retrieves the records matched by the given criteria in the database
681
			 *
682
			 * @see mshop/supplier/manager/search/ansi
683
			 */
684
685
			/** mshop/supplier/manager/search/ansi
686
			 * Retrieves the records matched by the given criteria in the database
687
			 *
688
			 * Fetches the records matched by the given criteria from the supplier
689
			 * database. The records must be from one of the sites that are
690
			 * configured via the context item. If the current site is part of
691
			 * a tree of sites, the SELECT statement can retrieve all records
692
			 * from the current site and the complete sub-tree of sites.
693
			 *
694
			 * As the records can normally be limited by criteria from sub-managers,
695
			 * their tables must be joined in the SQL context. This is done by
696
			 * using the "internaldeps" property from the definition of the ID
697
			 * column of the sub-managers. These internal dependencies specify
698
			 * the JOIN between the tables and the used columns for joining. The
699
			 * ":joins" placeholder is then replaced by the JOIN strings from
700
			 * the sub-managers.
701
			 *
702
			 * To limit the records matched, conditions can be added to the given
703
			 * criteria object. It can contain comparisons like column names that
704
			 * must match specific values which can be combined by AND, OR or NOT
705
			 * operators. The resulting string of SQL conditions replaces the
706
			 * ":cond" placeholder before the statement is sent to the database
707
			 * server.
708
			 *
709
			 * If the records that are retrieved should be ordered by one or more
710
			 * columns, the generated string of column / sort direction pairs
711
			 * replaces the ":order" placeholder. In case no ordering is required,
712
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
713
			 * markers is removed to speed up retrieving the records. Columns of
714
			 * sub-managers can also be used for ordering the result set but then
715
			 * no index can be used.
716
			 *
717
			 * The number of returned records can be limited and can start at any
718
			 * number between the begining and the end of the result set. For that
719
			 * the ":size" and ":start" placeholders are replaced by the
720
			 * corresponding values from the criteria object. The default values
721
			 * are 0 for the start and 100 for the size value.
722
			 *
723
			 * The SQL statement should conform to the ANSI standard to be
724
			 * compatible with most relational database systems. This also
725
			 * includes using double quotes for table and column names.
726
			 *
727
			 * @param string SQL statement for searching items
728
			 * @since 2014.03
729
			 * @category Developer
730
			 * @see mshop/supplier/manager/insert/ansi
731
			 * @see mshop/supplier/manager/update/ansi
732
			 * @see mshop/supplier/manager/newid/ansi
733
			 * @see mshop/supplier/manager/delete/ansi
734
			 * @see mshop/supplier/manager/count/ansi
735
			 */
736
			$cfgPathSearch = 'mshop/supplier/manager/search';
737
738
			/** mshop/supplier/manager/count/mysql
739
			 * Counts the number of records matched by the given criteria in the database
740
			 *
741
			 * @see mshop/supplier/manager/count/ansi
742
			 */
743
744
			/** mshop/supplier/manager/count/ansi
745
			 * Counts the number of records matched by the given criteria in the database
746
			 *
747
			 * Counts all records matched by the given criteria from the supplier
748
			 * database. The records must be from one of the sites that are
749
			 * configured via the context item. If the current site is part of
750
			 * a tree of sites, the statement can count all records from the
751
			 * current site and the complete sub-tree of sites.
752
			 *
753
			 * As the records can normally be limited by criteria from sub-managers,
754
			 * their tables must be joined in the SQL context. This is done by
755
			 * using the "internaldeps" property from the definition of the ID
756
			 * column of the sub-managers. These internal dependencies specify
757
			 * the JOIN between the tables and the used columns for joining. The
758
			 * ":joins" placeholder is then replaced by the JOIN strings from
759
			 * the sub-managers.
760
			 *
761
			 * To limit the records matched, conditions can be added to the given
762
			 * criteria object. It can contain comparisons like column names that
763
			 * must match specific values which can be combined by AND, OR or NOT
764
			 * operators. The resulting string of SQL conditions replaces the
765
			 * ":cond" placeholder before the statement is sent to the database
766
			 * server.
767
			 *
768
			 * Both, the strings for ":joins" and for ":cond" are the same as for
769
			 * the "search" SQL statement.
770
			 *
771
			 * Contrary to the "search" statement, it doesn't return any records
772
			 * but instead the number of records that have been found. As counting
773
			 * thousands of records can be a long running task, the maximum number
774
			 * of counted records is limited for performance reasons.
775
			 *
776
			 * The SQL statement should conform to the ANSI standard to be
777
			 * compatible with most relational database systems. This also
778
			 * includes using double quotes for table and column names.
779
			 *
780
			 * @param string SQL statement for counting items
781
			 * @since 2014.03
782
			 * @category Developer
783
			 * @see mshop/supplier/manager/insert/ansi
784
			 * @see mshop/supplier/manager/update/ansi
785
			 * @see mshop/supplier/manager/newid/ansi
786
			 * @see mshop/supplier/manager/delete/ansi
787
			 * @see mshop/supplier/manager/search/ansi
788
			 */
789
			$cfgPathCount = 'mshop/supplier/manager/count';
790
791
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
792
793
			while( ( $row = $results->fetch() ) !== null ) {
794
				$map[$row['supplier.id']] = $row;
795
			}
796
797
		$addrItems = [];
798
		if( in_array( 'supplier/address', $ref, true ) ) {
799
			$addrItems = $this->getAddressItems( array_keys( $map ), 'supplier' );
800
		}
801
802
		return $this->buildItems( $map, $ref, 'supplier', $addrItems );
803
	}
804
805
806
	/**
807
	 * Creates a new manager for supplier
808
	 *
809
	 * @param string $manager Name of the sub manager type in lower case
810
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
811
	 * @return \Aimeos\MShop\Common\Manager\Iface Sub manager
812
	 * @throws \Aimeos\MShop\Supplier\Exception If creating manager failed
813
	 */
814
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
815
	{
816
		return $this->getSubManagerBase( 'supplier', $manager, $name );
817
	}
818
819
820
	/**
821
	 * Creates a filter object.
822
	 *
823
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
824
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
825
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
826
	 */
827
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
828
	{
829
		return $this->filterBase( 'supplier', $default );
830
	}
831
832
833
	/**
834
	 * Creates a new supplier item.
835
	 *
836
	 * @param array $values List of attributes for supplier item
837
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listitems List of list items
838
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
839
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $addresses List of address items
840
	 * @return \Aimeos\MShop\Supplier\Item\Iface New supplier item
841
	 */
842
	protected function createItemBase( array $values = [], array $listitems = [], array $refItems = [],
843
		array $addresses = [] ) : \Aimeos\MShop\Common\Item\Iface
844
	{
845
		return new \Aimeos\MShop\Supplier\Item\Standard( $values, $listitems, $refItems, $addresses );
846
	}
847
}
848