/
Category.php
205 lines (168 loc) · 5.87 KB
/
Category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2016-2024
* @package MShop
* @subpackage Service
*/
namespace Aimeos\MShop\Service\Provider\Decorator;
/**
* Category-limiting decorator for service providers
*
* This decorator interacts with the ServiceUpdate and Autofill basket plugins!
* If the delivery/payment option isn't available any more, the ServiceUpdate
* plugin will remove it from the basket and the Autofill plugin will add one
* of the available options again.
*
* @package MShop
* @subpackage Service
*/
class Category
extends \Aimeos\MShop\Service\Provider\Decorator\Base
implements \Aimeos\MShop\Service\Provider\Decorator\Iface
{
private array $beConfig = array(
'category.include' => array(
'code' => 'category.include',
'internalcode' => 'category.include',
'label' => 'Code of allowed category and sub-categories for the service item',
'default' => '',
'required' => false,
),
'category.exclude' => array(
'code' => 'category.exclude',
'internalcode' => 'category.exclude',
'label' => 'Code of category and sub-categories not allowed for the service item',
'default' => '',
'required' => false,
),
);
/**
* Checks the backend configuration attributes for validity.
*
* @param array $attributes Attributes added by the shop owner in the administraton interface
* @return array An array with the attribute keys as key and an error message as values for all attributes that are
* known by the provider but aren't valid
*/
public function checkConfigBE( array $attributes ) : array
{
$error = $this->getProvider()->checkConfigBE( $attributes );
$error += $this->checkConfig( $this->beConfig, $attributes );
return $error;
}
/**
* Returns the configuration attribute definitions of the provider to generate a list of available fields and
* rules for the value of each field in the administration interface.
*
* @return array List of attribute definitions implementing \Aimeos\Base\Critera\Attribute\Iface
*/
public function getConfigBE() : array
{
return array_replace( parent::getConfigBE(), $this->getConfigItems( $this->beConfig ) );
}
/**
* Checks if ordered products are in the configured categories to display the service provider.
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket object
* @return bool True if payment provider can be used, false if not
*/
public function isAvailable( \Aimeos\MShop\Order\Item\Iface $basket ) : bool
{
$prodIds = $this->getProductIds( $basket );
if( $this->checkCategories( $prodIds, 'category.include' ) === false
|| $this->checkCategories( $prodIds, 'category.exclude' ) === true
) {
return false;
}
return $this->getProvider()->isAvailable( $basket );
}
/**
* Checks if at least one of the product is in the configured categories
*
* @param array $prodIds List of product IDs
* @param string $key Configuration key (category.include or category.exclude)
* @return bool|null True if one catalog code is part of the config, false if not, null for no configuration
*/
protected function checkCategories( array $prodIds, string $key ) : ?bool
{
if( ( $codes = $this->getConfigValue( $key ) ) == null ) {
return null;
}
$configCatalogIds = $this->getCatalogIds( explode( ',', $codes ) );
if( empty( $treeCatalogIds = $this->getTreeCatalogIds( $configCatalogIds ) ) ) {
return false;
}
$types = ['default', 'promotion'];
$manager = \Aimeos\MShop::create( $this->context(), 'product' );
// Fetch hidden product too (null for filter)
$filter = $manager->filter( null )->slice( 0, 1 );
$filter->add( 'product.id', '==', $prodIds )
->add( $filter->make( 'product:has', ['catalog', $types, $treeCatalogIds] ), '!=', null );
return !$manager->search( $filter )->isEmpty();
}
/**
* Returns the catalog IDs for the given catalog codes
*
* @param array $codes List of catalog codes
* @return array List of catalog IDs
*/
protected function getCatalogIds( array $codes ) : array
{
// Fetch hidden categories too (null for filter)
$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
$filter = $manager->filter( null )->add( ['catalog.code' => $codes] )->slice( 0, count( $codes ) );
return $manager->search( $filter )->keys()->all();
}
/**
* Returns the catalog IDs from the given catalog item and its children
*
* @param \Aimeos\MShop\Catalog\Item\Iface $catalogItem Catalog node object
* @return array List of catalog IDs
*/
protected function getNodeCatalogIds( \Aimeos\MShop\Catalog\Item\Iface $catalogItem ) : array
{
$catalogIds = [$catalogItem->getId()];
foreach( $catalogItem->getChildren() as $childNode )
{
if( $childNode->getStatus() > 0 ) {
$catalogIds = array_merge( $catalogIds, $this->getNodeCatalogIds( $childNode ) );
}
}
return $catalogIds;
}
/**
* Returns the products IDs from the products in the basket
*
* @param \Aimeos\MShop\Order\Item\Iface $basket Basket object with ordered products included
* @return array List of proudct IDs
*/
protected function getProductIds( \Aimeos\MShop\Order\Item\Iface $basket ) : array
{
$productIds = [];
foreach( $basket->getProducts() as $product )
{
$productIds[] = $product->getProductId();
if( $parentid = $product->getParentProductId() ) {
$productIds[] = $parentid;
}
}
return array_unique( $productIds );
}
/**
* Returns the catalog codes for the given catalog IDs
*
* @param array $catalogIds List of catalog IDs
* @return array List of catalog codes
*/
protected function getTreeCatalogIds( array $catalogIds ) : array
{
$ids = [];
$catalogManager = \Aimeos\MShop::create( $this->context(), 'catalog' );
foreach( $catalogIds as $catId )
{
$treeNode = $catalogManager->getTree( $catId );
$ids = array_merge( $ids, $this->getNodeCatalogIds( $treeNode ) );
}
return array_unique( $ids );
}
}