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