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
|
|
|
if (Foxy::store_name_warning() !== null) { |
138
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('StoreSubDomainHeaderWarning', _t( |
139
|
|
|
'ProductPage.StoreSubDomainHeaderWarning', |
140
|
|
|
'<p class="message error">Store domain must be entered in the |
141
|
|
|
<a href="/admin/foxy/">Foxy settings</a></p>' |
142
|
|
|
)), 'Title'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \SilverStripe\ORM\ValidationResult |
148
|
|
|
*/ |
149
|
|
|
public function validate(ValidationResult $validationResult) |
150
|
|
|
{ |
151
|
|
|
if (!$this->owner->Price) { |
152
|
|
|
$validationResult->addError( |
153
|
|
|
_t(__CLASS__ . '.PriceRequired', 'You must set a product price in the Foxy tab') |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!$this->owner->Code) { |
158
|
|
|
$validationResult->addError( |
159
|
|
|
_t(__CLASS__ . '.CodeRequired', 'You must set a product code in the Foxy tab') |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!$this->owner->FoxyCategoryID) { |
164
|
|
|
$validationResult->addError( |
165
|
|
|
_t(__CLASS__ . '.FoxyCategoryRequired', 'You must set a foxy category in the Foxy tab.') |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function isAvailable() |
174
|
|
|
{ |
175
|
|
|
if (!$this->owner->Available) { |
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
/* |
179
|
|
|
// TODO: hook up when product options are implemented |
180
|
|
|
if (!$this->owner->ProductOptions()->exists()) { |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
foreach ($this->owner->ProductOptions() as $option) { |
184
|
|
|
if ($option->Available) { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
*/ |
189
|
|
|
return true; |
190
|
|
|
//return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function isProduct() |
197
|
|
|
{ |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|