1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* this class helps to create/edit/delete variations |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class CreateEcommerceVariations extends Controller |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $allowed_actions = array( |
|
|
|
|
12
|
|
|
"jsonforform" => "ADMIN", |
13
|
|
|
"createvariations", |
14
|
|
|
"select", |
15
|
|
|
"rename", |
16
|
|
|
"add", |
17
|
|
|
"remove", |
18
|
|
|
"move", |
19
|
|
|
'cansavevariation' |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The selected Product (ID) |
24
|
|
|
* @var Int |
25
|
|
|
*/ |
26
|
|
|
protected $_productID = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The select Product (Object) |
30
|
|
|
* @var Product |
31
|
|
|
*/ |
32
|
|
|
protected $_product = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* type | value |
36
|
|
|
* @var String |
37
|
|
|
*/ |
38
|
|
|
protected $_typeorvalue = "type"; // or value! |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ProductAttributeValue | ProductAttributeType |
42
|
|
|
* @var String |
43
|
|
|
*/ |
44
|
|
|
protected $_classname = "type"; // or value! |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Name of the Name field |
48
|
|
|
* @var String |
49
|
|
|
*/ |
50
|
|
|
protected $_namefield = "Name"; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Name of the Label field |
54
|
|
|
* only for ProductAttributeType |
55
|
|
|
* @var String |
56
|
|
|
*/ |
57
|
|
|
protected $_labelfield = "Label"; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Id of the item being altered |
61
|
|
|
* or its parent... |
62
|
|
|
* @var Int |
63
|
|
|
*/ |
64
|
|
|
protected $_id = 0; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Value of the item being altered |
68
|
|
|
* @var String |
69
|
|
|
*/ |
70
|
|
|
protected $_value = ""; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Position in the sorting order |
74
|
|
|
* use -1 to distinguish it from 0 (first in sorting order) |
75
|
|
|
* @var Int |
76
|
|
|
*/ |
77
|
|
|
protected $_position = -1; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return message |
81
|
|
|
* @var String |
82
|
|
|
*/ |
83
|
|
|
protected $_message = ""; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Type of message |
87
|
|
|
* good | bad | warning |
88
|
|
|
* @var String |
89
|
|
|
*/ |
90
|
|
|
protected $_messageclass = "good"; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Type IDs that are selected in the PRODUCT |
94
|
|
|
* @var Array |
95
|
|
|
*/ |
96
|
|
|
protected $_selectedtypeid = array(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Value IDs that are selected in the PRODUCT |
100
|
|
|
* @var Array |
101
|
|
|
*/ |
102
|
|
|
protected $_selectedvalueid = array(); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* What is going to be sent back. |
106
|
|
|
* @var String |
107
|
|
|
*/ |
108
|
|
|
protected $output = ""; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The name for the session varilable. |
112
|
|
|
* @var String |
113
|
|
|
*/ |
114
|
|
|
private static $session_name_for_selected_values = "SelectecedValues"; |
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @var String |
119
|
|
|
*/ |
120
|
|
|
private static $url_segment = "createecommercevariations"; |
|
|
|
|
121
|
|
|
|
122
|
|
|
public function init() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
parent::init(); |
125
|
|
|
Versioned::set_reading_mode("Stage.Stage"); |
126
|
|
|
$shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code"); |
127
|
|
|
if (!Permission::check("CMS_ACCESS_CMSMain") && !Permission::check($shopAdminCode)) { |
128
|
|
|
return Security::permissionFailure($this, _t('Security.PERMFAILURE', ' This page is secured and you need CMS rights to access it. Enter your credentials below and we will send you right along.')); |
129
|
|
|
} |
130
|
|
|
if (isset($_GET["typeorvalue"])) { |
131
|
|
|
$this->_typeorvalue = $_GET["typeorvalue"]; |
132
|
|
|
} |
133
|
|
|
if (isset($_GET["id"])) { |
134
|
|
|
$this->_id = intval($_GET["id"]); |
135
|
|
|
} |
136
|
|
|
if (isset($_GET["value"])) { |
137
|
|
|
$this->_value = urldecode($_GET["value"]); |
138
|
|
|
} |
139
|
|
|
if (isset($_GET["position"])) { |
140
|
|
|
$this->_position = intval($_GET["_position"]); |
141
|
|
|
} |
142
|
|
|
if ($this->_typeorvalue == "type") { |
143
|
|
|
$this->_classname = 'ProductAttributeType'; |
144
|
|
|
$this->_namefield = 'Name'; |
145
|
|
|
} else { |
146
|
|
|
$this->_classname = 'ProductAttributeValue'; |
147
|
|
|
$this->_namefield = 'Value'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->_productID = $this->request->param("ProductID"); |
|
|
|
|
151
|
|
|
$this->_product = Product::get()->byID($this->_productID); |
152
|
|
|
if (!$this->_product) { |
153
|
|
|
user_error("could not find product for ID: ".$this->_productID, E_USER_WARNING); |
154
|
|
|
} |
155
|
|
|
$this->_selectedtypeid = $this->_product->getArrayOfLinkedProductAttributeTypeIDs(); |
156
|
|
|
$this->_selectedvalueid = $this->_product->getArrayOfLinkedProductAttributeValueIDs(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
public function Link($action = null) |
161
|
|
|
{ |
162
|
|
|
return Controller::join_links( |
163
|
|
|
Director::baseURL(), |
164
|
|
|
$this->Config()->get("url_segment"), |
165
|
|
|
$action |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function index() |
170
|
|
|
{ |
171
|
|
|
return 10; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function Output() |
175
|
|
|
{ |
176
|
|
|
return $this->output; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* |
181
|
|
|
* checks the selected types and values and |
182
|
|
|
* makes variations from it... |
183
|
|
|
*/ |
184
|
|
|
public function createvariations() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
//lazy array |
187
|
|
|
$missingTypesID = array(-1 => -1); |
188
|
|
|
$missingTypes = array(); |
|
|
|
|
189
|
|
|
foreach ($this->_selectedtypeid as $typeID) { |
190
|
|
|
if (! isset($_GET[$typeID])) { |
191
|
|
|
$missingTypesID[$typeID] = $typeID; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
$types = ProductAttributeType::get()->exclude(array("ID" => $missingTypesID)); |
195
|
|
|
if ($types->count()) { |
196
|
|
|
$allTypesAndValues = array(); |
197
|
|
|
foreach ($types as $type) { |
198
|
|
|
if (isset($_GET[$type->ID])) { |
199
|
|
|
$oldValuesArray = explode(',', $_GET[$type->ID]); |
200
|
|
|
$newValuesArray = array(); |
201
|
|
|
foreach ($oldValuesArray as $oldValuesArray_Key => $oldValuesArray_Value) { |
202
|
|
|
$newValuesArray[$oldValuesArray_Value] = $oldValuesArray_Value; |
203
|
|
|
} |
204
|
|
|
$allTypesAndValues[$type->ID] = $newValuesArray; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
$cpt = 0; |
208
|
|
|
if (count($allTypesAndValues) > 0) { |
209
|
|
|
//create the variations... |
210
|
|
|
$cpt = $this->_product->generateVariationsFromAttributeValues($allTypesAndValues); |
211
|
|
|
//reset values in this class ... |
212
|
|
|
$this->_selectedtypeid = $this->_product->getArrayOfLinkedProductAttributeTypeIDs(); |
213
|
|
|
$this->_selectedvalueid = $this->_product->getArrayOfLinkedProductAttributeValueIDs(); |
214
|
|
|
} |
215
|
|
|
if ($cpt > 0) { |
216
|
|
|
$this->_message = ($cpt == 1 ? '1 new variation has' : "$cpt new variations have") . ' been created successfully'; |
217
|
|
|
} else { |
218
|
|
|
$this->_message = 'No new variations created'; |
219
|
|
|
} |
220
|
|
|
} else { |
221
|
|
|
$this->_message = 'No attribute types'; |
222
|
|
|
} |
223
|
|
|
$this->output = $this->jsonforform(); |
224
|
|
|
return $this->output; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* |
230
|
|
|
* @return String |
231
|
|
|
*/ |
232
|
|
|
public function jsonforform() |
233
|
|
|
{ |
234
|
|
|
if (! $this->_message) { |
235
|
|
|
$this->_message = _t("CreateEcommerceVariations.STARTEDITING", "Start editing the list below to create variations."); |
236
|
|
|
} |
237
|
|
|
$result['Message'] = $this->_message; |
|
|
|
|
238
|
|
|
$result['MessageClass'] = $this->_messageclass; |
239
|
|
|
$types = ProductAttributeType::get(); |
240
|
|
|
if ($types->count()) { |
241
|
|
|
foreach ($types as $type) { |
242
|
|
|
$resultType = array( |
243
|
|
|
'ID' => $type->ID, |
244
|
|
|
'Name' => $type->Name, |
245
|
|
|
'EditLink' => $type->CMSEditLink(), |
246
|
|
|
'Checked' => isset($this->_selectedtypeid[$type->ID]), |
247
|
|
|
'Disabled' => ! $this->_product->canRemoveAttributeType($type), |
248
|
|
|
'CanDelete' => $type->canDelete() |
249
|
|
|
); |
250
|
|
|
$values = $type->Values(); |
251
|
|
|
if ($values) { |
252
|
|
|
foreach ($values as $value) { |
253
|
|
|
$resultType['Values'][] = array( |
254
|
|
|
'ID' => $value->ID, |
255
|
|
|
'Name' => $value->Value, |
256
|
|
|
'EditLink' => $value->CMSEditLink(), |
257
|
|
|
'Checked' => isset($this->_selectedvalueid[$value->ID]), |
258
|
|
|
'CanDelete' => $value->canDelete() |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
$result['Types'][] = $resultType; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
$this->output = Convert::array2json($result); |
266
|
|
|
return $this->output; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function select() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
// is it type of Value? |
272
|
|
|
// if type is value -> create / delete Product Variation (if allowed) |
273
|
|
|
// elseif type is type - > add / remove selection... |
274
|
|
|
$this->_product->addAttributeType($obj); |
|
|
|
|
275
|
|
|
$this->_product->removeAttributeType($obj); |
276
|
|
|
die("not completed yet"); |
|
|
|
|
277
|
|
|
$this->output = $this->jsonforform(); |
|
|
|
|
278
|
|
|
return $this->output; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function rename() |
282
|
|
|
{ |
283
|
|
|
//is it Type or Value? |
284
|
|
|
$className = $this->_classname; |
285
|
|
|
$obj = $className::get()->byID($this->_id); |
286
|
|
|
if ($obj) { |
287
|
|
|
$name = $obj->{$this->_namefield}; |
288
|
|
|
if ($obj instanceof ProductAttributeType) { |
289
|
|
|
$obj->{$this->_labelfield} = $this->_value; |
290
|
|
|
$name .= " (".$obj->{$this->_labelfield}.")"; |
291
|
|
|
} |
292
|
|
|
$obj->{$this->_namefield} = $this->_value; |
293
|
|
|
$obj->write(); |
294
|
|
|
$this->_message = _t("CreateEcommerceVariations.HASBEENRENAMED", "$name has been renamed to ".$this->_value, "."); |
295
|
|
|
} else { |
296
|
|
|
$this->_message = _t("CreateEcommerceVariations.CANNOTBEFOUND", "Entry can not be found."); |
297
|
|
|
$this->_messageclass = "bad"; |
298
|
|
|
} |
299
|
|
|
$this->output = $this->jsonforform(); |
300
|
|
|
return $this->output; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* add a Type or a Value |
305
|
|
|
*/ |
306
|
|
|
public function add() |
307
|
|
|
{ |
308
|
|
|
//is it Type or Value? |
309
|
|
|
$obj = new $this->_classname(); |
310
|
|
|
$obj->{$this->_namefield} = $this->_value; |
311
|
|
|
if ($this->_id) { |
312
|
|
|
$obj->TypeID = $this->_id; |
313
|
|
|
$obj->write(); |
314
|
|
|
} else { |
315
|
|
|
$obj->write(); |
316
|
|
|
if ($obj instanceof ProductAttributeType) { |
317
|
|
|
$this->_product->addAttributeType($obj); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
$this->_selectedtypeid = $this->_product->getArrayOfLinkedProductAttributeTypeIDs(); |
321
|
|
|
$this->_selectedvalueid = $this->_product->getArrayOfLinkedProductAttributeValueIDs(); |
322
|
|
|
$this->_message = $this->_value.' '._t("CreateEcommerceVariations.HASBEENADDED", 'has been added.'); |
323
|
|
|
$this->output = $this->jsonforform(); |
324
|
|
|
return $this->output; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* remove a Type or a Value |
329
|
|
|
*/ |
330
|
|
|
public function remove() |
331
|
|
|
{ |
332
|
|
|
//is it Type or Value? |
333
|
|
|
$className = $this->_classname; |
334
|
|
|
$obj = $className::get()->byID($this->_id); |
335
|
|
|
if ($obj) { |
336
|
|
|
$name = $obj->{$this->_namefield}; |
337
|
|
|
if ($obj->canDelete()) { |
338
|
|
|
if ($this->_typeorvalue == "type") { |
339
|
|
|
$this->_product->removeAttributeType($obj); |
340
|
|
|
} |
341
|
|
|
$obj->delete(); |
342
|
|
|
$obj->destroy(); |
343
|
|
|
$this->_selectedtypeid = $this->_product->getArrayOfLinkedProductAttributeTypeIDs(); |
344
|
|
|
$this->_selectedvalueid = $this->_product->getArrayOfLinkedProductAttributeValueIDs(); |
345
|
|
|
$this->_message = _t("CreateEcommerceVariations.HASBEENDELETED", "$name has been deleted."); |
346
|
|
|
} else { |
347
|
|
|
$this->_message = _t("CreateEcommerceVariations.CANNOTBEDELETED", "$name can not be deleted (it is probably used in a sale)."); |
348
|
|
|
$this->_messageclass = "bad"; |
349
|
|
|
} |
350
|
|
|
} else { |
351
|
|
|
$this->_message = _t("CreateEcommerceVariations.CANNOTBEFOUND", "Entry can not be found."); |
352
|
|
|
$this->_messageclass = "bad"; |
353
|
|
|
} |
354
|
|
|
$this->output = $this->jsonforform(); |
355
|
|
|
return $this->output; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function move() |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
//is it Type or Value? |
361
|
|
|
//move Item |
362
|
|
|
die("not completed yet"); |
|
|
|
|
363
|
|
|
$this->output = "ok"; |
|
|
|
|
364
|
|
|
return $this->output; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* |
369
|
|
|
* |
370
|
|
|
* @return Boolean |
371
|
|
|
*/ |
372
|
|
|
public function cansavevariation() |
|
|
|
|
373
|
|
|
{ |
374
|
|
|
$variation = null; |
375
|
|
|
if (isset($_GET['variation'])) { |
376
|
|
|
$obj = ProductVariation::get()->byID(intval($_GET['variation'])); |
|
|
|
|
377
|
|
|
} |
378
|
|
|
foreach ($this->_selectedtypeid as $typeID) { |
379
|
|
|
if (isset($_GET[$typeID])) { |
380
|
|
|
$value = $_GET[$typeID]; |
381
|
|
|
if (! $variation && ! $value) { |
382
|
|
|
$this->output = false; |
|
|
|
|
383
|
|
|
return $this->output; |
384
|
|
|
} |
385
|
|
|
if ($value) { |
386
|
|
|
$values[$typeID] = $value; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
} else { |
389
|
|
|
$this->output = false; |
390
|
|
|
return $this->output; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
$variations = $this->_product->getComponents('Variations', $variation ? "\"ProductVariation\".\"ID\" != '$variation->ID'" : ''); |
394
|
|
|
foreach ($variations as $otherVariation) { |
395
|
|
|
$otherValues = DB::query( |
396
|
|
|
" |
397
|
|
|
SELECT \"TypeID\", \"ProductAttributeValueID\" |
398
|
|
|
FROM \"ProductVariation_AttributeValues\" |
399
|
|
|
INNER JOIN \"ProductAttributeValue\" ON \"ProductAttributeValue\".\"ID\" = \"ProductAttributeValueID\" |
400
|
|
|
WHERE \"ProductVariationID\" = '$otherVariation->ID' ORDER BY \"TypeID\"" |
401
|
|
|
)->map()->toArray(); |
402
|
|
|
if ($otherValues == $values) { |
|
|
|
|
403
|
|
|
$this->output = false; |
404
|
|
|
return $this->output; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
$this->output = true; |
|
|
|
|
408
|
|
|
return $this->output; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
|
413
|
|
|
class CreateEcommerceVariations_Field extends LiteralField |
|
|
|
|
414
|
|
|
{ |
415
|
|
|
public function __construct($name, $additionalContent = '', $productID) |
|
|
|
|
416
|
|
|
{ |
417
|
|
|
Requirements::themedCSS("CreateEcommerceVariationsField", "ecommerce_product_variation"); |
418
|
|
|
$additionalContent .= $this->renderWith("CreateEcommerceVariations_Field"); |
419
|
|
|
parent::__construct($name, $additionalContent); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function ProductVariationGetPluralName() |
423
|
|
|
{ |
424
|
|
|
return Convert::raw2att(singleton("ProductVariation")->plural_name()); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
public function ProductAttributeTypeGetPluralName() |
428
|
|
|
{ |
429
|
|
|
return Convert::raw2att(singleton("ProductAttributeType")->plural_name()); |
430
|
|
|
} |
431
|
|
|
public function ProductAttributeValueGetPluralName() |
432
|
|
|
{ |
433
|
|
|
return Convert::raw2att(singleton("ProductAttributeValue")->plural_name()); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
public function CheckboxField($name, $title) |
437
|
|
|
{ |
438
|
|
|
return new CheckboxField($name, $title); |
439
|
|
|
} |
440
|
|
|
public function TextField($name, $title) |
441
|
|
|
{ |
442
|
|
|
return new TextField($name, $title); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
View Code Duplication |
public function AttributeSorterLink() |
|
|
|
|
446
|
|
|
{ |
447
|
|
|
$singleton = singleton("ProductAttributeType"); |
448
|
|
|
if (class_exists("DataObjectSorterController") && $singleton->hasExtension("DataObjectSorterDOD")) { |
449
|
|
|
return DataObjectSorterController::popup_link($className = "ProductAttributeType", $filterField = "", $filterValue = "", $linkText = "Sort Types"); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
View Code Duplication |
public function ValueSorterLink() |
|
|
|
|
453
|
|
|
{ |
454
|
|
|
$singleton = singleton("ProductAttributeValue"); |
455
|
|
|
if (class_exists("DataObjectSorterController") && $singleton->hasExtension("DataObjectSorterDOD")) { |
456
|
|
|
return DataObjectSorterController::popup_link($className = "ProductAttributeValue", $filterField = "TypeChangeToId", $filterValue = "ID", $linkText = "Sort Values"); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
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.