Total Complexity | 53 |
Total Lines | 1152 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
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 |
||
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 ) |
||
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 |
||
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. In case no ordering is required, |
||
997 | * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/" |
||
998 | * markers is removed to speed up retrieving the records. Columns of |
||
999 | * sub-managers can also be used for ordering the result set but then |
||
1000 | * no index can be used. |
||
1001 | * |
||
1002 | * The number of returned records can be limited and can start at any |
||
1003 | * number between the begining and the end of the result set. For that |
||
1004 | * the ":size" and ":start" placeholders are replaced by the |
||
1005 | * corresponding values from the criteria object. The default values |
||
1006 | * are 0 for the start and 100 for the size value. |
||
1007 | * |
||
1008 | * The SQL statement should conform to the ANSI standard to be |
||
1009 | * compatible with most relational database systems. This also |
||
1010 | * includes using double quotes for table and column names. |
||
1011 | * |
||
1012 | * @param string SQL statement for searching items |
||
1013 | * @since 2014.03 |
||
1014 | * @category Developer |
||
1015 | * @see mshop/product/manager/insert/ansi |
||
1016 | * @see mshop/product/manager/update/ansi |
||
1017 | * @see mshop/product/manager/newid/ansi |
||
1018 | * @see mshop/product/manager/delete/ansi |
||
1019 | * @see mshop/product/manager/count/ansi |
||
1020 | * @see mshop/product/manager/rate/ansi |
||
1021 | * @see mshop/product/manager/stock/ansi |
||
1022 | */ |
||
1023 | $cfgPathSearch = 'mshop/product/manager/search'; |
||
1024 | |||
1025 | /** mshop/product/manager/count/mysql |
||
1026 | * Counts the number of records matched by the given criteria in the database |
||
1027 | * |
||
1028 | * @see mshop/product/manager/count/ansi |
||
1029 | */ |
||
1030 | |||
1031 | /** mshop/product/manager/count/ansi |
||
1032 | * Counts the number of records matched by the given criteria in the database |
||
1033 | * |
||
1034 | * Counts all records matched by the given criteria from the product |
||
1035 | * database. The records must be from one of the sites that are |
||
1036 | * configured via the context item. If the current site is part of |
||
1037 | * a tree of sites, the statement can count all records from the |
||
1038 | * current site and the complete sub-tree of sites. |
||
1039 | * |
||
1040 | * As the records can normally be limited by criteria from sub-managers, |
||
1041 | * their tables must be joined in the SQL context. This is done by |
||
1042 | * using the "internaldeps" property from the definition of the ID |
||
1043 | * column of the sub-managers. These internal dependencies specify |
||
1044 | * the JOIN between the tables and the used columns for joining. The |
||
1045 | * ":joins" placeholder is then replaced by the JOIN strings from |
||
1046 | * the sub-managers. |
||
1047 | * |
||
1048 | * To limit the records matched, conditions can be added to the given |
||
1049 | * criteria object. It can contain comparisons like column names that |
||
1050 | * must match specific values which can be combined by AND, OR or NOT |
||
1051 | * operators. The resulting string of SQL conditions replaces the |
||
1052 | * ":cond" placeholder before the statement is sent to the database |
||
1053 | * server. |
||
1054 | * |
||
1055 | * Both, the strings for ":joins" and for ":cond" are the same as for |
||
1056 | * the "search" SQL statement. |
||
1057 | * |
||
1058 | * Contrary to the "search" statement, it doesn't return any records |
||
1059 | * but instead the number of records that have been found. As counting |
||
1060 | * thousands of records can be a long running task, the maximum number |
||
1061 | * of counted records is limited for performance reasons. |
||
1062 | * |
||
1063 | * The SQL statement should conform to the ANSI standard to be |
||
1064 | * compatible with most relational database systems. This also |
||
1065 | * includes using double quotes for table and column names. |
||
1066 | * |
||
1067 | * @param string SQL statement for counting items |
||
1068 | * @since 2014.03 |
||
1069 | * @category Developer |
||
1070 | * @see mshop/product/manager/insert/ansi |
||
1071 | * @see mshop/product/manager/update/ansi |
||
1072 | * @see mshop/product/manager/newid/ansi |
||
1073 | * @see mshop/product/manager/delete/ansi |
||
1074 | * @see mshop/product/manager/search/ansi |
||
1075 | * @see mshop/product/manager/rate/ansi |
||
1076 | * @see mshop/product/manager/stock/ansi |
||
1077 | */ |
||
1078 | $cfgPathCount = 'mshop/product/manager/count'; |
||
1079 | |||
1080 | $results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
||
1081 | |||
1082 | while( $row = $results->fetch() ) |
||
1083 | { |
||
1084 | if( ( $row['product.config'] = json_decode( $row['product.config'], true ) ) === null ) { |
||
1085 | $row['product.config'] = []; |
||
1086 | } |
||
1087 | |||
1088 | $map[$row['product.id']] = $row; |
||
1089 | } |
||
1090 | |||
1091 | |||
1092 | $propItems = []; $name = 'product/property'; |
||
1093 | if( isset( $ref[$name] ) || in_array( $name, $ref, true ) ) |
||
1094 | { |
||
1095 | $propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null; |
||
1096 | $propItems = $this->getPropertyItems( array_keys( $map ), 'product', $propTypes ); |
||
1097 | } |
||
1098 | |||
1099 | if( isset( $ref['stock'] ) || in_array( 'stock', $ref, true ) ) |
||
1100 | { |
||
1101 | foreach( $this->getStockItems( array_keys( $map ), $ref ) as $stockId => $stockItem ) { |
||
1102 | $map[$stockItem->getProductId()]['.stock'][$stockId] = $stockItem; |
||
1103 | } |
||
1104 | } |
||
1105 | |||
1106 | if( isset( $ref['locale/site'] ) || in_array( 'locale/site', $ref, true ) ) |
||
1107 | { |
||
1108 | foreach( $this->getSiteItems( $map ) as $prodId => $item ) { |
||
1109 | $map[$prodId]['.locale/site'] = $item; |
||
1110 | } |
||
1111 | } |
||
1112 | |||
1113 | return $this->buildItems( $map, $ref, 'product', $propItems ); |
||
1114 | } |
||
1115 | |||
1116 | |||
1117 | /** |
||
1118 | * Create new product item object initialized with given parameters. |
||
1119 | * |
||
1120 | * @param array $values Associative list of key/value pairs |
||
1121 | * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items |
||
1122 | * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items |
||
1123 | * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propertyItems List of property items |
||
1124 | * @return \Aimeos\MShop\Product\Item\Iface New product item |
||
1125 | */ |
||
1126 | protected function createItemBase( array $values = [], array $listItems = [], |
||
1132 | } |
||
1133 | |||
1134 | |||
1135 | /** |
||
1136 | * Returns the stock items for the given product codes |
||
1137 | * |
||
1138 | * @param array $entries List of product records |
||
1139 | * @return \Aimeos\Map List of product IDs as keys and items implementing \Aimeos\MShop\Locale\Item\Site\Iface as values |
||
1140 | */ |
||
1141 | protected function getSiteItems( array $entries ) : \Aimeos\Map |
||
1151 | } ); |
||
1152 | } |
||
1153 | |||
1154 | |||
1155 | /** |
||
1156 | * Returns the stock items for the given product codes |
||
1157 | * |
||
1158 | * @param string[] $ids Unique product codes |
||
1159 | * @param string[] $ref List of domains to fetch referenced items for |
||
1160 | * @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Stock\Item\Iface as values |
||
1161 | */ |
||
1162 | protected function getStockItems( array $ids, array $ref ) : \Aimeos\Map |
||
1175 |