1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Model\Option; |
4
|
|
|
|
5
|
|
|
use SpeckCatalog\Model\AbstractModel; |
6
|
|
|
use SpeckCatalog\Model\Option as Base; |
7
|
|
|
use SpeckCatalog\Model\Product\Relational as RelationalProduct; |
8
|
|
|
use SpeckCatalog\Model\Choice\Relational as RelationalChoice; |
9
|
|
|
|
10
|
|
|
class Relational extends Base |
11
|
|
|
{ |
12
|
|
|
protected $parent; |
13
|
|
|
protected $parentProducts; |
14
|
|
|
protected $choices; |
15
|
|
|
protected $images; |
16
|
|
|
protected $productId; //parent product id |
17
|
|
|
protected $choiceId; //parent choice id |
18
|
|
|
|
19
|
|
|
public function getKey() |
20
|
|
|
{ |
21
|
|
|
return $this->optionId; |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
public function getRecursivePrice($parentProductPrice = 0, $retailPrice = false) |
25
|
|
|
{ |
26
|
3 |
|
if ($this->getRequired()) { |
27
|
3 |
|
if ($this->has('choices')) { |
28
|
3 |
|
$choicePrices = array(); |
29
|
3 |
|
foreach ($this->getChoices() as $choice) { |
30
|
3 |
|
$choicePrices[] = $choice->getRecursivePrice($parentProductPrice, $retailPrice); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
3 |
|
asort($choicePrices, SORT_NUMERIC); |
34
|
3 |
|
return array_shift($choicePrices) ?: 0; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
6 |
|
public function getAdjustedPrice($retailPrice = false) |
43
|
|
|
{ |
44
|
6 |
|
$parent = $this->getParent(); |
45
|
6 |
|
if ($parent instanceof RelationalProduct) { |
46
|
3 |
|
return $parent->getPrice($retailPrice); |
47
|
|
|
} else { |
48
|
4 |
|
if (isset($parent)) { |
49
|
4 |
|
return $parent->getAdjustedPrice($retailPrice); |
50
|
|
|
} else { |
51
|
|
|
return 0; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return parentProducts |
59
|
|
|
*/ |
60
|
|
|
public function getParentProducts() |
61
|
|
|
{ |
62
|
|
|
return $this->parentProducts; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $parentProducts |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function setParentProducts($parentProducts) |
70
|
|
|
{ |
71
|
|
|
$this->parentProducts = $parentProducts; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return choices |
77
|
|
|
*/ |
78
|
3 |
|
public function getChoices() |
79
|
|
|
{ |
80
|
3 |
|
return $this->choices; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
public function addChoice($choice) |
84
|
|
|
{ |
85
|
6 |
|
$choice->setParent($this); |
86
|
6 |
|
$this->choices[] = $choice; |
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $choices |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function setChoices($choices) |
95
|
|
|
{ |
96
|
|
|
$this->choices = array(); |
97
|
|
|
|
98
|
|
|
foreach ($choices as $choice) { |
99
|
|
|
$this->addChoice($choice); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return images |
107
|
|
|
*/ |
108
|
|
|
public function getImages() |
109
|
|
|
{ |
110
|
|
|
return $this->images; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function addImage($image) |
114
|
|
|
{ |
115
|
|
|
$image->setParent($this); |
116
|
|
|
$this->images[] = $image; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $images |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function setImages($images) |
125
|
|
|
{ |
126
|
|
|
$this->images = array(); |
127
|
|
|
|
128
|
|
|
foreach ($images as $image) { |
129
|
|
|
$this->addImage($image); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return parent |
137
|
|
|
*/ |
138
|
8 |
|
public function getParent() |
139
|
|
|
{ |
140
|
8 |
|
return $this->parent; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $parent |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
8 |
|
public function setParent(AbstractModel $parent) |
148
|
|
|
{ |
149
|
8 |
|
$this->parent = $parent; |
150
|
|
|
|
151
|
8 |
|
if ($parent instanceof RelationalProduct) { |
152
|
3 |
|
$this->setProductId($parent->getProductId()); |
153
|
8 |
|
} elseif ($parent instanceof RelationalChoice) { |
154
|
4 |
|
$this->setChoiceId($parent->getChoiceId()); |
155
|
4 |
|
} |
156
|
|
|
|
157
|
8 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return productId |
162
|
|
|
*/ |
163
|
2 |
|
public function getProductId() |
164
|
|
|
{ |
165
|
2 |
|
return $this->productId; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $productId |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
5 |
|
public function setProductId($productId) |
173
|
|
|
{ |
174
|
5 |
|
$this->productId = $productId; |
175
|
5 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return choiceId |
180
|
|
|
*/ |
181
|
1 |
|
public function getChoiceId() |
182
|
|
|
{ |
183
|
1 |
|
return $this->choiceId; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $choiceId |
188
|
|
|
* @return self |
189
|
|
|
*/ |
190
|
6 |
|
public function setChoiceId($choiceId) |
191
|
|
|
{ |
192
|
6 |
|
$this->choiceId = $choiceId; |
193
|
6 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function __toString() |
197
|
|
|
{ |
198
|
|
|
if ($this->getName()) { |
199
|
|
|
return $this->getName(); |
200
|
|
|
} else { |
201
|
|
|
return 'Unnamed Option Group'; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getListType() |
206
|
|
|
{ |
207
|
|
|
switch ($this->optionTypeId) { |
208
|
|
|
case 1: |
209
|
|
|
return 'dropdown'; |
210
|
|
|
case 2: |
211
|
|
|
return 'radio'; |
212
|
|
|
case 3: |
213
|
|
|
return 'checkbox'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|