1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ProductAttributeValue extends DataObject implements EditableEcommerceObject |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Standard SS variable. |
8
|
|
|
*/ |
9
|
|
|
private static $api_access = array( |
10
|
|
|
'view' => array( |
11
|
|
|
"Value", |
12
|
|
|
"Type" |
13
|
|
|
) |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
private static $db = array( |
17
|
|
|
'Code' => 'Varchar(255)', |
18
|
|
|
'Value' => 'Varchar(255)', |
19
|
|
|
'Sort' => 'Int', |
20
|
|
|
'MergeIntoNote' => 'Varchar(255)' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $has_one = array( |
24
|
|
|
'Type' => 'ProductAttributeType', |
25
|
|
|
'MergeInto' => 'ProductAttributeValue' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
private static $belongs_many_many = array( |
29
|
|
|
'ProductVariation' => 'ProductVariation' |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
private static $summary_fields = array( |
33
|
|
|
'Type.FullName' => 'Type', |
34
|
|
|
'Value' => 'Value' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
private static $searchable_fields = array( |
38
|
|
|
'Value' => 'PartialMatchFilter' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
private static $casting = array( |
42
|
|
|
'Title' => 'HTMLText', |
43
|
|
|
'FullTitle' => 'Varchar', |
44
|
|
|
'ValueForDropdown' => "HTMLText", |
45
|
|
|
'ValueForTable' => "HTMLText" |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
private static $indexes = array( |
49
|
|
|
'Sort' => true, |
50
|
|
|
'Code' => true |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* finds or makes a ProductAttributeType, based on the lower case Name. |
55
|
|
|
* |
56
|
|
|
* @param productAttributeType | int $type |
57
|
|
|
* @param string $value |
58
|
|
|
* @param boolean $create |
59
|
|
|
* @param boolean $findByID |
60
|
|
|
* |
61
|
|
|
* @return ProductAttributeType |
62
|
|
|
*/ |
63
|
|
|
public static function find_or_make($type, $value, $create = true, $findByID = false) |
64
|
|
|
{ |
65
|
|
|
if ($type instanceof ProductAttributeType) { |
66
|
|
|
$type = $type->ID; |
67
|
|
|
} |
68
|
|
|
$cleanedValue = strtolower($value); |
69
|
|
|
if ($findByID) { |
70
|
|
|
$intValue = intval($value); |
71
|
|
|
$valueObj = ProductAttributeValue::get() |
72
|
|
|
->filter(array("ID" => $intValue, "TypeID" => intval($type))) |
73
|
|
|
->first(); |
74
|
|
|
); |
|
|
|
|
75
|
|
|
//debug::log("INT VALUE:" .$intValue."-".$type); |
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
$valueObj = DataObject::get_one( |
78
|
|
|
'ProductAttributeValue', |
79
|
|
|
"(LOWER(\"Code\") = '$cleanedValue' OR LOWER(\"Value\") = '$cleanedValue') AND TypeID = ".intval($type), |
80
|
|
|
$cacheDataObjectGetOne = false |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
if ($valueObj) { |
84
|
|
|
|
85
|
|
|
return $valueObj; |
86
|
|
|
} |
87
|
|
|
$valueObj = ProductAttributeValue::create(); |
88
|
|
|
$valueObj->Code = $cleanedValue; |
89
|
|
|
$valueObj->Value = $value; |
90
|
|
|
$valueObj->TypeID = $type; |
91
|
|
|
if ($create) { |
92
|
|
|
$valueObj->write(); |
93
|
|
|
} |
94
|
|
|
return $valueObj; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private static $default_sort = "\"TypeID\" ASC, \"Sort\" ASC"; |
98
|
|
|
|
99
|
|
|
private static $singular_name = "Variation Attribute Value"; |
100
|
|
|
public function i18n_singular_name() |
101
|
|
|
{ |
102
|
|
|
return _t("ProductAttributeValue.ATTRIBUTEVALUE", "Variation Attribute Value"); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private static $plural_name = "Variation Attribute Values"; |
106
|
|
|
public function i18n_plural_name() |
107
|
|
|
{ |
108
|
|
|
return _t("ProductAttributeValue.ATTRIBUTEVALUES", "Variation Attribute Values"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function canDelete($member = null) |
112
|
|
|
{ |
113
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
114
|
|
|
if ($extended !== null) { |
115
|
|
|
return $extended; |
116
|
|
|
} |
117
|
|
|
if (DB::query(" |
118
|
|
|
SELECT COUNT(*) |
119
|
|
|
FROM \"ProductVariation_AttributeValues\" |
120
|
|
|
INNER JOIN \"ProductVariation\" |
121
|
|
|
ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\" |
122
|
|
|
WHERE \"ProductAttributeValueID\" = ".$this->ID |
123
|
|
|
)->value() == 0) { |
124
|
|
|
return parent::canDelete($member); |
125
|
|
|
} |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getCMSFields() |
130
|
|
|
{ |
131
|
|
|
$fields = parent::getCMSFields(); |
132
|
|
|
$variationField = $fields->dataFieldByName('ProductVariation'); |
133
|
|
|
if($variationField) { |
134
|
|
|
$variationField->setConfig(new GridFieldConfigForOrderItems()); |
135
|
|
|
} |
136
|
|
|
$fields->AddFieldToTab( |
137
|
|
|
"Root.Advanced", |
138
|
|
|
DropdownField::create( |
139
|
|
|
'MergeIntoID', |
140
|
|
|
_t('ProductAttributeType.MERGE_INTO', 'Merge into ...'), |
141
|
|
|
array(0 => _t('ProductAttributeType.DO_NOT_MERGE', '-- do not merge --')) + |
142
|
|
|
ProductAttributeValue::get() |
143
|
|
|
->filter(array('TypeID' => $this->TypeID)) |
144
|
|
|
->exclude(array("ID" => $this->ID)) |
145
|
|
|
->map('ID', 'FullTitle')->toArray() |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
$fields->AddFieldToTab("Root.Advanced", new ReadOnlyField("MergeIntoNote", "Merge Results Notes")); |
149
|
|
|
return $fields; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* link to edit the record |
154
|
|
|
* @param String | Null $action - e.g. edit |
155
|
|
|
* @return String |
156
|
|
|
*/ |
157
|
|
|
public function CMSEditLink($action = null) |
158
|
|
|
{ |
159
|
|
|
return Controller::join_links( |
160
|
|
|
Director::baseURL(), |
161
|
|
|
"/admin/product-config/".$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/", |
162
|
|
|
$action |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* casted variable |
168
|
|
|
* returns the value for the option in the select dropdown box. |
169
|
|
|
* @return String (HTML) |
170
|
|
|
**/ |
171
|
|
|
public function ValueForDropdown() |
172
|
|
|
{ |
173
|
|
|
return $this->getValueForDropdown(); |
174
|
|
|
} |
175
|
|
|
public function getValueForDropdown() |
176
|
|
|
{ |
177
|
|
|
$value = $this->Value; |
178
|
|
|
$extensionValue = $this->extend("updateValueForDropdown"); |
179
|
|
|
if ($extensionValue !== null && is_array($extensionValue) && count($extensionValue)) { |
180
|
|
|
$value = implode("", $extensionValue); |
181
|
|
|
} |
182
|
|
|
return $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* casted variable |
187
|
|
|
* returns the value for the variations table |
188
|
|
|
* @return String (HTML) |
189
|
|
|
**/ |
190
|
|
|
public function ValueForTable() |
191
|
|
|
{ |
192
|
|
|
return $this->getValueForTable(); |
193
|
|
|
} |
194
|
|
|
public function getValueForTable() |
195
|
|
|
{ |
196
|
|
|
$value = $this->Value; |
197
|
|
|
$extensionValue = $this->extend("updateValueForTable"); |
198
|
|
|
if ($extensionValue !== null && is_array($extensionValue) && count($extensionValue)) { |
199
|
|
|
$value = implode("", $extensionValue); |
200
|
|
|
} |
201
|
|
|
return $value; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* casted variable |
206
|
|
|
* returns the value for the variations table |
207
|
|
|
* @return String |
208
|
|
|
**/ |
209
|
|
|
public function Title() |
210
|
|
|
{ |
211
|
|
|
return $this->getTitle(); |
212
|
|
|
} |
213
|
|
|
public function getTitle() |
214
|
|
|
{ |
215
|
|
|
return $this->getValueForTable(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* casted variable |
220
|
|
|
* returns the value for the variations table |
221
|
|
|
* @return String |
222
|
|
|
**/ |
223
|
|
|
public function FullTitle() |
224
|
|
|
{ |
225
|
|
|
return $this->getFullTitle(); |
226
|
|
|
} |
227
|
|
|
public function getFullTitle() |
228
|
|
|
{ |
229
|
|
|
if($type = $this->Type()) { |
230
|
|
|
$typeName = $type->Name; |
231
|
|
|
} else { |
232
|
|
|
$typeName = _t('ProductAttributeValue.NO_TYPE_NAME', 'NO TYPE'); |
233
|
|
|
} |
234
|
|
|
return $typeName.': '.$this->Value.' ('.$this->Code.')'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function onBeforeDelete() |
238
|
|
|
{ |
239
|
|
|
parent::onBeforeDelete(); |
240
|
|
|
//delete ProductVariation_AttributeValues were the Attribute Value does not exist. |
241
|
|
|
DB::query("DELETE FROM \"ProductVariation_AttributeValues\" WHERE \"ProductVariation_AttributeValues\".\"ProductAttributeValueID\" = ".$this->ID); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function onBeforeWrite() |
245
|
|
|
{ |
246
|
|
|
parent::onBeforeWrite(); |
247
|
|
|
if (!$this->Value) { |
248
|
|
|
$this->Value = $this->i18n_singular_name(); |
249
|
|
|
$i = 0; |
250
|
|
|
$className = $this->ClassName; |
251
|
|
|
while( DataObject::get_one($className, array("Value" => $this->Value), $cacheDataObjectGetOne = false) ) { |
252
|
|
|
$this->Value = $this->i18n_singular_name()."_".$i; |
253
|
|
|
$i++; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
// No Need To Remove Variations because of onBeforeDelete |
257
|
|
|
/*$variations = $this->ProductVariation(); |
|
|
|
|
258
|
|
|
foreach($variations as $variation) $variation->delete();*/ |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Event handler called after writing to the database. |
264
|
|
|
*/ |
265
|
|
|
public function onAfterWrite() |
266
|
|
|
{ |
267
|
|
|
parent::onAfterWrite(); |
268
|
|
|
if ($this->MergeIntoID) { |
269
|
|
|
$newAttributeValue = $this->MergeInto(); |
270
|
|
|
if($newAttributeValue && $newAttributeValue->exists()) { |
271
|
|
|
$newID = $this->MergeIntoID; |
272
|
|
|
$oldID = $this->ID; |
273
|
|
|
$oldTypeID = $this->TypeID; |
274
|
|
|
$newTypeID = $newAttributeValue->TypeID; |
275
|
|
|
DB::query(" |
276
|
|
|
UPDATE \"ProductVariation_AttributeValues\" |
277
|
|
|
SET \"ProductAttributeValueID\" = ".$newID." |
278
|
|
|
WHERE \"ProductAttributeValueID\" = ".$oldID."; |
279
|
|
|
"); |
280
|
|
|
DB::query(" |
281
|
|
|
UPDATE \"Product_VariationAttributes\" |
282
|
|
|
SET \"ProductAttributeTypeID\" = ".$newTypeID." |
283
|
|
|
WHERE \"ProductAttributeTypeID\" = ".$oldTypeID."; |
284
|
|
|
"); |
285
|
|
|
$mergedInto = _t('ProductAttributeValue.MERGED_INTO', 'Merged successfully into'); |
286
|
|
|
$this->MergeIntoNote = $mergedInto.' '.$newAttributeValue->FullTitle(); |
287
|
|
|
$toBeDeleted = _t('ProductAttributeValue.TO_BE_DELETED', 'To be deleted'); |
288
|
|
|
$this->Value = $toBeDeleted.' '.$this->Value; |
289
|
|
|
$this->MergeIntoID = 0; |
290
|
|
|
$this->write(); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.