Completed
Push — master ( a48248...e244a3 )
by Nicolaas
01:14
created

RepeatOrderModifier   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 12
dl 0
loc 172
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A i18n_singular_name() 0 4 1
A i18n_plural_name() 0 4 1
A ShowForm() 0 4 3
C getModifierForm() 0 95 10
A LiveCalculatedTotal() 0 4 1
A CanBeRemoved() 0 4 1
A ShowInTable() 0 4 1
A LiveName() 0 4 1
B PostSubmitAction() 0 26 7
1
<?php
2
3
class RepeatOrderModifier extends OrderModifier
4
{
5
    public static $singular_name = "Repeat Order Modifier";
6
    public function i18n_singular_name()
7
    {
8
        return _t("RepeatOrderModifier.REPEATORDERMODIFIER", "Repeat Order Modifier");
9
    }
10
11
    public static $plural_name = "Repeat Order Modifiers";
12
    public function i18n_plural_name()
13
    {
14
        return _t("RepeatOrderModifier.REPEATORDERMODIFIERS", "Repeat Order Modifiers");
15
    }
16
17
18
    /**
19
     * standard OrderModifier Method
20
     * Should we show a form in the checkout page for this modifier?
21
     */
22
    public function ShowForm()
23
    {
24
        return $this->Order()->Items() && $this->Order()->Items() && Member::currentUser();
25
    }
26
27
    public function getModifierForm(Controller $optionalController = NULL, Validator $optionalValidator = NULL)
28
    {
29
        $fields = FieldList::create();
30
        $fields->push($this->headingField());
31
        $fields->push($this->descriptionField());
32
        $order = ShoppingCart::current_order();
33
        $currentMember = Member::currentUser();
34
35
        $repeatOrder = null;
36
        if($order && $order->exists()) {
37
            $orderID = $order->ID;
38
            $repeatOrder = DataObject::get_one('RepeatOrder', ['OriginatingOrderID' => $order->ID]);
39
        } else {
40
            $orderID = 0;
41
        }
42
        $createLink = RepeatOrdersPage::get_repeat_order_link('createorder', $orderID);
43
        if ($repeatOrder && $currentMember) {
44
45
            $updateLink = RepeatOrdersPage::get_repeat_order_link('modify', $repeatOrder->ID);
46
            $cancelLink = RepeatOrdersPage::get_repeat_order_link('cancel', $repeatOrder->ID);
0 ignored issues
show
Unused Code introduced by
$cancelLink is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
48
            if ($repeatOrder->canModify()) {
49
                $fields->push(
50
                    LiteralField::create(
51
                        'modifyRepeatOrder',
52
<<<HTML
53
                        <div class="Actions">
54
                            <input id="ModifyRepeatOrderUpdate" class="action" type="button" value="Edit your associated Repeat Order" onclick="window.location='{$updateLink}';" />
55
                        </div>
56
HTML
57
                    )
58
                );
59
            } else {
60
                $fields->push(
61
                    LiteralField::create(
62
                        'createRepeatOrder',
63
<<<HTML
64
                        <div class="Actions">
65
                            <input id="ModifyRepeatOrderCreate" class="action" type="button" value="Create a new Repeat Order" onclick="window.location='{$createLink}';" />
66
                        </div>
67
HTML
68
                    )
69
                );
70
            }
71
            Requirements::customScript("jQuery(document).ready(function(){jQuery(\"input[name='action_processOrder']\").hide();});", "hide_action_processOrder");
72
        } elseif ($currentMember) {
73
            if ($order->RepeatOrderID) {
74
                $fields->push(
75
                    LiteralField::create(
76
                        "whatAreRepeatOrders",
77
<<<HTML
78
                        <div id="WhatAreRepeatOrders">This order is based on a Repeat Order.</div>
79
HTML
80
                    ));
81
            } else {
82
                $fields->push(
83
                    LiteralField::create(
84
                        'createRepeatOrder',
85
<<<HTML
86
                        <div class="Actions">
87
                            <input  id="ModifyRepeatOrderCreate" class="action" type="button" value="Turn this Order into a Repeat Order" onclick="window.location='{$createLink}';" />
88
                        </div>
89
HTML
90
                    )
91
                );
92
                $page = DataObject::get_one("RepeatOrdersPage");
93
                if ($page) {
94
                    $fields->push(
95
                        LiteralField::create("whatAreRepeatOrders",
96
<<<HTML
97
                        <div id="WhatAreRepeatOrders">$page->WhatAreRepeatOrders</div>
98
HTML
99
                    ));
100
                }
101
            }
102
        } else {
103
            $page = DataObject::get_one("RepeatOrdersPage");
104
            if ($page) {
105
                $fields->push(
106
                    LiteralField::create(
107
                        "whatAreRepeatOrders",
108
<<<HTML
109
                        <div id="WhatAreRepeatOrders">$page->OnceLoggedInYouCanCreateRepeatOrder</div>
110
HTML
111
                ));
112
            }
113
        }
114
        return RepeatOrderModifierForm::create(
115
            $optionalController,
116
            'RepeatOrderModifier',
117
            $fields,
118
            $actions = FieldList::create(),
119
            $optionalValidator
120
        );
121
    }
122
123
    public function LiveCalculatedTotal()
124
    {
125
        return 0;
126
    }
127
128
    public function CanBeRemoved()
129
    {
130
        return false;
131
    }
132
133
    public function ShowInTable()
134
    {
135
        return false;
136
    }
137
138
    public function LiveName()
139
    {
140
        return '';
141
    }
142
143
    /**
144
     * retursn and array like this: array(Title => "bla", Link => "/doit/now/");
145
     * This will be shown on the confirmation page....
146
     * @return Array
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
147
     */
148
    public function PostSubmitAction()
149
    {
150
        $order = $this->Order();
151
        if($order && $order->exists()) {
152
            if ($order->MemberID) {
153
                if($order->RepeatOrderID) {
154
                    return array(
155
                        "Title" => _t("RepeatOrder.MODIFYORDER", "Edit repeating order"),
156
                        "Link" => RepeatOrdersPage::get_repeat_order_link("view", $order->RepeatOrderID)
157
                    );
158
                }
159
                $existingRepeatOrder = RepeatOrder::get()->filter(['OriginatingOrderID' => $order->ID])->first();
160
                if($existingRepeatOrder && $existingRepeatOrder->exists()) {
161
                    return array(
162
                        "Title" => _t("RepeatOrder.MODIFYORDER", "Edit repeating order"),
163
                        "Link" => RepeatOrdersPage::get_repeat_order_link("modify", $existingRepeatOrder->ID)
164
                    );
165
                } else {
166
                    return array(
167
                        "Title" => _t("RepeatOrder.CREATEREPEATEORDER", "Turn this Order into a Repeat Order"),
168
                        "Link" => RepeatOrdersPage::get_repeat_order_link("createorder", $this->Order()->ID)
169
                    );
170
                }
171
            }
172
        }
173
    }
174
}
175