1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSDocumentCartController extends ContentController |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $url_handlers = array( |
|
|
|
|
6
|
|
|
'$Action//$ID' => 'handleAction', |
7
|
|
|
); |
8
|
|
|
|
9
|
|
|
private static $allowed_actions = array( |
|
|
|
|
10
|
|
|
'DMSCartEditForm', |
11
|
|
|
'add', |
12
|
|
|
'deduct', |
13
|
|
|
'remove', |
14
|
|
|
'view' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
public function init() |
18
|
|
|
{ |
19
|
|
|
parent::init(); |
20
|
|
|
Requirements::css(DMS_CART_DIR . '/css/dms-cart.css'); |
21
|
|
|
} |
22
|
|
|
/** |
23
|
|
|
* See {@link DMSDocumentCart::getItems()} |
24
|
|
|
* |
25
|
|
|
* @return ArrayList |
26
|
|
|
*/ |
27
|
|
|
public function items() |
28
|
|
|
{ |
29
|
|
|
return $this->getCart()->getItems(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Prepares receiver info for the template. |
34
|
|
|
* Additionally it uses Zend_Locale to retrieve the localised spelling of the Country |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function getReceiverInfo() |
39
|
|
|
{ |
40
|
|
|
$receiverInfo = $this->getCart()->getReceiverInfo(); |
41
|
|
|
|
42
|
|
|
if (isset($receiverInfo['DeliveryAddressCountry']) && $receiverInfo['DeliveryAddressCountry']) { |
43
|
|
|
$source = Zend_Locale::getTranslationList('territory', $receiverInfo['DeliveryAddressCountry'], 2); |
44
|
|
|
$receiverInfo['DeliveryAddressCountryLiteral'] = $source[$receiverInfo['DeliveryAddressCountry']]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!empty($receiverInfo)) { |
48
|
|
|
$result = $receiverInfo; |
49
|
|
|
} else { |
50
|
|
|
$result = array('Result' => 'no data'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* See DMSDocumentCart::isCartEmpty() |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function getIsCartEmpty() |
62
|
|
|
{ |
63
|
|
|
return $this->getCart()->isCartEmpty(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add quantity to an item that exists in {@link DMSDocumentCart}. |
68
|
|
|
* If the item does nt exist, try to add a new item of the particular |
69
|
|
|
* class given the URL parameters available. |
70
|
|
|
* |
71
|
|
|
* @param SS_HTTPRequest $request |
72
|
|
|
* |
73
|
|
|
* @return SS_HTTPResponse|string |
74
|
|
|
*/ |
75
|
|
|
public function add(SS_HTTPRequest $request) |
76
|
|
|
{ |
77
|
|
|
$quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1; |
78
|
|
|
$documentId = (int)$request->param('ID'); |
79
|
|
|
$result = true; |
80
|
|
|
$message = ''; |
81
|
|
|
|
82
|
|
|
if ($doc = DMSDocument::get()->byID($documentId)) { |
83
|
|
|
/** @var ValidationResult $validate */ |
84
|
|
|
$validate = $this->validateAddRequest($quantity, $doc); |
|
|
|
|
85
|
|
|
if ($validate->valid()) { |
86
|
|
|
if ($this->getCart()->getItem($documentId)) { |
87
|
|
|
$this->getCart()->updateItemQuantity($documentId, $quantity); |
88
|
|
|
} else { |
89
|
|
|
$requestItem = DMSRequestItem::create()->setDocument($doc)->setQuantity($quantity); |
|
|
|
|
90
|
|
|
$this->getCart()->addItem($requestItem); |
91
|
|
|
} |
92
|
|
|
$backURL = $request->getVar('BackURL'); |
93
|
|
|
// make sure that backURL is a relative path (starts with /) |
94
|
|
|
if (isset($backURL) && preg_match('/^\//', $backURL)) { |
95
|
|
|
$this->getCart()->setBackUrl($backURL); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
$message = $validate->starredList(); |
99
|
|
|
$result = false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
if ($request->isAjax()) { |
|
|
|
|
104
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
105
|
|
|
return Convert::raw2json(array('result' => $result, 'message' => $message)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!$result) { |
109
|
|
|
Session::set('dms-cart-validation-message', $message); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($backURL = $request->getVar('BackURL')) { |
113
|
|
|
return $this->redirect($backURL); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->redirectBack(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Deduct quantity from an item that exists in {@link DMSDocumentCart} |
121
|
|
|
* |
122
|
|
|
* @param SS_HTTPRequest $request |
123
|
|
|
* |
124
|
|
|
* @return SS_HTTPResponse|string |
125
|
|
|
*/ |
126
|
|
|
public function deduct(SS_HTTPRequest $request) |
127
|
|
|
{ |
128
|
|
|
$quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1; |
129
|
|
|
$this->getCart()->updateItemQuantity((int)$request->param('ID'), $quantity); |
130
|
|
|
$this->redirectBack(); |
131
|
|
|
|
132
|
|
View Code Duplication |
if ($request->isAjax()) { |
|
|
|
|
133
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
134
|
|
|
|
135
|
|
|
return Convert::raw2json(array('result' => true)); |
136
|
|
|
} |
137
|
|
|
if ($backURL = $request->getVar('BackURL')) { |
138
|
|
|
return $this->redirect($backURL); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->redirectBack(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Completely remove an item that exists in {@link DMSDocumentCart} |
146
|
|
|
* |
147
|
|
|
* @param SS_HTTPRequest $request |
148
|
|
|
* |
149
|
|
|
* @return string |
|
|
|
|
150
|
|
|
*/ |
151
|
|
|
public function remove(SS_HTTPRequest $request) |
152
|
|
|
{ |
153
|
|
|
$this->getCart()->removeItemByID(intval($request->param('ID'))); |
154
|
|
|
|
155
|
|
|
if ($request->isAjax()) { |
156
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
157
|
|
|
|
158
|
|
|
return Convert::raw2json(array('result' => !$this->getIsCartEmpty())); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->redirectBack(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Retrieves a {@link DMSDocumentCart} instance |
166
|
|
|
* |
167
|
|
|
* @return DMSDocumentCart |
168
|
|
|
*/ |
169
|
|
|
public function getCart() |
170
|
|
|
{ |
171
|
|
|
return singleton('DMSDocumentCart'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Validates a request to add a document to the cart |
176
|
|
|
* |
177
|
|
|
* @param int $quantity |
178
|
|
|
* @param DMSDocument $document |
179
|
|
|
* @return ValidationResult |
180
|
|
|
*/ |
181
|
|
|
protected function validateAddRequest($quantity, DMSDocument $document) |
182
|
|
|
{ |
183
|
|
|
$result = ValidationResult::create(); |
184
|
|
|
|
185
|
|
|
if (!$document->isAllowedInCart()) { |
186
|
|
|
$result->error(_t(__CLASS__ . '.ERROR_NOT_ALLOWED', 'You are not allowed to add this document')); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($document->getHasQuantityLimit() && $quantity > $document->getMaximumQuantity()) { |
190
|
|
|
$result->error(_t( |
191
|
|
|
__CLASS__ . '.ERROR_QUANTITY_EXCEEDED', |
192
|
|
|
'You can\'t add {quantity} of \'{title}\'', |
193
|
|
|
array('quantity' => $quantity, 'title' => $document->getTitle()) |
|
|
|
|
194
|
|
|
)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->extend('updateValidateAddRequest', $result, $quantity, $document); |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Updates the document quantities just before the request is sent. |
204
|
|
|
* |
205
|
|
|
* @param array $data |
206
|
|
|
* @param Form $form |
207
|
|
|
* @param SS_HTTPRequest $request |
208
|
|
|
* |
209
|
|
|
* @return SS_HTTPResponse |
|
|
|
|
210
|
|
|
*/ |
211
|
|
|
public function updateCartItems($data, Form $form, SS_HTTPRequest $request) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
if (!empty($data['ItemQuantity'])) { |
214
|
|
|
foreach ($data['ItemQuantity'] as $itemID => $quantity) { |
215
|
|
|
if (!is_numeric($quantity) || $quantity < 0) { |
216
|
|
|
continue; |
217
|
|
|
} |
218
|
|
|
// Only update if quantity has changed |
219
|
|
|
$item = $this->getCart()->getItem($itemID); |
220
|
|
|
if ($item->getQuantity() == $quantity) { |
221
|
|
|
continue; |
222
|
|
|
} |
223
|
|
|
// No validate item |
224
|
|
|
$validate = $this->validateAddRequest($quantity, $item->getDocument()); |
225
|
|
|
if ($validate->valid()) { |
226
|
|
|
// Removes, then adds a item new item. |
227
|
|
|
$this->getCart()->removeItem($item); |
|
|
|
|
228
|
|
|
$this->getCart()->addItem($item->setQuantity($quantity)); |
229
|
|
|
} else { |
230
|
|
|
$form->sessionMessage($validate->starredList(), 'bad'); |
231
|
|
|
return $this->redirectBack(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->redirect($this->getCart()->getBackUrl()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Presents an interface for user to update the cart quantities |
241
|
|
|
* |
242
|
|
|
* @param SS_HTTPRequest $request |
243
|
|
|
* @return ViewableData_Customised |
244
|
|
|
*/ |
245
|
|
|
public function view(SS_HTTPRequest $request) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$this->getCart()->setViewOnly(true); |
248
|
|
|
$form = $this->DMSCartEditForm(); |
249
|
|
|
return $this |
250
|
|
|
->customise( |
251
|
|
|
array( |
252
|
|
|
'Form' => $form, |
253
|
|
|
'Title' => _t(__CLASS__ . '.UPDATE_TITLE', 'Updating cart items') |
254
|
|
|
) |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Gets and displays an editable list of items within the cart. |
260
|
|
|
* |
261
|
|
|
* To extend use the following from within an Extension subclass: |
262
|
|
|
* |
263
|
|
|
* <code> |
264
|
|
|
* public function updateDMSCartEditForm($form) |
265
|
|
|
* { |
266
|
|
|
* // Do something here |
267
|
|
|
* } |
268
|
|
|
* </code> |
269
|
|
|
* |
270
|
|
|
* @return Form |
271
|
|
|
*/ |
272
|
|
|
public function DMSCartEditForm() |
273
|
|
|
{ |
274
|
|
|
$actions = FieldList::create( |
275
|
|
|
FormAction::create( |
276
|
|
|
'updateCartItems', |
277
|
|
|
_t(__CLASS__ . '.SAVE_BUTTON', 'Save changes') |
278
|
|
|
) |
279
|
|
|
); |
280
|
|
|
$form = Form::create( |
281
|
|
|
$this, |
282
|
|
|
'DMSCartEditForm', |
283
|
|
|
FieldList::create(), |
284
|
|
|
$actions |
285
|
|
|
); |
286
|
|
|
$form->setTemplate('DMSDocumentRequestForm'); |
287
|
|
|
$this->extend('updateDMSCartEditForm', $form); |
288
|
|
|
return $form; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Ensure that links for this controller use the customised route |
293
|
|
|
* |
294
|
|
|
* @param string $action |
|
|
|
|
295
|
|
|
* @return string |
|
|
|
|
296
|
|
|
*/ |
297
|
|
|
public function Link($action = null) |
298
|
|
|
{ |
299
|
|
|
if ($url = array_search(__CLASS__, (array)Config::inst()->get('Director', 'rules'))) { |
300
|
|
|
return $this->join_links($url, $action); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
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.