1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Define the Product Questions ... |
5
|
|
|
* |
6
|
|
|
* @package ecommerce |
7
|
|
|
* @subpackage ProductQuestion |
8
|
|
|
*/ |
9
|
|
|
class ProductQuestion extends DataObject |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Standard SS variable. |
14
|
|
|
*/ |
15
|
|
|
private static $db = array( |
|
|
|
|
16
|
|
|
'InternalCode' => 'Varchar(30)', |
17
|
|
|
'Question' => 'Varchar(255)', |
18
|
|
|
'AnswerRequired' => 'Boolean', |
19
|
|
|
'Label' => 'Varchar(100)', |
20
|
|
|
'DefaultAnswer' => 'Varchar(150)', |
21
|
|
|
'DefaultFormField' => 'Varchar(255)', |
22
|
|
|
'Options' => 'Text', |
23
|
|
|
"HasImages" => "Boolean", |
24
|
|
|
"ApplyToAllProducts" => "Boolean" |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Standard SS variable. |
29
|
|
|
*/ |
30
|
|
|
private static $casting = array( |
|
|
|
|
31
|
|
|
'Title' => 'Varchar', |
32
|
|
|
'FullName' => 'Varchar' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Standard SS variable. |
37
|
|
|
* Links questions to products |
38
|
|
|
*/ |
39
|
|
|
private static $many_many = array( |
|
|
|
|
40
|
|
|
'Products' => 'Product' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Standard SS variable. |
45
|
|
|
* Links to folder for images |
46
|
|
|
*/ |
47
|
|
|
private static $has_one = array( |
|
|
|
|
48
|
|
|
'Folder' => 'Folder' |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Standard SS variable. |
53
|
|
|
* Links questions to products |
54
|
|
|
*/ |
55
|
|
|
private static $summary_fields = array( |
|
|
|
|
56
|
|
|
'InternalCode' => 'InternalCode', |
57
|
|
|
'Question' => 'Question' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Standard SS variable. |
62
|
|
|
* Links questions to products |
63
|
|
|
*/ |
64
|
|
|
private static $searchable_fields = array( |
|
|
|
|
65
|
|
|
'InternalCode' => 'PartialMatch', |
66
|
|
|
'Question' => 'Varchar(255)', |
67
|
|
|
'Label' => 'PartialMatch', |
68
|
|
|
'DefaultAnswer' => 'PartialMatch', |
69
|
|
|
'DefaultFormField' => 'PartialMatch', |
70
|
|
|
'Options' => 'PartialMatch', |
71
|
|
|
"HasImages" => true |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Standard SS variable. |
76
|
|
|
*/ |
77
|
|
|
private static $default_sort = [ |
|
|
|
|
78
|
|
|
'Question' => 'ASC', |
79
|
|
|
'ID' => 'ASC' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Standard SS variable. |
84
|
|
|
*/ |
85
|
|
|
private static $defaults = array( |
|
|
|
|
86
|
|
|
"DefaultFormField" => "TextField", |
87
|
|
|
"DefaultAnswer" => "tba" |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* form fields used without a list |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
private static $available_form_fields_free = array( |
|
|
|
|
95
|
|
|
"TextField" => "Text Field", |
96
|
|
|
"TextareaField" => "Text Field with several lines", |
97
|
|
|
"DateField" => "Date Field", |
98
|
|
|
"EmailField" => "Email Field" |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* form fields used with a list |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private static $available_form_fields_list = array( |
|
|
|
|
106
|
|
|
"DropdownField" => "Dropdown Field", |
107
|
|
|
"OptionSetField" => "Option list Field" |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Maximum number of products on the site before the CheckboxSetField is |
112
|
|
|
* replaced with a GridField. |
113
|
|
|
* This field is used for selecting the products the question applies to. |
114
|
|
|
* |
115
|
|
|
* @var Int |
116
|
|
|
*/ |
117
|
|
|
private static $max_products_for_checkbox_set_field = 300; |
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Standard SS variable. |
121
|
|
|
*/ |
122
|
|
|
private static $singular_name = "Product Question"; |
|
|
|
|
123
|
|
|
public function i18n_singular_name() |
124
|
|
|
{ |
125
|
|
|
return _t("ProductQuestion.PRODUCT_QUESTION", "Product Question"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Standard SS variable. |
130
|
|
|
*/ |
131
|
|
|
private static $plural_name = "Product Questions"; |
|
|
|
|
132
|
|
|
public function i18n_plural_name() |
133
|
|
|
{ |
134
|
|
|
return _t("ProductQuestion.PRODUCT_QUESTIONS", "Product Questions"); |
135
|
|
|
} |
136
|
|
|
public static function get_plural_name() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$obj = Singleton("ProductQuestion"); |
139
|
|
|
return $obj->i18n_plural_name(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* turns an option into potential file names |
144
|
|
|
* e.g. red |
145
|
|
|
* returns |
146
|
|
|
* red.jpg, red.png, red.gif |
147
|
|
|
* @param String $option |
148
|
|
|
* @return Array |
149
|
|
|
*/ |
150
|
|
|
public static function create_file_array_from_option($option) |
151
|
|
|
{ |
152
|
|
|
$option = str_replace(' ', "-", trim($option)); |
153
|
|
|
$option = preg_replace("/[^a-z0-9_-]+/i", "", $option); |
154
|
|
|
$imageOptions = array( |
155
|
|
|
$option.".png", |
156
|
|
|
$option.".PNG", |
157
|
|
|
$option.".gif", |
158
|
|
|
$option.".GIF" , |
159
|
|
|
$option.".jpg", |
160
|
|
|
$option.".JPG", |
161
|
|
|
$option.".jpeg", |
162
|
|
|
$option.".JPEG" |
163
|
|
|
); |
164
|
|
|
return $imageOptions; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Standard SS method |
169
|
|
|
* @return FieldSet |
170
|
|
|
*/ |
171
|
|
|
public function getCMSFields() |
172
|
|
|
{ |
173
|
|
|
$fields = parent::getCMSFields(); |
174
|
|
|
$fields->addFieldToTab("Root.Images", new CheckboxField("HasImages", _t("ProductQuestion.HAS_IMAGES", "This question makes use of images"))); |
175
|
|
|
$fields->addFieldToTab("Root.Products", new CheckboxField("ApplyToAllProducts", _t("ProductQuestion.APPLY_TO_ALL_PRODUCTS", "Apply to all products ..."))); |
176
|
|
|
if (!$this->HasImages) { |
|
|
|
|
177
|
|
|
$fields->addFieldToTab( |
178
|
|
|
"Root.DefaultFormField", |
179
|
|
|
new OptionSetField( |
180
|
|
|
"DefaultFormField", |
181
|
|
|
_t("ProductQuestion.DEFAULTFORMFIELD", "Field type to use"), |
182
|
|
|
$this->Options ? $this->Config()->get("available_form_fields_list") : $this->Config()->get("available_form_fields_free") |
|
|
|
|
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
} else { |
186
|
|
|
$fields->removeFieldFromTab("Root.Main", "DefaultFormField"); |
187
|
|
|
} |
188
|
|
|
$productFieldTitle = _t("ProductQuestion.PRODUCTS", "Products showing this question"); |
189
|
|
|
if ($this->ApplyToAllProducts) { |
|
|
|
|
190
|
|
|
$productField = new HiddenField("Products"); |
191
|
|
|
} elseif (Product::get()->count() < $this->Config()->get("max_products_for_checkbox_set_field")) { |
192
|
|
|
$productField = new CheckboxSetField("Products", $productFieldTitle, Product::get()->map("ID", "FullName")->toArray()); |
193
|
|
|
} else { |
194
|
|
|
$productField = new GridField( |
195
|
|
|
'Products', |
196
|
|
|
_t("ProductQuestion.PRODUCTS", "Products showing this question"), |
197
|
|
|
$this->Products(), |
|
|
|
|
198
|
|
|
GridFieldEditOriginalPageConfigWithDelete::create() |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
$fields->addFieldToTab("Root.Products", $productField); |
202
|
|
|
if (!$this->ApplyToAllProducts) { |
|
|
|
|
203
|
|
View Code Duplication |
foreach ($this->Products() as $product) { |
|
|
|
|
204
|
|
|
$fields->addFieldToTab( |
205
|
|
|
"Root.Products", |
206
|
|
|
new LiteralField( |
207
|
|
|
"Product".$product->ID, |
208
|
|
|
"<h5><a href=\"".$product->CMSEditLink()."\">"._t("ProductQuestion.BACK_TO", "Edit ")." ".$product->Title."</a></h5>" |
209
|
|
|
) |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
if ($this->HasImages) { |
|
|
|
|
214
|
|
|
$folders = Folder::get(); |
215
|
|
|
if ($folders->count()) { |
216
|
|
|
$folderMap = $folders->map("ID", "Title")->toArray(); |
|
|
|
|
217
|
|
|
$folders = null; |
|
|
|
|
218
|
|
|
$fields->removeFieldFromTab("Root.Main", "Folder"); |
219
|
|
|
$fields->addFieldToTab( |
220
|
|
|
"Root.Images", |
221
|
|
|
$treeDropdownField = new TreeDropdownField("FolderID", _t("ProductQuestion.FOLDER", "Folder"), "Folder") |
222
|
|
|
); |
223
|
|
|
$treeDropdownFieldRightTitle = _t( |
224
|
|
|
"ProductQuestion.FOLDER_ID", |
225
|
|
|
"Select the folder in which the images live. |
226
|
|
|
<br /> |
227
|
|
|
<strong> |
228
|
|
|
The images need to have the exact same file name as the options listed. |
229
|
|
|
For example, if one of your options is 'red' then there should be a file in your folder called 'red.png' or 'red.jpg' or 'red.gif', |
230
|
|
|
the following filenames would not work: 'Red.png', 'red1.jpg', 'RED.gif', etc... |
231
|
|
|
</strong>" |
232
|
|
|
); |
233
|
|
|
$folder = Folder::get()->byID($this->FolderID); |
|
|
|
|
234
|
|
|
if ($folder) { |
235
|
|
|
$treeDropdownFieldRightTitle .= " |
236
|
|
|
<br /><a href=\"admin/assets/show/".$folder->ID."/\">"._t("ProductQuestion.OPEN", "Open")." ".$folder->Title."</a> |
237
|
|
|
<br /><a href=\"admin/assets/add/?ID".$folder->ID."/\">"._t("ProductQuestion.ADD_IMAGES_TO", "Add images to")." ".$folder->Title."</a>"; |
238
|
|
|
} |
239
|
|
|
$treeDropdownField->setRightTitle($treeDropdownFieldRightTitle); |
240
|
|
|
} |
241
|
|
|
if ($this->FolderID) { |
|
|
|
|
242
|
|
|
$imagesInFolder = Image::get()->filter(array("ParentID" => $this->FolderID)); |
|
|
|
|
243
|
|
|
if ($imagesInFolder->count()) { |
244
|
|
|
$imagesInFolderArray = $imagesInFolder->map("ID", "Name")->toArray(); |
245
|
|
|
$options = explode(",", $this->Options); |
|
|
|
|
246
|
|
|
$imagesInFolderField = new ReadonlyField("ImagesInFolder", _t("ProductQuestion.NO_IMAGES", "Images in folder"), implode("<br />", $imagesInFolderArray)); |
247
|
|
|
$imagesInFolderField->dontEscape = true; |
248
|
|
|
$fields->addFieldToTab("Root.Images", $imagesInFolderField); |
249
|
|
|
//matches |
250
|
|
|
if ($this->exists()) { |
251
|
|
|
$matchesInFolderArray = array(); |
252
|
|
|
$nonMatchesInFolderArray = array(); |
253
|
|
|
$options = explode(",", $this->Options); |
|
|
|
|
254
|
|
|
if (count($options)) { |
255
|
|
|
foreach ($options as $option) { |
256
|
|
|
$fileNameArray = self::create_file_array_from_option($option); |
257
|
|
|
foreach ($fileNameArray as $fileName) { |
258
|
|
|
if (in_array($fileName, $imagesInFolderArray)) { |
259
|
|
|
$matchesInFolderArray[$option] = "<strong>".$option."</strong>: ".$fileName; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
if (!isset($matchesInFolderArray[$option])) { |
263
|
|
|
$nonMatchesInFolderArray[$option] = "<strong>".$option." - add one these files: </strong>".implode(",", $fileNameArray); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
$matchesInFolderField = new ReadonlyField("MatchesInFolder", _t("ProductQuestion.MATCHES_IN_FOLDER", "Matches in folder"), implode("<br />", $matchesInFolderArray)); |
268
|
|
|
$matchesInFolderField->dontEscape = true; |
269
|
|
|
$fields->addFieldToTab("Root.Images", $matchesInFolderField); |
270
|
|
|
$nonMatchesInFolderField = new ReadonlyField("NonMatchesInFolder", _t("ProductQuestion.NON_MATCHES_IN_FOLDER", "NON Matches in folder"), implode("<br />", $nonMatchesInFolderArray)); |
271
|
|
|
$nonMatchesInFolderField->dontEscape = true; |
272
|
|
|
$fields->addFieldToTab("Root.Images", $nonMatchesInFolderField); |
273
|
|
|
} |
274
|
|
|
} else { |
275
|
|
|
$imagesInFolderField = new ReadonlyField("ImagesInFolder", "Images in folder", _t("ProductQuestion.NO_IMAGES", "There are no images in this folder")); |
276
|
|
|
$fields->addFieldToTab("Root.Main", $imagesInFolderField); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} else { |
280
|
|
|
$fields->removeByName("Folder"); |
281
|
|
|
} |
282
|
|
|
if ($this->Products()->count()) { |
|
|
|
|
283
|
|
|
$randomProduct = $this->Products()->First(); |
|
|
|
|
284
|
|
|
$fields->addFieldToTab("Root.Example", $this->getFieldForProduct($randomProduct)); |
285
|
|
|
} |
286
|
|
|
$folderExplanation = _t( |
287
|
|
|
"ProductQuestion.FOLDER_EXPLANATION", |
288
|
|
|
"Tick the box to link each option to an image (e.g. useful if you have colour swatches). |
289
|
|
|
Once ticked and the question is saved you will be able to select a folder from where to select the images." |
290
|
|
|
); |
291
|
|
|
$field = $fields->dataFieldByName("HasImages"); |
292
|
|
|
if ($field) { |
293
|
|
|
$field->setDescription($folderExplanation); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$this->extend('updateCMSFields', $fields); |
297
|
|
|
|
298
|
|
|
//custom field labels |
299
|
|
|
$internalCodeField = $fields->dataFieldByName("InternalCode"); |
300
|
|
|
$internalCodeField->setRightTitle(_t("ProductQuestion.INTERNALCODE", "Code used to identify question (not shown to customers)")); |
301
|
|
|
$questionField = $fields->dataFieldByName("Question"); |
302
|
|
|
$questionField->setRightTitle(_t("ProductQuestion.QUESTION", "Question (e.g. what configuration do you prefer?)")); |
303
|
|
|
$labelField = $fields->dataFieldByName("Label"); |
304
|
|
|
$labelField->setRightTitle(_t("ProductQuestion.LABEL", "Label (e.g. Your selected configuration)")); |
305
|
|
|
$defaultAnswerField = $fields->dataFieldByName("DefaultAnswer"); |
306
|
|
|
$defaultAnswerField->setRightTitle(_t("ProductQuestion.DEFAULT_ANSWER", "Default Answer if no Answer has been provided. Can be blank or, for example, tba.")); |
307
|
|
|
$optionsField = $fields->dataFieldByName("Options"); |
308
|
|
|
$optionsField->setRightTitle(_t("ProductQuestion.OPTIONS", "Predefined Options (leave blank for any option). These must be comma separated (e.g. red, blue, yellow, orange)")); |
309
|
|
|
return $fields; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* casted variable |
316
|
|
|
* @return String |
317
|
|
|
*/ |
318
|
|
|
public function FullName() |
319
|
|
|
{ |
320
|
|
|
return $this->getFullName(); |
321
|
|
|
} |
322
|
|
|
public function getFullName() |
323
|
|
|
{ |
324
|
|
|
return $this->Question." (".$this->InternalCode.")"; |
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* casted variable |
329
|
|
|
* @return String |
330
|
|
|
*/ |
331
|
|
|
public function Title() |
332
|
|
|
{ |
333
|
|
|
return $this->getTitle(); |
334
|
|
|
} |
335
|
|
|
public function getTitle() |
336
|
|
|
{ |
337
|
|
|
return $this->Question." (".$this->InternalCode.")"; |
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* |
342
|
|
|
* @return FormField |
343
|
|
|
*/ |
344
|
|
|
public function getFieldForProduct($product, $value = null) |
345
|
|
|
{ |
346
|
|
|
//switch from variation to product ... |
347
|
|
|
if ($product instanceof ProductVariation) { |
|
|
|
|
348
|
|
|
$product = $product->getParent(); |
349
|
|
|
} |
350
|
|
|
if ($this->Options) { |
|
|
|
|
351
|
|
|
//if HasImages? |
352
|
|
|
$finalOptions = array(); |
353
|
|
|
$optionArray = explode(",", $this->Options); |
|
|
|
|
354
|
|
|
foreach ($optionArray as $option) { |
355
|
|
|
$option = trim($option); |
356
|
|
|
$finalOptions[Convert::raw2htmlatt($option)] = $option; |
357
|
|
|
} |
358
|
|
|
if ($this->HasImages) { |
|
|
|
|
359
|
|
|
return new ProductQuestionImageSelectorField($this->getFieldForProductName($product), $this->Question, $finalOptions, $value, $this->FolderID); |
|
|
|
|
360
|
|
|
} else { |
361
|
|
|
$formFieldClass = $this->DefaultFormField; |
|
|
|
|
362
|
|
|
if (!$formFieldClass) { |
363
|
|
|
$formFieldClass = "DropdownField"; |
364
|
|
|
} |
365
|
|
|
$finalOptions = array("" => _t("ProductQuestion.PLEASE_SELECT", " -- please select --")) + $finalOptions; |
366
|
|
|
return $formFieldClass::create($this->getFieldForProductName($product), $this->Question, $finalOptions, $value); |
|
|
|
|
367
|
|
|
} |
368
|
|
|
} else { |
369
|
|
|
$formFieldClass = $this->DefaultFormField; |
|
|
|
|
370
|
|
|
if (!$formFieldClass) { |
371
|
|
|
$formFieldClassd = "TextField"; |
|
|
|
|
372
|
|
|
} |
373
|
|
|
return $formFieldClass::create($this->getFieldForProductName($product), $this->Question, $value); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function getFieldForProductName(Product $product) |
|
|
|
|
378
|
|
|
{ |
379
|
|
|
return "ProductQuestions[".$this->ID."]"; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* |
384
|
|
|
* making sure all the data is clean |
385
|
|
|
*/ |
386
|
|
|
public function onBeforeWrite() |
387
|
|
|
{ |
388
|
|
|
parent::onBeforeWrite(); |
389
|
|
|
if (!$this->InternalCode) { |
|
|
|
|
390
|
|
|
$this->InternalCode = $this->Label; |
|
|
|
|
391
|
|
|
} |
392
|
|
|
if (!$this->HasImages) { |
|
|
|
|
393
|
|
|
$this->FolderID = 0; |
|
|
|
|
394
|
|
|
} |
395
|
|
|
if ($this->Options) { |
|
|
|
|
396
|
|
|
$optionsForFields = $this->Config()->get("available_form_fields_list"); |
397
|
|
|
if (!isset($optionsForFields[$this->DefaultFormField])) { |
|
|
|
|
398
|
|
|
//get the first one if none is set. |
399
|
|
|
$this->DefaultFormField = key($optionsForFields); |
|
|
|
|
400
|
|
|
} |
401
|
|
|
} else { |
402
|
|
|
$optionsForFields = $this->Config()->get("available_form_fields_free"); |
403
|
|
|
if (!isset($optionsForFields[$this->DefaultFormField])) { |
|
|
|
|
404
|
|
|
//get the first one if none is set. |
405
|
|
|
$this->DefaultFormField = key($optionsForFields); |
|
|
|
|
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function onAfterWrite() |
411
|
|
|
{ |
412
|
|
|
parent::onAfterWrite(); |
413
|
|
|
if ($this->ApplyToAllProducts) { |
|
|
|
|
414
|
|
|
$this->Products()->removeAll(); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* link to edit the record |
420
|
|
|
* @param String | Null $action - e.g. edit |
421
|
|
|
* @return String |
422
|
|
|
*/ |
423
|
|
|
public function CMSEditLink($action = null) |
424
|
|
|
{ |
425
|
|
|
return Controller::join_links( |
426
|
|
|
Director::baseURL(), |
427
|
|
|
"/admin/product-config/".$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit", |
428
|
|
|
$action |
429
|
|
|
); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
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.