|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class DMSDocumentCartExtension |
|
5
|
|
|
* |
|
6
|
|
|
* @property Boolean AllowedInCart |
|
7
|
|
|
* @property Int PrintRequestCount |
|
8
|
|
|
* |
|
9
|
|
|
* @property DMSDocument owner |
|
10
|
|
|
*/ |
|
11
|
|
|
class DMSDocumentCartExtension extends DataExtension |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var DMSDocumentCartController |
|
15
|
|
|
*/ |
|
16
|
|
|
private $cartController; |
|
17
|
|
|
|
|
18
|
|
|
private static $db = array( |
|
|
|
|
|
|
19
|
|
|
'AllowedInCart' => 'Boolean', |
|
20
|
|
|
'MaximumCartQuantity' => 'Int', |
|
21
|
|
|
// Running total of print requests on this document |
|
22
|
|
|
'PrintRequestCount' => 'Int', |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
26
|
|
|
'PrintRequestCount' => 'Print Requests', |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Returns if a Document is permitted to reflect in a cart |
|
31
|
|
|
* |
|
32
|
|
|
* @return boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
public function isAllowedInCart() |
|
35
|
|
|
{ |
|
36
|
|
|
return ($this->owner->AllowedInCart && $this->owner->canView()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function updateCMSFields(FieldList $fields) |
|
40
|
|
|
{ |
|
41
|
|
|
Requirements::javascript(DMS_CART_DIR . '/javascript/dmscart.js'); |
|
42
|
|
|
|
|
43
|
|
|
$newFields = array( |
|
44
|
|
|
CheckboxField::create( |
|
45
|
|
|
'AllowedInCart', |
|
46
|
|
|
_t('DMSDocumentCart.ALLOWED_IN_CART', 'Allowed in document cart') |
|
47
|
|
|
)->addExtraClass('dms-allowed-in-cart'), |
|
48
|
|
|
TextField::create( |
|
49
|
|
|
'MaximumCartQuantity', |
|
50
|
|
|
_t('DMSDocumentCart.MAXIMUM_CART_QUANTITY', 'Maximum cart quantity') |
|
51
|
|
|
)->setRightTitle( |
|
52
|
|
|
_t( |
|
53
|
|
|
'DMSDocumentCart.MAXIMUM_CART_QUANTITY_HELP', |
|
54
|
|
|
'If set, this will enforce a maximum number of this item that can be ordered per cart' |
|
55
|
|
|
) |
|
56
|
|
|
)->addExtraClass('dms-maximum-cart-quantity hide') |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($newFields as $field) { |
|
60
|
|
|
$fields->insertBefore($field, 'Description'); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Increments the number of times a document was printed |
|
66
|
|
|
* |
|
67
|
|
|
* @return DMSDocument |
|
68
|
|
|
*/ |
|
69
|
|
|
public function incrementPrintRequest() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->owner->PrintRequestCount++; |
|
72
|
|
|
$this->owner->write(); |
|
73
|
|
|
|
|
74
|
|
|
return $this->owner; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if a given document already exists within the Cart. True if it does, false otherwise |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function isInCart() |
|
83
|
|
|
{ |
|
84
|
|
|
return (bool) $this->getCart()->isInCart($this->owner->ID); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns whether the current document has a limit on how many items can be added to a single cart |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getHasQuantityLimit() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->owner->getMaximumQuantity() > 0; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the maximum quantity of this document that can be ordered in a single cart |
|
99
|
|
|
* |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getMaximumQuantity() |
|
103
|
|
|
{ |
|
104
|
|
|
return (int) $this->owner->MaximumCartQuantity; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Builds and returns a valid DMSDocumentController URL from the given $action link |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $action Can be either 'add', 'remove' or 'checkout |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
* |
|
114
|
|
|
* @throws InvalidArgumentException if the provided $action is not allowed. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getActionLink($action = 'add') |
|
117
|
|
|
{ |
|
118
|
|
|
$action = strtolower($action); |
|
119
|
|
|
$allowedActions = array_merge($this->getCartController()->allowedActions(), array('checkout')); |
|
120
|
|
|
if (!in_array($action, $allowedActions)) { |
|
121
|
|
|
throw new InvalidArgumentException("{$action} is not accepted for this method."); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($action === 'checkout') { |
|
125
|
|
|
$result = DMSCheckoutController::singleton()->Link(); |
|
126
|
|
|
} else { |
|
127
|
|
|
$result = Controller::join_links('documentcart', $action, $this->owner->ID); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Retrieves a DMSDocumentCartController handle |
|
135
|
|
|
* |
|
136
|
|
|
* @return DMSDocumentCartController |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCartController() |
|
139
|
|
|
{ |
|
140
|
|
|
if (!$this->cartController) { |
|
141
|
|
|
$this->cartController = DMSDocumentCartController::create(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->cartController; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Retrieves a DMSDocumentCart handle |
|
149
|
|
|
* |
|
150
|
|
|
* @return DMSDocumentCart |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getCart() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->getCartController()->getCart(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns any validation messages that may have been in the session and clears them |
|
159
|
|
|
* |
|
160
|
|
|
* @return false |
|
|
|
|
|
|
161
|
|
|
*/ |
|
162
|
|
|
public function getValidationResult() |
|
163
|
|
|
{ |
|
164
|
|
|
if ($result = Session::get('dms-cart-validation-message')) { |
|
165
|
|
|
Session::clear('dms-cart-validation-message'); |
|
166
|
|
|
return $result; |
|
167
|
|
|
} |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Add a "print request count" field to the summary fields for editing a DMS document |
|
173
|
|
|
* |
|
174
|
|
|
* @see DMSDocument::getFieldsForFile |
|
175
|
|
|
* @param FieldGroup $fieldGroup |
|
176
|
|
|
*/ |
|
177
|
|
|
public function updateFieldsForFile(FieldGroup $fieldGroup) |
|
178
|
|
|
{ |
|
179
|
|
|
$fields = $fieldGroup->FieldList(); |
|
180
|
|
|
|
|
181
|
|
|
/** @var FieldList $summaryFields */ |
|
182
|
|
|
$summaryFields = $fields->fieldByName('FilePreview.FilePreviewData.FilePreviewDataFields'); |
|
183
|
|
|
if (!($summaryFields instanceof CompositeField)) { |
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$summaryFields->FieldList()->push( |
|
188
|
|
|
ReadonlyField::create( |
|
189
|
|
|
'PrintRequestCount', |
|
190
|
|
|
_t( |
|
191
|
|
|
__CLASS__ . '.PRINT_REQUEST_COUNT', |
|
192
|
|
|
'Print request count:' |
|
193
|
|
|
), |
|
194
|
|
|
$this->owner->PrintRequestCount |
|
195
|
|
|
) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
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.