1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DMSDocumentCart represents the shopping cart. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
class DMSDocumentCart extends ViewableData |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* A handle to the classes' {@link DMSCartBackendInterface} |
10
|
|
|
* |
11
|
|
|
* @var DMSCartBackendInterface |
12
|
|
|
*/ |
13
|
|
|
protected $backend; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Variable to control whether a cart is being updated or not |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $viewOnly = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Instantiate a cart backend either by that provided, or a session default |
24
|
|
|
* |
25
|
|
|
* @param DMSCartBackendInterface $backend |
|
|
|
|
26
|
|
|
* @throws DMSDocumentCartException If a backend was provided but doesn't implement the backend interface |
27
|
|
|
*/ |
28
|
|
|
public function __construct($backend = null) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
if ($backend && !($backend instanceof DMSCartBackendInterface)) { |
32
|
|
|
throw new DMSDocumentCartException('Backend must implement DMSCartBackendInterface!'); |
33
|
|
|
} |
34
|
|
|
$this->backend = ($backend) ?: DMSSessionBackend::singleton(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns all the cart items as an array |
39
|
|
|
* |
40
|
|
|
* @return ArrayList |
41
|
|
|
*/ |
42
|
|
|
public function getItems() |
43
|
|
|
{ |
44
|
|
|
$validItems = ArrayList::create(); |
45
|
|
|
foreach ($this->backend->getItems() as $item) { |
46
|
|
|
/** @var DMSRequestItem $item */ |
47
|
|
|
if (!$item->getDocument()) { |
48
|
|
|
$this->backend->removeItem($item); |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$validItems->push($item); |
52
|
|
|
} |
53
|
|
|
return $validItems; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets a partial caching key that can be used to prevent the getItems method from hitting the database every |
58
|
|
|
* time to check whether a document exists. Includes a hash of the valid items in the cart (including their |
59
|
|
|
* quantity). |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getCartSummaryCacheKey() |
64
|
|
|
{ |
65
|
|
|
return 'dms-cart-items-' . md5(serialize($this->getItems())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add an {@link DMSRequestItem} object into the cart. |
70
|
|
|
* |
71
|
|
|
* @param DMSRequestItem $item |
72
|
|
|
* |
73
|
|
|
* @return DMSDocumentCart |
74
|
|
|
*/ |
75
|
|
|
public function addItem(DMSRequestItem $item) |
76
|
|
|
{ |
77
|
|
|
$this->backend->addItem($item); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get a {@link DMSRequestItem} object from the cart. |
84
|
|
|
* |
85
|
|
|
* @param int $itemID The ID of the item |
86
|
|
|
* |
87
|
|
|
* @return DMSRequestItem|boolean |
88
|
|
|
*/ |
89
|
|
|
public function getItem($itemID) |
90
|
|
|
{ |
91
|
|
|
return $this->backend->getItem($itemID); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
96
|
|
|
* |
97
|
|
|
* @param DMSRequestItem $item |
98
|
|
|
* |
99
|
|
|
* @return DMSDocumentCart |
100
|
|
|
*/ |
101
|
|
|
public function removeItem(DMSRequestItem $item) |
102
|
|
|
{ |
103
|
|
|
$this->backend->removeItem($item); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
110
|
|
|
* |
111
|
|
|
* @param int $itemID |
112
|
|
|
* |
113
|
|
|
* @return DMSDocumentCart |
114
|
|
|
*/ |
115
|
|
|
public function removeItemByID($itemID) |
116
|
|
|
{ |
117
|
|
|
$this->backend->removeItemByID($itemID); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.' |
124
|
|
|
* A positive $quantity increments the total, whereas a negative value decrements the total. A cart item |
125
|
|
|
* is removed completely if it's value reaches <= 0. |
126
|
|
|
* |
127
|
|
|
* @param int $itemID |
128
|
|
|
* @param int $quantity |
129
|
|
|
* |
130
|
|
|
* @return DMSDocumentCart |
131
|
|
|
*/ |
132
|
|
|
public function updateItemQuantity($itemID, $quantity) |
133
|
|
|
{ |
134
|
|
|
if ($item = $this->getItem($itemID)) { |
135
|
|
|
$currentQuantity = $item->getQuantity(); |
136
|
|
|
$newQuantity = $currentQuantity + $quantity; |
137
|
|
|
if ($newQuantity <= 0) { |
138
|
|
|
$this->removeItemByID($itemID); |
139
|
|
|
} else { |
140
|
|
|
$item->setQuantity($newQuantity); |
141
|
|
|
$this->addItem($item); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Completely empties a cart |
150
|
|
|
* |
151
|
|
|
* @return DMSDocumentCart |
152
|
|
|
*/ |
153
|
|
|
public function emptyCart() |
154
|
|
|
{ |
155
|
|
|
$this->backend->emptyCart(); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Checks if a cart is empty. |
162
|
|
|
* Returns true if cart is empty, false otherwise. |
163
|
|
|
* |
164
|
|
|
* @return boolean |
165
|
|
|
*/ |
166
|
|
|
public function isCartEmpty() |
167
|
|
|
{ |
168
|
|
|
$items = $this->getItems(); |
169
|
|
|
|
170
|
|
|
return !$items->exists(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the backURL to be a Session variable for the current Document Cart |
175
|
|
|
* |
176
|
|
|
* @param string $backURL |
177
|
|
|
* |
178
|
|
|
* @return DMSDocumentCart |
179
|
|
|
*/ |
180
|
|
|
public function setBackUrl($backURL) |
181
|
|
|
{ |
182
|
|
|
$this->backend->setBackUrl($backURL); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the backURL for the current Document Cart |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getBackUrl() |
193
|
|
|
{ |
194
|
|
|
return $this->backend->getBackUrl(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
199
|
|
|
* |
200
|
|
|
* @param array $receiverInfo |
201
|
|
|
* |
202
|
|
|
* @return DMSDocumentCart |
203
|
|
|
*/ |
204
|
|
|
public function setReceiverInfo($receiverInfo) |
205
|
|
|
{ |
206
|
|
|
$this->backend->setReceiverInfo($receiverInfo); |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function getReceiverInfo() |
217
|
|
|
{ |
218
|
|
|
return $this->backend->getReceiverInfo(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the recipients in a Viewable format |
223
|
|
|
* |
224
|
|
|
* @return ArrayData|bool |
225
|
|
|
*/ |
226
|
|
|
public function getReceiverInfoNice() |
227
|
|
|
{ |
228
|
|
|
return (is_array($this->getReceiverInfo())) ? ArrayData::create($this->getReceiverInfo()) : false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Gets the backend handler |
233
|
|
|
* |
234
|
|
|
* @return DMSSessionBackend |
235
|
|
|
*/ |
236
|
|
|
public function getBackend() |
237
|
|
|
{ |
238
|
|
|
return $this->backend; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Checks if an item exists within a cart. Returns true (if exists) or false. |
243
|
|
|
* |
244
|
|
|
* @param int $itemID |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
public function isInCart($itemID) |
249
|
|
|
{ |
250
|
|
|
return (bool) $this->getItem($itemID); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Persists a cart submission to the database |
255
|
|
|
* |
256
|
|
|
* @param Form $form |
257
|
|
|
* |
258
|
|
|
* @return int |
259
|
|
|
*/ |
260
|
|
|
public function saveSubmission(Form $form) |
261
|
|
|
{ |
262
|
|
|
$submission = DMSDocumentCartSubmission::create(); |
263
|
|
|
$form->saveInto($submission); |
264
|
|
|
$return = $submission->write(); |
265
|
|
|
$this->getItems()->each(function ($row) use ($submission) { |
266
|
|
|
$values = array( |
267
|
|
|
'Quantity' => $row->getQuantity(), |
268
|
|
|
'DocumentID' => $row->getDocument()->ID, |
269
|
|
|
); |
270
|
|
|
$submissionItem = DMSDocumentCartSubmissionItem::create($values); |
271
|
|
|
$submission->Items()->add($submissionItem); |
|
|
|
|
272
|
|
|
|
273
|
|
|
$row->getDocument()->incrementPrintRequest(); |
274
|
|
|
}); |
275
|
|
|
|
276
|
|
|
return $return; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Returns true if the cart is being updated. False otherwise |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
|
|
public function isViewOnly() |
284
|
|
|
{ |
285
|
|
|
return $this->viewOnly; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Sets the updating flag |
290
|
|
|
* |
291
|
|
|
* @param bool $viewOnly |
292
|
|
|
* @return DMSDocumentCart |
293
|
|
|
*/ |
294
|
|
|
public function setViewOnly($viewOnly) |
295
|
|
|
{ |
296
|
|
|
$this->viewOnly = (bool) $viewOnly; |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Displays a view-only table of the cart items. |
302
|
|
|
* |
303
|
|
|
* @return HTMLText |
304
|
|
|
*/ |
305
|
|
|
public function getSummary() |
306
|
|
|
{ |
307
|
|
|
return $this->renderWith('DMSDocumentCartSummary'); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Utility method to link to the current controllers action |
312
|
|
|
* |
313
|
|
|
* @param string $action |
|
|
|
|
314
|
|
|
* @return string |
|
|
|
|
315
|
|
|
*/ |
316
|
|
|
public function getLink($action = null) |
317
|
|
|
{ |
318
|
|
|
return DMSDocumentCartController::create()->Link($action); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
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.