Completed
Push — master ( 364b5f...db455e )
by
unknown
04:01
created

code/AccountPage.php (19 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
17
     * standard SS variable.
18
     *
19
     *@var array
20
     */
21
    private static $casting = array(
0 ignored issues
show
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...
22
        'RunningTotal' => 'Currency',
23
        'RunningPaid' => 'Currency',
24
        'RunningOutstanding' => 'Currency',
25
    );
26
27
    /**
28
     *@var float
29
     */
30
    protected $calculatedTotal = 0;
31
32
    /**
33
     *@var float
34
     */
35
    protected $calculatedPaid = 0;
36
37
    /**
38
     *@var float
39
     */
40
    protected $calculatedOutstanding = 0;
41
42
    /**
43
     *@var DataList
44
     */
45
    protected $pastOrders = null;
46
47
    /**
48
     * Standard SS variable.
49
     *
50
     * @Var String
51
     */
52
    private static $icon = 'ecommerce/images/icons/AccountPage';
0 ignored issues
show
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...
53
54
    /**
55
     * Standard SS function, we only allow for one AccountPage to exist
56
     * but we do allow for extensions to exist at the same time.
57
     *
58
     * @param Member $member
0 ignored issues
show
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...
59
     *
60
     * @return bool
61
     **/
62
    public function canCreate($member = null)
63
    {
64
        return AccountPage::get()->filter(array('ClassName' => 'AccountPage'))->Count() ? false : $this->canEdit($member);
65
    }
66
67
    /**
68
     * Shop Admins can edit.
69
     *
70
     * @param Member $member
0 ignored issues
show
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...
71
     *
72
     * @return bool
73
     */
74
    public function canEdit($member = null)
75
    {
76
        if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) {
77
            return true;
78
        }
79
80
        return parent::canEdit($member);
81
    }
82
83
    /**
84
     * Standard SS method.
85
     *
86
     * @param Member $member
0 ignored issues
show
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...
87
     *
88
     * @return bool
89
     */
90
    public function canDelete($member = null)
91
    {
92
        return $this->canEdit($member);
93
    }
94
95
    /**
96
     * Standard SS method.
97
     *
98
     * @param Member $member
0 ignored issues
show
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...
99
     *
100
     * @return bool
101
     */
102
    public function canPublish($member = null)
103
    {
104
        return $this->canEdit($member);
105
    }
106
107
    /**
108
     * standard SS variable.
109
     *
110
     * @Var String
111
     */
112
    private static $singular_name = 'Account Page';
0 ignored issues
show
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...
113
    public function i18n_singular_name()
114
    {
115
        return _t('AccountPage.SINGULARNAME', 'Account Page');
116
    }
117
118
    /**
119
     * standard SS variable.
120
     *
121
     * @Var String
122
     */
123
    private static $plural_name = 'Account Pages';
0 ignored issues
show
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...
124
    public function i18n_plural_name()
125
    {
126
        return _t('AccountPage.PLURALNAME', 'Account Pages');
127
    }
128
129
    /**
130
     * Standard SS variable.
131
     *
132
     * @var string
133
     */
134
    private static $description = 'A page where the customer can view all their orders and update their details.';
0 ignored issues
show
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...
135
136
    /**
137
     * Returns the link to the AccountPage on this site.
138
     * @param string $action [optional]
0 ignored issues
show
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...
139
     * @return string (URLSegment)
140
     */
141
    public static function find_link($action = null)
142
    {
143
        $page = DataObject::get_one(
144
            'AccountPage',
145
            array('ClassName' => 'AccountPage')
146
        );
147
        if ($page) {
148
            return $page->Link($action);
149
        }
150
    }
151
152
    /**
153
     * Returns a list of all previous orders for the member / account.
154
     *
155
     * @return DataList
156
     */
157
    public function PastOrders()
158
    {
159
        $this->calculatePastOrders();
160
161
        return $this->pastOrders;
162
    }
163
164
    /**
165
     * casted variable.
166
     *
167
     * @return float (casted as Currency)
168
     */
169
    public function getRunningTotal()
170
    {
171
        return $this->getRunningTotal();
172
    }
173
    public function RunningTotal()
174
    {
175
        $this->calculatePastOrders();
176
177
        return $this->calculatedTotal;
178
    }
179
180
    /**
181
     * casted variable.
182
     *
183
     * @return float (casted as Currency)
184
     */
185
    public function getRunningPaid()
186
    {
187
        return $this->getRunningPaid();
188
    }
189
    public function RunningPaid()
190
    {
191
        $this->calculatePastOrders();
192
193
        return $this->calculatedPaid;
194
    }
195
196
    /**
197
     * casted variable.
198
     *
199
     * @return float (casted as Currency)
200
     */
201
    public function getRunningOutstanding()
202
    {
203
        return $this->getRunningOutstanding();
204
    }
205
    public function RunningOutstanding()
206
    {
207
        $this->calculatePastOrders();
208
209
        return $this->calculatedOutstanding;
210
    }
211
212
    /**
213
     * retrieves previous orders and adds totals to it...
214
     * return DataList.
215
     **/
216
    protected function calculatePastOrders()
217
    {
218
        if (!$this->pastOrders) {
219
            $this->pastOrders = $this->pastOrdersSelection();
220
            $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...
221
            $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...
222
            $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...
223
            $member = Member::currentUser();
0 ignored issues
show
$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...
224
            $canDelete = false;
0 ignored issues
show
$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...
225
            if ($this->pastOrders->count()) {
226
                foreach ($this->pastOrders as $order) {
227
                    $this->calculatedTotal += $order->Total();
228
                    $this->calculatedPaid += $order->TotalPaid();
229
                    $this->calculatedOutstanding += $order->TotalOutstanding();
230
                }
231
            }
232
        }
233
234
        return $this->pastOrders;
235
    }
236
237
    /**
238
     * @return DataList (Orders)
239
     */
240
    protected function pastOrdersSelection()
241
    {
242
        $memberID = intval(Member::currentUserID());
243
        if (!$memberID) {
244
            //set t
245
            $memberID = RAND(0, 1000000) * -1;
246
        }
247
        if ($memberID) {
248
            return Order::get()
249
                ->where(
250
                    '"Order"."MemberID" = '.$memberID.'
251
                    AND ("CancelledByID" = 0 OR "CancelledByID" IS NULL)'
252
                )
253
                ->innerJoin('OrderStep', '"Order"."StatusID" = "OrderStep"."ID"');
254
        }
255
256
        return 0;
257
    }
258
259
    /**
260
     * tells us if the current page is part of e-commerce.
261
     *
262
     * @return bool
263
     */
264
    public function IsEcommercePage()
265
    {
266
        return true;
267
    }
268
}
269
270
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...
271
{
272
    //TODO: why do we need this?
273
    private static $allowed_actions = array(
0 ignored issues
show
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...
274
        'MemberForm',
275
    );
276
277
    /**
278
     * standard controller function.
279
     **/
280
    public function init()
281
    {
282
        parent::init();
283
        if (!$this->AccountMember() && 1 == 2) {
284
            $messages = array(
285
                'default' => '<p class="message good">'._t('Account.LOGINFIRST', 'You will need to log in before you can access the account page. ').'</p>',
286
                'logInAgain' => _t('Account.LOGINAGAIN', 'You have been logged out. If you would like to log in again, please do so below.'),
287
            );
288
            Security::permissionFailure($this, $messages);
289
290
            return false;
291
        }
292
        Requirements::themedCSS('AccountPage', 'ecommerce');
293
    }
294
295
    /**
296
     * Return a form allowing the user to edit
297
     * their details with the shop.
298
     *
299
     * @return ShopAccountForm
300
     */
301
    public function MemberForm()
302
    {
303
        return ShopAccountForm::create($this, 'MemberForm', $mustCreateAccount = true);
304
    }
305
306
    /**
307
     * Returns the current member.
308
     */
309
    public function AccountMember()
310
    {
311
        return Member::currentUser();
312
    }
313
314
315
    /**
316
     * The link that Google et al. need to index.
317
     * @return string
318
     */
319
    public function CanonicalLink()
320
    {
321
        $link = $this->Link();
322
        $this->extend('UpdateCanonicalLink', $link);
323
324
        return $link;
325
    }
326
}
327