|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Renders the cart inside a form, so that it is editable. |
|
5
|
|
|
* |
|
6
|
|
|
* @package shop |
|
7
|
|
|
*/ |
|
8
|
|
|
class CartForm extends Form |
|
9
|
|
|
{ |
|
10
|
|
|
protected $cart; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct($controller, $name = "CartForm", $cart = null, $template = "Cart") |
|
13
|
|
|
{ |
|
14
|
|
|
$this->cart = $cart; |
|
15
|
|
|
$fields = FieldList::create( |
|
16
|
|
|
CartEditField::create("Items", "", $this->cart) |
|
17
|
|
|
->setTemplate($template) |
|
18
|
|
|
); |
|
19
|
|
|
$actions = FieldList::create( |
|
20
|
|
|
FormAction::create("updatecart", _t('CartForm.UpdateCart', "Update Cart")) |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Update the cart using data collected |
|
28
|
|
|
*/ |
|
29
|
|
|
public function updatecart($data, $form) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$items = $this->cart->Items(); |
|
32
|
|
|
$updatecount = $removecount = 0; |
|
33
|
|
|
|
|
34
|
|
|
$request = $this->getRequest(); |
|
35
|
|
|
$order = ShoppingCart::curr(); |
|
36
|
|
|
if ($request && $request->isAjax() && $order) { |
|
37
|
|
|
ShopTools::install_locale($order->Locale); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$numericConverter = NumericField::create('_temp'); |
|
41
|
|
|
|
|
42
|
|
|
$messages = []; |
|
43
|
|
|
$badMessages = []; |
|
44
|
|
|
if (isset($data['Items']) && is_array($data['Items'])) { |
|
45
|
|
|
foreach ($data['Items'] as $itemid => $fields) { |
|
46
|
|
|
$item = $items->byID($itemid); |
|
47
|
|
|
if (!$item) { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
//delete lines |
|
51
|
|
|
if (isset($fields['Remove']) || (isset($fields['Quantity']) && (int)$fields['Quantity'] <= 0)) { |
|
52
|
|
|
if (ShoppingCart::singleton()->remove($item->Buyable())) { |
|
53
|
|
|
$removecount++; |
|
54
|
|
|
} else { |
|
55
|
|
|
$badMessages[] = ShoppingCart::singleton()->getMessage(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
//update quantities |
|
61
|
|
|
if (isset($fields['Quantity']) && $quantity = Convert::raw2sql($fields['Quantity'])) { |
|
62
|
|
|
$numericConverter->setValue($quantity); |
|
63
|
|
|
if (!ShoppingCart::singleton()->setQuantity($item->Buyable(), $numericConverter->dataValue())) { |
|
64
|
|
|
$badMessages[] = ShoppingCart::singleton()->getMessage(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
//update variations |
|
68
|
|
|
if (isset($fields['ProductVariationID']) && $id = Convert::raw2sql($fields['ProductVariationID'])) { |
|
69
|
|
|
if ($item->ProductVariationID != $id) { |
|
70
|
|
|
$item->ProductVariationID = $id; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
//TODO: make updates through ShoppingCart class |
|
74
|
|
|
//TODO: combine with items that now match exactly |
|
75
|
|
|
//TODO: validate changes |
|
76
|
|
|
if ($item->isChanged()) { |
|
77
|
|
|
$item->write(); |
|
78
|
|
|
$updatecount++; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
if ($removecount) { |
|
83
|
|
|
$messages['remove'] = _t( |
|
84
|
|
|
'CartForm.REMOVED_ITEMS', |
|
85
|
|
|
"Removed {count} items.", |
|
86
|
|
|
"count is the amount that was removed", |
|
87
|
|
|
array('count' => $removecount) |
|
|
|
|
|
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
if ($updatecount) { |
|
91
|
|
|
$messages['updatecount'] = _t( |
|
92
|
|
|
'CartForm.UPDATED_ITEMS', |
|
93
|
|
|
"Updated {count} items.", |
|
94
|
|
|
"count is the amount that was updated", |
|
95
|
|
|
array('count' => $updatecount) |
|
|
|
|
|
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
if (count($messages)) { |
|
99
|
|
|
$form->sessionMessage(implode(" ", $messages), "good"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (count($badMessages)) { |
|
103
|
|
|
$form->sessionMessage(implode(" ", $badMessages), "bad"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->extend('updateCartFormResponse', $request, $response, $form); |
|
107
|
|
|
|
|
108
|
|
|
return $response ? $response : $this->controller->redirectBack(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.