1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSDocumentCartCheckoutPage_Controller extends Page_Controller |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $allowed_actions = array( |
|
|
|
|
6
|
|
|
'DMSDocumentRequestForm', |
7
|
|
|
'complete' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An array containing the recipients basic information |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public static $receiverInfo = array( |
16
|
|
|
'ReceiverName' => '', |
17
|
|
|
'ReceiverPhone' => '', |
18
|
|
|
'ReceiverEmail' => '', |
19
|
|
|
'DeliveryAddressLine1' => '', |
20
|
|
|
'DeliveryAddressLine2' => '', |
21
|
|
|
'DeliveryAddressCountry' => '', |
22
|
|
|
'DeliveryAddressPostCode' => '', |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
public function init() |
26
|
|
|
{ |
27
|
|
|
parent::init(); |
28
|
|
|
|
29
|
|
|
Requirements::css(DMS_CART_DIR.'/css/dms-cart.css'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Gets and displays an editable list of items within the cart, as well as a contact form with entry |
34
|
|
|
* fields for the recipients information. |
35
|
|
|
* |
36
|
|
|
* To extend use the following from within an Extension subclass: |
37
|
|
|
* |
38
|
|
|
* <code> |
39
|
|
|
* public function updateDMSDocumentRequestForm($form) |
40
|
|
|
* { |
41
|
|
|
* // Do something here |
42
|
|
|
* } |
43
|
|
|
* </code> |
44
|
|
|
* |
45
|
|
|
* @return Form |
46
|
|
|
*/ |
47
|
|
|
public function DMSDocumentRequestForm() |
48
|
|
|
{ |
49
|
|
|
$fields = DMSDocumentCartSubmission::create()->scaffoldFormFields(); |
50
|
|
|
$fields->replaceField('DeliveryAddressLine2', TextField::create('DeliveryAddressLine2', '')); |
51
|
|
|
$fields->replaceField('DeliveryAddressCountry', CountryDropdownField::create( |
52
|
|
|
'DeliveryAddressCountry', |
53
|
|
|
_t('DMSDocumentCartCheckoutPage.RECEIVER_COUNTRY', 'Country') |
54
|
|
|
)->setValue('NZ')); |
55
|
|
|
|
56
|
|
|
$requiredFields = array( |
57
|
|
|
'ReceiverName', |
58
|
|
|
'ReceiverPhone', |
59
|
|
|
'ReceiverEmail', |
60
|
|
|
'DeliveryAddressLine1', |
61
|
|
|
'DeliveryAddressCountry', |
62
|
|
|
'DeliveryAddressPostCode', |
63
|
|
|
); |
64
|
|
|
foreach ($fields as $field) { |
65
|
|
|
if (in_array($field->name, $requiredFields)) { |
66
|
|
|
$field->addExtraClass('requiredField'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
$validator = RequiredFields::create($requiredFields); |
70
|
|
|
$actions = FieldList::create( |
71
|
|
|
FormAction::create( |
72
|
|
|
'doRequestSend', |
73
|
|
|
_t('DMSDocumentCartCheckoutPage.SEND_ACTION', 'Send your request') |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$form = Form::create( |
78
|
|
|
$this, |
79
|
|
|
'DMSDocumentRequestForm', |
80
|
|
|
$fields, |
81
|
|
|
$actions, |
82
|
|
|
$validator |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
if ($receiverInfo = $this->getCart()->getReceiverInfo()) { |
86
|
|
|
$form->loadDataFrom($receiverInfo); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$form->setTemplate('DMSDocumentRequestForm'); |
90
|
|
|
$this->extend('updateDMSDocumentRequestForm', $form); |
91
|
|
|
|
92
|
|
|
return $form; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sends an email to both the configured recipient as well as the requester. The |
97
|
|
|
* configured recipient is bcc'ed to the email in order to fulfill it. |
98
|
|
|
* |
99
|
|
|
* To extend use the following from within an Extension subclass: |
100
|
|
|
* |
101
|
|
|
* <code> |
102
|
|
|
* public function updateSend($email) |
103
|
|
|
* { |
104
|
|
|
* // Do something here |
105
|
|
|
* } |
106
|
|
|
* </code> |
107
|
|
|
* @return mixed |
108
|
|
|
* |
109
|
|
|
* @throws DMSDocumentCartException |
110
|
|
|
*/ |
111
|
|
|
public function send() |
112
|
|
|
{ |
113
|
|
|
$member = $this->CartEmailRecipient(); |
114
|
|
|
|
115
|
|
|
if (!$member->exists()) { |
116
|
|
|
throw new DMSDocumentCartException('No recipient has been configured. Please do so from the CMS'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$cart = $this->getCart(); |
120
|
|
|
$from = Config::inst()->get('Email', 'admin_email'); |
121
|
|
|
$emailAddress = ($info = $cart->getReceiverInfo()) ? $info['ReceiverEmail'] : $from; |
122
|
|
|
$email = Email::create( |
123
|
|
|
$from, |
124
|
|
|
$emailAddress, |
125
|
|
|
_t('DMSDocumentCartCheckoutPage.EMAIL_SUBJECT', 'Request for Printed Publications') |
126
|
|
|
); |
127
|
|
|
$email->setBcc($member->Email); |
128
|
|
|
$renderedCart = $cart->renderWith('DocumentCart_email'); |
129
|
|
|
$body = sprintf( |
130
|
|
|
'<p>%s</p>', |
131
|
|
|
_t( |
132
|
|
|
'DMSDocumentCartCheckoutPage.EMAIL_BODY', |
133
|
|
|
'A request for printed publications has been submitted with the following details:' |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
$body .= $renderedCart->getValue(); |
137
|
|
|
$email->setBody($body)->setReplyTo($emailAddress); |
138
|
|
|
$this->extend('updateSend', $email); |
139
|
|
|
|
140
|
|
|
return $email->send(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Handles form submission. |
145
|
|
|
* Totals requested are updated, delivery details added, email sent for fulfillment |
146
|
|
|
* and print request totals updated. |
147
|
|
|
* |
148
|
|
|
* @param array $data |
149
|
|
|
* @param Form $form |
150
|
|
|
* @param SS_HTTPRequest $request |
151
|
|
|
* |
152
|
|
|
* @return SS_HTTPResponse |
153
|
|
|
*/ |
154
|
|
|
public function doRequestSend($data, Form $form, SS_HTTPRequest $request) |
155
|
|
|
{ |
156
|
|
|
$this->updateCartItems($data); |
157
|
|
|
$this->updateCartReceiverInfo($data); |
158
|
|
|
$this->send(); |
159
|
|
|
$this->getCart()->saveSubmission($form); |
160
|
|
|
$this->getCart()->emptyCart(); |
161
|
|
|
|
162
|
|
|
return $this->redirect($this->Link('complete')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Displays the preconfigured thank you message to the user upon completion |
167
|
|
|
* |
168
|
|
|
* @return ViewableData_Customised |
169
|
|
|
*/ |
170
|
|
|
public function complete() |
171
|
|
|
{ |
172
|
|
|
return $this->customise( |
173
|
|
|
ArrayData::create( |
174
|
|
|
array( |
175
|
|
|
'Content' => $this->ThanksMessage, |
176
|
|
|
) |
177
|
|
|
) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retrieves a {@link DMSDocumentCart} instance |
183
|
|
|
* |
184
|
|
|
* @return DMSDocumentCart |
185
|
|
|
*/ |
186
|
|
|
public function getCart() |
187
|
|
|
{ |
188
|
|
|
return singleton('DMSDocumentCart'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Updates the document quantities just before the request is sent. |
193
|
|
|
* |
194
|
|
|
* @param array $data |
195
|
|
|
*/ |
196
|
|
|
public function updateCartItems($data) |
197
|
|
|
{ |
198
|
|
|
if (!empty($data['ItemQuantity'])) { |
199
|
|
|
foreach ($data['ItemQuantity'] as $itemID => $quantity) { |
200
|
|
|
// Only update if quantity has changed |
201
|
|
|
$item = $this->getCart()->getItem($itemID); |
202
|
|
|
if ($item->getQuantity() == $quantity) { |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
$this->getCart()->updateItemQuantity($itemID, $quantity - 1); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Updates the cart receiver info just before the request is sent. |
212
|
|
|
* |
213
|
|
|
* @param array $data |
214
|
|
|
*/ |
215
|
|
|
public function updateCartReceiverInfo($data) |
216
|
|
|
{ |
217
|
|
|
$info = array_merge(self::$receiverInfo, $data); |
218
|
|
|
$this->getCart()->setReceiverInfo($info); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
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.