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
|
|
|
return ArrayList::create($this->backend->getItems()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add an {@link DMSRequestItem} object into the cart. |
49
|
|
|
* |
50
|
|
|
* @param DMSRequestItem $item |
51
|
|
|
* |
52
|
|
|
* @return DMSDocumentCart |
53
|
|
|
*/ |
54
|
|
|
public function addItem(DMSRequestItem $item) |
55
|
|
|
{ |
56
|
|
|
$this->backend->addItem($item); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a {@link DMSRequestItem} object from the cart. |
63
|
|
|
* |
64
|
|
|
* @param int $itemID The ID of the item |
65
|
|
|
* |
66
|
|
|
* @return DMSRequestItem|boolean |
67
|
|
|
*/ |
68
|
|
|
public function getItem($itemID) |
69
|
|
|
{ |
70
|
|
|
return $this->backend->getItem($itemID); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
75
|
|
|
* |
76
|
|
|
* @param DMSRequestItem $item |
77
|
|
|
* |
78
|
|
|
* @return DMSDocumentCart |
79
|
|
|
*/ |
80
|
|
|
public function removeItem(DMSRequestItem $item) |
81
|
|
|
{ |
82
|
|
|
$this->backend->removeItem($item); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
89
|
|
|
* |
90
|
|
|
* @param int $itemID |
91
|
|
|
* |
92
|
|
|
* @return DMSDocumentCart |
93
|
|
|
*/ |
94
|
|
|
public function removeItemByID($itemID) |
95
|
|
|
{ |
96
|
|
|
$this->backend->removeItemByID($itemID); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.' |
103
|
|
|
* A positive $quantity increments the total, whereas a negative value decrements the total. A cart item |
104
|
|
|
* is removed completely if it's value reaches <= 0. |
105
|
|
|
* |
106
|
|
|
* @param int $itemID |
107
|
|
|
* @param int $quantity |
108
|
|
|
* |
109
|
|
|
* @return DMSDocumentCart |
110
|
|
|
*/ |
111
|
|
|
public function updateItemQuantity($itemID, $quantity) |
112
|
|
|
{ |
113
|
|
|
if ($item = $this->getItem($itemID)) { |
114
|
|
|
$currentQuantity = $item->getQuantity(); |
115
|
|
|
$newQuantity = $currentQuantity + $quantity; |
116
|
|
|
if ($newQuantity <= 0) { |
117
|
|
|
$this->removeItemByID($itemID); |
118
|
|
|
} else { |
119
|
|
|
$item->setQuantity($newQuantity); |
120
|
|
|
$this->addItem($item); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Completely empties a cart |
129
|
|
|
* |
130
|
|
|
* @return DMSDocumentCart |
131
|
|
|
*/ |
132
|
|
|
public function emptyCart() |
133
|
|
|
{ |
134
|
|
|
$this->backend->emptyCart(); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks if a cart is empty. |
141
|
|
|
* Returns true if cart is empty, false otherwise. |
142
|
|
|
* |
143
|
|
|
* @return boolean |
144
|
|
|
*/ |
145
|
|
|
public function isCartEmpty() |
146
|
|
|
{ |
147
|
|
|
$items = $this->getItems(); |
148
|
|
|
|
149
|
|
|
return !$items->exists(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the backURL to be a Session variable for the current Document Cart |
154
|
|
|
* |
155
|
|
|
* @param string $backURL |
156
|
|
|
* |
157
|
|
|
* @return DMSDocumentCart |
158
|
|
|
*/ |
159
|
|
|
public function setBackUrl($backURL) |
160
|
|
|
{ |
161
|
|
|
$this->backend->setBackUrl($backURL); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the backURL for the current Document Cart |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getBackUrl() |
172
|
|
|
{ |
173
|
|
|
return $this->backend->getBackUrl(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
178
|
|
|
* |
179
|
|
|
* @param array $receiverInfo |
180
|
|
|
* |
181
|
|
|
* @return DMSDocumentCart |
182
|
|
|
*/ |
183
|
|
|
public function setReceiverInfo($receiverInfo) |
184
|
|
|
{ |
185
|
|
|
$this->backend->setReceiverInfo($receiverInfo); |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
public function getReceiverInfo() |
196
|
|
|
{ |
197
|
|
|
return $this->backend->getReceiverInfo(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the recipients in a Viewable format |
202
|
|
|
* |
203
|
|
|
* @return ArrayData|bool |
204
|
|
|
*/ |
205
|
|
|
public function getReceiverInfoNice() |
206
|
|
|
{ |
207
|
|
|
return (is_array($this->getReceiverInfo())) ? ArrayData::create($this->getReceiverInfo()) : false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Gets the backend handler |
212
|
|
|
* |
213
|
|
|
* @return DMSSessionBackend |
214
|
|
|
*/ |
215
|
|
|
public function getBackend() |
216
|
|
|
{ |
217
|
|
|
return $this->backend; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Checks if an item exists within a cart. Returns true (if exists) or false. |
222
|
|
|
* |
223
|
|
|
* @param int $itemID |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
public function isInCart($itemID) |
228
|
|
|
{ |
229
|
|
|
return (bool) $this->getItem($itemID); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Persists a cart submission to the database |
234
|
|
|
* |
235
|
|
|
* @param Form $form |
236
|
|
|
* |
237
|
|
|
* @return int |
238
|
|
|
*/ |
239
|
|
|
public function saveSubmission(Form $form) |
240
|
|
|
{ |
241
|
|
|
$submission = DMSDocumentCartSubmission::create(); |
242
|
|
|
$form->saveInto($submission); |
243
|
|
|
$return = $submission->write(); |
244
|
|
|
$this->getItems()->each(function ($row) use ($submission) { |
245
|
|
|
$values = array( |
246
|
|
|
'Quantity' => $row->getQuantity(), |
247
|
|
|
'DocumentID' => $row->getDocument()->ID, |
248
|
|
|
); |
249
|
|
|
$submissionItem = DMSDocumentCartSubmissionItem::create($values); |
250
|
|
|
$submission->Items()->add($submissionItem); |
|
|
|
|
251
|
|
|
|
252
|
|
|
$row->getDocument()->incrementPrintRequest(); |
253
|
|
|
}); |
254
|
|
|
|
255
|
|
|
return $return; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns true if the cart is being updated. False otherwise |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
|
|
public function isViewOnly() |
263
|
|
|
{ |
264
|
|
|
return $this->viewOnly; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Sets the updating flag |
269
|
|
|
* |
270
|
|
|
* @param bool $viewOnly |
271
|
|
|
* @return DMSDocumentCart |
272
|
|
|
*/ |
273
|
|
|
public function setViewOnly($viewOnly) |
274
|
|
|
{ |
275
|
|
|
$this->viewOnly = (bool) $viewOnly; |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Displays a view-only table of the cart items. |
281
|
|
|
* |
282
|
|
|
* @return HTMLText |
283
|
|
|
*/ |
284
|
|
|
public function getSummary() |
285
|
|
|
{ |
286
|
|
|
return $this->renderWith('DMSDocumentCartSummary'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Utility method to link to the current controllers action |
291
|
|
|
* |
292
|
|
|
* @param string $action |
|
|
|
|
293
|
|
|
* @return string |
|
|
|
|
294
|
|
|
*/ |
295
|
|
|
public function getLink($action = null) |
296
|
|
|
{ |
297
|
|
|
return DMSDocumentCartController::create()->Link($action); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
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.