1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Event; |
4
|
|
|
|
5
|
|
|
class FrontendEnabled |
6
|
|
|
{ |
7
|
|
|
/* |
8
|
|
|
* After insert/update of a productuom, |
9
|
|
|
* check all uoms belonging to that product, |
10
|
|
|
* if any productuom is enabled, enable the product, |
11
|
|
|
* otherwise, disable it. |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
public function insertProductUom($e) |
16
|
|
|
{ |
17
|
|
|
$dbResult = $e->getParam('result'); |
18
|
|
|
if (!count($dbResult)) { |
19
|
|
|
return; //no rows affected |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$productId = is_array($e->getParam('data')) |
23
|
|
|
? $e->getParam('data')['product_id'] |
24
|
|
|
: $e->getParam('data')->getProductId(); |
25
|
|
|
$productService = $e->getTarget()->getProductService(); |
26
|
|
|
$find = array('product_id' => $productId); |
27
|
|
|
$product = $productService->find($find, array('uoms')); |
28
|
|
|
|
29
|
|
|
$enabled = $this->canProductBeEnabled($product); |
30
|
|
|
|
31
|
|
|
$productService->setEnabledProduct($productId, $enabled); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function updateProductUom($e) |
35
|
|
|
{ |
36
|
|
|
$productUomService = $e->getTarget(); |
37
|
|
|
$productService = $productUomService->getProductService(); |
38
|
|
|
|
39
|
|
|
$where = $e->getParam('where'); |
40
|
|
|
$uoms = $productUomService->findRows($where); |
41
|
|
|
|
42
|
|
|
$ids = array(); |
43
|
|
|
foreach ($uoms as $uom) { |
44
|
|
|
$ids[$uom['product_id']] = $uom['product_id']; |
45
|
|
|
} |
46
|
|
|
$products = $productService->getProductsById($ids); |
47
|
|
|
|
48
|
|
|
foreach ($products as $product) { |
49
|
|
|
$productService->populate($product, array('uoms')); |
50
|
|
|
$row = array( |
51
|
|
|
'product_id' => $productId, |
|
|
|
|
52
|
|
|
'enabled' => $this->canProductBeEnabled($product) |
53
|
|
|
); |
54
|
|
|
$where = array('product_id' => $productId); |
|
|
|
|
55
|
|
|
$productService->update($row, $where); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function canProductBeEnabled($product) |
60
|
|
|
{ |
61
|
|
|
if ($product->getProductTypeId() == 2) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
foreach ($product->getUoms() as $uom) { |
65
|
|
|
if ($uom->getEnabled()) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* After insert/update of product(s), |
74
|
|
|
* find any choices that reference the product, |
75
|
|
|
* if the product is enabled, enable the choice, |
76
|
|
|
* otherwise, disable it. |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
|
80
|
|
|
public static function updateProduct($e) |
81
|
|
|
{ |
82
|
|
|
$productService = $e->getTarget(); |
83
|
|
|
$choiceService = $productService->getChoiceService(); |
84
|
|
|
|
85
|
|
|
$where = $e->getParam('where'); |
86
|
|
|
$products = $productService->findMany($where, array('uoms')); |
87
|
|
|
|
88
|
|
|
foreach ($products as $product) { |
89
|
|
|
$productId = $product->getProductId(); |
90
|
|
|
$enabled = ($product->getEnabled()) ? 1 : 0; |
91
|
|
|
$choiceService->update( |
92
|
|
|
array('enabled' => $enabled), //data |
93
|
|
|
array('product_id' => $productId) //where |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$productIds = $e->getTarget(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function insertProduct($e) |
101
|
|
|
{ |
102
|
|
|
$result = $e->getParam('result'); |
103
|
|
|
if (!$result) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
$productId = $result; |
107
|
|
|
$data = array('product_id' => $productId); |
108
|
|
|
|
109
|
|
|
$productService = $e->getTarget(); |
110
|
|
|
$product = $productService->find($data); |
111
|
|
|
|
112
|
|
|
$enabled = ($product->getEnabled()) ? 1 : 0; |
113
|
|
|
|
114
|
|
|
$choiceService = $productService->getChoiceService(); |
115
|
|
|
$choiceService->update( |
116
|
|
|
array('enabled' => $enabled), //data |
117
|
|
|
array('product_id' => $productId) //where |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.