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