1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @description: ShopAccountForm allows shop members to update their details. |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* @authors: Nicolaas [at] Sunny Side Up .co.nz |
7
|
|
|
* @package: ecommerce |
8
|
|
|
* @sub-package: forms |
9
|
|
|
* @inspiration: Silverstripe Ltd, Jeremy |
10
|
|
|
**/ |
11
|
|
|
class ShopAccountForm extends Form |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param Controller $controller |
15
|
|
|
* @param string $name, Name of the form |
|
|
|
|
16
|
|
|
*/ |
17
|
|
|
public function __construct($controller, $name, $mustCreateAccount = false) |
18
|
|
|
{ |
19
|
|
|
$member = Member::currentUser(); |
20
|
|
|
$requiredFields = null; |
|
|
|
|
21
|
|
|
if ($member && $member->exists()) { |
22
|
|
|
$fields = $member->getEcommerceFields(false); |
23
|
|
|
$clearCartAndLogoutLink = ShoppingCart_Controller::clear_cart_and_logout_link(); |
24
|
|
|
$loginMessage = |
25
|
|
|
'<span class="customerName">'.trim(Convert::raw2xml($member->FirstName).' '.Convert::raw2xml($member->Surname)).'</span>, ' |
26
|
|
|
.'<a href="'.$clearCartAndLogoutLink.'">'._t('Account.LOGOUT', 'Log out now?'). |
27
|
|
|
'</a>'; |
28
|
|
|
if ($loginMessage) { |
29
|
|
|
$loginField = new ReadonlyField( |
30
|
|
|
'LoggedInAsNote', |
31
|
|
|
_t('Account.LOGGEDIN', 'You are currently logged in as '), |
32
|
|
|
$loginMessage |
33
|
|
|
); |
34
|
|
|
$loginField->dontEscape = true; |
35
|
|
|
$fields->push($loginField); |
36
|
|
|
} |
37
|
|
|
$actions = new FieldList(); |
38
|
|
|
if ($order = ShoppingCart::current_order()) { |
39
|
|
|
if ($order->getTotalItems()) { |
40
|
|
|
$actions->push(new FormAction('proceed', _t('Account.SAVE_AND_PROCEED', 'Save changes and proceed to checkout'))); |
41
|
|
|
} else { |
42
|
|
|
$actions->push(new FormAction('submit', _t('Account.SAVE', 'Save Changes'))); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
if (!$member) { |
47
|
|
|
$member = new Member(); |
48
|
|
|
} |
49
|
|
|
$fields = new FieldList(); |
50
|
|
|
$urlParams = $controller->getURLParams(); |
51
|
|
|
$backURLLink = Director::baseURL(); |
52
|
|
|
if ($urlParams) { |
|
|
|
|
53
|
|
|
foreach ($urlParams as $urlParam) { |
54
|
|
|
if ($urlParam) { |
55
|
|
|
$backURLLink = Controller::join_links($backURLLink, $urlParam); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$backURLLink = urlencode($backURLLink); |
60
|
|
|
$fields->push(new LiteralField('MemberInfo', '<p class="message good">'._t('OrderForm.MEMBERINFO', 'If you already have an account then please').' <a href="Security/login?BackURL='.$backURLLink.'">'._t('OrderForm.LOGIN', 'log in').'</a>.</p>')); |
61
|
|
|
$memberFields = $member->getEcommerceFields($mustCreateAccount); |
62
|
|
|
if ($memberFields) { |
63
|
|
|
foreach ($memberFields as $memberField) { |
64
|
|
|
$fields->push($memberField); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
$passwordField = new PasswordField('PasswordCheck1', _t('Account.PASSWORD', 'Password')); |
68
|
|
|
$passwordFieldCheck = new PasswordField('PasswordCheck2', _t('Account.PASSWORDCHECK', 'Password (repeat)')); |
69
|
|
|
$fields->push($passwordField); |
70
|
|
|
$fields->push($passwordFieldCheck); |
71
|
|
|
$actions = new FieldList( |
72
|
|
|
new FormAction('creatememberandaddtoorder', _t('Account.SAVE', 'Create Account')) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$requiredFields = ShopAccountForm_Validator::create($member->getEcommerceRequiredFields()); |
77
|
|
|
parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
78
|
|
|
$this->setAttribute('autocomplete', 'off'); |
79
|
|
|
//extensions need to be set after __construct |
80
|
|
|
//extension point |
81
|
|
|
$this->extend('updateFields', $fields); |
82
|
|
|
$this->setFields($fields); |
83
|
|
|
$this->extend('updateActions', $actions); |
84
|
|
|
$this->setActions($actions); |
85
|
|
|
$this->extend('updateValidator', $requiredFields); |
86
|
|
|
$this->setValidator($requiredFields); |
87
|
|
|
|
88
|
|
|
if ($member) { |
89
|
|
|
$this->loadDataFrom($member); |
90
|
|
|
} |
91
|
|
|
$oldData = Session::get("FormInfo.{$this->FormName()}.data"); |
92
|
|
|
if ($oldData && (is_array($oldData) || is_object($oldData))) { |
93
|
|
|
$this->loadDataFrom($oldData); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
$this->extend('updateShopAccountForm', $this); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Save the changes to the form, and go back to the account page. |
100
|
|
|
* |
101
|
|
|
* @return bool + redirection |
102
|
|
|
*/ |
103
|
|
|
public function submit($data, $form, $request) |
104
|
|
|
{ |
105
|
|
|
return $this->processForm($data, $form, $request, ''); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Save the changes to the form, and redirect to the checkout page. |
110
|
|
|
* |
111
|
|
|
* @return bool + redirection |
112
|
|
|
*/ |
113
|
|
|
public function proceed($data, $form, $request) |
114
|
|
|
{ |
115
|
|
|
return $this->processForm($data, $form, $request, CheckoutPage::find_link()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* create a member and add it to the order |
120
|
|
|
* then redirect back... |
121
|
|
|
* |
122
|
|
|
* @param array $data |
123
|
|
|
* @param Form $form |
124
|
|
|
*/ |
125
|
|
|
public function creatememberandaddtoorder($data, $form) |
126
|
|
|
{ |
127
|
|
|
$member = new Member(); |
128
|
|
|
$order = ShoppingCart::current_order(); |
129
|
|
|
if ($order && $order->exists()) { |
130
|
|
|
$form->saveInto($member); |
131
|
|
|
$password = ShopAccountForm_PasswordValidator::clean_password($data); |
132
|
|
|
if ($password) { |
133
|
|
|
$member->changePassword($password); |
|
|
|
|
134
|
|
|
if ($member->validate()->valid()) { |
135
|
|
|
$member->write(); |
136
|
|
|
if ($member->exists()) { |
137
|
|
|
if (!$order->MemberID) { |
|
|
|
|
138
|
|
|
$order->MemberID = $member->ID; |
|
|
|
|
139
|
|
|
$order->write(); |
140
|
|
|
} |
141
|
|
|
$member->login(); |
142
|
|
|
$this->sessionMessage(_t('ShopAccountForm.SAVEDDETAILS', 'Your details has been saved.'), 'good'); |
143
|
|
|
} else { |
144
|
|
|
$this->sessionMessage(_t('ShopAccountForm.COULD_NOT_CREATE_RECORD', 'Could not save create a record for your details.'), 'bad'); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
$this->sessionMessage(_t('ShopAccountForm.COULD_NOT_VALIDATE_MEMBER', 'Could not save your details.'), 'bad'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
$this->sessionMessage(_t('ShopAccountForm.COULDNOTFINDORDER', 'Could not find order.'), 'bad'); |
152
|
|
|
} |
153
|
|
|
$this->controller->redirectBack(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
*@return bool + redirection |
158
|
|
|
**/ |
159
|
|
|
protected function processForm($data, $form, $request, $link = '') |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$member = Member::currentUser(); |
162
|
|
|
if (!$member) { |
163
|
|
|
$form->sessionMessage(_t('Account.DETAILSNOTSAVED', 'Your details could not be saved.'), 'bad'); |
164
|
|
|
$this->controller->redirectBack(); |
165
|
|
|
} |
166
|
|
|
$form->saveInto($member); |
167
|
|
|
$password = ShopAccountForm_PasswordValidator::clean_password($data); |
168
|
|
|
if ($password) { |
169
|
|
|
$member->changePassword($password); |
170
|
|
|
} elseif ($data['PasswordCheck1']) { |
171
|
|
|
$form->sessionMessage(_t('Account.NO_VALID_PASSWORD', 'You need to enter a valid password.'), 'bad'); |
172
|
|
|
$this->controller->redirectBack(); |
173
|
|
|
} |
174
|
|
|
if ($member->validate()->valid()) { |
175
|
|
|
$member->write(); |
176
|
|
|
if ($link) { |
177
|
|
|
return $this->controller->redirect($link); |
178
|
|
|
} else { |
179
|
|
|
$form->sessionMessage(_t('Account.DETAILSSAVED', 'Your details have been saved.'), 'good'); |
180
|
|
|
$this->controller->redirectBack(); |
181
|
|
|
} |
182
|
|
|
} else { |
183
|
|
|
$form->sessionMessage(_t('Account.NO_VALID_DATA', 'Your details can not be updated.'), 'bad'); |
184
|
|
|
$this->controller->redirectBack(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* saves the form into session. |
190
|
|
|
* |
191
|
|
|
* @param array $data - data from form. |
|
|
|
|
192
|
|
|
*/ |
193
|
|
|
public function saveDataToSession() |
194
|
|
|
{ |
195
|
|
|
$data = $this->getData(); |
196
|
|
|
unset($data['AccountInfo']); |
197
|
|
|
unset($data['LoginDetails']); |
198
|
|
|
unset($data['LoggedInAsNote']); |
199
|
|
|
unset($data['PasswordCheck1']); |
200
|
|
|
unset($data['PasswordCheck2']); |
201
|
|
|
Session::set("FormInfo.{$this->FormName()}.data", $data); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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.