1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class ProductPage |
5
|
|
|
* @package foxystripe |
6
|
|
|
* @property string $Title |
7
|
|
|
* @property HTMLText $Content |
8
|
|
|
*/ |
9
|
|
|
class FoxyStripeProduct extends DataObject implements PermissionProvider |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private static $singular_name = 'Product'; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private static $plural_name = 'Products'; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $description = 'A product that can be added to the shopping cart'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $db = [ |
29
|
|
|
'Title' => 'Varchar(255)', |
30
|
|
|
'Content' => 'HTMLText', |
31
|
|
|
'Price' => 'Currency', |
32
|
|
|
'Weight' => 'Decimal', |
33
|
|
|
'Code' => 'Varchar(100)', |
34
|
|
|
'ReceiptTitle' => 'HTMLVarchar(255)', |
35
|
|
|
'Featured' => 'Boolean', |
36
|
|
|
'Available' => 'Boolean', |
37
|
|
|
'DiscountTitle' => 'Varchar(50)' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $has_one = [ |
44
|
|
|
'PreviewImage' => 'Image', |
45
|
|
|
'Category' => 'FoxyCartProductCategory' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private static $has_many = [ |
52
|
|
|
'ProductImages' => 'ProductImage', |
53
|
|
|
'ProductOptions' => 'OptionItem', |
54
|
|
|
'OrderDetails' => 'OrderDetail', |
55
|
|
|
'ProductDiscountTiers' => 'ProductDiscountTier' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private static $belongs_many_many = [ |
62
|
|
|
'ProductHolders' => 'ProductHolder' |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private static $indexes = [ |
69
|
|
|
'Code' => true, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private static $defaults = [ |
76
|
|
|
'ShowInMenus' => false, |
77
|
|
|
'Available' => true, |
78
|
|
|
'Weight' => '1.0' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private static $summary_fields = [ |
85
|
|
|
'Title', |
86
|
|
|
'Code', |
87
|
|
|
'Price.Nice', |
88
|
|
|
'Category.Title' |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
private static $searchable_fields = [ |
95
|
|
|
'Title', |
96
|
|
|
'Code', |
97
|
|
|
'Featured', |
98
|
|
|
'Available', |
99
|
|
|
'Category.ID' |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
public function fieldLabels($includeRelations = true) |
103
|
|
|
{ |
104
|
|
|
$labels = parent::fieldLabels($includeRelations); |
105
|
|
|
|
106
|
|
|
$labels['Title'] = _t('ProductPage.TitleLabel', 'Name'); |
107
|
|
|
$labels['Code'] = _t('ProductPage.CodeLabel', "Code"); |
108
|
|
|
$labels['Price.Nice'] = _t('ProductPage.PriceLabel', 'Price'); |
109
|
|
|
$labels['Featured.Nice'] = _t('ProductPage.NiceLabel', 'Featured'); |
110
|
|
|
$labels['Available.Nice'] = _t('ProductPage.AvailableLabel', 'Available'); |
111
|
|
|
$labels['Category.ID'] = _t('ProductPage.IDLabel', 'Category'); |
112
|
|
|
$labels['Category.Title'] = _t('ProductPage.CategoryTitleLabel', 'Category'); |
113
|
|
|
|
114
|
|
|
return $labels; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return FieldList |
119
|
|
|
*/ |
120
|
|
|
public function getCMSFields() |
121
|
|
|
{ |
122
|
|
|
$fields = parent::getCMSFields(); |
123
|
|
|
|
124
|
|
|
// allow extensions of ProductPage to override the PreviewImage field description |
125
|
|
|
$previewDescription = ($this->stat('customPreviewDescription')) ? $this->stat('customPreviewDescription') : _t('ProductPage.PreviewImageDescription', 'Image used throughout site to represent this product'); |
126
|
|
|
|
127
|
|
|
// Cateogry Dropdown field w/ add new |
128
|
|
|
$source = function () { |
129
|
|
|
return FoxyCartProductCategory::get()->map()->toArray(); |
130
|
|
|
}; |
131
|
|
|
$catField = DropdownField::create('CategoryID', _t('ProductPage.Category', 'FoxyCart Category'), $source()) |
132
|
|
|
->setEmptyString('') |
133
|
|
|
->setDescription(_t( |
134
|
|
|
'ProductPage.CategoryDescription', |
135
|
|
|
'Required, must also exist in |
136
|
|
|
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank"> |
137
|
|
|
FoxyCart Categories |
138
|
|
|
</a>. |
139
|
|
|
Used to set category specific options like shipping and taxes. Managed in |
140
|
|
|
<a href="admin/settings"> |
141
|
|
|
Settings > FoxyStripe > Categories |
142
|
|
|
</a>' |
143
|
|
|
)); |
144
|
|
|
if (class_exists('QuickAddNewExtension')) $catField->useAddNew('FoxyCartProductCategory', $source); |
145
|
|
|
|
146
|
|
|
// Product Images gridfield |
147
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
148
|
|
|
if (class_exists('GridFieldSortableRows')) $config->addComponent(new GridFieldSortableRows('SortOrder')); |
149
|
|
|
if (class_exists('GridFieldBulkImageUpload')) { |
150
|
|
|
$config->addComponent(new GridFieldBulkUpload()); |
151
|
|
|
$config->getComponentByType('GridFieldBulkUpload')->setUfConfig('folderName', 'Uploads/ProductImages'); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
$prodImagesField = GridField::create( |
154
|
|
|
'ProductImages', |
155
|
|
|
_t('ProductPage.ProductImages', 'Images'), |
156
|
|
|
$this->ProductImages(), |
|
|
|
|
157
|
|
|
$config |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
// Product Options field |
161
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
162
|
|
|
if (class_exists('GridFieldSortableRows')) { |
163
|
|
|
$config->addComponent(new GridFieldSortableRows('SortOrder')); |
164
|
|
|
$products = $this->ProductOptions()->sort('SortOrder'); |
|
|
|
|
165
|
|
|
} else { |
166
|
|
|
$products = $this->ProductOptions(); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
169
|
|
|
$prodOptField = GridField::create( |
170
|
|
|
'ProductOptions', |
171
|
|
|
_t('ProductPage.ProductOptions', 'Options'), |
172
|
|
|
$products, |
173
|
|
|
$config |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
// Details tab |
177
|
|
|
$fields->addFieldsToTab('Root.Details', [ |
178
|
|
|
HeaderField::create('DetailHD', 'Product Details', 2), |
179
|
|
|
CheckboxField::create('Available') |
180
|
|
|
->setTitle(_t('ProductPage.Available', 'Available for purchase')) |
181
|
|
|
->setDescription(_t( |
182
|
|
|
'ProductPage.AvailableDescription', |
183
|
|
|
'If unchecked, will remove "Add to Cart" form and instead display "Currently unavailable"' |
184
|
|
|
)), |
185
|
|
|
TextField::create('Code') |
186
|
|
|
->setTitle(_t('ProductPage.Code', 'Product Code')) |
187
|
|
|
->setDescription(_t( |
188
|
|
|
'ProductPage.CodeDescription', |
189
|
|
|
'Required, must be unique. Product identifier used by FoxyCart in transactions' |
190
|
|
|
)), |
191
|
|
|
$catField, |
192
|
|
|
CurrencyField::create('Price') |
193
|
|
|
->setTitle(_t('ProductPage.Price', 'Price')) |
194
|
|
|
->setDescription(_t( |
195
|
|
|
'ProductPage.PriceDescription', |
196
|
|
|
'Base price for this product. Can be modified using Product Options' |
197
|
|
|
)), |
198
|
|
|
NumericField::create('Weight') |
199
|
|
|
->setTitle(_t('ProductPage.Weight', 'Weight')) |
200
|
|
|
->setDescription(_t( |
201
|
|
|
'ProductPage.WeightDescription', |
202
|
|
|
'Base weight for this product in lbs. Can be modified using Product Options' |
203
|
|
|
)), |
204
|
|
|
CheckboxField::create('Featured') |
205
|
|
|
->setTitle(_t('ProductPage.Featured', 'Featured Product')), |
206
|
|
|
TextField::create('ReceiptTitle') |
207
|
|
|
->setTitle(_t('ProductPage.ReceiptTitle', 'Product Title for Receipt')) |
208
|
|
|
->setDescription(_t( |
209
|
|
|
'ProductPage.ReceiptTitleDescription', 'Optional' |
210
|
|
|
)) |
211
|
|
|
]); |
212
|
|
|
|
213
|
|
|
// Images tab |
214
|
|
|
$fields->addFieldsToTab('Root.Images', [ |
215
|
|
|
HeaderField::create('MainImageHD', _t('ProductPage.MainImageHD', 'Product Image'), 2), |
216
|
|
|
UploadField::create('PreviewImage', '') |
217
|
|
|
->setDescription($previewDescription) |
218
|
|
|
->setFolderName('Uploads/Products') |
219
|
|
|
->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']) |
220
|
|
|
->setAllowedMaxFileNumber(1), |
221
|
|
|
HeaderField::create('ProductImagesHD', _t('ProductPage.ProductImagesHD' . 'Product Image Gallery'), 2), |
222
|
|
|
$prodImagesField |
223
|
|
|
->setDescription(_t( |
224
|
|
|
'ProductPage.ProductImagesDescription', |
225
|
|
|
'Additional Product Images, shown in gallery on Product page' |
226
|
|
|
)) |
227
|
|
|
]); |
228
|
|
|
|
229
|
|
|
// Options Tab |
230
|
|
|
$fields->addFieldsToTab('Root.Options', [ |
231
|
|
|
HeaderField::create('OptionsHD', _t('ProductPage.OptionsHD', 'Product Options'), 2), |
232
|
|
|
LiteralField::create('OptionsDescrip', _t( |
233
|
|
|
'Page.OptionsDescrip', |
234
|
|
|
'<p>Product Options allow products to be customized by attributes such as size or color. |
235
|
|
|
Options can also modify the product\'s price, weight or code.</p>' |
236
|
|
|
)), |
237
|
|
|
$prodOptField |
238
|
|
|
]); |
239
|
|
|
|
240
|
|
|
if (!$this->DiscountTitle && $this->ProductDiscountTiers()->exists()) { |
|
|
|
|
241
|
|
|
$fields->addFieldTotab('Root.Discounts', new LiteralField("ProductDiscountHeaderWarning", "<p class=\"message warning\">A discount title is required for FoxyCart to properly parse the value. The discounts will not be applied until a title is entered.</p>")); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$fields->addFieldToTab('Root.Discounts', TextField::create('DiscountTitle')->setTitle(_t('Product.DiscountTitle', 'Discount Title'))); |
245
|
|
|
$discountsConfig = GridFieldConfig_RelationEditor::create(); |
246
|
|
|
$discountsConfig->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
247
|
|
|
$discountsConfig->removeComponentsByType('GridFieldDeleteAction'); |
248
|
|
|
$discountsConfig->addComponent(new GridFieldDeleteAction(false)); |
249
|
|
|
$discountGrid = GridField::create('ProductDiscountTiers', 'Product Discounts', $this->ProductDiscountTiers(), $discountsConfig); |
|
|
|
|
250
|
|
|
$fields->addFieldToTab('Root.Discounts', $discountGrid); |
251
|
|
|
|
252
|
|
View Code Duplication |
if (FoxyCart::store_name_warning() !== null) { |
|
|
|
|
253
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create("StoreSubDomainHeaderWarning", _t( |
254
|
|
|
'ProductPage.StoreSubDomainHeaderWarning', |
255
|
|
|
"<p class=\"message error\">Store sub-domain must be entered in the <a href=\"/admin/settings/\">site settings</a></p>" |
256
|
|
|
)), 'Title'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// allows CMS fields to be extended |
260
|
|
|
$this->extend('updateCMSFields', $fields); |
261
|
|
|
|
262
|
|
|
return $fields; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function onBeforeWrite() |
266
|
|
|
{ |
267
|
|
|
parent::onBeforeWrite(); |
268
|
|
|
if (!$this->CategoryID) { |
|
|
|
|
269
|
|
|
$default = FoxyCartProductCategory::get()->filter(['Code' => 'DEFAULT'])->first(); |
270
|
|
|
$this->CategoryID = $default->ID; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
//update many_many lists when multi-group is on |
274
|
|
|
if (SiteConfig::current_site_config()->MultiGroup) { |
275
|
|
|
$holders = $this->ProductHolders(); |
|
|
|
|
276
|
|
|
$product = ProductPage::get()->byID($this->ID); |
277
|
|
|
if (isset($product->ParentID)) { |
278
|
|
|
$origParent = $product->ParentID; |
279
|
|
|
} else { |
280
|
|
|
$origParent = null; |
281
|
|
|
} |
282
|
|
|
$currentParent = $this->ParentID; |
|
|
|
|
283
|
|
|
if ($origParent != $currentParent) { |
284
|
|
|
if ($holders->find('ID', $origParent)) { |
285
|
|
|
$holders->removeByID($origParent); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
$holders->add($currentParent); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$title = ltrim($this->Title); |
293
|
|
|
$title = rtrim($title); |
294
|
|
|
$this->Title = $title; |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function onAfterWrite() |
300
|
|
|
{ |
301
|
|
|
parent::onAfterWrite(); |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function onBeforeDelete() |
307
|
|
|
{ |
308
|
|
|
if ($this->Status != "Published") { |
|
|
|
|
309
|
|
|
if ($this->ProductOptions()) { |
|
|
|
|
310
|
|
|
$options = $this->getComponents('ProductOptions'); |
311
|
|
|
foreach ($options as $option) { |
312
|
|
|
$option->delete(); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
if ($this->ProductImages()) { |
|
|
|
|
316
|
|
|
//delete product image dataobjects, not the images themselves. |
317
|
|
|
$images = $this->getComponents('ProductImages'); |
318
|
|
|
foreach ($images as $image) { |
319
|
|
|
$image->delete(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
parent::onBeforeDelete(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function validate() |
327
|
|
|
{ |
328
|
|
|
$result = parent::validate(); |
329
|
|
|
|
330
|
|
|
/*if($this->ID>0){ |
|
|
|
|
331
|
|
|
if($this->Price <= 0) { |
332
|
|
|
$result->error('Must set a positive price value'); |
333
|
|
|
} |
334
|
|
|
if($this->Weight <= 0){ |
335
|
|
|
$result->error('Must set a positive weight value'); |
336
|
|
|
} |
337
|
|
|
if($this->Code == ''){ |
338
|
|
|
$result->error('Must set a product code'); |
339
|
|
|
} |
340
|
|
|
}*/ |
341
|
|
|
|
342
|
|
|
return $result; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function getCMSValidator() |
346
|
|
|
{ |
347
|
|
|
return new RequiredFields(['CategoryID', 'Price', 'Weight', 'Code']); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public static function getGeneratedValue($productCode = null, $optionName = null, $optionValue = null, $method = 'name', $output = false, $urlEncode = false) |
351
|
|
|
{ |
352
|
|
|
$optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName; |
353
|
|
|
return (SiteConfig::current_site_config()->CartValidation) |
354
|
|
|
? FoxyCart_Helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode) : |
355
|
|
|
$optionValue; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// get FoxyCart Store Name for JS call |
359
|
|
|
public function getCartScript() |
360
|
|
|
{ |
361
|
|
|
return '<script src="https://cdn.foxycart.com/' . FoxyCart::getFoxyCartStoreName() . '/loader.js" async defer></script>'; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function getDiscountFieldValue() |
365
|
|
|
{ |
366
|
|
|
$tiers = $this->ProductDiscountTiers(); |
|
|
|
|
367
|
|
|
$bulkString = ''; |
368
|
|
|
foreach ($tiers as $tier) { |
369
|
|
|
$bulkString .= "|{$tier->Quantity}-{$tier->Percentage}"; |
370
|
|
|
} |
371
|
|
|
return "{$this->Title}{allunits{$bulkString}}"; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param Member $member |
376
|
|
|
* @return boolean |
377
|
|
|
*/ |
378
|
|
|
public function canEdit($member = null) |
379
|
|
|
{ |
380
|
|
|
return Permission::check('Product_CANCRUD'); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function canDelete($member = null) |
384
|
|
|
{ |
385
|
|
|
return Permission::check('Product_CANCRUD'); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function canCreate($member = null) |
389
|
|
|
{ |
390
|
|
|
return Permission::check('Product_CANCRUD'); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function canPublish($member = null) |
|
|
|
|
394
|
|
|
{ |
395
|
|
|
return Permission::check('Product_CANCRUD'); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
public function providePermissions() |
399
|
|
|
{ |
400
|
|
|
return [ |
401
|
|
|
'Product_CANCRUD' => 'Allow user to manage Products and related objects' |
402
|
|
|
]; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return FoxyStripePurchaseForm |
407
|
|
|
*/ |
408
|
|
|
public function getPurchaseForm() |
409
|
|
|
{ |
410
|
|
|
|
411
|
|
|
$product = $this; |
412
|
|
|
|
413
|
|
|
$form = FoxyStripePurchaseForm::create(Controller::curr(), __FUNCTION__, null, null, null, $product); |
414
|
|
|
|
415
|
|
|
$this->extend('updateFoxyStripePurchaseForm', $form); |
416
|
|
|
|
417
|
|
|
return $form; |
418
|
|
|
|
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.