|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class ImagesWithStyleSelection extends DataObject |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
####################### |
|
10
|
|
|
### Names Section |
|
11
|
|
|
####################### |
|
12
|
|
|
|
|
13
|
|
|
private static $singular_name = 'Selection of Images'; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
public function i18n_singular_name() |
|
16
|
|
|
{ |
|
17
|
|
|
return _t('ImagesWithStyleSelection.SINGULAR_NAME', 'Selection of Images'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
private static $plural_name = 'Selections of Images'; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
public function i18n_plural_name() |
|
23
|
|
|
{ |
|
24
|
|
|
return _t('ImagesWithStyleSelection.PLURAL_NAME', 'Selections of Images'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
####################### |
|
29
|
|
|
### Model Section |
|
30
|
|
|
####################### |
|
31
|
|
|
|
|
32
|
|
|
private static $db = [ |
|
|
|
|
|
|
33
|
|
|
'Title' => 'Varchar(255)', // this needs to be lengthy to avoid the same names ... |
|
34
|
|
|
'Description' => 'Text' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
38
|
|
|
'PlaceToStoreImages' => 'Folder' |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
42
|
|
|
'StyledImages' => 'ImageWithStyle' |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
|
|
46
|
|
|
'StyledImages' => [ |
|
47
|
|
|
'SortOrder' => 'Int', |
|
48
|
|
|
] |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
####################### |
|
54
|
|
|
### Further DB Field Details |
|
55
|
|
|
####################### |
|
56
|
|
|
|
|
57
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
58
|
|
|
'Created' => true, |
|
59
|
|
|
'Title' => 'unique("Title")' |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
private static $defaults = [ |
|
|
|
|
|
|
63
|
|
|
'Title' => '' |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
private static $default_sort = [ |
|
|
|
|
|
|
67
|
|
|
'Created' => 'DESC' |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
private static $required_fields = [ |
|
|
|
|
|
|
71
|
|
|
'Title' |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
75
|
|
|
'Title' => 'PartialMatchFilter' |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
####################### |
|
80
|
|
|
### Field Names and Presentation Section |
|
81
|
|
|
####################### |
|
82
|
|
|
|
|
83
|
|
|
private static $field_labels = [ |
|
|
|
|
|
|
84
|
|
|
'StyledImages' => 'Images to be included' |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
private static $field_labels_right = [ |
|
|
|
|
|
|
88
|
|
|
'StyledImages' => 'Select as many as you like and sort them in the right order' |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
92
|
|
|
'Title' => 'Name', |
|
93
|
|
|
'StyledImages.Count' => 'Number of Images' |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
####################### |
|
98
|
|
|
### Casting Section |
|
99
|
|
|
####################### |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
####################### |
|
103
|
|
|
### can Section |
|
104
|
|
|
####################### |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
####################### |
|
109
|
|
|
### write Section |
|
110
|
|
|
####################### |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
View Code Duplication |
public function validate() |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$result = parent::validate(); |
|
118
|
|
|
$fieldLabels = $this->FieldLabels(); |
|
119
|
|
|
$indexes = $this->Config()->get('indexes'); |
|
120
|
|
|
$requiredFields = $this->Config()->get('required_fields'); |
|
121
|
|
|
if (is_array($requiredFields)) { |
|
122
|
|
|
foreach ($requiredFields as $field) { |
|
123
|
|
|
$value = $this->$field; |
|
124
|
|
|
if (! $value) { |
|
125
|
|
|
$fieldWithoutID = $field; |
|
126
|
|
|
if (substr($fieldWithoutID, -2) === 'ID') { |
|
127
|
|
|
$fieldWithoutID = substr($fieldWithoutID, 0, -2); |
|
128
|
|
|
} |
|
129
|
|
|
$myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID; |
|
130
|
|
|
$result->error( |
|
131
|
|
|
_t( |
|
132
|
|
|
'ImagesWithStyleSelection.'.$field.'_REQUIRED', |
|
133
|
|
|
$myName.' is required' |
|
134
|
|
|
), |
|
135
|
|
|
'REQUIRED_ImagesWithStyleSelection_'.$field |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') { |
|
139
|
|
|
$id = (empty($this->ID) ? 0 : $this->ID); |
|
140
|
|
|
$count = ImagesWithStyleSelection::get() |
|
141
|
|
|
->filter(array($field => $value)) |
|
142
|
|
|
->exclude(array('ID' => $id)) |
|
143
|
|
|
->count(); |
|
144
|
|
|
if ($count > 0) { |
|
145
|
|
|
$myName = $fieldLabels['$field']; |
|
146
|
|
|
$result->error( |
|
147
|
|
|
_t( |
|
148
|
|
|
'ImagesWithStyleSelection.'.$field.'_UNIQUE', |
|
149
|
|
|
$myName.' needs to be unique' |
|
150
|
|
|
), |
|
151
|
|
|
'UNIQUE_ImagesWithStyleSelection_'.$field |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $result; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function onBeforeWrite() |
|
162
|
|
|
{ |
|
163
|
|
|
parent::onBeforeWrite(); |
|
164
|
|
|
//... |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function onAfterWrite() |
|
168
|
|
|
{ |
|
169
|
|
|
parent::onAfterWrite(); |
|
170
|
|
|
if ($this->PlaceToStoreImagesID) { |
|
|
|
|
|
|
171
|
|
|
$allImages = Image::get()->filter(['ParentID' => $this->PlaceToStoreImagesID])->column('ID'); |
|
|
|
|
|
|
172
|
|
|
$existingImages = $this->RawImages()->column('ID'); |
|
173
|
|
|
$difference = array_diff($allImages, $existingImages); |
|
174
|
|
|
$list = $this->StyledImages(); |
|
|
|
|
|
|
175
|
|
|
if (count($difference)) { |
|
176
|
|
|
foreach ($difference as $imageID) { |
|
177
|
|
|
$image = Image::get()->byID($imageID); |
|
178
|
|
|
if ($image) { |
|
179
|
|
|
$styledImage = ImageWithStyle::create(); |
|
180
|
|
|
$styledImage->Title = $image->Name; |
|
|
|
|
|
|
181
|
|
|
$styledImage->ImageID = $imageID; |
|
|
|
|
|
|
182
|
|
|
$styledImage->StyleID = ImageStyle::get_default_style()->ID; |
|
|
|
|
|
|
183
|
|
|
$styledImage->write(); |
|
184
|
|
|
$list->add($styledImage); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
//... |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function requireDefaultRecords() |
|
193
|
|
|
{ |
|
194
|
|
|
parent::requireDefaultRecords(); |
|
195
|
|
|
//... |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
####################### |
|
200
|
|
|
### Import / Export Section |
|
201
|
|
|
####################### |
|
202
|
|
|
|
|
203
|
|
|
public function getExportFields() |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
//.. |
|
206
|
|
|
return parent::getExportFields(); |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
####################### |
|
212
|
|
|
### CMS Edit Section |
|
213
|
|
|
####################### |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
public function CMSEditLink() |
|
217
|
|
|
{ |
|
218
|
|
|
$controller = singleton("ImageWithStyleAdmin"); |
|
219
|
|
|
|
|
220
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit"; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function CMSAddLink() |
|
224
|
|
|
{ |
|
225
|
|
|
$controller = singleton("ImageWithStyleAdmin"); |
|
226
|
|
|
|
|
227
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new"; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
public function getCMSFields() |
|
232
|
|
|
{ |
|
233
|
|
|
$fields = parent::getCMSFields(); |
|
234
|
|
|
$fields->removeByName('PlaceToStoreImages'); |
|
235
|
|
|
$fields->addFieldToTab( |
|
236
|
|
|
'Root.Main', |
|
237
|
|
|
$treeField = TreeDropdownField::create( |
|
238
|
|
|
'PlaceToStoreImagesID', |
|
239
|
|
|
'Image Folder', |
|
240
|
|
|
'Folder' |
|
241
|
|
|
)->setRightTitle('Optional - set folder ... all images in this folder will automatically be added.') |
|
242
|
|
|
); |
|
243
|
|
|
if ($this->PlaceToStoreImagesID) { |
|
|
|
|
|
|
244
|
|
|
$folder = $this->PlaceToStoreImages(); |
|
|
|
|
|
|
245
|
|
|
if ($folder && $folder->exists()) { |
|
246
|
|
|
$rightFieldTitle = $treeField->RightTitle(); |
|
247
|
|
|
$rightFieldTitle .= ' |
|
248
|
|
|
<br /><a href="/admin/assets/show/'.$folder->ID.'/" target="_blank">add images directly to folder</a> |
|
249
|
|
|
<br />After you have updated the folder make sure to save this list to receive the latest updates.'; |
|
250
|
|
|
$treeField->setRightTitle($rightFieldTitle); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
//do first?? |
|
254
|
|
|
$rightFieldDescriptions = $this->Config()->get('field_labels_right'); |
|
255
|
|
View Code Duplication |
foreach ($rightFieldDescriptions as $field => $desc) { |
|
|
|
|
|
|
256
|
|
|
$formField = $fields->DataFieldByName($field); |
|
257
|
|
|
if (! $formField) { |
|
258
|
|
|
$formField = $fields->DataFieldByName($field.'ID'); |
|
259
|
|
|
} |
|
260
|
|
|
if ($formField) { |
|
261
|
|
|
$formField->setDescription($desc); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
//... |
|
265
|
|
|
if ($this->exists()) { |
|
266
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
|
267
|
|
|
$config->addComponent(new GridFieldSortableRows('SortOrder')); |
|
268
|
|
|
$fields->removeByName('StyledImages'); |
|
269
|
|
|
$fields->addFieldToTab( |
|
270
|
|
|
'Root.Images', |
|
271
|
|
|
GridField::create( |
|
272
|
|
|
'StyledImages', |
|
273
|
|
|
'Images', |
|
274
|
|
|
$this->StyledImages(), |
|
|
|
|
|
|
275
|
|
|
$config |
|
276
|
|
|
) |
|
277
|
|
|
); |
|
278
|
|
|
} |
|
279
|
|
|
return $fields; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @return DataList |
|
284
|
|
|
*/ |
|
285
|
|
|
public function RawImages() |
|
286
|
|
|
{ |
|
287
|
|
|
$array = []; |
|
288
|
|
|
if ($this->StyledImages()->count()) { |
|
|
|
|
|
|
289
|
|
|
foreach ($this->StyledImages()->column('ImageID') as $id) { |
|
|
|
|
|
|
290
|
|
|
if ($id) { |
|
291
|
|
|
$array[$id] = $id; |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return Image::get()->filter(['ID' => $array]); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* force the list to use a certain folder ... |
|
301
|
|
|
* @param string $folderName |
|
302
|
|
|
* @return ImagesWithStyleSelection |
|
303
|
|
|
*/ |
|
304
|
|
|
public function createFolder($folderName) |
|
305
|
|
|
{ |
|
306
|
|
|
$folder = Folder::find_or_make($folderName); |
|
307
|
|
|
$this->PlaceToStoreImagesID = $folder->ID; |
|
|
|
|
|
|
308
|
|
|
$folder->write(); |
|
309
|
|
|
|
|
310
|
|
|
return $this; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
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.