Completed
Push — master ( a48064...b28689 )
by Nicolaas
03:30
created

AccountPage_Controller::CanonicalLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @description:
4
 * The Account Page allows the user to update their details.
5
 * You do not need to be logged in to the account page in order to view it... If you are not logged in
6
 * then the account page can be a page to create an account.
7
 *
8
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
9
 * @package: ecommerce
10
 * @sub-package: Pages
11
 * @inspiration: Silverstripe Ltd, Jeremy
12
 **/
13
class AccountPage extends Page
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    /**
16
     * standard SS variable.
17
     *
18
     *@var array
19
     */
20
    private static $casting = array(
0 ignored issues
show
Unused Code introduced by
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'RunningTotal' => 'Currency',
22
        'RunningPaid' => 'Currency',
23
        'RunningOutstanding' => 'Currency',
24
    );
25
26
    /**
27
     *@var float
28
     */
29
    protected $calculatedTotal = 0;
30
31
    /**
32
     *@var float
33
     */
34
    protected $calculatedPaid = 0;
35
36
    /**
37
     *@var float
38
     */
39
    protected $calculatedOutstanding = 0;
40
41
    /**
42
     *@var DataList
43
     */
44
    protected $pastOrders = null;
45
46
    /**
47
     * Standard SS variable.
48
     *
49
     * @Var String
50
     */
51
    private static $icon = 'ecommerce/images/icons/AccountPage';
0 ignored issues
show
Unused Code introduced by
The property $icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
    /**
54
     * Standard SS function, we only allow for one AccountPage to exist
55
     * but we do allow for extensions to exist at the same time.
56
     *
57
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
58
     *
59
     * @return bool
60
     **/
61
    public function canCreate($member = null)
62
    {
63
        return AccountPage::get()->filter(array('ClassName' => 'AccountPage'))->Count() ? false : $this->canEdit($member);
64
    }
65
66
    /**
67
     * Shop Admins can edit.
68
     *
69
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     *
71
     * @return bool
72
     */
73
    public function canEdit($member = null)
74
    {
75
        if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) {
76
            return true;
77
        }
78
79
        return parent::canEdit($member);
80
    }
81
82
    /**
83
     * Standard SS method.
84
     *
85
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
86
     *
87
     * @return bool
88
     */
89
    public function canDelete($member = null)
90
    {
91
        return $this->canEdit($member);
92
    }
93
94
    /**
95
     * Standard SS method.
96
     *
97
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     *
99
     * @return bool
100
     */
101
    public function canPublish($member = null)
102
    {
103
        return $this->canEdit($member);
104
    }
105
106
    /**
107
     * standard SS variable.
108
     *
109
     * @Var String
110
     */
111
    private static $singular_name = 'Account Page';
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
112
    public function i18n_singular_name()
113
    {
114
        return _t('AccountPage.SINGULARNAME', 'Account Page');
115
    }
116
117
    /**
118
     * standard SS variable.
119
     *
120
     * @Var String
121
     */
122
    private static $plural_name = 'Account Pages';
0 ignored issues
show
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
123
    public function i18n_plural_name()
124
    {
125
        return _t('AccountPage.PLURALNAME', 'Account Pages');
126
    }
127
128
    /**
129
     * Standard SS variable.
130
     *
131
     * @var string
132
     */
133
    private static $description = 'A page where the customer can view all their orders and update their details.';
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
134
135
    /**
136
     * Returns the link to the AccountPage on this site.
137
     * @param string $action [optional]
0 ignored issues
show
Documentation introduced by
Should the type for parameter $action not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
138
     * @return string (URLSegment)
139
     */
140
    public static function find_link($action = null)
141
    {
142
        $page = DataObject::get_one(
143
            'AccountPage',
144
            array('ClassName' => 'AccountPage')
145
        );
146
        if ($page) {
147
            return $page->Link($action);
148
        }
149
    }
150
151
    /**
152
     * Returns a list of all previous orders for the member / account.
153
     *
154
     * @return DataList
155
     */
156
    public function PastOrders()
157
    {
158
        $this->calculatePastOrders();
159
160
        return $this->pastOrders;
161
    }
162
163
    /**
164
     * casted variable.
165
     *
166
     * @return float (casted as Currency)
167
     */
168
    public function getRunningTotal()
169
    {
170
        return $this->getRunningTotal();
171
    }
172
    public function RunningTotal()
173
    {
174
        $this->calculatePastOrders();
175
176
        return $this->calculatedTotal;
177
    }
178
179
    /**
180
     * casted variable.
181
     *
182
     * @return float (casted as Currency)
183
     */
184
    public function getRunningPaid()
185
    {
186
        return $this->getRunningPaid();
187
    }
188
    public function RunningPaid()
189
    {
190
        $this->calculatePastOrders();
191
192
        return $this->calculatedPaid;
193
    }
194
195
    /**
196
     * casted variable.
197
     *
198
     * @return float (casted as Currency)
199
     */
200
    public function getRunningOutstanding()
201
    {
202
        return $this->getRunningOutstanding();
203
    }
204
    public function RunningOutstanding()
205
    {
206
        $this->calculatePastOrders();
207
208
        return $this->calculatedOutstanding;
209
    }
210
211
    /**
212
     * retrieves previous orders and adds totals to it...
213
     * return DataList.
214
     **/
215
    protected function calculatePastOrders()
216
    {
217
        if (!$this->pastOrders) {
218
            $this->pastOrders = $this->pastOrdersSelection();
219
            $this->calculatedTotal = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $calculatedTotal was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
220
            $this->calculatedPaid = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $calculatedPaid was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
221
            $this->calculatedOutstanding = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $calculatedOutstanding was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
222
            $member = Member::currentUser();
0 ignored issues
show
Unused Code introduced by
$member 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...
223
            $canDelete = false;
0 ignored issues
show
Unused Code introduced by
$canDelete 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...
224
            if ($this->pastOrders->count()) {
225
                foreach ($this->pastOrders as $order) {
226
                    $this->calculatedTotal += $order->Total();
227
                    $this->calculatedPaid += $order->TotalPaid();
228
                    $this->calculatedOutstanding += $order->TotalOutstanding();
229
                }
230
            }
231
        }
232
233
        return $this->pastOrders;
234
    }
235
236
    /**
237
     * @return DataList (Orders)
238
     */
239
    protected function pastOrdersSelection()
240
    {
241
        $memberID = intval(Member::currentUserID());
242
        if (!$memberID) {
243
            //set t
244
            $memberID = RAND(0, 1000000) * -1;
245
        }
246
        if ($memberID) {
247
            return Order::get()
248
                ->where(
249
                    '"Order"."MemberID" = '.$memberID.'
250
                    AND ("CancelledByID" = 0 OR "CancelledByID" IS NULL)')
251
                ->innerJoin('OrderStep', '"Order"."StatusID" = "OrderStep"."ID"');
252
        }
253
254
        return 0;
255
    }
256
257
    /**
258
     * tells us if the current page is part of e-commerce.
259
     *
260
     * @return bool
261
     */
262
    public function IsEcommercePage()
263
    {
264
        return true;
265
    }
266
}
267
268
class AccountPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
269
{
270
    //TODO: why do we need this?
271
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
272
        'MemberForm',
273
    );
274
275
    /**
276
     * standard controller function.
277
     **/
278
    public function init()
279
    {
280
        parent::init();
281
        if (!$this->AccountMember() && 1 == 2) {
282
            $messages = array(
283
                'default' => '<p class="message good">'._t('Account.LOGINFIRST', 'You will need to log in before you can access the account page. ').'</p>',
284
                'logInAgain' => _t('Account.LOGINAGAIN', 'You have been logged out. If you would like to log in again, please do so below.'),
285
            );
286
            Security::permissionFailure($this, $messages);
287
288
            return false;
289
        }
290
        Requirements::themedCSS('AccountPage', 'ecommerce');
291
    }
292
293
    /**
294
     * Return a form allowing the user to edit
295
     * their details with the shop.
296
     *
297
     * @return ShopAccountForm
298
     */
299
    public function MemberForm()
300
    {
301
        return ShopAccountForm::create($this, 'MemberForm', $mustCreateAccount = true);
302
    }
303
304
    /**
305
     * Returns the current member.
306
     */
307
    public function AccountMember()
308
    {
309
        return Member::currentUser();
310
    }
311
312
313
    /**
314
     * The link that Google et al. need to index.
315
     * @return string
316
     */
317
    public function CanonicalLink()
318
    {
319
        $link = $this->Link();
320
        $this->extend('UpdateCanonicalLink', $link);
321
322
        return $link;
323
    }
324
325
326
}
327