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
|
|
|
public function __construct($backend = null) |
16
|
|
|
{ |
17
|
|
|
parent::__construct(); |
18
|
|
|
$this->backend = ($backend) ?: DMSSessionBackend::create(); // Default to DMSSessionBackend if not provided |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns all the cart items as an array |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getItems() |
27
|
|
|
{ |
28
|
|
|
$result = $this->backend->getItems(); |
29
|
|
|
|
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns all the cart items as an ArrayList |
35
|
|
|
* |
36
|
|
|
* @return ArrayList |
37
|
|
|
*/ |
38
|
|
|
public function getItemsAsArrayList() |
39
|
|
|
{ |
40
|
|
|
return ArrayList::create($this->getItems()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add an {@link DMSRequestItem} object into the cart. |
45
|
|
|
* |
46
|
|
|
* @param DMSRequestItem $item |
47
|
|
|
* |
48
|
|
|
* @return DMSDocumentCart |
49
|
|
|
*/ |
50
|
|
|
public function addItem(DMSRequestItem $item) |
51
|
|
|
{ |
52
|
|
|
$this->backend->addItem($item); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get a {@link DMSRequestItem} object from the cart. |
59
|
|
|
* |
60
|
|
|
* @param int $itemID The ID of the item |
61
|
|
|
* |
62
|
|
|
* @return DMSRequestItem|boolean |
63
|
|
|
*/ |
64
|
|
|
public function getItem($itemID) |
65
|
|
|
{ |
66
|
|
|
return $this->backend->getItem($itemID); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
71
|
|
|
* |
72
|
|
|
* @param DMSRequestItem $item |
73
|
|
|
* |
74
|
|
|
* @return DMSDocumentCart |
75
|
|
|
*/ |
76
|
|
|
public function removeItem(DMSRequestItem $item) |
77
|
|
|
{ |
78
|
|
|
$this->backend->removeItem($item); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Removes a {@link DMSRequestItem} from the cart by it's id |
85
|
|
|
* |
86
|
|
|
* @param int $itemID |
87
|
|
|
* |
88
|
|
|
* @return DMSDocumentCart |
89
|
|
|
*/ |
90
|
|
|
public function removeItemByID($itemID) |
91
|
|
|
{ |
92
|
|
|
$this->backend->removeItemByID($itemID); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.' |
99
|
|
|
* A positive $quantity increments the total, whereas a negative value decrements the total. A cart item |
100
|
|
|
* is removed completely if it's value reaches <= 0. |
101
|
|
|
* |
102
|
|
|
* @param int $itemID |
103
|
|
|
* @param int $quantity |
104
|
|
|
* |
105
|
|
|
* @return DMSDocumentCart |
106
|
|
|
*/ |
107
|
|
|
public function updateItemQuantity($itemID, $quantity) |
108
|
|
|
{ |
109
|
|
|
if ($item = $this->getItem($itemID)) { |
110
|
|
|
$currentQuantity = $item->getQuantity(); |
111
|
|
|
$newQuantity = $currentQuantity + $quantity; |
112
|
|
|
if ($newQuantity <= 0) { |
113
|
|
|
$this->removeItemByID($itemID); |
114
|
|
|
} else { |
115
|
|
|
$item->setQuantity($newQuantity); |
116
|
|
|
$this->addItem($item); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Completely empties a cart |
125
|
|
|
* |
126
|
|
|
* @return DMSDocumentCart |
127
|
|
|
*/ |
128
|
|
|
public function emptyCart() |
129
|
|
|
{ |
130
|
|
|
$this->backend->emptyCart(); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Checks if a cart is empty |
137
|
|
|
* |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
public function isCartEmpty() |
141
|
|
|
{ |
142
|
|
|
$items = $this->getItems(); |
143
|
|
|
|
144
|
|
|
return empty($items); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the backURL to be a Session variable for the current Document Cart |
149
|
|
|
* |
150
|
|
|
* @param string $backURL |
151
|
|
|
* |
152
|
|
|
* @return DMSDocumentCart |
153
|
|
|
*/ |
154
|
|
|
public function setBackUrl($backURL) |
155
|
|
|
{ |
156
|
|
|
$this->backend->setBackUrl($backURL); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the backURL for the current Document Cart |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getBackUrl() |
167
|
|
|
{ |
168
|
|
|
return $this->backend->getBackUrl(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
173
|
|
|
* |
174
|
|
|
* @param array $receiverInfo |
175
|
|
|
* |
176
|
|
|
* @return DMSDocumentCart |
177
|
|
|
*/ |
178
|
|
|
public function setReceiverInfo($receiverInfo) |
179
|
|
|
{ |
180
|
|
|
$this->backend->setReceiverInfo($receiverInfo); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function getReceiverInfo() |
191
|
|
|
{ |
192
|
|
|
return $this->backend->getReceiverInfo(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the recipients in a Viewable format |
197
|
|
|
* |
198
|
|
|
* @return ArrayData|bool |
199
|
|
|
*/ |
200
|
|
|
public function getReceiverInfoNice() |
201
|
|
|
{ |
202
|
|
|
|
203
|
|
|
return (is_array($this->getReceiverInfo())) ? ArrayData::create($this->getReceiverInfo()) : false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Gets the backend handler |
208
|
|
|
* |
209
|
|
|
* @return DMSSessionBackend |
210
|
|
|
*/ |
211
|
|
|
public function getBackend() |
212
|
|
|
{ |
213
|
|
|
return $this->backend; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Checks if an item exists within a cart. Returns true (if exists) or false. |
218
|
|
|
* |
219
|
|
|
* @param int $itemID |
220
|
|
|
* |
221
|
|
|
* @return bool |
|
|
|
|
222
|
|
|
*/ |
223
|
|
|
public function isInCart($itemID) |
224
|
|
|
{ |
225
|
|
|
return $this->getItem($itemID); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Persists a cart submission to the database |
230
|
|
|
* |
231
|
|
|
* @param Form $form |
232
|
|
|
* |
233
|
|
|
* @return int |
234
|
|
|
*/ |
235
|
|
|
public function saveSubmission(Form $form) |
236
|
|
|
{ |
237
|
|
|
$submission = DMSDocumentCartSubmission::create(); |
238
|
|
|
$form->saveInto($submission); |
239
|
|
|
$return = $submission->write(); |
240
|
|
|
$this->getItemsAsArrayList()->each(function ($row) use ($submission) { |
241
|
|
|
$values = array( |
242
|
|
|
'Quantity' => $row->getQuantity(), |
243
|
|
|
'DocumentID' => $row->getDocument()->ID, |
244
|
|
|
); |
245
|
|
|
$submissionItem = DMSDocumentCartSubmissionItem::create($values); |
246
|
|
|
$submission->Items()->add($submissionItem); |
|
|
|
|
247
|
|
|
}); |
248
|
|
|
|
249
|
|
|
return $return; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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.