1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class ImageWithStyle extends DataObject |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
####################### |
9
|
|
|
### Names Section |
10
|
|
|
####################### |
11
|
|
|
|
12
|
|
|
private static $singular_name = 'Image With Style'; |
|
|
|
|
13
|
|
|
|
14
|
|
|
function i18n_singular_name() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
return _t('ImageWithStyle.SINGULAR_NAME', 'Image With Style'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
private static $plural_name = 'Images With Style'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
function i18n_plural_name() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return _t('ImageWithStyle.PLURAL_NAME', 'Images With Style'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
####################### |
28
|
|
|
### Model Section |
29
|
|
|
####################### |
30
|
|
|
|
31
|
|
|
private static $db = [ |
|
|
|
|
32
|
|
|
'Title' => 'Varchar', |
33
|
|
|
'Description' => 'Text', |
34
|
|
|
'Var1' => 'Varchar', |
35
|
|
|
'Var2' => 'Varchar', |
36
|
|
|
'Var3' => 'Varchar', |
37
|
|
|
'Var4' => 'Varchar', |
38
|
|
|
'Var5' => 'Varchar' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
private static $has_one = [ |
|
|
|
|
42
|
|
|
'Image' => 'Image', |
43
|
|
|
'Style' => 'ImageStyle' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private static $belongs_many_many = [ |
|
|
|
|
47
|
|
|
'Selections' => 'ImagesWithStyleSelection' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
####################### |
52
|
|
|
### Further DB Field Details |
53
|
|
|
####################### |
54
|
|
|
|
55
|
|
|
private static $indexes = [ |
|
|
|
|
56
|
|
|
'Created' => true |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
private static $default_sort = [ |
|
|
|
|
60
|
|
|
'Created' => 'DESC' |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
private static $required_fields = [ |
|
|
|
|
64
|
|
|
'Title', |
65
|
|
|
'StyleID' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
private static $searchable_fields = [ |
|
|
|
|
69
|
|
|
'Title' => 'PartialMatchFilter' |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
####################### |
74
|
|
|
### Field Names and Presentation Section |
75
|
|
|
####################### |
76
|
|
|
|
77
|
|
|
private static $field_labels = [ |
|
|
|
|
78
|
|
|
'Style' => 'Display Style', |
79
|
|
|
'Title' => 'Image Title', |
80
|
|
|
'Selections' => 'Lists' |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
private static $field_labels_right = [ |
|
|
|
|
84
|
|
|
'Pages' => 'On what pages is this image visible' |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
private static $summary_fields = [ |
|
|
|
|
88
|
|
|
'Title' => 'Title', |
89
|
|
|
'Style.Title' => 'Style', |
90
|
|
|
'Image.CMSThumbnail' => 'Image' |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
private static $casting = [ |
|
|
|
|
95
|
|
|
'ImageElement' => 'HTMLText' |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
private static $unique_vars = ['Var1', 'Var2', 'Var3', 'Var4', 'Var5']; |
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
####################### |
102
|
|
|
### Casting Section |
103
|
|
|
####################### |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
####################### |
107
|
|
|
### can Section |
108
|
|
|
####################### |
109
|
|
|
|
110
|
|
|
function canDelete($member = null) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
####################### |
118
|
|
|
### write Section |
119
|
|
|
####################### |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function validate() |
125
|
|
|
{ |
126
|
|
|
$result = parent::validate(); |
127
|
|
|
$fieldLabels = $this->FieldLabels(); |
128
|
|
|
$indexes = $this->Config()->get('indexes'); |
129
|
|
|
$requiredFields = $this->Config()->get('required_fields'); |
130
|
|
|
if($this->exists() && $this->hasRealStyle()) { |
131
|
|
|
$requiredFields[] = 'ImageID'; |
132
|
|
|
} |
133
|
|
|
if(is_array($requiredFields)) { |
134
|
|
|
foreach($requiredFields as $field) { |
135
|
|
|
$value = $this->$field; |
136
|
|
|
if(! $value) { |
137
|
|
|
$fieldWithoutID = $field; |
138
|
|
|
if(substr($fieldWithoutID, -2) === 'ID') { |
139
|
|
|
$fieldWithoutID = substr($fieldWithoutID, 0, -2); |
140
|
|
|
} |
141
|
|
|
$myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID; |
142
|
|
|
$result->error( |
143
|
|
|
_t( |
144
|
|
|
'ImageWithStyle.'.$field.'_REQUIRED', |
145
|
|
|
$myName.' is required' |
146
|
|
|
), |
147
|
|
|
'REQUIRED_ImageWithStyle_'.$field |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') { |
151
|
|
|
$id = (empty($this->ID) ? 0 : $this->ID); |
152
|
|
|
$count = ImageWithStyle::get() |
153
|
|
|
->filter(array($field => $value)) |
154
|
|
|
->exclude(array('ID' => $id)) |
155
|
|
|
->count(); |
156
|
|
|
if($count > 0) { |
157
|
|
|
$myName = $fieldLabels['$field']; |
158
|
|
|
$result->error( |
159
|
|
|
_t( |
160
|
|
|
'ImageWithStyle.'.$field.'_UNIQUE', |
161
|
|
|
$myName.' needs to be unique' |
162
|
|
|
), |
163
|
|
|
'UNIQUE_ImageWithStyle_'.$field |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $result; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function onBeforeWrite() |
174
|
|
|
{ |
175
|
|
|
parent::onBeforeWrite(); |
176
|
|
|
//... |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function onAfterWrite() |
180
|
|
|
{ |
181
|
|
|
parent::onAfterWrite(); |
182
|
|
|
//... |
183
|
|
|
$image = $this->Image(); |
|
|
|
|
184
|
|
|
if($image && $image->exists()) { |
185
|
|
|
$image->Title = $this->Title; |
|
|
|
|
186
|
|
|
$image->write(); |
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("tba"); |
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("tba"); |
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
|
|
|
|
233
|
|
|
//do first?? |
234
|
|
|
$rightFieldDescriptions = $this->Config()->get('field_labels_right'); |
235
|
|
View Code Duplication |
foreach($rightFieldDescriptions as $field => $desc) { |
|
|
|
|
236
|
|
|
$formField = $fields->DataFieldByName($field); |
237
|
|
|
if(! $formField) { |
238
|
|
|
$formField = $fields->DataFieldByName($field.'ID'); |
239
|
|
|
} |
240
|
|
|
if($formField) { |
241
|
|
|
$formField->setDescription($desc); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
//... |
245
|
|
|
if($this->hasRealStyle()) { |
246
|
|
|
$style = $this->Style(); |
|
|
|
|
247
|
|
|
$varsArray = $this->Config()->get('unique_vars'); |
248
|
|
|
//var_dump($this->Style()); |
|
|
|
|
249
|
|
|
foreach($varsArray as $varName){ |
250
|
|
|
if($this->Style()->hasStyleVariable($varName)){ |
|
|
|
|
251
|
|
|
$name = $varName . 'Name'; |
252
|
|
|
$type = $varName . 'Type'; |
253
|
|
|
$descriptionField = $varName . 'Description'; |
254
|
|
|
switch ($this->Style()->$type){ |
|
|
|
|
255
|
|
|
case 'Pixels': |
256
|
|
|
case 'Percentage': |
257
|
|
|
$newField = NumericField::create($varName, $style->$name) |
258
|
|
|
->setRightTitle($this->Style()->$descriptionField.' ('.$style->$type.'). Negative number are allowed in some instances.'); |
|
|
|
|
259
|
|
|
break; |
260
|
|
|
case 'Options': |
261
|
|
|
$newField = OptionSetField::create($varName, $style->$name, $this->Style()->OptionsAsArray($varName)) |
|
|
|
|
262
|
|
|
->setRightTitle($this->Style()->$descriptionField); |
|
|
|
|
263
|
|
|
break; |
264
|
|
|
} |
265
|
|
|
$fields->replaceField( |
266
|
|
|
$varName, |
267
|
|
|
$newField |
|
|
|
|
268
|
|
|
); |
269
|
|
|
} else { |
270
|
|
|
$fields->removeByName($varName); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
if($style->ClassNameForCSS) { |
274
|
|
|
$fields->replaceField( |
275
|
|
|
'Image', |
276
|
|
|
$uploadField = PerfectCMSImagesUploadField::create('Image', 'Image', null, $style->ClassNameForCSS) |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
} else { |
280
|
|
|
$varsArray = $this->Config()->get('unique_vars'); |
281
|
|
|
foreach($varsArray as $var){ |
282
|
|
|
$fields->removeByName($var); |
283
|
|
|
} |
284
|
|
|
$fields->replaceField( |
285
|
|
|
'Image', |
286
|
|
|
$uploadField = LiteralField::create('ImageReminder', '<h2>Image Upload</h2><p>Please select a style first (see below) and save this record. After that you can upload an image</p>') |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$fields->replaceField( |
291
|
|
|
'Selections', |
292
|
|
|
CheckboxSetField::create( |
293
|
|
|
'Selections', |
294
|
|
|
'Included in ...', |
295
|
|
|
ImagesWithStyleSelection::get()->map() |
296
|
|
|
) |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
return $fields; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return string (HTML) |
|
|
|
|
304
|
|
|
*/ |
305
|
|
|
public function ImageElement() |
306
|
|
|
{ |
307
|
|
|
return $this->getImageElement(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return string (HTML) |
|
|
|
|
313
|
|
|
*/ |
314
|
|
|
public function getImageElement() |
315
|
|
|
{ |
316
|
|
|
$html = ''; |
317
|
|
|
if($this->ImageID && $this->hasRealStyle()){ |
|
|
|
|
318
|
|
|
$image = $this->Image(); |
|
|
|
|
319
|
|
|
if($image && $image->exists()) { |
320
|
|
|
$style = $this->Style(); |
|
|
|
|
321
|
|
|
$array = [ |
322
|
|
|
'Styles' => $this->buildStyles(), |
323
|
|
|
'ClassNameForCSS' => $style->ClassNameForCSS, |
324
|
|
|
'ImageTag' => $image->PerfectCMSImageTag($style->ClassNameForCSS), |
325
|
|
|
'Caption' => $this->Description, |
|
|
|
|
326
|
|
|
'ImageObject' => $this->Image() |
|
|
|
|
327
|
|
|
]; |
328
|
|
|
return ArrayData::create($array)->renderWith('ImageWithStyle'); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
return $html; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return string (HTML) |
336
|
|
|
*/ |
337
|
|
|
public function buildStyles() |
338
|
|
|
{ |
339
|
|
|
$styles = ''; |
340
|
|
|
if($this->hasRealStyle()) { |
341
|
|
|
$style = $this->Style(); |
|
|
|
|
342
|
|
|
$stylesArray = []; |
343
|
|
|
$varsArray = $this->Config()->get('unique_vars');; |
|
|
|
|
344
|
|
|
//var_dump($this->Style()); |
|
|
|
|
345
|
|
|
foreach($varsArray as $var){ |
346
|
|
|
if($this->$var){ |
347
|
|
|
$name = $var . 'Name'; |
348
|
|
|
$type = $var . 'Type'; |
349
|
|
|
$styleString = $style->$name . ': ' . $this->$var . $this->convertVarTypeToCSS($style->$type) . ';'; |
350
|
|
|
array_push($stylesArray, $styleString); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
if(! empty($stylesArray)){ |
354
|
|
|
$styles = ' style="'.implode(" ", $stylesArray).'"'; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
return $styles; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
protected function hasRealStyle() |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
if($this->StyleID) { |
|
|
|
|
363
|
|
|
$style = $this->Style(); |
|
|
|
|
364
|
|
|
return $style->exists(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param string |
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
|
|
protected function convertVarTypeToCSS($type) |
375
|
|
|
{ |
376
|
|
|
$str = ''; |
377
|
|
|
switch ($type){ |
378
|
|
|
case 'Pixels': |
379
|
|
|
$str = 'px'; |
380
|
|
|
break; |
381
|
|
|
case 'Percentage': |
382
|
|
|
$str = '%'; |
383
|
|
|
break; |
384
|
|
|
case 'Options': |
385
|
|
|
break; |
386
|
|
|
} |
387
|
|
|
return $str; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
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.