Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

code/AccountPage.php (3 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
14
{
15
    /**
16
     * standard SS variable.
17
     *
18
     *@var array
19
     */
20
    private static $casting = array(
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';
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
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
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
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
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';
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';
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.';
134
135
    /**
136
     * Returns the link to the AccountPage on this site.
137
     * @param string $action [optional]
138
     * @return string (URLSegment)
139
     */
140
    public static function find_link($action = null)
141
    {
142
        $page = AccountPage::get()
143
            ->filter(array('ClassName' => 'AccountPage'))
144
            ->First();
145
        if ($page) {
146
            return $page->Link($action);
147
        }
148
    }
149
150
    /**
151
     * Returns a list of all previous orders for the member / account.
152
     *
153
     * @return DataList
154
     */
155
    public function PastOrders()
156
    {
157
        $this->calculatePastOrders();
158
159
        return $this->pastOrders;
160
    }
161
162
    /**
163
     * casted variable.
164
     *
165
     * @return float (casted as Currency)
166
     */
167
    public function getRunningTotal()
168
    {
169
        return $this->getRunningTotal();
170
    }
171
    public function RunningTotal()
172
    {
173
        $this->calculatePastOrders();
174
175
        return $this->calculatedTotal;
176
    }
177
178
    /**
179
     * casted variable.
180
     *
181
     * @return float (casted as Currency)
182
     */
183
    public function getRunningPaid()
184
    {
185
        return $this->getRunningPaid();
186
    }
187
    public function RunningPaid()
188
    {
189
        $this->calculatePastOrders();
190
191
        return $this->calculatedPaid;
192
    }
193
194
    /**
195
     * casted variable.
196
     *
197
     * @return float (casted as Currency)
198
     */
199
    public function getRunningOutstanding()
200
    {
201
        return $this->getRunningOutstanding();
202
    }
203
    public function RunningOutstanding()
204
    {
205
        $this->calculatePastOrders();
206
207
        return $this->calculatedOutstanding;
208
    }
209
210
    /**
211
     * retrieves previous orders and adds totals to it...
212
     * return DataList.
213
     **/
214
    protected function calculatePastOrders()
215
    {
216
        if (!$this->pastOrders) {
217
            $this->pastOrders = $this->pastOrdersSelection();
218
            $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...
219
            $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...
220
            $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...
221
            $member = Member::currentUser();
222
            $canDelete = false;
223
            if ($this->pastOrders->count()) {
224
                foreach ($this->pastOrders as $order) {
225
                    $this->calculatedTotal += $order->Total();
226
                    $this->calculatedPaid += $order->TotalPaid();
227
                    $this->calculatedOutstanding += $order->TotalOutstanding();
228
                }
229
            }
230
        }
231
232
        return $this->pastOrders;
233
    }
234
235
    /**
236
     * @return DataList (Orders)
237
     */
238
    protected function pastOrdersSelection()
239
    {
240
        $memberID = intval(Member::currentUserID());
241
        if (!$memberID) {
242
            //set t
243
            $memberID = RAND(0, 1000000) * -1;
244
        }
245
        if ($memberID) {
246
            return Order::get()
247
                ->where(
248
                    '"Order"."MemberID" = '.$memberID.'
249
                    AND ("CancelledByID" = 0 OR "CancelledByID" IS NULL)')
250
                ->innerJoin('OrderStep', '"Order"."StatusID" = "OrderStep"."ID"');
251
        }
252
253
        return 0;
254
    }
255
256
    /**
257
     * tells us if the current page is part of e-commerce.
258
     *
259
     * @return bool
260
     */
261
    public function IsEcommercePage()
262
    {
263
        return true;
264
    }
265
}
266
267
class AccountPage_Controller extends Page_Controller
268
{
269
    //TODO: why do we need this?
270
    private static $allowed_actions = array(
271
        'MemberForm',
272
    );
273
274
    /**
275
     * standard controller function.
276
     **/
277
    public function init()
278
    {
279
        parent::init();
280
        if (!$this->AccountMember() && 1 == 2) {
281
            $messages = array(
282
                'default' => '<p class="message good">'._t('Account.LOGINFIRST', 'You will need to log in before you can access the account page. ').'</p>',
283
                'logInAgain' => _t('Account.LOGINAGAIN', 'You have been logged out. If you would like to log in again, please do so below.'),
284
            );
285
            Security::permissionFailure($this, $messages);
286
287
            return false;
288
        }
289
        Requirements::themedCSS('AccountPage', 'ecommerce');
290
    }
291
292
    /**
293
     * Return a form allowing the user to edit
294
     * their details with the shop.
295
     *
296
     * @return ShopAccountForm
297
     */
298
    public function MemberForm()
299
    {
300
        return ShopAccountForm::create($this, 'MemberForm', $mustCreateAccount = true);
301
    }
302
303
    /**
304
     * Returns the current member.
305
     */
306
    public function AccountMember()
307
    {
308
        return Member::currentUser();
309
    }
310
}
311