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-2024 |
7
|
|
|
* @package MShop |
8
|
|
|
* @subpackage Product |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MShop\Product\Manager; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default product manager. |
17
|
|
|
* |
18
|
|
|
* @package MShop |
19
|
|
|
* @subpackage Product |
20
|
|
|
*/ |
21
|
|
|
class Standard |
22
|
|
|
extends \Aimeos\MShop\Common\Manager\Base |
23
|
|
|
implements \Aimeos\MShop\Product\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface, |
24
|
|
|
\Aimeos\MShop\Common\Manager\ListsRef\Iface, \Aimeos\MShop\Common\Manager\PropertyRef\Iface |
25
|
|
|
{ |
26
|
|
|
/** mshop/product/manager/name |
27
|
|
|
* Class name of the used product manager implementation |
28
|
|
|
* |
29
|
|
|
* Each default manager can be replace by an alternative imlementation. |
30
|
|
|
* To use this implementation, you have to set the last part of the class |
31
|
|
|
* name as configuration value so the manager factory knows which class it |
32
|
|
|
* has to instantiate. |
33
|
|
|
* |
34
|
|
|
* For example, if the name of the default class is |
35
|
|
|
* |
36
|
|
|
* \Aimeos\MShop\Product\Manager\Standard |
37
|
|
|
* |
38
|
|
|
* and you want to replace it with your own version named |
39
|
|
|
* |
40
|
|
|
* \Aimeos\MShop\Product\Manager\Mymanager |
41
|
|
|
* |
42
|
|
|
* then you have to set the this configuration option: |
43
|
|
|
* |
44
|
|
|
* mshop/product/manager/name = Mymanager |
45
|
|
|
* |
46
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
47
|
|
|
* so take care that the configuration value is exactly named like the last |
48
|
|
|
* part of the class name. |
49
|
|
|
* |
50
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
51
|
|
|
* characters are possible! You should always start the last part of the class |
52
|
|
|
* name with an upper case character and continue only with lower case characters |
53
|
|
|
* or numbers. Avoid chamel case names like "MyManager"! |
54
|
|
|
* |
55
|
|
|
* @param string Last part of the class name |
56
|
|
|
* @since 2014.03 |
57
|
|
|
* @category Developer |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
/** mshop/product/manager/decorators/excludes |
61
|
|
|
* Excludes decorators added by the "common" option from the product manager |
62
|
|
|
* |
63
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
64
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
65
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
66
|
|
|
* modify what is returned to the caller. |
67
|
|
|
* |
68
|
|
|
* This option allows you to remove a decorator added via |
69
|
|
|
* "mshop/common/manager/decorators/default" before they are wrapped |
70
|
|
|
* around the product manager. |
71
|
|
|
* |
72
|
|
|
* mshop/product/manager/decorators/excludes = array( 'decorator1' ) |
73
|
|
|
* |
74
|
|
|
* This would remove the decorator named "decorator1" from the list of |
75
|
|
|
* common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via |
76
|
|
|
* "mshop/common/manager/decorators/default" for the product manager. |
77
|
|
|
* |
78
|
|
|
* @param array List of decorator names |
79
|
|
|
* @since 2014.03 |
80
|
|
|
* @category Developer |
81
|
|
|
* @see mshop/common/manager/decorators/default |
82
|
|
|
* @see mshop/product/manager/decorators/global |
83
|
|
|
* @see mshop/product/manager/decorators/local |
84
|
|
|
*/ |
85
|
|
|
|
86
|
|
|
/** mshop/product/manager/decorators/global |
87
|
|
|
* Adds a list of globally available decorators only to the product manager |
88
|
|
|
* |
89
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
90
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
91
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
92
|
|
|
* modify what is returned to the caller. |
93
|
|
|
* |
94
|
|
|
* This option allows you to wrap global decorators |
95
|
|
|
* ("\Aimeos\MShop\Common\Manager\Decorator\*") around the product manager. |
96
|
|
|
* |
97
|
|
|
* mshop/product/manager/decorators/global = array( 'decorator1' ) |
98
|
|
|
* |
99
|
|
|
* This would add the decorator named "decorator1" defined by |
100
|
|
|
* "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the product |
101
|
|
|
* manager. |
102
|
|
|
* |
103
|
|
|
* @param array List of decorator names |
104
|
|
|
* @since 2014.03 |
105
|
|
|
* @category Developer |
106
|
|
|
* @see mshop/common/manager/decorators/default |
107
|
|
|
* @see mshop/product/manager/decorators/excludes |
108
|
|
|
* @see mshop/product/manager/decorators/local |
109
|
|
|
*/ |
110
|
|
|
|
111
|
|
|
/** mshop/product/manager/decorators/local |
112
|
|
|
* Adds a list of local decorators only to the product manager |
113
|
|
|
* |
114
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
115
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
116
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
117
|
|
|
* modify what is returned to the caller. |
118
|
|
|
* |
119
|
|
|
* This option allows you to wrap local decorators |
120
|
|
|
* ("\Aimeos\MShop\Product\Manager\Decorator\*") around the product manager. |
121
|
|
|
* |
122
|
|
|
* mshop/product/manager/decorators/local = array( 'decorator2' ) |
123
|
|
|
* |
124
|
|
|
* This would add the decorator named "decorator2" defined by |
125
|
|
|
* "\Aimeos\MShop\Product\Manager\Decorator\Decorator2" only to the product |
126
|
|
|
* manager. |
127
|
|
|
* |
128
|
|
|
* @param array List of decorator names |
129
|
|
|
* @since 2014.03 |
130
|
|
|
* @category Developer |
131
|
|
|
* @see mshop/common/manager/decorators/default |
132
|
|
|
* @see mshop/product/manager/decorators/excludes |
133
|
|
|
* @see mshop/product/manager/decorators/global |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
use \Aimeos\MShop\Common\Manager\ListsRef\Traits; |
138
|
|
|
use \Aimeos\MShop\Common\Manager\PropertyRef\Traits; |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
private array $searchConfig = array( |
142
|
|
|
'product.id' => array( |
143
|
|
|
'code' => 'product.id', |
144
|
|
|
'internalcode' => 'mpro."id"', |
145
|
|
|
'label' => 'ID', |
146
|
|
|
'type' => 'int', |
147
|
|
|
), |
148
|
|
|
'product.siteid' => array( |
149
|
|
|
'code' => 'product.siteid', |
150
|
|
|
'internalcode' => 'mpro."siteid"', |
151
|
|
|
'label' => 'Site ID', |
152
|
|
|
'public' => false, |
153
|
|
|
), |
154
|
|
|
'product.type' => array( |
155
|
|
|
'code' => 'product.type', |
156
|
|
|
'internalcode' => 'mpro."type"', |
157
|
|
|
'label' => 'Type', |
158
|
|
|
), |
159
|
|
|
'product.label' => array( |
160
|
|
|
'code' => 'product.label', |
161
|
|
|
'internalcode' => 'mpro."label"', |
162
|
|
|
'label' => 'Label', |
163
|
|
|
), |
164
|
|
|
'product.code' => array( |
165
|
|
|
'code' => 'product.code', |
166
|
|
|
'internalcode' => 'mpro."code"', |
167
|
|
|
'label' => 'SKU', |
168
|
|
|
), |
169
|
|
|
'product.url' => array( |
170
|
|
|
'code' => 'product.url', |
171
|
|
|
'internalcode' => 'mpro."url"', |
172
|
|
|
'label' => 'URL segment', |
173
|
|
|
), |
174
|
|
|
'product.dataset' => array( |
175
|
|
|
'code' => 'product.dataset', |
176
|
|
|
'internalcode' => 'mpro."dataset"', |
177
|
|
|
'label' => 'Data set', |
178
|
|
|
), |
179
|
|
|
'product.datestart' => array( |
180
|
|
|
'code' => 'product.datestart', |
181
|
|
|
'internalcode' => 'mpro."start"', |
182
|
|
|
'label' => 'Start date/time', |
183
|
|
|
'type' => 'datetime', |
184
|
|
|
), |
185
|
|
|
'product.dateend' => array( |
186
|
|
|
'code' => 'product.dateend', |
187
|
|
|
'internalcode' => 'mpro."end"', |
188
|
|
|
'label' => 'End date/time', |
189
|
|
|
'type' => 'datetime', |
190
|
|
|
), |
191
|
|
|
'product.instock' => array( |
192
|
|
|
'code' => 'product.instock', |
193
|
|
|
'internalcode' => 'mpro."instock"', |
194
|
|
|
'label' => 'Product in stock', |
195
|
|
|
'type' => 'int', |
196
|
|
|
), |
197
|
|
|
'product.status' => array( |
198
|
|
|
'code' => 'product.status', |
199
|
|
|
'internalcode' => 'mpro."status"', |
200
|
|
|
'label' => 'Status', |
201
|
|
|
'type' => 'int', |
202
|
|
|
), |
203
|
|
|
'product.scale' => array( |
204
|
|
|
'code' => 'product.scale', |
205
|
|
|
'internalcode' => 'mpro."scale"', |
206
|
|
|
'label' => 'Quantity scale', |
207
|
|
|
'type' => 'float', |
208
|
|
|
), |
209
|
|
|
'product.boost' => array( |
210
|
|
|
'code' => 'product.boost', |
211
|
|
|
'internalcode' => 'mpro."boost"', |
212
|
|
|
'label' => 'Boost factor', |
213
|
|
|
'type' => 'float', |
214
|
|
|
), |
215
|
|
|
'product.config' => array( |
216
|
|
|
'code' => 'product.config', |
217
|
|
|
'internalcode' => 'mpro."config"', |
218
|
|
|
'label' => 'Configuration', |
219
|
|
|
'type' => 'json', |
220
|
|
|
'public' => false, |
221
|
|
|
), |
222
|
|
|
'product.target' => array( |
223
|
|
|
'code' => 'product.target', |
224
|
|
|
'internalcode' => 'mpro."target"', |
225
|
|
|
'label' => 'URL target', |
226
|
|
|
'public' => false, |
227
|
|
|
), |
228
|
|
|
'product.ctime' => array( |
229
|
|
|
'code' => 'product.ctime', |
230
|
|
|
'internalcode' => 'mpro."ctime"', |
231
|
|
|
'label' => 'Create date/time', |
232
|
|
|
'type' => 'datetime', |
233
|
|
|
'public' => false, |
234
|
|
|
), |
235
|
|
|
'product.mtime' => array( |
236
|
|
|
'code' => 'product.mtime', |
237
|
|
|
'internalcode' => 'mpro."mtime"', |
238
|
|
|
'label' => 'Modify date/time', |
239
|
|
|
'type' => 'datetime', |
240
|
|
|
'public' => false, |
241
|
|
|
), |
242
|
|
|
'product.editor' => array( |
243
|
|
|
'code' => 'product.editor', |
244
|
|
|
'internalcode' => 'mpro."editor"', |
245
|
|
|
'label' => 'Editor', |
246
|
|
|
'public' => false, |
247
|
|
|
), |
248
|
|
|
'product.rating' => array( |
249
|
|
|
'code' => 'product.rating', |
250
|
|
|
'internalcode' => 'mpro."rating"', |
251
|
|
|
'label' => 'Rating value', |
252
|
|
|
'type' => 'decimal', |
253
|
|
|
'public' => false, |
254
|
|
|
), |
255
|
|
|
'product.ratings' => array( |
256
|
|
|
'code' => 'product.ratings', |
257
|
|
|
'internalcode' => 'mpro."ratings"', |
258
|
|
|
'label' => 'Number of ratings', |
259
|
|
|
'type' => 'int', |
260
|
|
|
'public' => false, |
261
|
|
|
), |
262
|
|
|
'product:has' => array( |
263
|
|
|
'code' => 'product:has()', |
264
|
|
|
'internalcode' => ':site AND :key AND mproli."id"', |
265
|
|
|
'internaldeps' => ['LEFT JOIN "mshop_product_list" AS mproli ON ( mproli."parentid" = mpro."id" )'], |
266
|
|
|
'label' => 'Product has list item, parameter(<domain>[,<list type>[,<reference ID>)]]', |
267
|
|
|
'type' => 'null', |
268
|
|
|
'public' => false, |
269
|
|
|
), |
270
|
|
|
'product:prop' => array( |
271
|
|
|
'code' => 'product:prop()', |
272
|
|
|
'internalcode' => ':site AND :key AND mpropr."id"', |
273
|
|
|
'internaldeps' => ['LEFT JOIN "mshop_product_property" AS mpropr ON ( mpropr."parentid" = mpro."id" )'], |
274
|
|
|
'label' => 'Product has property item, parameter(<property type>[,<language code>[,<property value>]])', |
275
|
|
|
'type' => 'null', |
276
|
|
|
'public' => false, |
277
|
|
|
), |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
private string $date; |
281
|
|
|
private array $cacheTags = []; |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Creates the product manager that will use the given context object. |
286
|
|
|
* |
287
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object with required objects |
288
|
|
|
*/ |
289
|
|
|
public function __construct( \Aimeos\MShop\ContextIface $context ) |
290
|
|
|
{ |
291
|
|
|
parent::__construct( $context ); |
292
|
|
|
|
293
|
|
|
/** mshop/product/manager/resource |
294
|
|
|
* Name of the database connection resource to use |
295
|
|
|
* |
296
|
|
|
* You can configure a different database connection for each data domain |
297
|
|
|
* and if no such connection name exists, the "db" connection will be used. |
298
|
|
|
* It's also possible to use the same database connection for different |
299
|
|
|
* data domains by configuring the same connection name using this setting. |
300
|
|
|
* |
301
|
|
|
* @param string Database connection name |
302
|
|
|
* @since 2023.04 |
303
|
|
|
*/ |
304
|
|
|
$this->setResourceName( $context->config()->get( 'mshop/product/manager/resource', 'db-product' ) ); |
305
|
|
|
$this->date = $context->datetime(); |
306
|
|
|
|
307
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
308
|
|
|
$level = $context->config()->get( 'mshop/product/manager/sitemode', $level ); |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
$this->searchConfig['product:has']['function'] = function( &$source, array $params ) use ( $level ) { |
312
|
|
|
|
313
|
|
|
$keys = []; |
314
|
|
|
|
315
|
|
|
foreach( (array) ( $params[1] ?? '' ) as $type ) { |
316
|
|
|
foreach( (array) ( $params[2] ?? '' ) as $id ) { |
317
|
|
|
$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$sitestr = $this->siteString( 'mproli."siteid"', $level ); |
322
|
|
|
$keystr = $this->toExpression( 'mproli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' ); |
323
|
|
|
$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source ); |
324
|
|
|
|
325
|
|
|
return $params; |
326
|
|
|
}; |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
$this->searchConfig['product:prop']['function'] = function( &$source, array $params ) use ( $level ) { |
330
|
|
|
|
331
|
|
|
$keys = []; |
332
|
|
|
$langs = array_key_exists( 1, $params ) ? ( $params[1] ?? 'null' ) : ''; |
333
|
|
|
|
334
|
|
|
foreach( (array) $langs as $lang ) { |
335
|
|
|
foreach( (array) ( $params[2] ?? '' ) as $val ) { |
336
|
|
|
$keys[] = substr( $params[0] . '|' . ( $lang === null ? 'null|' : ( $lang ? $lang . '|' : '' ) ) . $val, 0, 255 ); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$sitestr = $this->siteString( 'mpropr."siteid"', $level ); |
341
|
|
|
$keystr = $this->toExpression( 'mpropr."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' ); |
342
|
|
|
$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source ); |
343
|
|
|
|
344
|
|
|
return $params; |
345
|
|
|
}; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Removes old entries from the storage. |
351
|
|
|
* |
352
|
|
|
* @param iterable $siteids List of IDs for sites whose entries should be deleted |
353
|
|
|
* @return \Aimeos\MShop\Product\Manager\Iface Manager object for chaining method calls |
354
|
|
|
*/ |
355
|
|
|
public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
356
|
|
|
{ |
357
|
|
|
$path = 'mshop/product/manager/submanagers'; |
358
|
|
|
foreach( $this->context()->config()->get( $path, ['lists', 'property', 'type'] ) as $domain ) { |
359
|
|
|
$this->object()->getSubManager( $domain )->clear( $siteids ); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $this->clearBase( $siteids, 'mshop/product/manager/delete' ); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Commits the running database transaction on the connection identified by the given name |
368
|
|
|
* |
369
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
370
|
|
|
*/ |
371
|
|
|
public function commit() : \Aimeos\MShop\Common\Manager\Iface |
372
|
|
|
{ |
373
|
|
|
parent::commit(); |
374
|
|
|
|
375
|
|
|
$this->context()->cache()->deleteByTags( $this->cacheTags ); |
376
|
|
|
$this->cacheTags = []; |
377
|
|
|
|
378
|
|
|
return $this; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Creates a new empty item instance |
384
|
|
|
* |
385
|
|
|
* @param array $values Values the item should be initialized with |
386
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface New product item object |
387
|
|
|
*/ |
388
|
|
|
public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
389
|
|
|
{ |
390
|
|
|
$values['product.siteid'] = $values['product.siteid'] ?? $this->context()->locale()->getSiteId(); |
391
|
|
|
return $this->createItemBase( $values ); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Creates a filter object. |
397
|
|
|
* |
398
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
399
|
|
|
* @param bool $site TRUE for adding site criteria to limit items by the site of related items |
400
|
|
|
* @return \Aimeos\Base\Criteria\Iface Returns the filter object |
401
|
|
|
*/ |
402
|
|
|
public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
403
|
|
|
{ |
404
|
|
|
if( $default !== false ) |
405
|
|
|
{ |
406
|
|
|
$object = $this->filterBase( 'product', $default ); |
407
|
|
|
|
408
|
|
|
$expr = [$object->getConditions()]; |
409
|
|
|
|
410
|
|
|
$temp = array( |
411
|
|
|
$object->compare( '==', 'product.type', 'event' ), |
412
|
|
|
$object->compare( '==', 'product.datestart', null ), |
413
|
|
|
$object->compare( '<=', 'product.datestart', $this->date ), |
414
|
|
|
); |
415
|
|
|
$expr[] = $object->or( $temp ); |
416
|
|
|
|
417
|
|
|
$temp = array( |
418
|
|
|
$object->compare( '==', 'product.dateend', null ), |
419
|
|
|
$object->compare( '>=', 'product.dateend', $this->date ), |
420
|
|
|
); |
421
|
|
|
|
422
|
|
|
/** mshop/product/manager/strict-events |
423
|
|
|
* Hide events automatically if they are over |
424
|
|
|
* |
425
|
|
|
* Events are hidden by default if they are finished, removed from the |
426
|
|
|
* list view and can't be bought any more. If you sell webinars including |
427
|
|
|
* an archive of old ones you want to continue to sell for example, then |
428
|
|
|
* these webinars should be still shown. |
429
|
|
|
* |
430
|
|
|
* Setting this configuration option to false will display event products |
431
|
|
|
* that are already over and customers can still buy them. |
432
|
|
|
* |
433
|
|
|
* @param bool TRUE to hide events after they are over (default), FALSE to continue to show them |
434
|
|
|
* @category Developer |
435
|
|
|
* @category User |
436
|
|
|
* @since 2019.10 |
437
|
|
|
*/ |
438
|
|
|
if( !$this->context()->config()->get( 'mshop/product/manager/strict-events', true ) ) { |
439
|
|
|
$temp[] = $object->compare( '==', 'product.type', 'event' ); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
$expr[] = $object->or( $temp ); |
443
|
|
|
|
444
|
|
|
$object->setConditions( $object->and( $expr ) ); |
445
|
|
|
|
446
|
|
|
return $object; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
return parent::filter(); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Removes multiple items. |
455
|
|
|
* |
456
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[]|string[] $items List of item objects or IDs of the items |
457
|
|
|
* @return \Aimeos\MShop\Product\Manager\Iface Manager object for chaining method calls |
458
|
|
|
*/ |
459
|
|
|
public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface |
460
|
|
|
{ |
461
|
|
|
/** mshop/product/manager/delete/mysql |
462
|
|
|
* Deletes the items matched by the given IDs from the database |
463
|
|
|
* |
464
|
|
|
* @see mshop/product/manager/delete/ansi |
465
|
|
|
*/ |
466
|
|
|
|
467
|
|
|
/** mshop/product/manager/delete/ansi |
468
|
|
|
* Deletes the items matched by the given IDs from the database |
469
|
|
|
* |
470
|
|
|
* Removes the records specified by the given IDs from the product database. |
471
|
|
|
* The records must be from the site that is configured via the |
472
|
|
|
* context item. |
473
|
|
|
* |
474
|
|
|
* The ":cond" placeholder is replaced by the name of the ID column and |
475
|
|
|
* the given ID or list of IDs while the site ID is bound to the question |
476
|
|
|
* mark. |
477
|
|
|
* |
478
|
|
|
* The SQL statement should conform to the ANSI standard to be |
479
|
|
|
* compatible with most relational database systems. This also |
480
|
|
|
* includes using double quotes for table and column names. |
481
|
|
|
* |
482
|
|
|
* @param string SQL statement for deleting items |
483
|
|
|
* @since 2014.03 |
484
|
|
|
* @category Developer |
485
|
|
|
* @see mshop/product/manager/insert/ansi |
486
|
|
|
* @see mshop/product/manager/update/ansi |
487
|
|
|
* @see mshop/product/manager/newid/ansi |
488
|
|
|
* @see mshop/product/manager/search/ansi |
489
|
|
|
* @see mshop/product/manager/count/ansi |
490
|
|
|
* @see mshop/product/manager/rate/ansi |
491
|
|
|
* @see mshop/product/manager/stock/ansi |
492
|
|
|
*/ |
493
|
|
|
$path = 'mshop/product/manager/delete'; |
494
|
|
|
|
495
|
|
|
$this->deleteItemsBase( $items, $path )->deleteRefItems( $items ); |
496
|
|
|
$this->cacheTags = array_merge( $this->cacheTags, map( $items )->cast()->prefix( 'product-' )->all() ); |
497
|
|
|
|
498
|
|
|
return $this; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Returns the item specified by its code and domain/type if necessary |
504
|
|
|
* |
505
|
|
|
* @param string $code Code of the item |
506
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
507
|
|
|
* @param string|null $domain Domain of the item if necessary to identify the item uniquely |
508
|
|
|
* @param string|null $type Type code of the item if necessary to identify the item uniquely |
509
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
510
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Item object |
511
|
|
|
*/ |
512
|
|
|
public function find( string $code, array $ref = [], string $domain = null, string $type = null, |
513
|
|
|
?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
514
|
|
|
{ |
515
|
|
|
return $this->findBase( array( 'product.code' => $code ), $ref, $default ); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Returns the product item for the given product ID. |
521
|
|
|
* |
522
|
|
|
* @param string $id Unique ID of the product item |
523
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
524
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
525
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface Returns the product item of the given id |
526
|
|
|
* @throws \Aimeos\MShop\Exception If item couldn't be found |
527
|
|
|
*/ |
528
|
|
|
public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
529
|
|
|
{ |
530
|
|
|
return $this->getItemBase( 'product.id', $id, $ref, $default ); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Returns the available manager types |
536
|
|
|
* |
537
|
|
|
* @param bool $withsub Return also the resource type of sub-managers if true |
538
|
|
|
* @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
539
|
|
|
*/ |
540
|
|
|
public function getResourceType( bool $withsub = true ) : array |
541
|
|
|
{ |
542
|
|
|
$path = 'mshop/product/manager/submanagers'; |
543
|
|
|
return $this->getResourceTypeBase( 'product', $path, ['lists', 'property'], $withsub ); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Returns the attributes that can be used for searching. |
549
|
|
|
* |
550
|
|
|
* @param bool $withsub Return also attributes of sub-managers if true |
551
|
|
|
* @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items |
552
|
|
|
*/ |
553
|
|
|
public function getSearchAttributes( bool $withsub = true ) : array |
554
|
|
|
{ |
555
|
|
|
/** mshop/product/manager/submanagers |
556
|
|
|
* List of manager names that can be instantiated by the product manager |
557
|
|
|
* |
558
|
|
|
* Managers provide a generic interface to the underlying storage. |
559
|
|
|
* Each manager has or can have sub-managers caring about particular |
560
|
|
|
* aspects. Each of these sub-managers can be instantiated by its |
561
|
|
|
* parent manager using the getSubManager() method. |
562
|
|
|
* |
563
|
|
|
* The search keys from sub-managers can be normally used in the |
564
|
|
|
* manager as well. It allows you to search for items of the manager |
565
|
|
|
* using the search keys of the sub-managers to further limit the |
566
|
|
|
* retrieved list of items. |
567
|
|
|
* |
568
|
|
|
* @param array List of sub-manager names |
569
|
|
|
* @since 2014.03 |
570
|
|
|
* @category Developer |
571
|
|
|
*/ |
572
|
|
|
$path = 'mshop/product/manager/submanagers'; |
573
|
|
|
|
574
|
|
|
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub ); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Returns a new manager for product extensions. |
580
|
|
|
* |
581
|
|
|
* @param string $manager Name of the sub manager type in lower case |
582
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
583
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Submanager, e.g. type, property, etc. |
584
|
|
|
*/ |
585
|
|
|
public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
586
|
|
|
{ |
587
|
|
|
return $this->getSubManagerBase( 'product', $manager, $name ); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Updates the rating of the item |
593
|
|
|
* |
594
|
|
|
* @param string $id ID of the item |
595
|
|
|
* @param string $rating Decimal value of the rating |
596
|
|
|
* @param int $ratings Total number of ratings for the item |
597
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
598
|
|
|
*/ |
599
|
|
|
public function rate( string $id, string $rating, int $ratings ) : \Aimeos\MShop\Common\Manager\Iface |
600
|
|
|
{ |
601
|
|
|
$context = $this->context(); |
602
|
|
|
$conn = $context->db( $this->getResourceName() ); |
603
|
|
|
|
604
|
|
|
/** mshop/product/manager/rate/mysql |
605
|
|
|
* Updates the rating of the product in the database |
606
|
|
|
* |
607
|
|
|
* @see mshop/product/manager/rate/ansi |
608
|
|
|
*/ |
609
|
|
|
|
610
|
|
|
/** mshop/product/manager/rate/ansi |
611
|
|
|
* Updates the rating of the product in the database |
612
|
|
|
* |
613
|
|
|
* The SQL statement must be a string suitable for being used as |
614
|
|
|
* prepared statement. It must include question marks for binding |
615
|
|
|
* the values for the rating to the statement before they are |
616
|
|
|
* sent to the database server. The order of the columns must |
617
|
|
|
* correspond to the order in the rate() method, so the |
618
|
|
|
* correct values are bound to the columns. |
619
|
|
|
* |
620
|
|
|
* The SQL statement should conform to the ANSI standard to be |
621
|
|
|
* compatible with most relational database systems. This also |
622
|
|
|
* includes using double quotes for table and column names. |
623
|
|
|
* |
624
|
|
|
* @param string SQL statement for update ratings |
625
|
|
|
* @since 2020.10 |
626
|
|
|
* @category Developer |
627
|
|
|
* @see mshop/product/manager/insert/ansi |
628
|
|
|
* @see mshop/product/manager/update/ansi |
629
|
|
|
* @see mshop/product/manager/newid/ansi |
630
|
|
|
* @see mshop/product/manager/delete/ansi |
631
|
|
|
* @see mshop/product/manager/search/ansi |
632
|
|
|
* @see mshop/product/manager/count/ansi |
633
|
|
|
* @see mshop/product/manager/stock/ansi |
634
|
|
|
*/ |
635
|
|
|
$path = 'mshop/product/manager/rate'; |
636
|
|
|
|
637
|
|
|
$stmt = $this->getCachedStatement( $conn, $path, $this->getSqlConfig( $path ) ); |
|
|
|
|
638
|
|
|
|
639
|
|
|
$stmt->bind( 1, $rating ); |
640
|
|
|
$stmt->bind( 2, $ratings, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
641
|
|
|
$stmt->bind( 3, $context->locale()->getSiteId() ); |
642
|
|
|
$stmt->bind( 4, (int) $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
643
|
|
|
|
644
|
|
|
$stmt->execute()->finish(); |
645
|
|
|
|
646
|
|
|
return $this; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* Updates if the product is in stock |
652
|
|
|
* |
653
|
|
|
* @param string $id ID of the procuct item |
654
|
|
|
* @param int $value "0" or "1" if product is in stock or not |
655
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
656
|
|
|
*/ |
657
|
|
|
public function stock( string $id, int $value ) : \Aimeos\MShop\Common\Manager\Iface |
658
|
|
|
{ |
659
|
|
|
$context = $this->context(); |
660
|
|
|
$conn = $context->db( $this->getResourceName() ); |
661
|
|
|
|
662
|
|
|
/** mshop/product/manager/stock/mysql |
663
|
|
|
* Updates the rating of the product in the database |
664
|
|
|
* |
665
|
|
|
* @see mshop/product/manager/stock/ansi |
666
|
|
|
*/ |
667
|
|
|
|
668
|
|
|
/** mshop/product/manager/stock/ansi |
669
|
|
|
* Updates the rating of the product in the database |
670
|
|
|
* |
671
|
|
|
* The SQL statement must be a string suitable for being used as |
672
|
|
|
* prepared statement. It must include question marks for binding |
673
|
|
|
* the values for the rating to the statement before they are |
674
|
|
|
* sent to the database server. The order of the columns must |
675
|
|
|
* correspond to the order in the stock() method, so the |
676
|
|
|
* correct values are bound to the columns. |
677
|
|
|
* |
678
|
|
|
* The SQL statement should conform to the ANSI standard to be |
679
|
|
|
* compatible with most relational database systems. This also |
680
|
|
|
* includes using double quotes for table and column names. |
681
|
|
|
* |
682
|
|
|
* @param string SQL statement for update ratings |
683
|
|
|
* @since 2021.10 |
684
|
|
|
* @category Developer |
685
|
|
|
* @see mshop/product/manager/insert/ansi |
686
|
|
|
* @see mshop/product/manager/update/ansi |
687
|
|
|
* @see mshop/product/manager/newid/ansi |
688
|
|
|
* @see mshop/product/manager/delete/ansi |
689
|
|
|
* @see mshop/product/manager/search/ansi |
690
|
|
|
* @see mshop/product/manager/count/ansi |
691
|
|
|
* @see mshop/product/manager/rate/ansi |
692
|
|
|
*/ |
693
|
|
|
$path = 'mshop/product/manager/stock'; |
694
|
|
|
|
695
|
|
|
$stmt = $this->getCachedStatement( $conn, $path, $this->getSqlConfig( $path ) ); |
|
|
|
|
696
|
|
|
|
697
|
|
|
$stmt->bind( 1, $value, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
698
|
|
|
$stmt->bind( 2, $context->locale()->getSiteId() ); |
699
|
|
|
$stmt->bind( 3, (int) $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
700
|
|
|
|
701
|
|
|
$stmt->execute()->finish(); |
702
|
|
|
|
703
|
|
|
return $this; |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Adds or updates an item object or a list of them. |
709
|
|
|
* |
710
|
|
|
* @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved |
711
|
|
|
* @param bool $fetch True if the new ID should be returned in the item |
712
|
|
|
* @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items |
713
|
|
|
*/ |
714
|
|
|
public function save( $items, bool $fetch = true ) |
715
|
|
|
{ |
716
|
|
|
$items = parent::save( $items, $fetch ); |
717
|
|
|
|
718
|
|
|
if( ( $ids = map( $items )->getId()->filter() )->count() === map( $items )->count() ) { |
719
|
|
|
$this->cacheTags = array_merge( $this->cacheTags, map( $ids )->prefix( 'product-' )->all() ); |
720
|
|
|
} else { |
721
|
|
|
$this->cacheTags[] = 'product'; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
return $items; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Adds a new product to the storage. |
730
|
|
|
* |
731
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $item Product item that should be saved to the storage |
732
|
|
|
* @param bool $fetch True if the new ID should be returned in the item |
733
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface Updated item including the generated ID |
734
|
|
|
*/ |
735
|
|
|
protected function saveItem( \Aimeos\MShop\Product\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Product\Item\Iface |
736
|
|
|
{ |
737
|
|
|
if( !$item->isModified() ) |
738
|
|
|
{ |
739
|
|
|
$item = $this->savePropertyItems( $item, 'product', $fetch ); |
740
|
|
|
return $this->saveListItems( $item, 'product', $fetch ); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
$context = $this->context(); |
744
|
|
|
$conn = $context->db( $this->getResourceName() ); |
745
|
|
|
|
746
|
|
|
$id = $item->getId(); |
747
|
|
|
$date = date( 'Y-m-d H:i:s' ); |
748
|
|
|
$columns = $this->object()->getSaveAttributes(); |
749
|
|
|
|
750
|
|
|
if( $id === null ) |
751
|
|
|
{ |
752
|
|
|
/** mshop/product/manager/insert/mysql |
753
|
|
|
* Inserts a new product record into the database table |
754
|
|
|
* |
755
|
|
|
* @see mshop/product/manager/insert/ansi |
756
|
|
|
*/ |
757
|
|
|
|
758
|
|
|
/** mshop/product/manager/insert/ansi |
759
|
|
|
* Inserts a new product record into the database table |
760
|
|
|
* |
761
|
|
|
* Items with no ID yet (i.e. the ID is NULL) will be created in |
762
|
|
|
* the database and the newly created ID retrieved afterwards |
763
|
|
|
* using the "newid" SQL statement. |
764
|
|
|
* |
765
|
|
|
* The SQL statement must be a string suitable for being used as |
766
|
|
|
* prepared statement. It must include question marks for binding |
767
|
|
|
* the values from the product item to the statement before they are |
768
|
|
|
* sent to the database server. The number of question marks must |
769
|
|
|
* be the same as the number of columns listed in the INSERT |
770
|
|
|
* statement. The order of the columns must correspond to the |
771
|
|
|
* order in the save() method, so the correct values are |
772
|
|
|
* bound to the columns. |
773
|
|
|
* |
774
|
|
|
* The SQL statement should conform to the ANSI standard to be |
775
|
|
|
* compatible with most relational database systems. This also |
776
|
|
|
* includes using double quotes for table and column names. |
777
|
|
|
* |
778
|
|
|
* @param string SQL statement for inserting records |
779
|
|
|
* @since 2014.03 |
780
|
|
|
* @category Developer |
781
|
|
|
* @see mshop/product/manager/update/ansi |
782
|
|
|
* @see mshop/product/manager/newid/ansi |
783
|
|
|
* @see mshop/product/manager/delete/ansi |
784
|
|
|
* @see mshop/product/manager/search/ansi |
785
|
|
|
* @see mshop/product/manager/count/ansi |
786
|
|
|
* @see mshop/product/manager/rate/ansi |
787
|
|
|
* @see mshop/product/manager/stock/ansi |
788
|
|
|
*/ |
789
|
|
|
$path = 'mshop/product/manager/insert'; |
790
|
|
|
$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) ); |
|
|
|
|
791
|
|
|
} |
792
|
|
|
else |
793
|
|
|
{ |
794
|
|
|
/** mshop/product/manager/update/mysql |
795
|
|
|
* Updates an existing product record in the database |
796
|
|
|
* |
797
|
|
|
* @see mshop/product/manager/update/ansi |
798
|
|
|
*/ |
799
|
|
|
|
800
|
|
|
/** mshop/product/manager/update/ansi |
801
|
|
|
* Updates an existing product record in the database |
802
|
|
|
* |
803
|
|
|
* Items which already have an ID (i.e. the ID is not NULL) will |
804
|
|
|
* be updated in the database. |
805
|
|
|
* |
806
|
|
|
* The SQL statement must be a string suitable for being used as |
807
|
|
|
* prepared statement. It must include question marks for binding |
808
|
|
|
* the values from the product item to the statement before they are |
809
|
|
|
* sent to the database server. The order of the columns must |
810
|
|
|
* correspond to the order in the save() method, so the |
811
|
|
|
* correct values are bound to the columns. |
812
|
|
|
* |
813
|
|
|
* The SQL statement should conform to the ANSI standard to be |
814
|
|
|
* compatible with most relational database systems. This also |
815
|
|
|
* includes using double quotes for table and column names. |
816
|
|
|
* |
817
|
|
|
* @param string SQL statement for updating records |
818
|
|
|
* @since 2014.03 |
819
|
|
|
* @category Developer |
820
|
|
|
* @see mshop/product/manager/insert/ansi |
821
|
|
|
* @see mshop/product/manager/newid/ansi |
822
|
|
|
* @see mshop/product/manager/delete/ansi |
823
|
|
|
* @see mshop/product/manager/search/ansi |
824
|
|
|
* @see mshop/product/manager/count/ansi |
825
|
|
|
* @see mshop/product/manager/rate/ansi |
826
|
|
|
* @see mshop/product/manager/stock/ansi |
827
|
|
|
*/ |
828
|
|
|
$path = 'mshop/product/manager/update'; |
829
|
|
|
$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false ); |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
$idx = 1; |
833
|
|
|
$stmt = $this->getCachedStatement( $conn, $path, $sql ); |
834
|
|
|
|
835
|
|
|
foreach( $columns as $name => $entry ) { |
836
|
|
|
$stmt->bind( $idx++, $item->get( $name ), \Aimeos\Base\Criteria\SQL::type( $entry->getType() ) ); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
$stmt->bind( $idx++, $item->getType() ); |
840
|
|
|
$stmt->bind( $idx++, $item->getCode() ); |
841
|
|
|
$stmt->bind( $idx++, $item->getDataset() ); |
842
|
|
|
$stmt->bind( $idx++, $item->getLabel() ); |
843
|
|
|
$stmt->bind( $idx++, $item->getUrl() ); |
844
|
|
|
$stmt->bind( $idx++, $item->inStock(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
845
|
|
|
$stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
846
|
|
|
$stmt->bind( $idx++, $item->getScale(), \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT ); |
847
|
|
|
$stmt->bind( $idx++, $item->getDateStart() ); |
848
|
|
|
$stmt->bind( $idx++, $item->getDateEnd() ); |
849
|
|
|
$stmt->bind( $idx++, json_encode( $item->getConfig(), JSON_FORCE_OBJECT ) ); |
850
|
|
|
$stmt->bind( $idx++, $item->getTarget() ); |
851
|
|
|
$stmt->bind( $idx++, $item->boost() ); |
852
|
|
|
$stmt->bind( $idx++, $context->editor() ); |
853
|
|
|
$stmt->bind( $idx++, $date ); // mtime |
854
|
|
|
$stmt->bind( $idx++, $item->getTimeCreated() ?: $date ); |
855
|
|
|
|
856
|
|
|
if( $id !== null ) { |
857
|
|
|
$stmt->bind( $idx++, $context->locale()->getSiteId() . '%' ); |
858
|
|
|
$stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
859
|
|
|
} else { |
860
|
|
|
$stmt->bind( $idx++, $this->siteId( $item->getSiteId(), \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) ); |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
$stmt->execute()->finish(); |
864
|
|
|
|
865
|
|
|
if( $id === null ) |
866
|
|
|
{ |
867
|
|
|
/** mshop/product/manager/newid/mysql |
868
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
869
|
|
|
* |
870
|
|
|
* @see mshop/product/manager/newid/ansi |
871
|
|
|
*/ |
872
|
|
|
|
873
|
|
|
/** mshop/product/manager/newid/ansi |
874
|
|
|
* Retrieves the ID generated by the database when inserting a new record |
875
|
|
|
* |
876
|
|
|
* As soon as a new record is inserted into the database table, |
877
|
|
|
* the database server generates a new and unique identifier for |
878
|
|
|
* that record. This ID can be used for retrieving, updating and |
879
|
|
|
* deleting that specific record from the table again. |
880
|
|
|
* |
881
|
|
|
* For MySQL: |
882
|
|
|
* SELECT LAST_INSERT_ID() |
883
|
|
|
* For PostgreSQL: |
884
|
|
|
* SELECT currval('seq_mpro_id') |
885
|
|
|
* For SQL Server: |
886
|
|
|
* SELECT SCOPE_IDENTITY() |
887
|
|
|
* For Oracle: |
888
|
|
|
* SELECT "seq_mpro_id".CURRVAL FROM DUAL |
889
|
|
|
* |
890
|
|
|
* There's no way to retrive the new ID by a SQL statements that |
891
|
|
|
* fits for most database servers as they implement their own |
892
|
|
|
* specific way. |
893
|
|
|
* |
894
|
|
|
* @param string SQL statement for retrieving the last inserted record ID |
895
|
|
|
* @since 2014.03 |
896
|
|
|
* @category Developer |
897
|
|
|
* @see mshop/product/manager/insert/ansi |
898
|
|
|
* @see mshop/product/manager/update/ansi |
899
|
|
|
* @see mshop/product/manager/delete/ansi |
900
|
|
|
* @see mshop/product/manager/search/ansi |
901
|
|
|
* @see mshop/product/manager/count/ansi |
902
|
|
|
* @see mshop/product/manager/rate/ansi |
903
|
|
|
* @see mshop/product/manager/stock/ansi |
904
|
|
|
*/ |
905
|
|
|
$path = 'mshop/product/manager/newid'; |
906
|
|
|
$id = $this->newId( $conn, $path ); |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
$item->setId( $id ); |
910
|
|
|
|
911
|
|
|
$item = $this->savePropertyItems( $item, 'product', $fetch ); |
912
|
|
|
return $this->saveListItems( $item, 'product', $fetch ); |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
|
916
|
|
|
/** |
917
|
|
|
* Search for products based on the given criteria. |
918
|
|
|
* |
919
|
|
|
* @param \Aimeos\Base\Criteria\Iface $search Search criteria object |
920
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
921
|
|
|
* @param int|null &$total Number of items that are available in total |
922
|
|
|
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Product\Item\Iface with ids as keys |
923
|
|
|
*/ |
924
|
|
|
public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map |
925
|
|
|
{ |
926
|
|
|
$map = []; |
927
|
|
|
$context = $this->context(); |
928
|
|
|
$conn = $context->db( $this->getResourceName() ); |
929
|
|
|
|
930
|
|
|
$required = ['product']; |
931
|
|
|
|
932
|
|
|
/** mshop/product/manager/sitemode |
933
|
|
|
* Mode how items from levels below or above in the site tree are handled |
934
|
|
|
* |
935
|
|
|
* By default, only items from the current site are fetched from the |
936
|
|
|
* storage. If the ai-sites extension is installed, you can create a |
937
|
|
|
* tree of sites. Then, this setting allows you to define for the |
938
|
|
|
* whole product domain if items from parent sites are inherited, |
939
|
|
|
* sites from child sites are aggregated or both. |
940
|
|
|
* |
941
|
|
|
* Available constants for the site mode are: |
942
|
|
|
* * 0 = only items from the current site |
943
|
|
|
* * 1 = inherit items from parent sites |
944
|
|
|
* * 2 = aggregate items from child sites |
945
|
|
|
* * 3 = inherit and aggregate items at the same time |
946
|
|
|
* |
947
|
|
|
* You also need to set the mode in the locale manager |
948
|
|
|
* (mshop/locale/manager/sitelevel) to one of the constants. |
949
|
|
|
* If you set it to the same value, it will work as described but you |
950
|
|
|
* can also use different modes. For example, if inheritance and |
951
|
|
|
* aggregation is configured the locale manager but only inheritance |
952
|
|
|
* in the domain manager because aggregating items makes no sense in |
953
|
|
|
* this domain, then items wil be only inherited. Thus, you have full |
954
|
|
|
* control over inheritance and aggregation in each domain. |
955
|
|
|
* |
956
|
|
|
* @param int Constant from Aimeos\MShop\Locale\Manager\Base class |
957
|
|
|
* @category Developer |
958
|
|
|
* @since 2018.01 |
959
|
|
|
* @see mshop/locale/manager/sitelevel |
960
|
|
|
*/ |
961
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
962
|
|
|
$level = $context->config()->get( 'mshop/product/manager/sitemode', $level ); |
963
|
|
|
|
964
|
|
|
/** mshop/product/manager/search/mysql |
965
|
|
|
* Retrieves the records matched by the given criteria in the database |
966
|
|
|
* |
967
|
|
|
* @see mshop/product/manager/search/ansi |
968
|
|
|
*/ |
969
|
|
|
|
970
|
|
|
/** mshop/product/manager/search/ansi |
971
|
|
|
* Retrieves the records matched by the given criteria in the database |
972
|
|
|
* |
973
|
|
|
* Fetches the records matched by the given criteria from the product |
974
|
|
|
* database. The records must be from one of the sites that are |
975
|
|
|
* configured via the context item. If the current site is part of |
976
|
|
|
* a tree of sites, the SELECT statement can retrieve all records |
977
|
|
|
* from the current site and the complete sub-tree of sites. |
978
|
|
|
* |
979
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
980
|
|
|
* their tables must be joined in the SQL context. This is done by |
981
|
|
|
* using the "internaldeps" property from the definition of the ID |
982
|
|
|
* column of the sub-managers. These internal dependencies specify |
983
|
|
|
* the JOIN between the tables and the used columns for joining. The |
984
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
985
|
|
|
* the sub-managers. |
986
|
|
|
* |
987
|
|
|
* To limit the records matched, conditions can be added to the given |
988
|
|
|
* criteria object. It can contain comparisons like column names that |
989
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
990
|
|
|
* operators. The resulting string of SQL conditions replaces the |
991
|
|
|
* ":cond" placeholder before the statement is sent to the database |
992
|
|
|
* server. |
993
|
|
|
* |
994
|
|
|
* If the records that are retrieved should be ordered by one or more |
995
|
|
|
* columns, the generated string of column / sort direction pairs |
996
|
|
|
* replaces the ":order" placeholder. Columns of |
997
|
|
|
* sub-managers can also be used for ordering the result set but then |
998
|
|
|
* no index can be used. |
999
|
|
|
* |
1000
|
|
|
* The number of returned records can be limited and can start at any |
1001
|
|
|
* number between the begining and the end of the result set. For that |
1002
|
|
|
* the ":size" and ":start" placeholders are replaced by the |
1003
|
|
|
* corresponding values from the criteria object. The default values |
1004
|
|
|
* are 0 for the start and 100 for the size value. |
1005
|
|
|
* |
1006
|
|
|
* The SQL statement should conform to the ANSI standard to be |
1007
|
|
|
* compatible with most relational database systems. This also |
1008
|
|
|
* includes using double quotes for table and column names. |
1009
|
|
|
* |
1010
|
|
|
* @param string SQL statement for searching items |
1011
|
|
|
* @since 2014.03 |
1012
|
|
|
* @category Developer |
1013
|
|
|
* @see mshop/product/manager/insert/ansi |
1014
|
|
|
* @see mshop/product/manager/update/ansi |
1015
|
|
|
* @see mshop/product/manager/newid/ansi |
1016
|
|
|
* @see mshop/product/manager/delete/ansi |
1017
|
|
|
* @see mshop/product/manager/count/ansi |
1018
|
|
|
* @see mshop/product/manager/rate/ansi |
1019
|
|
|
* @see mshop/product/manager/stock/ansi |
1020
|
|
|
*/ |
1021
|
|
|
$cfgPathSearch = 'mshop/product/manager/search'; |
1022
|
|
|
|
1023
|
|
|
/** mshop/product/manager/count/mysql |
1024
|
|
|
* Counts the number of records matched by the given criteria in the database |
1025
|
|
|
* |
1026
|
|
|
* @see mshop/product/manager/count/ansi |
1027
|
|
|
*/ |
1028
|
|
|
|
1029
|
|
|
/** mshop/product/manager/count/ansi |
1030
|
|
|
* Counts the number of records matched by the given criteria in the database |
1031
|
|
|
* |
1032
|
|
|
* Counts all records matched by the given criteria from the product |
1033
|
|
|
* database. The records must be from one of the sites that are |
1034
|
|
|
* configured via the context item. If the current site is part of |
1035
|
|
|
* a tree of sites, the statement can count all records from the |
1036
|
|
|
* current site and the complete sub-tree of sites. |
1037
|
|
|
* |
1038
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
1039
|
|
|
* their tables must be joined in the SQL context. This is done by |
1040
|
|
|
* using the "internaldeps" property from the definition of the ID |
1041
|
|
|
* column of the sub-managers. These internal dependencies specify |
1042
|
|
|
* the JOIN between the tables and the used columns for joining. The |
1043
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
1044
|
|
|
* the sub-managers. |
1045
|
|
|
* |
1046
|
|
|
* To limit the records matched, conditions can be added to the given |
1047
|
|
|
* criteria object. It can contain comparisons like column names that |
1048
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
1049
|
|
|
* operators. The resulting string of SQL conditions replaces the |
1050
|
|
|
* ":cond" placeholder before the statement is sent to the database |
1051
|
|
|
* server. |
1052
|
|
|
* |
1053
|
|
|
* Both, the strings for ":joins" and for ":cond" are the same as for |
1054
|
|
|
* the "search" SQL statement. |
1055
|
|
|
* |
1056
|
|
|
* Contrary to the "search" statement, it doesn't return any records |
1057
|
|
|
* but instead the number of records that have been found. As counting |
1058
|
|
|
* thousands of records can be a long running task, the maximum number |
1059
|
|
|
* of counted records is limited for performance reasons. |
1060
|
|
|
* |
1061
|
|
|
* The SQL statement should conform to the ANSI standard to be |
1062
|
|
|
* compatible with most relational database systems. This also |
1063
|
|
|
* includes using double quotes for table and column names. |
1064
|
|
|
* |
1065
|
|
|
* @param string SQL statement for counting items |
1066
|
|
|
* @since 2014.03 |
1067
|
|
|
* @category Developer |
1068
|
|
|
* @see mshop/product/manager/insert/ansi |
1069
|
|
|
* @see mshop/product/manager/update/ansi |
1070
|
|
|
* @see mshop/product/manager/newid/ansi |
1071
|
|
|
* @see mshop/product/manager/delete/ansi |
1072
|
|
|
* @see mshop/product/manager/search/ansi |
1073
|
|
|
* @see mshop/product/manager/rate/ansi |
1074
|
|
|
* @see mshop/product/manager/stock/ansi |
1075
|
|
|
*/ |
1076
|
|
|
$cfgPathCount = 'mshop/product/manager/count'; |
1077
|
|
|
|
1078
|
|
|
$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
1079
|
|
|
|
1080
|
|
|
while( $row = $results->fetch() ) |
1081
|
|
|
{ |
1082
|
|
|
if( ( $row['product.config'] = json_decode( $row['product.config'], true ) ) === null ) { |
1083
|
|
|
$row['product.config'] = []; |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
$map[$row['product.id']] = $row; |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
|
1090
|
|
|
$propItems = []; $name = 'product/property'; |
1091
|
|
|
if( isset( $ref[$name] ) || in_array( $name, $ref, true ) ) |
1092
|
|
|
{ |
1093
|
|
|
$propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null; |
1094
|
|
|
$propItems = $this->getPropertyItems( array_keys( $map ), 'product', $propTypes ); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
if( isset( $ref['stock'] ) || in_array( 'stock', $ref, true ) ) |
1098
|
|
|
{ |
1099
|
|
|
foreach( $this->getStockItems( array_keys( $map ), $ref ) as $stockId => $stockItem ) { |
1100
|
|
|
$map[$stockItem->getProductId()]['.stock'][$stockId] = $stockItem; |
1101
|
|
|
} |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
if( isset( $ref['locale/site'] ) || in_array( 'locale/site', $ref, true ) ) |
1105
|
|
|
{ |
1106
|
|
|
foreach( $this->getSiteItems( $map ) as $prodId => $item ) { |
1107
|
|
|
$map[$prodId]['.locale/site'] = $item; |
1108
|
|
|
} |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
return $this->buildItems( $map, $ref, 'product', $propItems ); |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
|
1115
|
|
|
/** |
1116
|
|
|
* Create new product item object initialized with given parameters. |
1117
|
|
|
* |
1118
|
|
|
* @param array $values Associative list of key/value pairs |
1119
|
|
|
* @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items |
1120
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items |
1121
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $propertyItems List of property items |
1122
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface New product item |
1123
|
|
|
*/ |
1124
|
|
|
protected function createItemBase( array $values = [], array $listItems = [], |
1125
|
|
|
array $refItems = [], array $propertyItems = [] ) : \Aimeos\MShop\Common\Item\Iface |
1126
|
|
|
{ |
1127
|
|
|
$values['.date'] = $this->date; |
1128
|
|
|
|
1129
|
|
|
return new \Aimeos\MShop\Product\Item\Standard( $values, $listItems, $refItems, $propertyItems ); |
1130
|
|
|
} |
1131
|
|
|
|
1132
|
|
|
|
1133
|
|
|
/** |
1134
|
|
|
* Returns the stock items for the given product codes |
1135
|
|
|
* |
1136
|
|
|
* @param array $entries List of product records |
1137
|
|
|
* @return \Aimeos\Map List of product IDs as keys and items implementing \Aimeos\MShop\Locale\Item\Site\Iface as values |
1138
|
|
|
*/ |
1139
|
|
|
protected function getSiteItems( array $entries ) : \Aimeos\Map |
1140
|
|
|
{ |
1141
|
|
|
$siteIds = map( $entries )->col( 'product.siteid' ); |
1142
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' ); |
1143
|
|
|
|
1144
|
|
|
$filter = $manager->filter( true )->add( ['locale.site.siteid' => $siteIds] )->slice( 0, 0x7fffffff ); |
1145
|
|
|
$items = $manager->search( $filter )->col( null, 'locale.site.siteid' ); |
1146
|
|
|
|
1147
|
|
|
return map( $entries )->map( function( $entry, $prodId ) use ( $items ) { |
1148
|
|
|
return $items->get( $entry['product.siteid'] ?? null ); |
1149
|
|
|
} ); |
1150
|
|
|
} |
1151
|
|
|
|
1152
|
|
|
|
1153
|
|
|
/** |
1154
|
|
|
* Returns the stock items for the given product codes |
1155
|
|
|
* |
1156
|
|
|
* @param string[] $ids Unique product codes |
1157
|
|
|
* @param string[] $ref List of domains to fetch referenced items for |
1158
|
|
|
* @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Stock\Item\Iface as values |
1159
|
|
|
*/ |
1160
|
|
|
protected function getStockItems( array $ids, array $ref ) : \Aimeos\Map |
1161
|
|
|
{ |
1162
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'stock' ); |
1163
|
|
|
|
1164
|
|
|
$filter = $manager->filter( true )->add( 'stock.productid', '==', $ids )->slice( 0, 0x7fffffff ); |
1165
|
|
|
|
1166
|
|
|
if( isset( $ref['stock'] ) && is_array( $ref['stock'] ) ) { |
1167
|
|
|
$filter->add( 'stock.type', '==', $ref['stock'] ); |
1168
|
|
|
} |
1169
|
|
|
|
1170
|
|
|
return $manager->search( $filter ); |
1171
|
|
|
} |
1172
|
|
|
} |
1173
|
|
|
|