1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author nicolaas [at] sunnysideup.co.nz |
4
|
|
|
* @requires ecommerce |
5
|
|
|
* @requires ecommerce_product_variation |
6
|
|
|
*/ |
7
|
|
|
class GiftVoucherProductPage extends Product |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
private static $db = array( |
|
|
|
|
10
|
|
|
'DescriptionFieldLabel' => 'Varchar(255)', |
11
|
|
|
'AmountFieldLabel' => 'Varchar(255)', |
12
|
|
|
'ActionFieldLabel' => 'Varchar(255)', |
13
|
|
|
'MinimumAmount' => 'Decimal(9,2)', |
14
|
|
|
'MaximumAmount' => 'Decimal(9,2)', |
15
|
|
|
'RecommendedAmounts' => 'Varchar(255)', |
16
|
|
|
'CanSetDescription' => 'Boolean', |
17
|
|
|
'DefaultDescription' => 'Varchar(255)', |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
private static $defaults = array( |
|
|
|
|
21
|
|
|
'DescriptionFieldLabel' => 'Enter Description', |
22
|
|
|
'AmountFieldLabel' => 'Enter Amount', |
23
|
|
|
'ActionFieldLabel' => 'Add to cart', |
24
|
|
|
'MinimumAmount' => 1, |
25
|
|
|
'MaximumAmount' => 100, |
26
|
|
|
'AllowPurchase' => false, |
27
|
|
|
'Price' => 0, |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
private static $field_labels = array( |
|
|
|
|
31
|
|
|
'DescriptionFieldLabel' => 'Description Label', |
32
|
|
|
'AmountFieldLabel' => 'Amount Label', |
33
|
|
|
'ActionFieldLabel' => 'Button Label', |
34
|
|
|
'MinimumAmount' => 'Minimum Amount', |
35
|
|
|
'MaximumAmount' => 'Maximum Amount', |
36
|
|
|
'RecommendedAmounts' => 'Hinted amounts', |
37
|
|
|
'CanSetDescription' => 'Customer Adds Description', |
38
|
|
|
'DefaultDescription' => 'Default Description', |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
private static $field_labels_right = array( |
|
|
|
|
42
|
|
|
'DescriptionFieldLabel' => 'e.g. please enter title for payment', |
43
|
|
|
'AmountFieldLabel' => 'e.g. please enter amount for payment', |
44
|
|
|
'ActionFieldLabel' => 'e.g. pay now', |
45
|
|
|
'MinimumAmount' => 'e.g. 10.00', |
46
|
|
|
'MaximumAmount' => 'e.g. 100.00', |
47
|
|
|
'RecommendedAmounts' => 'create a list of recommended payment amounts, separated by a space, e.g. 10.00 12.00 19.00 23.00', |
48
|
|
|
'CanSetDescription' => 'can the customer add their own description to the payment?', |
49
|
|
|
'DefaultDescription' => 'e.g. generic product, this field is optional', |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
private static $singular_name = 'Any Price Product'; |
|
|
|
|
53
|
|
|
public function i18n_singular_name() |
54
|
|
|
{ |
55
|
|
|
return _t('GiftVoucherProductPage.GIFT_VOUCHER_PRODUCT_PAGE', 'Gift Voucher Page'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static $plural_name = 'Any Price Products'; |
|
|
|
|
59
|
|
|
public function i18n_plural_name() |
60
|
|
|
{ |
61
|
|
|
return _t('GiftVoucherProductPage.GIFT_VOUCHER_PRODUCT_PAGES', 'Gift Voucher Pages'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static $icon = 'ecommerce_giftvoucher/images/treeicons/GiftVoucherProductPage'; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @config |
68
|
|
|
* |
69
|
|
|
* @var string Description of the class functionality, typically shown to a user |
70
|
|
|
* when selecting which page type to create. Translated through {@link provideI18nEntities()}. |
71
|
|
|
*/ |
72
|
|
|
private static $description = 'Generic product that can be used to allow customers to choose a specific amount to pay.'; |
|
|
|
|
73
|
|
|
|
74
|
|
|
public function canCreate($member = null) |
75
|
|
|
{ |
76
|
|
|
return SiteTree::get()->filter(array('ClassName' => 'GiftVoucherProductPage'))->count() ? false : true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function canPurchase(Member $member = null, $checkPrice = true) |
81
|
|
|
{ |
82
|
|
|
return parent::canPurchase($member, false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
protected $defaultClassNameForOrderItem = 'GiftVoucherProductPage_ProductOrderItem'; |
91
|
|
|
|
92
|
|
|
public function getCMSFields() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$fields = parent::getCMSFields(); |
95
|
|
|
$fieldLabels = $this->fieldLabels(); |
96
|
|
|
$fieldLabelsRight = Config::inst()->get('GiftVoucherProductPage', 'field_labels_right'); |
97
|
|
|
$exampleLink = Director::absoluteURL($this->Link('setamount')).'/123.45/?description='.urlencode('test payment only'); |
98
|
|
|
$exampleLinkExplanation = sprintf(_t('GiftVoucherProductPage.EXPLANATION', '<br /><br /><h5>How to preset the amount?</h5><p>The link <a href="%1$s">%1$s</a> will pre-set the amount to 123.45. You can use this link (and vary the amount as needed) to cutomers to receive payments.</p>.'), $exampleLink); |
99
|
|
|
$fields->addFieldsToTab( |
100
|
|
|
'Root.Form', |
101
|
|
|
array( |
102
|
|
|
TextField::create('DescriptionFieldLabel', $fieldLabels['DescriptionFieldLabel'])->setDescription($fieldLabelsRight['DescriptionFieldLabel']), |
103
|
|
|
TextField::create('AmountFieldLabel', $fieldLabels['AmountFieldLabel'])->setDescription($fieldLabelsRight['AmountFieldLabel']), |
104
|
|
|
TextField::create('ActionFieldLabel', $fieldLabels['ActionFieldLabel'])->setDescription($fieldLabelsRight['ActionFieldLabel']), |
105
|
|
|
NumericField::create('MinimumAmount', $fieldLabels['MinimumAmount'])->setDescription($fieldLabelsRight['MinimumAmount']), |
106
|
|
|
NumericField::create('MaximumAmount', $fieldLabels['MaximumAmount'])->setDescription($fieldLabelsRight['MaximumAmount']), |
107
|
|
|
TextField::create('RecommendedAmounts', $fieldLabels['RecommendedAmounts'])->setDescription($fieldLabelsRight['RecommendedAmounts']), |
108
|
|
|
CheckboxField::create('CanSetDescription', $fieldLabels['CanSetDescription'])->setDescription($fieldLabelsRight['CanSetDescription']), |
109
|
|
|
TextField::create('DefaultDescription', $fieldLabels['DefaultDescription'])->setDescription($fieldLabelsRight['DefaultDescription']), |
110
|
|
|
LiteralField::create('ExampleLinkExplanation', $exampleLinkExplanation), |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
if (!$this->CanSetDescription) { |
114
|
|
|
$fields->removeByName('DescriptionFieldLabel'); |
115
|
|
|
} |
116
|
|
|
// Standard product detail fields |
117
|
|
|
$fields->removeFieldsFromTab( |
118
|
|
|
'Root.Details', |
119
|
|
|
array( |
120
|
|
|
'Weight', |
121
|
|
|
'Price', |
122
|
|
|
'Model', |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
// Flags for this product which affect it's behaviour on the site |
127
|
|
|
$fields->removeFieldsFromTab( |
128
|
|
|
'Root.Details', |
129
|
|
|
array( |
130
|
|
|
'FeaturedProduct', |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return $fields; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
class GiftVoucherProductPage_Controller extends Product_Controller |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
private static $allowed_actions = array( |
|
|
|
|
141
|
|
|
'AddNewPriceForm', |
142
|
|
|
'doaddnewpriceform', |
143
|
|
|
'setamount' |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
public function AddNewPriceForm() |
147
|
|
|
{ |
148
|
|
|
if ($this->canPurchase()) { |
149
|
|
|
$requiredFields = array(); |
150
|
|
|
$amount = $this->MinimumAmount; |
151
|
|
|
if ($newAmount = Session::get('GiftVoucherProductPageAmount')) { |
152
|
|
|
$amount = $newAmount; |
153
|
|
|
} |
154
|
|
|
$description = $this->DefaultDescription; |
155
|
|
|
if ($newDescription = Session::get('GiftVoucherProductPageDescription')) { |
156
|
|
|
$description = $newDescription; |
157
|
|
|
} |
158
|
|
|
$fields = FieldList::create(); |
159
|
|
|
if ($this->CanSetDescription) { |
160
|
|
|
$fields->push(TextField::create('Description', $this->DescriptionFieldLabel, $description)); |
161
|
|
|
$requiredFields[] = 'Description'; |
162
|
|
|
} |
163
|
|
|
$fields->push(CurrencyField::create('Amount', $this->AmountFieldLabel, $amount)); |
164
|
|
|
$requiredFields[] = 'Amount'; |
165
|
|
|
|
166
|
|
|
$actions = FieldList::create( |
167
|
|
|
FormAction::create('doaddnewpriceform', $this->ActionFieldLabel) |
168
|
|
|
); |
169
|
|
|
$requiredFields = RequiredFields::create($requiredFields); |
170
|
|
|
|
171
|
|
|
return Form::create( |
172
|
|
|
$controller = $this, |
173
|
|
|
$name = 'AddNewPriceForm', |
174
|
|
|
$fields, |
175
|
|
|
$actions, |
176
|
|
|
$requiredFields |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function doaddnewpriceform($data, $form) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
//check amount |
184
|
|
|
$amount = $this->parseFloat($data['Amount']); |
185
|
|
|
if ($this->MinimumAmount > 0 && ($amount < $this->MinimumAmount)) { |
186
|
|
|
$form->sessionMessage(_t('GiftVoucherProductPage.ERRORINFORMTOOLOW', 'Please enter a higher amount.'), 'bad'); |
187
|
|
|
$this->redirectBack(); |
188
|
|
|
|
189
|
|
|
return; |
190
|
|
|
} elseif ($this->MaximumAmount > 0 && ($amount > $this->MaximumAmount)) { |
191
|
|
|
$form->sessionMessage(_t('GiftVoucherProductPage.ERRORINFORMTOOHIGH', 'Please enter a lower amount.'), 'bad'); |
192
|
|
|
$this->redirectBack(); |
193
|
|
|
|
194
|
|
|
return; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
//clear settings from URL |
198
|
|
|
Session::clear('GiftVoucherProductPageAmount'); |
199
|
|
|
Session::clear('GiftVoucherProductPageDescription'); |
200
|
|
|
|
201
|
|
|
//create a description |
202
|
|
|
if (isset($data['Description']) && $data['Description']) { |
203
|
|
|
$description = Convert::raw2sql($data['Description']); |
204
|
|
|
} elseif ($this->DefaultDescription) { |
205
|
|
|
$description = $this->DefaultDescription; |
206
|
|
|
} else { |
|
|
|
|
207
|
|
|
//.. |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
//create order item and update it ... if needed |
211
|
|
|
$orderItem = $this->createOrderItem($amount, $description, $data); |
|
|
|
|
212
|
|
|
$orderItem = $this->updateOrderItem($orderItem, $data, $form); |
213
|
|
|
|
214
|
|
|
if (! $orderItem) { |
215
|
|
|
$form->sessionMessage(_t('GiftVoucherProductPage.ERROROTHER', 'Sorry, we could not add your entry.'), 'bad'); |
216
|
|
|
$this->redirectBack(); |
217
|
|
|
|
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
$checkoutPage = CheckoutPage::get()->First(); |
221
|
|
|
if ($checkoutPage) { |
222
|
|
|
return $this->redirect($checkoutPage->Link()); |
223
|
|
|
} |
224
|
|
|
return array(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function setamount($request) |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
if ($amount = floatval($request->param('ID'))) { |
230
|
|
|
Session::set('GiftVoucherProductPageAmount', $amount); |
231
|
|
|
} |
232
|
|
|
if ($description = Convert::raw2sql($request->param('OtherID'))) { |
|
|
|
|
233
|
|
|
Session::set('GiftVoucherProductPageDescription', $_GET['description']); |
234
|
|
|
} |
235
|
|
|
$this->redirect($this->Link()); |
236
|
|
|
|
237
|
|
|
return array(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* clean up the amount, we may improve this in the future. |
242
|
|
|
* |
243
|
|
|
* @return float |
244
|
|
|
*/ |
245
|
|
|
protected function parseFloat($floatString) |
246
|
|
|
{ |
247
|
|
|
return preg_replace('/([^0-9\\.])/i', '', $floatString); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param Variation (optional) $variation |
252
|
|
|
* @return OrderItem | null |
253
|
|
|
*/ |
254
|
|
|
protected function createOrderItem($amount, $description, $data) |
255
|
|
|
{ |
256
|
|
|
$shoppingCart = ShoppingCart::singleton(); |
257
|
|
|
$orderItem = $shoppingCart->addBuyable($this->dataRecord); |
258
|
|
|
$orderItem->setCustomCalculatedTotal($amount); |
259
|
|
|
$orderItem->setCustomDescription($description); |
260
|
|
|
return $orderItem; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* you can add this method to a class extending |
265
|
|
|
* GiftVoucherProductPage_Controller so that you can do something with the OrderItem |
266
|
|
|
* |
267
|
|
|
* @param OrderItem $orderItem |
268
|
|
|
* @param array $data |
269
|
|
|
* @param Form $form |
270
|
|
|
* |
271
|
|
|
* @return OrderItem |
272
|
|
|
*/ |
273
|
|
|
protected function updateOrderItem($orderItem, $data, $form) |
274
|
|
|
{ |
275
|
|
|
return $orderItem; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
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.