|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Model\Foxy; |
|
6
|
|
|
use Dynamic\Foxy\Model\FoxyCategory; |
|
7
|
|
|
use Dynamic\Foxy\Model\Setting; |
|
8
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
9
|
|
|
use SilverStripe\Forms\CurrencyField; |
|
10
|
|
|
use SilverStripe\Forms\DropdownField; |
|
11
|
|
|
use SilverStripe\Forms\FieldList; |
|
12
|
|
|
use SilverStripe\Forms\LiteralField; |
|
13
|
|
|
use SilverStripe\Forms\NumericField; |
|
14
|
|
|
use SilverStripe\Forms\TextField; |
|
15
|
|
|
use SilverStripe\ORM\DataExtension; |
|
16
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
17
|
|
|
|
|
18
|
|
|
class Purchasable extends DataExtension |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $db = [ |
|
|
|
|
|
|
24
|
|
|
'Price' => 'Currency', |
|
25
|
|
|
'Code' => 'Varchar(100)', |
|
26
|
|
|
'ReceiptTitle' => 'HTMLVarchar(255)', |
|
27
|
|
|
'Available' => 'Boolean', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
34
|
|
|
'FoxyCategory' => FoxyCategory::class, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
41
|
|
|
'Code' => [ |
|
42
|
|
|
'type' => 'unique', |
|
43
|
|
|
'columns' => ['Code'], |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $defaults = [ |
|
|
|
|
|
|
51
|
|
|
'ShowInMenus' => false, |
|
52
|
|
|
'Available' => true, |
|
53
|
|
|
'Weight' => '0.0', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
60
|
|
|
'Image.CMSThumbnail', |
|
61
|
|
|
'Title', |
|
62
|
|
|
'Code', |
|
63
|
|
|
'Price.Nice', |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
70
|
|
|
'Title', |
|
71
|
|
|
'Code', |
|
72
|
|
|
'Available', |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param bool $includerelations |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function updateFieldLabels(&$labels) |
|
81
|
|
|
{ |
|
82
|
|
|
$labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Product Name'); |
|
83
|
|
|
$labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code'); |
|
84
|
|
|
$labels['Price'] = _t(__CLASS__ . '.PriceLabel', 'Price'); |
|
85
|
|
|
$labels['Price.Nice'] = _t(__CLASS__ . '.PriceLabel', 'Price'); |
|
86
|
|
|
$labels['Available'] = _t(__CLASS__ . '.AvailableLabel', 'Available for purchase'); |
|
87
|
|
|
$labels['Available.Nice'] = _t(__CLASS__ . '.AvailableLabelNice', 'Available'); |
|
88
|
|
|
$labels['Image.CMSThumbnail'] = _t(__CLASS__ . '.ImageLabel', 'Image'); |
|
89
|
|
|
$labels['ReceiptTitle'] = _t(__CLASS__ . '.ReceiptTitleLabel', 'Product title for receipt'); |
|
90
|
|
|
$labels['FoxyCategoryID'] = _t(__CLASS__ . '.FoxyCategoryLabel', 'Foxy Category'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param FieldList $fields |
|
95
|
|
|
*/ |
|
96
|
|
|
public function updateCMSFields(FieldList $fields) |
|
97
|
|
|
{ |
|
98
|
|
|
$fields->addFieldsToTab( |
|
99
|
|
|
'Root.Foxy', |
|
100
|
|
|
[ |
|
101
|
|
|
CurrencyField::create('Price') |
|
102
|
|
|
->setDescription(_t( |
|
103
|
|
|
__CLASS__ . '.PriceDescription', |
|
104
|
|
|
'Base price for this product. Can be modified using Product Options' |
|
105
|
|
|
)), |
|
106
|
|
|
TextField::create('Code') |
|
107
|
|
|
->setDescription(_t( |
|
108
|
|
|
__CLASS__ . '.CodeDescription', |
|
109
|
|
|
'Required, must be unique. Product identifier used by FoxyCart in transactions' |
|
110
|
|
|
)), |
|
111
|
|
|
DropdownField::create('FoxyCategoryID') |
|
112
|
|
|
->setSource(FoxyCategory::get()->map()) |
|
113
|
|
|
->setDescription(_t( |
|
114
|
|
|
__CLASS__ . '.FoxyCategoryDescription', |
|
115
|
|
|
'Required. Must also exist in |
|
116
|
|
|
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" |
|
117
|
|
|
target="_blank"> |
|
118
|
|
|
Foxy Categories |
|
119
|
|
|
</a>. |
|
120
|
|
|
Used to set category specific options like shipping and taxes. Managed in Foxy > Categories' |
|
121
|
|
|
)) |
|
122
|
|
|
->setEmptyString(''), |
|
123
|
|
|
TextField::create('ReceiptTitle') |
|
124
|
|
|
->setDescription(_t( |
|
125
|
|
|
__CLASS__ . '.ReceiptTitleDescription', |
|
126
|
|
|
'Optional. Alternate title to display on order receipt' |
|
127
|
|
|
)), |
|
128
|
|
|
CheckboxField::create('Available') |
|
129
|
|
|
->setDescription(_t( |
|
130
|
|
|
__CLASS__ . '.AvailableDescription', |
|
131
|
|
|
'If unchecked, will remove "Add to Cart" form and instead display "Currently unavailable"' |
|
132
|
|
|
)), |
|
133
|
|
|
], |
|
134
|
|
|
'Content' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return \SilverStripe\ORM\ValidationResult |
|
140
|
|
|
*/ |
|
141
|
|
|
public function validate(ValidationResult $validationResult) |
|
142
|
|
|
{ |
|
143
|
|
|
if (!$this->owner->Price) { |
|
144
|
|
|
$validationResult->addError( |
|
145
|
|
|
_t(__CLASS__ . '.PriceRequired', 'You must set a product price in the Foxy tab') |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (!$this->owner->Code) { |
|
150
|
|
|
$validationResult->addError( |
|
151
|
|
|
_t(__CLASS__ . '.CodeRequired', 'You must set a product code in the Foxy tab') |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (!$this->owner->FoxyCategoryID) { |
|
156
|
|
|
$validationResult->addError( |
|
157
|
|
|
_t(__CLASS__ . '.FoxyCategoryRequired', 'You must set a foxy category in the Foxy tab.') |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
public function isAvailable() |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$this->owner->Available) { |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
/* |
|
171
|
|
|
// TODO: hook up when product options are implemented |
|
172
|
|
|
if (!$this->owner->ProductOptions()->exists()) { |
|
173
|
|
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
foreach ($this->owner->ProductOptions() as $option) { |
|
176
|
|
|
if ($option->Available) { |
|
177
|
|
|
return true; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
*/ |
|
181
|
|
|
return true; |
|
182
|
|
|
//return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function isProduct() |
|
189
|
|
|
{ |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|