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
|
|
|
//debug::log("INT VALUE:" .$intValue."-".$type); |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$valueObj = DataObject::get_one( |
77
|
|
|
'ProductAttributeValue', |
78
|
|
|
"(LOWER(\"Code\") = '$cleanedValue' OR LOWER(\"Value\") = '$cleanedValue') AND TypeID = ".intval($type), |
79
|
|
|
$cacheDataObjectGetOne = false |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
if ($valueObj) { |
83
|
|
|
|
84
|
|
|
return $valueObj; |
85
|
|
|
} |
86
|
|
|
$valueObj = ProductAttributeValue::create(); |
87
|
|
|
$valueObj->Code = $cleanedValue; |
|
|
|
|
88
|
|
|
$valueObj->Value = $value; |
|
|
|
|
89
|
|
|
$valueObj->TypeID = $type; |
|
|
|
|
90
|
|
|
if ($create) { |
91
|
|
|
$valueObj->write(); |
92
|
|
|
} |
93
|
|
|
return $valueObj; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private static $default_sort = "\"TypeID\" ASC, \"Sort\" ASC"; |
|
|
|
|
97
|
|
|
|
98
|
|
|
private static $singular_name = "Variation Attribute Value"; |
|
|
|
|
99
|
|
|
public function i18n_singular_name() |
100
|
|
|
{ |
101
|
|
|
return _t("ProductAttributeValue.ATTRIBUTEVALUE", "Variation Attribute Value"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private static $plural_name = "Variation Attribute Values"; |
|
|
|
|
105
|
|
|
public function i18n_plural_name() |
106
|
|
|
{ |
107
|
|
|
return _t("ProductAttributeValue.ATTRIBUTEVALUES", "Variation Attribute Values"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function canDelete($member = null) |
111
|
|
|
{ |
112
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
113
|
|
|
if ($extended !== null) { |
114
|
|
|
return $extended; |
115
|
|
|
} |
116
|
|
|
if (DB::query(" |
117
|
|
|
SELECT COUNT(*) |
118
|
|
|
FROM \"ProductVariation_AttributeValues\" |
119
|
|
|
INNER JOIN \"ProductVariation\" |
120
|
|
|
ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\" |
121
|
|
|
WHERE \"ProductAttributeValueID\" = ".$this->ID |
122
|
|
|
)->value() == 0) { |
123
|
|
|
return parent::canDelete($member); |
124
|
|
|
} |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getCMSFields() |
129
|
|
|
{ |
130
|
|
|
$fields = parent::getCMSFields(); |
131
|
|
|
$variationField = $fields->dataFieldByName('ProductVariation'); |
132
|
|
|
if($variationField) { |
133
|
|
|
$variationField->setConfig(new GridFieldConfigForOrderItems()); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
$fields->AddFieldToTab( |
136
|
|
|
"Root.Advanced", |
137
|
|
|
DropdownField::create( |
138
|
|
|
'MergeIntoID', |
139
|
|
|
_t('ProductAttributeType.MERGE_INTO', 'Merge into ...'), |
140
|
|
|
array(0 => _t('ProductAttributeType.DO_NOT_MERGE', '-- do not merge --')) + |
141
|
|
|
ProductAttributeValue::get() |
142
|
|
|
->filter(array('TypeID' => $this->TypeID)) |
|
|
|
|
143
|
|
|
->exclude(array("ID" => $this->ID)) |
144
|
|
|
->map('ID', 'FullTitle')->toArray() |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
$fields->AddFieldToTab("Root.Advanced", new ReadOnlyField("MergeIntoNote", "Merge Results Notes")); |
148
|
|
|
return $fields; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* link to edit the record |
153
|
|
|
* @param String | Null $action - e.g. edit |
154
|
|
|
* @return String |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function CMSEditLink($action = null) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
return Controller::join_links( |
159
|
|
|
Director::baseURL(), |
160
|
|
|
"/admin/product-config/".$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/", |
161
|
|
|
$action |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* casted variable |
167
|
|
|
* returns the value for the option in the select dropdown box. |
168
|
|
|
* @return String (HTML) |
169
|
|
|
**/ |
170
|
|
|
public function ValueForDropdown() |
171
|
|
|
{ |
172
|
|
|
return $this->getValueForDropdown(); |
173
|
|
|
} |
174
|
|
View Code Duplication |
public function getValueForDropdown() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$value = $this->Value; |
|
|
|
|
177
|
|
|
$extensionValue = $this->extend("updateValueForDropdown"); |
178
|
|
|
if ($extensionValue !== null && is_array($extensionValue) && count($extensionValue)) { |
179
|
|
|
$value = implode("", $extensionValue); |
180
|
|
|
} |
181
|
|
|
return $value; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* casted variable |
186
|
|
|
* returns the value for the variations table |
187
|
|
|
* @return String (HTML) |
188
|
|
|
**/ |
189
|
|
|
public function ValueForTable() |
190
|
|
|
{ |
191
|
|
|
return $this->getValueForTable(); |
192
|
|
|
} |
193
|
|
View Code Duplication |
public function getValueForTable() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$value = $this->Value; |
|
|
|
|
196
|
|
|
$extensionValue = $this->extend("updateValueForTable"); |
197
|
|
|
if ($extensionValue !== null && is_array($extensionValue) && count($extensionValue)) { |
198
|
|
|
$value = implode("", $extensionValue); |
199
|
|
|
} |
200
|
|
|
return $value; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* casted variable |
205
|
|
|
* returns the value for the variations table |
206
|
|
|
* @return String |
207
|
|
|
**/ |
208
|
|
|
public function Title() |
209
|
|
|
{ |
210
|
|
|
return $this->getTitle(); |
211
|
|
|
} |
212
|
|
|
public function getTitle() |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
return $this->getValueForTable(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* casted variable |
219
|
|
|
* returns the value for the variations table |
220
|
|
|
* @return String |
221
|
|
|
**/ |
222
|
|
|
public function FullTitle() |
223
|
|
|
{ |
224
|
|
|
return $this->getFullTitle(); |
225
|
|
|
} |
226
|
|
|
public function getFullTitle() |
227
|
|
|
{ |
228
|
|
|
if($type = $this->Type()) { |
|
|
|
|
229
|
|
|
$typeName = $type->Name; |
230
|
|
|
} else { |
231
|
|
|
$typeName = _t('ProductAttributeValue.NO_TYPE_NAME', 'NO TYPE'); |
232
|
|
|
} |
233
|
|
|
return $typeName.': '.$this->Value.' ('.$this->Code.')'; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function onBeforeDelete() |
237
|
|
|
{ |
238
|
|
|
parent::onBeforeDelete(); |
239
|
|
|
//delete ProductVariation_AttributeValues were the Attribute Value does not exist. |
240
|
|
|
DB::query("DELETE FROM \"ProductVariation_AttributeValues\" WHERE \"ProductVariation_AttributeValues\".\"ProductAttributeValueID\" = ".$this->ID); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function onBeforeWrite() |
244
|
|
|
{ |
245
|
|
|
parent::onBeforeWrite(); |
246
|
|
|
if (!$this->Value) { |
|
|
|
|
247
|
|
|
$this->Value = $this->i18n_singular_name(); |
|
|
|
|
248
|
|
|
$i = 0; |
249
|
|
|
$className = $this->ClassName; |
250
|
|
|
while( DataObject::get_one($className, array("Value" => $this->Value), $cacheDataObjectGetOne = false) ) { |
|
|
|
|
251
|
|
|
$this->Value = $this->i18n_singular_name()."_".$i; |
|
|
|
|
252
|
|
|
$i++; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
// No Need To Remove Variations because of onBeforeDelete |
256
|
|
|
/*$variations = $this->ProductVariation(); |
|
|
|
|
257
|
|
|
foreach($variations as $variation) $variation->delete();*/ |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Event handler called after writing to the database. |
263
|
|
|
*/ |
264
|
|
|
public function onAfterWrite() |
265
|
|
|
{ |
266
|
|
|
parent::onAfterWrite(); |
267
|
|
|
if ($this->MergeIntoID) { |
|
|
|
|
268
|
|
|
$newAttributeValue = $this->MergeInto(); |
|
|
|
|
269
|
|
|
if($newAttributeValue && $newAttributeValue->exists()) { |
270
|
|
|
$newID = $this->MergeIntoID; |
|
|
|
|
271
|
|
|
$oldID = $this->ID; |
272
|
|
|
$oldTypeID = $this->TypeID; |
|
|
|
|
273
|
|
|
$newTypeID = $newAttributeValue->TypeID; |
274
|
|
|
DB::query(" |
275
|
|
|
UPDATE \"ProductVariation_AttributeValues\" |
276
|
|
|
SET \"ProductAttributeValueID\" = ".$newID." |
277
|
|
|
WHERE \"ProductAttributeValueID\" = ".$oldID."; |
278
|
|
|
"); |
279
|
|
|
DB::query(" |
280
|
|
|
UPDATE \"Product_VariationAttributes\" |
281
|
|
|
SET \"ProductAttributeTypeID\" = ".$newTypeID." |
282
|
|
|
WHERE \"ProductAttributeTypeID\" = ".$oldTypeID."; |
283
|
|
|
"); |
284
|
|
|
$mergedInto = _t('ProductAttributeValue.MERGED_INTO', 'Merged successfully into'); |
285
|
|
|
$this->MergeIntoNote = $mergedInto.' '.$newAttributeValue->FullTitle(); |
|
|
|
|
286
|
|
|
$toBeDeleted = _t('ProductAttributeValue.TO_BE_DELETED', 'To be deleted'); |
287
|
|
|
$this->Value = $toBeDeleted.' '.$this->Value; |
|
|
|
|
288
|
|
|
$this->MergeIntoID = 0; |
|
|
|
|
289
|
|
|
$this->write(); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
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.