|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Page; |
|
4
|
|
|
|
|
5
|
|
|
use Bummzack\SortableFile\Forms\SortableUploadField; |
|
6
|
|
|
use Dynamic\Products\Model\Brochure; |
|
7
|
|
|
use SilverStripe\Assets\File; |
|
8
|
|
|
use SilverStripe\Assets\Image; |
|
9
|
|
|
use SilverStripe\Forms\FieldList; |
|
10
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
|
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
|
13
|
|
|
use SilverStripe\Forms\TextField; |
|
14
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
|
15
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
|
16
|
|
|
|
|
17
|
|
|
class Product extends \Page |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var |
|
21
|
|
|
*/ |
|
22
|
|
|
private $image; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var |
|
26
|
|
|
*/ |
|
27
|
|
|
private $has_images; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $table_name = 'Product'; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $db = [ |
|
|
|
|
|
|
38
|
|
|
'SKU' => 'Varchar(100)', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
45
|
|
|
'Images' => File::class, |
|
46
|
|
|
'Brochures' => Brochure::class, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
|
|
53
|
|
|
'Images' => [ |
|
54
|
|
|
'SortOrder' => 'Int', |
|
55
|
|
|
], |
|
56
|
|
|
'Brochures' => [ |
|
57
|
|
|
'SortOrder' => 'Int', |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
*/ |
|
64
|
|
|
private static $owns = [ |
|
|
|
|
|
|
65
|
|
|
'Images', |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The relation name was established before requests for videos. |
|
70
|
|
|
* The relation has subsequently been updated from Image::class to File::class |
|
71
|
|
|
* to allow for additional file types such as mp4 |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
private static $allowed_images_extensions = [ |
|
|
|
|
|
|
76
|
|
|
'gif', |
|
77
|
|
|
'jpeg', |
|
78
|
|
|
'jpg', |
|
79
|
|
|
'png', |
|
80
|
|
|
'bmp', |
|
81
|
|
|
'ico', |
|
82
|
|
|
'mp4', |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var array |
|
87
|
|
|
*/ |
|
88
|
|
|
private static $allowed_children = []; |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return \SilverStripe\Forms\FieldList |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getCMSFields() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
96
|
|
|
$fields->insertBefore( |
|
97
|
|
|
'Content', |
|
98
|
|
|
TextField::create('SKU', 'Product SKU') |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
// Images tab |
|
102
|
|
|
$images = SortableUploadField::create('Images') |
|
103
|
|
|
->setSortColumn('SortOrder') |
|
104
|
|
|
->setIsMultiUpload(true) |
|
105
|
|
|
->setAllowedExtensions($this->config()->get('allowed_images_extensions')) |
|
106
|
|
|
->setFolderName('Uploads/Products/Images'); |
|
107
|
|
|
|
|
108
|
|
|
$fields->addFieldsToTab('Root.Images', [ |
|
109
|
|
|
$images, |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
if ($this->ID) { |
|
113
|
|
|
// Brochures |
|
114
|
|
|
$config = GridFieldConfig_RecordEditor::create(); |
|
115
|
|
|
$config->addComponents([ |
|
116
|
|
|
new GridFieldOrderableRows('SortOrder'), |
|
117
|
|
|
new GridFieldAddExistingSearchButton(), |
|
118
|
|
|
]) |
|
119
|
|
|
->removeComponentsByType([ |
|
120
|
|
|
GridFieldAddExistingAutocompleter::class, |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
$brochures = GridField::create( |
|
124
|
|
|
'Brochures', |
|
125
|
|
|
'Brochures', |
|
126
|
|
|
$this->Brochures()->sort('SortOrder'), |
|
|
|
|
|
|
127
|
|
|
$config |
|
128
|
|
|
); |
|
129
|
|
|
$fields->addFieldsToTab('Root.Files.Brochures', [ |
|
130
|
|
|
$brochures, |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
}); |
|
134
|
|
|
|
|
135
|
|
|
return parent::getCMSFields(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setImage() |
|
142
|
|
|
{ |
|
143
|
|
|
if ($this->getHasImages()) { |
|
144
|
|
|
$this->image = $this->Images()->sort('SortOrder')->first(); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
return $this; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return mixed |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getImage() |
|
153
|
|
|
{ |
|
154
|
|
|
if (!$this->image) { |
|
155
|
|
|
$this->setImage(); |
|
156
|
|
|
} |
|
157
|
|
|
return $this->image; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return mixed |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getThumbnail() |
|
164
|
|
|
{ |
|
165
|
|
|
if ($image = $this->getImage()) { |
|
166
|
|
|
if ($thumb = Image::get()->byID($image->ID)) { |
|
167
|
|
|
return $thumb->CMSThumbnail(); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return $this |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setHasImages() |
|
178
|
|
|
{ |
|
179
|
|
|
$this->has_images = $this->Images()->exists(); |
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return mixed |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getHasImages() |
|
187
|
|
|
{ |
|
188
|
|
|
if (!$this->has_images) { |
|
189
|
|
|
$this->setHasImages(); |
|
190
|
|
|
} |
|
191
|
|
|
return $this->has_images; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|