Completed
Push — master ( 21d98e...803d86 )
by Nicolaas
01:18
created

RepeatOrdersPage_Controller::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * RepeatOrdersPage page shows order history and a form to allow
4
 * the member to edit his/her details.
5
 *
6
 * @package ecommerce
7
 * @subpackage ecommerce ecommerce_Repeatorders
8
 * @author nicolaas [at] sunnysideup.co.nz
9
 */
10
class RepeatOrdersPage extends AccountPage
11
{
12
13
    /**
14
     * Standard SS method
15
     */
16
    private static $db = array(
17
        "WhatAreRepeatOrders" => "HTMLText", // explanation of repeat orders in general
18
        "OnceLoggedInYouCanCreateRepeatOrder" => "HTMLText" //explaining the benefits of logging in for Repeat Orders
19
    );
20
21
    /**
22
     * Standard SS method
23
     */
24
    private static $week_days = array(
25
        "Monday" => "Monday",
26
        "Tuesday" => "Tuesday",
27
        "Wednesday" => "Wednesday",
28
        "Thursday" => "Thursday",
29
        "Friday" => "Friday",
30
        "Saturday" => "Saturday",
31
        "Sunday" => "Sunday"
32
    );
33
34
    /**
35
     * Return a link to view the order on the account page.
36
     * actions are: create, update, view
37
     * @param String $action
38
     * @param int|string $orderID ID of the order
0 ignored issues
show
Documentation introduced by
There is no parameter named $orderID. Did you maybe mean $repeatOrderID?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
39
     */
40
    public static function get_repeat_order_link($action = 'view', $repeatOrderID = 0)
41
    {
42
        $page = DataObject::get_one(__CLASS__);
43
        if (!$page) {
44
            user_error('No RepeatOrderPage was found. Please create one in the CMS!', E_USER_ERROR);
45
        }
46
        return $page->Link($action)."/".$repeatOrderID."/";
47
    }
48
49
    /**
50
     * standard SS Method
51
     */
52
    public function canCreate($member = null)
53
    {
54
        if(DataObject::get_one("RepeatOrdersPage")) {
55
            return false;
56
        } else {
57
            return parent::canCreate($member);
58
        }
59
    }
60
61
62
    /**
63
     * standard SS Method
64
     */
65
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
    {
67
        $fields = parent::getCMSFields();
68
        $fields->addFieldToTab("Root.Content.ExplainingRepeatOrders", new HTMLEditorField($name = "WhatAreRepeatOrders", $title = "What Are Repeat Orders - Explanation Used throughout the site.", $rows = 3, $cols = 3));
69
        $fields->addFieldToTab("Root.Content.ExplainingRepeatOrders", new HTMLEditorField($name = "OnceLoggedInYouCanCreateRepeatOrder", $title = "Explanation for people who are not logged-in yet explaining that they can turn an order into a Repeat order...", $rows = 3, $cols = 3));
70
        return $fields;
71
    }
72
73
    /**
74
     * Returns all {@link Order} records for this
75
     * member that are completed.
76
     *
77
     * @return ArrayList
78
     */
79
    public function RepeatOrders()
80
    {
81
        $memberID = Member::currentUserID();
82
        return RepeatOrder::get()
83
            ->where("\"MemberID\" = '$memberID' AND \"Status\" NOT IN ('MemberCancelled', 'AdminCancelled')")
84
            ->sort("\"Created\" DESC");
85
    }
86
87
    /**
88
     * Automatically create an AccountPage if one is not found
89
     * on the site at the time the database is built (dev/build).
90
     */
91
    public function requireDefaultRecords()
92
    {
93
        parent::requireDefaultRecords();
94
        if (!DataObject::get_one('RepeatOrdersPage')) {
95
            $page = RepeatOrdersPage::create();
96
            $page->Title = 'Repeat Orders';
97
            $page->Content = '<p>This is the Repeat orders account page. It is used for shop users to login and create or change their Repeat orders.</p>';
98
            $page->URLSegment = 'repeat-orders';
99
            $page->WhatAreRepeatOrders = '<p>Repeat Orders allow you to regularly repeat an order.</p>';
100
            $page->OnceLoggedInYouCanCreateRepeatOrder = '<p>Once logged in you can setup a repeating order.</p>';
101
            $page->ShowInMenus = 0;
102
            $page->ShowInSearch = 0;
103
            $page->writeToStage('Stage');
104
            $page->publish('Stage', 'Live');
105
            DB::alteration_message('Repeat Order page \'Repeat Orders\' created', 'created');
106
        }
107
    }
108
109
    /**
110
     * Standard SS method
111
     * Sets the days available for repeating orders.
112
     */
113
    public function onBeforeWrite()
114
    {
115
        parent::onBeforeWrite();
116
    }
117
}
118
119
120
class RepeatOrdersPage_Controller extends AccountPage_Controller
121
{
122
    public function init()
123
    {
124
        parent::init();
125
    }
126
127
    public function createorder($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128
    {
129
        $orderID = intval($request->param("ID"));
130
        $order = null;
131
        if ($orderID) {
132
            $order = Order::get_by_id_if_can_view($orderID);
133
        }
134
        if (!$order) {
135
            $order = ShoppingCart::current_order();
136
        }
137
        //TODO: move items to order
138
        $params = array(
139
            'Order' => $order,
140
        );
141
        return $this->renderWith(
142
            ['RepeatOrdersPage_edit', 'Page'],
143
            $params
144
        );
145
    }
146
147
    public function cancel($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
148
    {
149
        if ($repeatOrderID = intval($request->param("ID"))) {
150
            $repeatOrder = DataObject::get_one('RepeatOrder', ["ID" => $repeatOrderID]);
151
            if ($repeatOrder && $repeatOrder->canEdit()) {
152
                $repeatOrder->Status = 'MemberCancelled';
153
                $repeatOrder->write();
154
155
                return Director::redirectBack();
0 ignored issues
show
Bug introduced by
The method redirectBack() does not exist on Director. Did you maybe mean direct()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
156
            }
157
        }
158
        die("Could not cancel repeat order.");
159
    }
160
161
    public function view($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
162
    {
163
        $params = array(
164
            'RepeatOrder' => false,
165
            'Message' => 'Repeating order could not be found.'
166
        );
167
        if ($repeatOrderID = intval($request->param("ID"))) {
168
            $repeatOrder = DataObject::get_one('RepeatOrder', "RepeatOrder.ID = '$repeatOrderID'");
169
            if ($repeatOrder && $repeatOrder->canView()) {
170
                $params = array(
171
                    'RepeatOrder' => $repeatOrder,
172
                    'Message' => "Please review order below."
173
                );
174
            } else {
175
                $params = array(
176
                    'RepeatOrder' => null,
177
                    'Message' => "You can not view this Order."
178
                );
179
            }
180
        }
181
182
        return $this->renderWith(
183
            ['RepeatOrdersPage_view', 'Page'],
184
            $params
185
        );
186
    }
187
188
    public function modify($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
189
    {
190
        $params = array(
191
            'RepeatOrder' => false,
192
            'Message' => 'There is no order by that ID.'
193
        );
194
        if ($repeatOrderID = intval($request->param("ID"))) {
195
            $repeatOrder = DataObject::get_by_id('RepeatOrder', $repeatOrderID);
196
            if ($repeatOrder->canEdit()) {
197
                $params = array(
198
                    'RepeatOrder' => false,
199
                    'Message' => 'Please edit your details below.'
200
                );
201
            }
202
        }
203
        return $this->renderWith(
204
            ['RepeatOrdersPage_edit', 'Page'],
205
            $params
206
        );
207
    }
208
209
    /**
210
     *
211
     * @return RepeatOrderForm
212
     */
213
    public function RepeatOrderForm()
214
    {
215
        $action = $this->request->param('Action');
216
        $repeatOrderID = intval($this->request->param('ID'));
217
        $orderID = 0;
218
        if ($action == 'createorder' || isset($_REQUEST['action_doCreate'])) {
219 View Code Duplication
            if (isset($_REQUEST['action_doCreate']) && isset($_REQUEST['repeatOrderID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
                $repeatOrderID = intval($_REQUEST['repeatOrderID']);
221
            }
222
            if ($action == 'createorder') {
223
                $orderID = $repeatOrderID;
224
                $repeatOrderID = 0;
225
            }
226
            return RepeatOrderForm::create(
227
                $this,
228
                'RepeatOrderForm',
229
                $repeatOrderID,
230
                $orderID
231
            );
232
        } elseif ($action == 'update' || isset($_REQUEST['action_doSave'])) {
233 View Code Duplication
            if (isset($_REQUEST['action_doSave']) && isset($_REQUEST['RepeatOrderID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
                $repeatOrderID = intval($_REQUEST['RepeatOrderID']);
235
            }
236
            return RepeatOrderForm::create(
237
                $this,
238
                'RepeatOrderForm',
239
                $repeatOrderID,
240
                $orderID
241
            );
242
        } elseif ($repeatOrderID) {
243
            return RepeatOrderForm::create(
244
                $this,
245
                'RepeatOrderForm',
246
                $repeatOrderID,
247
                $orderID
248
            );
249
        } else {
250
            return $this->redirect('404-could-not-find-order');
251
        }
252
    }
253
    /**
254
     * Show a list of all repeating orders.
255
     * @return HTML
256
     */
257
    public function admin()
258
    {
259
        $shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code");
260
        if (Permission::check("ADMIN") || Permission::check($shopAdminCode)) {
261
            RepeatOrder::create_automatically_created_orders();
262
            $params = array(
263
                "AllRepeatOrders" => RepeatOrder::get()->filter(["Status" => 'Active'])
264
            );
265
            Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
266
            //Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js");
267
            //Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
268
            Requirements::javascript("ecommerce_repeatorders/javascript/RepeatOrdersPage_admin.js");
269
            Requirements::themedCSS("RepeatOrdersPage_admin");
270
271
            return $this->renderWith(
272
                ['RepeatOrdersPage_admin', 'Page'],
273
                $params
274
            );
275
        } else {
276
            return Security::permissionFailure($this, _t('OrderReport.PERMISSIONFAILURE', 'Sorry you do not have permission for this function. Please login as an Adminstrator'));
277
        }
278
    }
279
}
280