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
|
|
|
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
|
|
|
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', |
34
|
|
|
'Description' => 'Text' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
private static $many_many = [ |
|
|
|
|
38
|
|
|
'StyledImages' => 'ImageWithStyle' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
42
|
|
|
'StyledImages' => [ |
43
|
|
|
'SortOrder' => 'Int', |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
####################### |
50
|
|
|
### Further DB Field Details |
51
|
|
|
####################### |
52
|
|
|
|
53
|
|
|
private static $indexes = [ |
|
|
|
|
54
|
|
|
'Created' => true, |
55
|
|
|
'Title' => 'unique("Title")' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
private static $defaults = [ |
|
|
|
|
59
|
|
|
'Title' => '' |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
private static $default_sort = [ |
|
|
|
|
63
|
|
|
'Created' => 'DESC' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
private static $required_fields = [ |
|
|
|
|
67
|
|
|
'Title' |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
private static $searchable_fields = [ |
|
|
|
|
71
|
|
|
'Title' => 'PartialMatchFilter' |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
####################### |
76
|
|
|
### Field Names and Presentation Section |
77
|
|
|
####################### |
78
|
|
|
|
79
|
|
|
private static $field_labels = [ |
|
|
|
|
80
|
|
|
'StyledImages' => 'Images to be included' |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
private static $field_labels_right = [ |
|
|
|
|
84
|
|
|
'StyledImages' => 'Select as many as you like and sort them in the right order' |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
private static $summary_fields = [ |
|
|
|
|
88
|
|
|
'Title' => 'Name', |
89
|
|
|
'StyledImages.Count' => 'Number of Images' |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
####################### |
94
|
|
|
### Casting Section |
95
|
|
|
####################### |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
####################### |
99
|
|
|
### can Section |
100
|
|
|
####################### |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
####################### |
105
|
|
|
### write Section |
106
|
|
|
####################### |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
View Code Duplication |
public function validate() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$result = parent::validate(); |
114
|
|
|
$fieldLabels = $this->FieldLabels(); |
115
|
|
|
$indexes = $this->Config()->get('indexes'); |
116
|
|
|
$requiredFields = $this->Config()->get('required_fields'); |
117
|
|
|
if(is_array($requiredFields)) { |
118
|
|
|
foreach($requiredFields as $field) { |
119
|
|
|
$value = $this->$field; |
120
|
|
|
if(! $value) { |
121
|
|
|
$fieldWithoutID = $field; |
122
|
|
|
if(substr($fieldWithoutID, -2) === 'ID') { |
123
|
|
|
$fieldWithoutID = substr($fieldWithoutID, 0, -2); |
124
|
|
|
} |
125
|
|
|
$myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID; |
126
|
|
|
$result->error( |
127
|
|
|
_t( |
128
|
|
|
'ImagesWithStyleSelection.'.$field.'_REQUIRED', |
129
|
|
|
$myName.' is required' |
130
|
|
|
), |
131
|
|
|
'REQUIRED_ImagesWithStyleSelection_'.$field |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') { |
135
|
|
|
$id = (empty($this->ID) ? 0 : $this->ID); |
136
|
|
|
$count = ImagesWithStyleSelection::get() |
137
|
|
|
->filter(array($field => $value)) |
138
|
|
|
->exclude(array('ID' => $id)) |
139
|
|
|
->count(); |
140
|
|
|
if($count > 0) { |
141
|
|
|
$myName = $fieldLabels['$field']; |
142
|
|
|
$result->error( |
143
|
|
|
_t( |
144
|
|
|
'ImagesWithStyleSelection.'.$field.'_UNIQUE', |
145
|
|
|
$myName.' needs to be unique' |
146
|
|
|
), |
147
|
|
|
'UNIQUE_ImagesWithStyleSelection_'.$field |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $result; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function onBeforeWrite() |
158
|
|
|
{ |
159
|
|
|
parent::onBeforeWrite(); |
160
|
|
|
//... |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function onAfterWrite() |
164
|
|
|
{ |
165
|
|
|
parent::onAfterWrite(); |
166
|
|
|
//... |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function requireDefaultRecords() |
170
|
|
|
{ |
171
|
|
|
parent::requireDefaultRecords(); |
172
|
|
|
//... |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
####################### |
177
|
|
|
### Import / Export Section |
178
|
|
|
####################### |
179
|
|
|
|
180
|
|
|
public function getExportFields() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
//.. |
183
|
|
|
return parent::getExportFields(); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
####################### |
189
|
|
|
### CMS Edit Section |
190
|
|
|
####################### |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function CMSEditLink() |
194
|
|
|
{ |
195
|
|
|
$controller = singleton("ImageWithStyleAdmin"); |
196
|
|
|
|
197
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit"; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function CMSAddLink() |
201
|
|
|
{ |
202
|
|
|
$controller = singleton("ImageWithStyleAdmin"); |
203
|
|
|
|
204
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
public function getCMSFields() |
209
|
|
|
{ |
210
|
|
|
$fields = parent::getCMSFields(); |
211
|
|
|
|
212
|
|
|
//do first?? |
213
|
|
|
$rightFieldDescriptions = $this->Config()->get('field_labels_right'); |
214
|
|
View Code Duplication |
foreach($rightFieldDescriptions as $field => $desc) { |
|
|
|
|
215
|
|
|
$formField = $fields->DataFieldByName($field); |
216
|
|
|
if(! $formField) { |
217
|
|
|
$formField = $fields->DataFieldByName($field.'ID'); |
218
|
|
|
} |
219
|
|
|
if($formField) { |
220
|
|
|
$formField->setDescription($desc); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
//... |
224
|
|
|
if($this->exists()) { |
225
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
226
|
|
|
$config->addComponent(new GridFieldSortableRows('SortOrder')); |
227
|
|
|
$fields->removeByName('StyledImages'); |
228
|
|
|
$fields->addFieldToTab( |
229
|
|
|
'Root.Images', |
230
|
|
|
GridField::create( |
231
|
|
|
'StyledImages', |
232
|
|
|
'Images', |
233
|
|
|
$this->StyledImages(), |
|
|
|
|
234
|
|
|
$config |
235
|
|
|
) |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
return $fields; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
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.