1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Service; |
4
|
|
|
|
5
|
|
|
use SpeckCatalog\Model\Product\Relational as RelationalProduct; |
6
|
|
|
use SpeckCatalog\Model\Choice\Relational as RelationalChoice; |
7
|
|
|
|
8
|
|
|
class Option extends AbstractService |
9
|
|
|
{ |
10
|
|
|
protected $entityMapper = 'speckcatalog_option_mapper'; |
11
|
|
|
protected $choiceService; |
12
|
|
|
protected $imageService; |
13
|
|
|
protected $productService; |
14
|
|
|
|
15
|
1 |
|
public function getByProductId($productId, $populate = false, $recursive = false, array $extraFields = array()) |
16
|
|
|
{ |
17
|
1 |
|
$options = $this->getEntityMapper()->getByProductId($productId, $extraFields); |
18
|
1 |
|
if ($populate) { |
19
|
1 |
|
foreach ($options as $option) { |
20
|
1 |
|
$this->populate($option, $recursive); |
21
|
1 |
|
} |
22
|
1 |
|
} |
23
|
1 |
|
return $options; |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function getByParentChoiceId($choiceId, $populate = false) |
27
|
|
|
{ |
28
|
1 |
|
$options = $this->getEntityMapper()->getByParentChoiceId($choiceId); |
29
|
1 |
|
if ($populate) { |
30
|
1 |
|
foreach ($options as $option) { |
31
|
1 |
|
$this->populate($option); |
32
|
1 |
|
} |
33
|
1 |
|
} |
34
|
1 |
|
return $options; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getBuildersByProductId($productId) |
38
|
|
|
{ |
39
|
|
|
$choices = $this->getEntityMapper()->getBuildersByProductId($productId); |
40
|
|
|
var_dump($choices); |
|
|
|
|
41
|
|
|
die(); |
|
|
|
|
42
|
|
|
$builders = array(); |
|
|
|
|
43
|
|
|
foreach ($choices as $choice) { |
44
|
|
|
$builders[$choice['product_id']][] = $choice['choice_id']; |
45
|
|
|
} |
46
|
|
|
foreach ($builders as $pid => $choiceIds) { |
47
|
|
|
sort($choiceIds); |
48
|
|
|
$builders[$pid] = implode(',', $choiceIds); |
49
|
|
|
} |
50
|
|
|
return $builders; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function populate($option, $recursive = false, $children = true) |
54
|
|
|
{ |
55
|
1 |
|
$optionId = $option->getOptionId(); |
56
|
|
|
|
57
|
1 |
|
$allChildren = ($children === true) ? true : false; |
58
|
1 |
|
$children = (is_array($children)) ? $children : array(); |
59
|
|
|
|
60
|
1 |
|
if ($allChildren || in_array('choices', $children)) { |
61
|
1 |
|
$choiceService = $this->getChoiceService(); |
62
|
1 |
|
$choices = $choiceService->getByOptionId($optionId, true, $recursive); |
63
|
1 |
|
$option->setChoices($choices); |
64
|
1 |
|
} |
65
|
1 |
|
if ($allChildren || in_array('images', $children)) { |
66
|
1 |
|
$imageService = $this->getImageService(); |
67
|
1 |
|
$option->setImages($imageService->getImages('option', $optionId)); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
2 |
|
public function insert($option) |
72
|
|
|
{ |
73
|
2 |
|
$parent = $this->getExistingParent($option); |
74
|
2 |
|
$option = parent::insert($option); |
75
|
|
|
|
76
|
2 |
|
if ($parent instanceof \SpeckCatalog\Model\Product) { |
77
|
1 |
|
$this->getProductService()->addOption($parent, $option); |
78
|
1 |
|
$option->setParent($parent); |
79
|
1 |
|
} |
80
|
2 |
|
if ($parent instanceof \SpeckCatalog\Model\Choice) { |
81
|
1 |
|
$this->getChoiceService()->addOption($parent, $option); |
82
|
1 |
|
$option->setParent($parent); |
83
|
1 |
|
} |
84
|
2 |
|
return $option; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function getExistingParent($option) |
88
|
|
|
{ |
89
|
2 |
|
if (is_array($option)) { |
90
|
1 |
|
$hydrator = new \Zend\Stdlib\Hydrator\ClassMethods; |
|
|
|
|
91
|
1 |
|
$option = $hydrator->hydrate($option, new \SpeckCatalog\Model\Option\Relational); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
2 |
|
$parent = null; |
95
|
2 |
|
if ($option->getProductId()) { |
96
|
1 |
|
$data = array('product_id' => $option->getProductId()); |
97
|
1 |
|
$parent = $this->getProductService()->find($data); |
98
|
2 |
|
} elseif ($option->getChoiceId()) { |
99
|
1 |
|
$data = array('choice_id' => $option->getChoiceId()); |
100
|
1 |
|
$parent = $this->getChoiceService()->find($data); |
101
|
1 |
|
} |
102
|
2 |
|
return $parent; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function update($option, array $originalValues = null) |
106
|
|
|
{ |
107
|
2 |
|
if (null === $originalValues && is_array($option)) { |
108
|
1 |
|
$originalValues['option_id'] = $option['option_id']; |
109
|
1 |
|
} |
110
|
2 |
|
if (null === $originalValues && $option instanceof \SpeckCatalog\Model\Option) { |
111
|
1 |
|
$originalValues['option_id'] = $option->getOptionId(); |
112
|
1 |
|
} |
113
|
2 |
|
parent::update($option, $originalValues); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
public function removeChoice($optionData, $choiceData) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$choiceService = $this->getChoiceService(); |
119
|
|
|
|
120
|
|
|
$found = $choiceService->find($choiceData); |
121
|
|
|
if (!$found) { |
122
|
|
|
throw new \Exception(sprintf('choice not found - %n', $choiceData['choice_id'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$choiceService->delete($choiceData); //delete |
126
|
|
|
if ($choiceService->find($choiceData)) { |
127
|
|
|
throw new \Exception(sprintf('delete unsuccessful - %n remains after delete', $choiceData['choice_id'])); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return true; //choice was found, and then deleted successfully |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function sortChoices($optionId, array $order = array()) |
134
|
|
|
{ |
135
|
1 |
|
return $this->getEntityMapper()->sortChoices($optionId, $order); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return choiceService |
140
|
|
|
*/ |
141
|
3 |
|
public function getChoiceService() |
142
|
|
|
{ |
143
|
3 |
|
if (null === $this->choiceService) { |
144
|
1 |
|
$this->choiceService = $this->getServiceLocator()->get('speckcatalog_choice_service'); |
145
|
1 |
|
} |
146
|
3 |
|
return $this->choiceService; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $choiceService |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
4 |
|
public function setChoiceService($choiceService) |
154
|
|
|
{ |
155
|
4 |
|
$this->choiceService = $choiceService; |
156
|
4 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return imageService |
161
|
|
|
*/ |
162
|
3 |
|
public function getImageService() |
163
|
|
|
{ |
164
|
3 |
|
if (null === $this->imageService) { |
165
|
1 |
|
$this->imageService = $this->getServiceLocator()->get('speckcatalog_option_image_service'); |
166
|
1 |
|
} |
167
|
3 |
|
return $this->imageService; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $imageService |
172
|
|
|
* @return self |
173
|
|
|
*/ |
174
|
2 |
|
public function setImageService($imageService) |
175
|
|
|
{ |
176
|
2 |
|
$this->imageService = $imageService; |
177
|
2 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return productService |
182
|
|
|
*/ |
183
|
2 |
|
public function getProductService() |
184
|
|
|
{ |
185
|
2 |
|
if (null === $this->productService) { |
186
|
1 |
|
$this->productService = $this->getServiceLocator()->get('speckcatalog_product_service'); |
187
|
1 |
|
} |
188
|
2 |
|
return $this->productService; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param $productService |
193
|
|
|
* @return self |
194
|
|
|
*/ |
195
|
1 |
|
public function setProductService($productService) |
196
|
|
|
{ |
197
|
1 |
|
$this->productService = $productService; |
198
|
1 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|