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 = 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; |
|
|
|
|
220
|
|
|
$this->calculatedPaid = 0; |
|
|
|
|
221
|
|
|
$this->calculatedOutstanding = 0; |
|
|
|
|
222
|
|
|
$member = Member::currentUser(); |
|
|
|
|
223
|
|
|
$canDelete = false; |
|
|
|
|
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 |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
//TODO: why do we need this? |
271
|
|
|
private static $allowed_actions = array( |
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.