Total Complexity | 47 |
Total Lines | 1110 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 extends Base |
||
22 | implements \Aimeos\MShop\Order\Manager\Base\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface |
||
23 | { |
||
24 | private $searchConfig = array( |
||
25 | 'order.base.id' => array( |
||
26 | 'code' => 'order.base.id', |
||
27 | 'internalcode' => 'mordba."id"', |
||
28 | 'internaldeps' => array( 'LEFT JOIN "mshop_order_base" AS mordba ON ( mord."baseid" = mordba."id" )' ), |
||
29 | 'label' => 'Order ID', |
||
30 | 'type' => 'integer', |
||
31 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
||
32 | 'public' => false, |
||
33 | ), |
||
34 | 'order.base.siteid' => array( |
||
35 | 'code' => 'order.base.siteid', |
||
36 | 'internalcode' => 'mordba."siteid"', |
||
37 | 'label' => 'Order site ID', |
||
38 | 'type' => 'string', |
||
39 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
40 | 'public' => false, |
||
41 | ), |
||
42 | 'order.base.sitecode' => array( |
||
43 | 'code' => 'order.base.sitecode', |
||
44 | 'internalcode' => 'mordba."sitecode"', |
||
45 | 'label' => 'Order site code', |
||
46 | 'type' => 'string', |
||
47 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
48 | 'public' => false, |
||
49 | ), |
||
50 | 'order.base.customerid' => array( |
||
51 | 'code' => 'order.base.customerid', |
||
52 | 'internalcode' => 'mordba."customerid"', |
||
53 | 'label' => 'Order customer ID', |
||
54 | 'type' => 'string', |
||
55 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
56 | ), |
||
57 | 'order.base.customerref' => array( |
||
58 | 'code' => 'order.base.customerref', |
||
59 | 'internalcode' => 'mordba."customerref"', |
||
60 | 'label' => 'Order customer reference', |
||
61 | 'type' => 'string', |
||
62 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
63 | ), |
||
64 | 'order.base.languageid' => array( |
||
65 | 'code' => 'order.base.languageid', |
||
66 | 'internalcode' => 'mordba."langid"', |
||
67 | 'label' => 'Order language code', |
||
68 | 'type' => 'string', |
||
69 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
70 | ), |
||
71 | 'order.base.currencyid' => array( |
||
72 | 'code' => 'order.base.currencyid', |
||
73 | 'internalcode' => 'mordba."currencyid"', |
||
74 | 'label' => 'Order currencyid code', |
||
75 | 'type' => 'string', |
||
76 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
77 | ), |
||
78 | 'order.base.price' => array( |
||
79 | 'code' => 'order.base.price', |
||
80 | 'internalcode' => 'mordba."price"', |
||
81 | 'label' => 'Order price amount', |
||
82 | 'type' => 'string', |
||
83 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
84 | ), |
||
85 | 'order.base.costs' => array( |
||
86 | 'code' => 'order.base.costs', |
||
87 | 'internalcode' => 'mordba."costs"', |
||
88 | 'label' => 'Order shipping amount', |
||
89 | 'type' => 'string', |
||
90 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
91 | ), |
||
92 | 'order.base.rebate' => array( |
||
93 | 'code' => 'order.base.rebate', |
||
94 | 'internalcode' => 'mordba."rebate"', |
||
95 | 'label' => 'Order rebate amount', |
||
96 | 'type' => 'string', |
||
97 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
98 | ), |
||
99 | 'order.base.taxvalue' => array( |
||
100 | 'code' => 'order.base.taxvalue', |
||
101 | 'internalcode' => 'mordba."tax"', |
||
102 | 'label' => 'Order tax amount', |
||
103 | 'type' => 'string', |
||
104 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
105 | ), |
||
106 | 'order.base.taxflag' => array( |
||
107 | 'code' => 'order.base.taxflag', |
||
108 | 'internalcode' => 'mordba."taxflag"', |
||
109 | 'label' => 'Order tax flag (0=net, 1=gross)', |
||
110 | 'type' => 'string', |
||
111 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT, |
||
112 | ), |
||
113 | 'order.base.comment' => array( |
||
114 | 'code' => 'order.base.comment', |
||
115 | 'internalcode' => 'mordba."comment"', |
||
116 | 'label' => 'Order comment', |
||
117 | 'type' => 'string', |
||
118 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
119 | ), |
||
120 | 'order.base.ctime' => array( |
||
121 | 'code' => 'order.base.ctime', |
||
122 | 'internalcode' => 'mordba."ctime"', |
||
123 | 'label' => 'Order create date/time', |
||
124 | 'type' => 'datetime', |
||
125 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
126 | 'public' => false, |
||
127 | ), |
||
128 | 'order.base.mtime' => array( |
||
129 | 'code' => 'order.base.mtime', |
||
130 | 'internalcode' => 'mordba."mtime"', |
||
131 | 'label' => 'Order modify date/time', |
||
132 | 'type' => 'datetime', |
||
133 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
134 | 'public' => false, |
||
135 | ), |
||
136 | 'order.base.editor' => array( |
||
137 | 'code' => 'order.base.editor', |
||
138 | 'internalcode' => 'mordba."editor"', |
||
139 | 'label' => 'Order editor', |
||
140 | 'type' => 'string', |
||
141 | 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
||
142 | 'public' => false, |
||
143 | ), |
||
144 | ); |
||
145 | |||
146 | |||
147 | /** |
||
148 | * Initializes the object. |
||
149 | * |
||
150 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
151 | */ |
||
152 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
156 | } |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Counts the number items that are available for the values of the given key. |
||
161 | * |
||
162 | * @param \Aimeos\MW\Criteria\Iface $search Search criteria |
||
163 | * @param string $key Search key to aggregate items for |
||
164 | * @param string|null $value Search key for aggregating the value column |
||
165 | * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average) |
||
166 | * @return \Aimeos\Map List of the search keys as key and the number of counted items as value |
||
167 | */ |
||
168 | public function aggregate( \Aimeos\MW\Criteria\Iface $search, string $key, string $value = null, string $type = null ) : \Aimeos\Map |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Removes old entries from the storage. |
||
265 | * |
||
266 | * @param string[] $siteids List of IDs for sites whose entries should be deleted |
||
267 | * @return \Aimeos\MShop\Order\Manager\Base\Iface Manager object for chaining method calls |
||
268 | */ |
||
269 | public function clear( array $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
||
270 | { |
||
271 | $path = 'mshop/order/manager/base/submanagers'; |
||
272 | $default = array( 'address', 'coupon', 'product', 'service' ); |
||
273 | |||
274 | foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) { |
||
275 | $this->getObject()->getSubManager( $domain )->clear( $siteids ); |
||
276 | } |
||
277 | |||
278 | return $this->clearBase( $siteids, 'mshop/order/manager/base/standard/delete' ); |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Creates a new empty item instance |
||
284 | * |
||
285 | * @param array $values Values the item should be initialized with |
||
286 | * @return \Aimeos\MShop\Order\Item\Base\Iface New order base item object |
||
287 | */ |
||
288 | public function createItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
||
301 | } |
||
302 | |||
303 | |||
304 | /** |
||
305 | * Creates a search critera object |
||
306 | * |
||
307 | * @param bool $default Add default criteria (optional) |
||
308 | * @param bool $site TRUE to add site criteria to show orders with available products only |
||
309 | * @return \Aimeos\MW\Criteria\Iface New search criteria object |
||
310 | */ |
||
311 | public function createSearch( bool $default = false, bool $site = false ) : \Aimeos\MW\Criteria\Iface |
||
334 | } |
||
335 | |||
336 | |||
337 | /** |
||
338 | * Removes multiple items. |
||
339 | * |
||
340 | * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items |
||
341 | * @return \Aimeos\MShop\Order\Manager\Base\Iface Manager object for chaining method calls |
||
342 | */ |
||
343 | public function deleteItems( array $itemIds ) : \Aimeos\MShop\Common\Manager\Iface |
||
378 | } |
||
379 | |||
380 | |||
381 | /** |
||
382 | * Returns the order base item specified by the given ID. |
||
383 | * |
||
384 | * @param string $id Unique id of the order base |
||
385 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
386 | * @param bool $default Add default criteria |
||
387 | * @return \Aimeos\MShop\Order\Item\Base\Iface Returns Order base item of the given id |
||
388 | * @throws \Aimeos\MShop\Exception If item couldn't be found |
||
389 | */ |
||
390 | public function getItem( string $id, array $ref = [], bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
||
393 | } |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Returns the available manager types |
||
398 | * |
||
399 | * @param bool $withsub Return also the resource type of sub-managers if true |
||
400 | * @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
||
401 | */ |
||
402 | public function getResourceType( bool $withsub = true ) : array |
||
406 | } |
||
407 | |||
408 | |||
409 | /** |
||
410 | * Returns the attributes that can be used for searching. |
||
411 | * |
||
412 | * @param bool $withsub Return also attributes of sub-managers if true |
||
413 | * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items |
||
414 | */ |
||
415 | public function getSearchAttributes( bool $withsub = true ) : array |
||
438 | } |
||
439 | |||
440 | |||
441 | /** |
||
442 | * Returns a new manager for order base extensions. |
||
443 | * |
||
444 | * @param string $manager Name of the sub manager type in lower case |
||
445 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
446 | * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g address, coupon, product, service, etc. |
||
447 | */ |
||
448 | public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
||
449 | { |
||
450 | /** mshop/order/manager/base/name |
||
451 | * Class name of the used order base manager implementation |
||
452 | * |
||
453 | * Each default order base manager can be replaced by an alternative imlementation. |
||
454 | * To use this implementation, you have to set the last part of the class |
||
455 | * name as configuration value so the manager factory knows which class it |
||
456 | * has to instantiate. |
||
457 | * |
||
458 | * For example, if the name of the default class is |
||
459 | * |
||
460 | * \Aimeos\MShop\Order\Manager\Base\Standard |
||
461 | * |
||
462 | * and you want to replace it with your own version named |
||
463 | * |
||
464 | * \Aimeos\MShop\Order\Manager\Base\Mybase |
||
465 | * |
||
466 | * then you have to set the this configuration option: |
||
467 | * |
||
468 | * mshop/order/manager/base/name = Mybase |
||
469 | * |
||
470 | * The value is the last part of your own class name and it's case sensitive, |
||
471 | * so take care that the configuration value is exactly named like the last |
||
472 | * part of the class name. |
||
473 | * |
||
474 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
475 | * characters are possible! You should always start the last part of the class |
||
476 | * name with an upper case character and continue only with lower case characters |
||
477 | * or numbers. Avoid chamel case names like "MyBase"! |
||
478 | * |
||
479 | * @param string Last part of the class name |
||
480 | * @since 2014.03 |
||
481 | * @category Developer |
||
482 | */ |
||
483 | |||
484 | /** mshop/order/manager/base/decorators/excludes |
||
485 | * Excludes decorators added by the "common" option from the order base manager |
||
486 | * |
||
487 | * Decorators extend the functionality of a class by adding new aspects |
||
488 | * (e.g. log what is currently done), executing the methods of the underlying |
||
489 | * class only in certain conditions (e.g. only for logged in users) or |
||
490 | * modify what is returned to the caller. |
||
491 | * |
||
492 | * This option allows you to remove a decorator added via |
||
493 | * "mshop/common/manager/decorators/default" before they are wrapped |
||
494 | * around the order base manager. |
||
495 | * |
||
496 | * mshop/order/manager/base/decorators/excludes = array( 'decorator1' ) |
||
497 | * |
||
498 | * This would remove the decorator named "decorator1" from the list of |
||
499 | * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via |
||
500 | * "mshop/common/manager/decorators/default" for the order base manager. |
||
501 | * |
||
502 | * @param array List of decorator names |
||
503 | * @since 2014.03 |
||
504 | * @category Developer |
||
505 | * @see mshop/common/manager/decorators/default |
||
506 | * @see mshop/order/manager/base/decorators/global |
||
507 | * @see mshop/order/manager/base/decorators/local |
||
508 | */ |
||
509 | |||
510 | /** mshop/order/manager/base/decorators/global |
||
511 | * Adds a list of globally available decorators only to the order base manager |
||
512 | * |
||
513 | * Decorators extend the functionality of a class by adding new aspects |
||
514 | * (e.g. log what is currently done), executing the methods of the underlying |
||
515 | * class only in certain conditions (e.g. only for logged in users) or |
||
516 | * modify what is returned to the caller. |
||
517 | * |
||
518 | * This option allows you to wrap global decorators |
||
519 | * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the order base |
||
520 | * manager. |
||
521 | * |
||
522 | * mshop/order/manager/base/decorators/global = array( 'decorator1' ) |
||
523 | * |
||
524 | * This would add the decorator named "decorator1" defined by |
||
525 | * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the order |
||
526 | * base manager. |
||
527 | * |
||
528 | * @param array List of decorator names |
||
529 | * @since 2014.03 |
||
530 | * @category Developer |
||
531 | * @see mshop/common/manager/decorators/default |
||
532 | * @see mshop/order/manager/base/decorators/excludes |
||
533 | * @see mshop/order/manager/base/decorators/local |
||
534 | */ |
||
535 | |||
536 | /** mshop/order/manager/base/decorators/local |
||
537 | * Adds a list of local decorators only to the order base manager |
||
538 | * |
||
539 | * Decorators extend the functionality of a class by adding new aspects |
||
540 | * (e.g. log what is currently done), executing the methods of the underlying |
||
541 | * class only in certain conditions (e.g. only for logged in users) or |
||
542 | * modify what is returned to the caller. |
||
543 | * |
||
544 | * This option allows you to wrap local decorators |
||
545 | * ("\Aimeos\MShop\Order\Manager\Base\Decorator\*") around the order base |
||
546 | * manager. |
||
547 | * |
||
548 | * mshop/order/manager/base/decorators/local = array( 'decorator2' ) |
||
549 | * |
||
550 | * This would add the decorator named "decorator2" defined by |
||
551 | * "\Aimeos\MShop\Order\Manager\Base\Decorator\Decorator2" only to the |
||
552 | * order base manager. |
||
553 | * |
||
554 | * @param array List of decorator names |
||
555 | * @since 2014.03 |
||
556 | * @category Developer |
||
557 | * @see mshop/common/manager/decorators/default |
||
558 | * @see mshop/order/manager/base/decorators/excludes |
||
559 | * @see mshop/order/manager/base/decorators/global |
||
560 | */ |
||
561 | |||
562 | return $this->getSubManagerBase( 'order', 'base/' . $manager, $name ); |
||
563 | } |
||
564 | |||
565 | |||
566 | /** |
||
567 | * Adds or updates an order base item in the storage. |
||
568 | * |
||
569 | * @param \Aimeos\MShop\Order\Item\Base\Iface $item Order base object (sub-items are not saved) |
||
570 | * @param bool $fetch True if the new ID should be returned in the item |
||
571 | * @return \Aimeos\MShop\Order\Item\Base\Iface $item Updated item including the generated ID |
||
572 | */ |
||
573 | public function saveItem( \Aimeos\MShop\Order\Item\Base\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Order\Item\Base\Iface |
||
755 | } |
||
756 | |||
757 | |||
758 | /** |
||
759 | * Search for orders based on the given criteria. |
||
760 | * |
||
761 | * @param \Aimeos\MW\Criteria\Iface $search Search criteria object |
||
762 | * @param string[] $ref List of domains to fetch list items and referenced items for, e.g. |
||
763 | * "order/base/address", "order/base/coupon", "order/base/product", "order/base/service" |
||
764 | * @param int|null &$total Number of items that are available in total |
||
765 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Base\Iface with ids as keys |
||
766 | */ |
||
767 | public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map |
||
768 | { |
||
769 | $context = $this->getContext(); |
||
770 | $priceManager = \Aimeos\MShop::create( $context, 'price' ); |
||
771 | $localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
||
772 | |||
773 | $dbm = $context->getDatabaseManager(); |
||
774 | $dbname = $this->getResourceName(); |
||
775 | $conn = $dbm->acquire( $dbname ); |
||
776 | |||
777 | $map = $items = $custItems = []; |
||
778 | |||
779 | try |
||
780 | { |
||
781 | $required = array( 'order.base' ); |
||
782 | |||
783 | $level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
||
784 | $level = $context->getConfig()->get( 'mshop/order/manager/sitemode', $level ); |
||
785 | |||
786 | /** mshop/order/manager/base/standard/search/mysql |
||
787 | * Retrieves the records matched by the given criteria in the database |
||
788 | * |
||
789 | * @see mshop/order/manager/base/standard/search/ansi |
||
790 | */ |
||
791 | |||
792 | /** mshop/order/manager/base/standard/search/ansi |
||
793 | * Retrieves the records matched by the given criteria in the database |
||
794 | * |
||
795 | * Fetches the records matched by the given criteria from the order |
||
796 | * database. The records must be from one of the sites that are |
||
797 | * configured via the context item. If the current site is part of |
||
798 | * a tree of sites, the SELECT statement can retrieve all records |
||
799 | * from the current site and the complete sub-tree of sites. |
||
800 | * |
||
801 | * As the records can normally be limited by criteria from sub-managers, |
||
802 | * their tables must be joined in the SQL context. This is done by |
||
803 | * using the "internaldeps" property from the definition of the ID |
||
804 | * column of the sub-managers. These internal dependencies specify |
||
805 | * the JOIN between the tables and the used columns for joining. The |
||
806 | * ":joins" placeholder is then replaced by the JOIN strings from |
||
807 | * the sub-managers. |
||
808 | * |
||
809 | * To limit the records matched, conditions can be added to the given |
||
810 | * criteria object. It can contain comparisons like column names that |
||
811 | * must match specific values which can be combined by AND, OR or NOT |
||
812 | * operators. The resulting string of SQL conditions replaces the |
||
813 | * ":cond" placeholder before the statement is sent to the database |
||
814 | * server. |
||
815 | * |
||
816 | * If the records that are retrieved should be ordered by one or more |
||
817 | * columns, the generated string of column / sort direction pairs |
||
818 | * replaces the ":order" placeholder. In case no ordering is required, |
||
819 | * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/" |
||
820 | * markers is removed to speed up retrieving the records. Columns of |
||
821 | * sub-managers can also be used for ordering the result set but then |
||
822 | * no index can be used. |
||
823 | * |
||
824 | * The number of returned records can be limited and can start at any |
||
825 | * number between the begining and the end of the result set. For that |
||
826 | * the ":size" and ":start" placeholders are replaced by the |
||
827 | * corresponding values from the criteria object. The default values |
||
828 | * are 0 for the start and 100 for the size value. |
||
829 | * |
||
830 | * The SQL statement should conform to the ANSI standard to be |
||
831 | * compatible with most relational database systems. This also |
||
832 | * includes using double quotes for table and column names. |
||
833 | * |
||
834 | * @param string SQL statement for searching items |
||
835 | * @since 2014.03 |
||
836 | * @category Developer |
||
837 | * @see mshop/order/manager/base/standard/insert/ansi |
||
838 | * @see mshop/order/manager/base/standard/update/ansi |
||
839 | * @see mshop/order/manager/base/standard/newid/ansi |
||
840 | * @see mshop/order/manager/base/standard/delete/ansi |
||
841 | * @see mshop/order/manager/base/standard/count/ansi |
||
842 | */ |
||
843 | $cfgPathSearch = 'mshop/order/manager/base/standard/search'; |
||
844 | |||
845 | /** mshop/order/manager/base/standard/count/mysql |
||
846 | * Counts the number of records matched by the given criteria in the database |
||
847 | * |
||
848 | * @see mshop/order/manager/base/standard/count/ansi |
||
849 | */ |
||
850 | |||
851 | /** mshop/order/manager/base/standard/count/ansi |
||
852 | * Counts the number of records matched by the given criteria in the database |
||
853 | * |
||
854 | * Counts all records matched by the given criteria from the order |
||
855 | * database. The records must be from one of the sites that are |
||
856 | * configured via the context item. If the current site is part of |
||
857 | * a tree of sites, the statement can count all records from the |
||
858 | * current site and the complete sub-tree of sites. |
||
859 | * |
||
860 | * As the records can normally be limited by criteria from sub-managers, |
||
861 | * their tables must be joined in the SQL context. This is done by |
||
862 | * using the "internaldeps" property from the definition of the ID |
||
863 | * column of the sub-managers. These internal dependencies specify |
||
864 | * the JOIN between the tables and the used columns for joining. The |
||
865 | * ":joins" placeholder is then replaced by the JOIN strings from |
||
866 | * the sub-managers. |
||
867 | * |
||
868 | * To limit the records matched, conditions can be added to the given |
||
869 | * criteria object. It can contain comparisons like column names that |
||
870 | * must match specific values which can be combined by AND, OR or NOT |
||
871 | * operators. The resulting string of SQL conditions replaces the |
||
872 | * ":cond" placeholder before the statement is sent to the database |
||
873 | * server. |
||
874 | * |
||
875 | * Both, the strings for ":joins" and for ":cond" are the same as for |
||
876 | * the "search" SQL statement. |
||
877 | * |
||
878 | * Contrary to the "search" statement, it doesn't return any records |
||
879 | * but instead the number of records that have been found. As counting |
||
880 | * thousands of records can be a long running task, the maximum number |
||
881 | * of counted records is limited for performance reasons. |
||
882 | * |
||
883 | * The SQL statement should conform to the ANSI standard to be |
||
884 | * compatible with most relational database systems. This also |
||
885 | * includes using double quotes for table and column names. |
||
886 | * |
||
887 | * @param string SQL statement for counting items |
||
888 | * @since 2014.03 |
||
889 | * @category Developer |
||
890 | * @see mshop/order/manager/base/standard/insert/ansi |
||
891 | * @see mshop/order/manager/base/standard/update/ansi |
||
892 | * @see mshop/order/manager/base/standard/newid/ansi |
||
893 | * @see mshop/order/manager/base/standard/delete/ansi |
||
894 | * @see mshop/order/manager/base/standard/search/ansi |
||
895 | */ |
||
896 | $cfgPathCount = 'mshop/order/manager/base/standard/count'; |
||
897 | |||
898 | $results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, |
||
899 | $required, $total, $level ); |
||
900 | |||
901 | while( ( $row = $results->fetch() ) !== null ) { |
||
902 | $map[$row['order.base.id']] = $row; |
||
903 | } |
||
904 | |||
905 | $dbm->release( $conn, $dbname ); |
||
906 | } |
||
907 | catch( \Exception $e ) |
||
908 | { |
||
909 | $dbm->release( $conn, $dbname ); |
||
910 | throw $e; |
||
911 | } |
||
912 | |||
913 | if( ( isset( $ref['customer'] ) || in_array( 'customer', $ref ) ) |
||
914 | && !( $ids = map( $map )->col( 'order.base.customerid' )->filter() )->empty() |
||
915 | ) { |
||
916 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
917 | $search = $manager->createSearch()->setSlice( 0, count( $ids ) ); |
||
918 | $search->setConditions( $search->compare( '==', 'customer.id', $ids ) ); |
||
919 | $custItems = $manager->searchItems( $search, $ref ); |
||
920 | } |
||
921 | |||
922 | foreach( $map as $id => $row ) |
||
923 | { |
||
924 | $price = $priceManager->createItem(); |
||
925 | $price->setCurrencyId( $row['order.base.currencyid'] ); |
||
926 | $price->setValue( $row['order.base.price'] ); |
||
927 | $price->setCosts( $row['order.base.costs'] ); |
||
928 | $price->setRebate( $row['order.base.rebate'] ); |
||
929 | $price->setTaxValue( $row['order.base.taxvalue'] ); |
||
930 | $price->setTaxFlag( $row['order.base.taxflag'] ); |
||
931 | |||
932 | // you may need the site object! take care! |
||
933 | $localeItem = $localeManager->createItem(); |
||
934 | $localeItem->setLanguageId( $row['order.base.languageid'] ); |
||
935 | $localeItem->setCurrencyId( $row['order.base.currencyid'] ); |
||
936 | $localeItem->setSiteId( $row['order.base.siteid'] ); |
||
937 | |||
938 | $map[$id] = [$price, $localeItem, $row, $custItems[$row['order.base.customerid'] ?? null] ?? null]; |
||
939 | } |
||
940 | |||
941 | return $this->buildItems( $map, $ref ); |
||
942 | } |
||
943 | |||
944 | |||
945 | /** |
||
946 | * Creates a new basket containing the items from the order excluding the coupons. |
||
947 | * If the last parameter is ture, the items will be marked as new and |
||
948 | * modified so an additional order is stored when the basket is saved. |
||
949 | * |
||
950 | * @param string $id Base ID of the order to load |
||
951 | * @param int $parts Bitmap of the basket parts that should be loaded |
||
952 | * @param bool $fresh Create a new basket by copying the existing one and remove IDs |
||
953 | * @param bool $default True to use default criteria, false for no limitation |
||
954 | * @return \Aimeos\MShop\Order\Item\Base\Iface Basket including all items |
||
955 | */ |
||
956 | public function load( string $id, int $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, bool $fresh = false, |
||
957 | bool $default = false ) : \Aimeos\MShop\Order\Item\Base\Iface |
||
958 | { |
||
959 | $search = $this->getObject()->createSearch( $default ); |
||
960 | $expr = [ |
||
961 | $search->compare( '==', 'order.base.id', $id ), |
||
962 | $search->getConditions(), |
||
963 | ]; |
||
964 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
965 | |||
966 | $context = $this->getContext(); |
||
967 | $dbm = $context->getDatabaseManager(); |
||
968 | $dbname = $this->getResourceName(); |
||
969 | $conn = $dbm->acquire( $dbname ); |
||
970 | |||
971 | try |
||
972 | { |
||
973 | $sitelevel = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
||
974 | $sitelevel = $context->getConfig()->get( 'mshop/order/manager/sitemode', $sitelevel ); |
||
975 | |||
976 | $cfgPathSearch = 'mshop/order/manager/base/standard/search'; |
||
977 | $cfgPathCount = 'mshop/order/manager/base/standard/count'; |
||
978 | $required = array( 'order.base' ); |
||
979 | $total = null; |
||
980 | |||
981 | $results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $sitelevel ); |
||
982 | |||
983 | if( ( $row = $results->fetch() ) === null ) { |
||
984 | throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order base item with order ID "%1$s" not found', $id ) ); |
||
985 | } |
||
986 | $results->finish(); |
||
987 | |||
988 | $dbm->release( $conn, $dbname ); |
||
989 | } |
||
990 | catch( \Exception $e ) |
||
991 | { |
||
992 | $dbm->release( $conn, $dbname ); |
||
993 | throw $e; |
||
994 | } |
||
995 | |||
996 | $priceManager = \Aimeos\MShop::create( $context, 'price' ); |
||
997 | $localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
||
998 | |||
999 | $price = $priceManager->createItem( [ |
||
1000 | 'price.currencyid' => $row['order.base.currencyid'], |
||
1001 | 'price.value' => $row['order.base.price'], |
||
1002 | 'price.costs' => $row['order.base.costs'], |
||
1003 | 'price.rebate' => $row['order.base.rebate'], |
||
1004 | 'price.taxflag' => $row['order.base.taxflag'], |
||
1005 | 'price.taxvalue' => $row['order.base.taxvalue'], |
||
1006 | ] ); |
||
1007 | |||
1008 | // you may need the site object! take care! |
||
1009 | $localeItem = $localeManager->createItem( [ |
||
1010 | 'locale.languageid' => $row['order.base.languageid'], |
||
1011 | 'locale.currencyid' => $row['order.base.currencyid'], |
||
1012 | 'locale.siteid' => $row['order.base.siteid'], |
||
1013 | ] ); |
||
1014 | |||
1015 | if( $fresh === false ) { |
||
1016 | $basket = $this->loadItems( $id, $price, $localeItem, $row, $parts ); |
||
1017 | } else { |
||
1018 | $basket = $this->loadFresh( $id, $price, $localeItem, $row, $parts ); |
||
1019 | } |
||
1020 | |||
1021 | $pluginManager = \Aimeos\MShop::create( $this->getContext(), 'plugin' ); |
||
1022 | $pluginManager->register( $basket, 'order' ); |
||
1023 | |||
1024 | return $basket; |
||
1025 | } |
||
1026 | |||
1027 | |||
1028 | /** |
||
1029 | * Saves the complete basket to the storage including the items attached. |
||
1030 | * |
||
1031 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object containing all information |
||
1032 | * @param int $parts Bitmap of the basket parts that should be stored |
||
1033 | * @return \Aimeos\MShop\Order\Item\Base\Iface Stored order basket |
||
1034 | */ |
||
1035 | public function store( \Aimeos\MShop\Order\Item\Base\Iface $basket, |
||
1059 | } |
||
1060 | |||
1061 | |||
1062 | /** |
||
1063 | * Creates the order base item objects from the map and adds the referenced items |
||
1064 | * |
||
1065 | * @param array $map Associative list of order base IDs as keys and list of price/locale/row as values |
||
1066 | * @param string[] $ref Domain items that should be added as well, e.g. |
||
1067 | * "order/base/address", "order/base/coupon", "order/base/product", "order/base/service" |
||
1068 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Base\Iface with IDs as keys |
||
1069 | */ |
||
1070 | protected function buildItems( array $map, array $ref ) : \Aimeos\Map |
||
1109 | } |
||
1110 | |||
1111 | |||
1112 | /** |
||
1113 | * Returns a new and empty order base item (shopping basket). |
||
1114 | * |
||
1115 | * @param \Aimeos\MShop\Price\Item\Iface $price Default price of the basket (usually 0.00) |
||
1116 | * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale item containing the site, language and currency |
||
1117 | * @param array $values Associative list of key/value pairs containing, e.g. the order or user ID |
||
1118 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface[] $products List of ordered product items |
||
1119 | * @param \Aimeos\MShop\Order\Item\Base\Address\Iface[] $addresses List of order address items |
||
1120 | * @param \Aimeos\MShop\Order\Item\Base\Service\Iface[] $services List of order serviceitems |
||
1121 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface[] $coupons Associative list of coupon codes as keys and items as values |
||
1122 | * @param \Aimeos\MShop\Customer\Item\Iface|null $custItem Customer item object if requested |
||
1123 | * @return \Aimeos\MShop\Order\Item\Base\Iface Order base object |
||
1124 | */ |
||
1125 | protected function createItemBase( \Aimeos\MShop\Price\Item\Iface $price, \Aimeos\MShop\Locale\Item\Iface $locale, |
||
1133 |