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