CartForm   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 25

2 Methods

Rating   Name   Duplication   Size   Complexity  
F updatecart() 0 80 24
A __construct() 0 13 1
1
<?php
2
3
namespace SilverShop\Forms;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Extension\ShopConfigExtension;
7
use SilverShop\ShopTools;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\Forms\NumericField;
14
15
/**
16
 * Renders the cart inside a form, so that it is editable.
17
 *
18
 * @package shop
19
 */
20
class CartForm extends Form
21
{
22
    protected $cart;
23
24
    public function __construct($controller, $name = 'CartForm', $cart = null, $template = 'SilverShop\Cart\Cart')
25
    {
26
        $this->cart = $cart;
27
        $fields = FieldList::create(
28
            CartEditField::create('Items', '', $this->cart)
29
                ->setTemplate($template)
30
        );
31
        $actions = FieldList::create(
32
            FormAction::create('updatecart', _t(__CLASS__ . '.UpdateCart', 'Update Cart'))
33
                ->setUseButtonTag(Config::inst()->get(ShopConfigExtension::class, 'forms_use_button_tag'))
34
        );
35
36
        parent::__construct($controller, $name, $fields, $actions);
37
    }
38
39
    /**
40
     * Update the cart using data collected
41
     */
42
    public function updatecart($data, $form)
43
    {
44
        $items = $this->cart->Items();
45
        $updatecount = $removecount = 0;
46
47
        $request = $this->getRequest();
48
        $order = ShoppingCart::curr();
49
        if ($request && $request->isAjax() && $order) {
50
            ShopTools::install_locale($order->Locale);
51
        }
52
53
        $numericConverter = NumericField::create('_temp');
54
55
        $messages = [];
56
        $badMessages = [];
57
        if (isset($data['Items']) && is_array($data['Items'])) {
58
            foreach ($data['Items'] as $itemid => $fields) {
59
                $item = $items->byID($itemid);
60
                if (!$item) {
61
                    continue;
62
                }
63
                //delete lines
64
                if (isset($fields['Remove']) || (isset($fields['Quantity']) && (int)$fields['Quantity'] <= 0)) {
65
                    if (ShoppingCart::singleton()->removeOrderItem($item)) {
66
                        $removecount++;
67
                    } else {
68
                        $badMessages[] = ShoppingCart::singleton()->getMessage();
69
                    }
70
71
                    continue;
72
                }
73
                //update quantities
74
                if (isset($fields['Quantity']) && $quantity = Convert::raw2sql($fields['Quantity'])) {
75
                    $numericConverter->setValue($quantity);
76
                    if (!ShoppingCart::singleton()->updateOrderItemQuantity($item, $numericConverter->dataValue())) {
77
                        $badMessages[] = ShoppingCart::singleton()->getMessage();
78
                    }
79
                }
80
                //update variations
81
                if (isset($fields['ProductVariationID']) && $id = Convert::raw2sql($fields['ProductVariationID'])) {
82
                    if ($item->ProductVariationID != $id) {
83
                        $item->ProductVariationID = $id;
84
                    }
85
                }
86
                //TODO: make updates through ShoppingCart class
87
                //TODO: combine with items that now match exactly
88
                //TODO: validate changes
89
                if ($item->isChanged()) {
90
                    $item->write();
91
                    $updatecount++;
92
                }
93
            }
94
        }
95
        if ($removecount) {
96
            $messages['remove'] = _t(
97
                __CLASS__ . '.REMOVED_ITEMS',
98
                'Removed {count} items.',
99
                'count is the amount that was removed',
100
                ['count' => $removecount]
101
            );
102
        }
103
        if ($updatecount) {
104
            $messages['updatecount'] = _t(
105
                __CLASS__ . '.UPDATED_ITEMS',
106
                'Updated {count} items.',
107
                'count is the amount that was updated',
108
                ['count' => $updatecount]
109
            );
110
        }
111
        if (count($messages)) {
112
            $form->sessionMessage(implode(' ', $messages), 'good');
113
        }
114
115
        if (count($badMessages)) {
116
            $form->sessionMessage(implode(' ', $badMessages), 'bad');
117
        }
118
119
        $this->extend('updateCartFormResponse', $request, $response, $form, $removecount, $updatecount);
120
121
        return $response ? $response : $this->controller->redirectBack();
122
    }
123
}
124