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
|
|
|
public 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
|
|
|
public 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
|
|
|
public 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
|
|
|
if(!$this->StyleID) { |
|
|
|
|
178
|
|
|
$this->StyleID = $defaultStyle->ID; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function onAfterWrite() |
183
|
|
|
{ |
184
|
|
|
parent::onAfterWrite(); |
185
|
|
|
//... |
186
|
|
|
$image = $this->Image(); |
|
|
|
|
187
|
|
|
if ($image && $image->exists()) { |
188
|
|
|
$image->Title = $this->Title; |
|
|
|
|
189
|
|
|
$image->write(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function requireDefaultRecords() |
194
|
|
|
{ |
195
|
|
|
parent::requireDefaultRecords(); |
196
|
|
|
//... |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
####################### |
201
|
|
|
### Import / Export Section |
202
|
|
|
####################### |
203
|
|
|
|
204
|
|
|
public function getExportFields() |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
//.. |
207
|
|
|
return parent::getExportFields(); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
|
212
|
|
|
####################### |
213
|
|
|
### CMS Edit Section |
214
|
|
|
####################### |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
public function CMSEditLink() |
218
|
|
|
{ |
219
|
|
|
$controller = singleton("tba"); |
220
|
|
|
|
221
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit"; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function CMSAddLink() |
225
|
|
|
{ |
226
|
|
|
$controller = singleton("tba"); |
227
|
|
|
|
228
|
|
|
return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new"; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
public function getCMSFields() |
233
|
|
|
{ |
234
|
|
|
$fields = parent::getCMSFields(); |
235
|
|
|
|
236
|
|
|
//do first?? |
237
|
|
|
$rightFieldDescriptions = $this->Config()->get('field_labels_right'); |
238
|
|
View Code Duplication |
foreach ($rightFieldDescriptions as $field => $desc) { |
|
|
|
|
239
|
|
|
$formField = $fields->DataFieldByName($field); |
240
|
|
|
if (! $formField) { |
241
|
|
|
$formField = $fields->DataFieldByName($field.'ID'); |
242
|
|
|
} |
243
|
|
|
if ($formField) { |
244
|
|
|
$formField->setDescription($desc); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
//... |
248
|
|
|
if ($this->hasRealStyle()) { |
249
|
|
|
$style = $this->Style(); |
|
|
|
|
250
|
|
|
$varsArray = $this->Config()->get('unique_vars'); |
251
|
|
|
//var_dump($this->Style()); |
|
|
|
|
252
|
|
|
foreach ($varsArray as $varName) { |
253
|
|
|
if ($this->Style()->hasStyleVariable($varName)) { |
|
|
|
|
254
|
|
|
$name = $varName . 'Name'; |
255
|
|
|
$type = $varName . 'Type'; |
256
|
|
|
$descriptionField = $varName . 'Description'; |
257
|
|
|
switch ($this->Style()->$type) { |
|
|
|
|
258
|
|
|
case 'Pixels': |
259
|
|
|
case 'Percentage': |
260
|
|
|
$newField = NumericField::create($varName, $style->$name) |
261
|
|
|
->setRightTitle($this->Style()->$descriptionField.' ('.$style->$type.'). Negative number are allowed in some instances.'); |
|
|
|
|
262
|
|
|
break; |
263
|
|
|
case 'Options': |
264
|
|
|
$newField = OptionSetField::create($varName, $style->$name, $this->Style()->OptionsAsArray($varName)) |
|
|
|
|
265
|
|
|
->setRightTitle($this->Style()->$descriptionField); |
|
|
|
|
266
|
|
|
break; |
267
|
|
|
} |
268
|
|
|
$fields->replaceField( |
269
|
|
|
$varName, |
270
|
|
|
$newField |
|
|
|
|
271
|
|
|
); |
272
|
|
|
} else { |
273
|
|
|
$fields->removeByName($varName); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
if ($style->ClassNameForCSS) { |
277
|
|
|
$fields->replaceField( |
278
|
|
|
'Image', |
279
|
|
|
$uploadField = PerfectCMSImagesUploadField::create('Image', 'Image', null, $style->ClassNameForCSS) |
280
|
|
|
); |
281
|
|
|
$uploadField->setFolderName($this->BestFolder()); |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
|
|
$varsArray = $this->Config()->get('unique_vars'); |
285
|
|
|
foreach ($varsArray as $var) { |
286
|
|
|
$fields->removeByName($var); |
287
|
|
|
} |
288
|
|
|
$fields->replaceField( |
289
|
|
|
'Image', |
290
|
|
|
$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>') |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$fields->replaceField( |
295
|
|
|
'Selections', |
296
|
|
|
CheckboxSetField::create( |
297
|
|
|
'Selections', |
298
|
|
|
'Included in ...', |
299
|
|
|
ImagesWithStyleSelection::get()->map() |
300
|
|
|
) |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
return $fields; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return string (HTML) |
|
|
|
|
308
|
|
|
*/ |
309
|
|
|
public function ImageElement() |
310
|
|
|
{ |
311
|
|
|
return $this->getImageElement(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return string (HTML) |
|
|
|
|
317
|
|
|
*/ |
318
|
|
|
public function getImageElement() |
319
|
|
|
{ |
320
|
|
|
$html = ''; |
321
|
|
|
if ($this->ImageID && $this->hasRealStyle()) { |
|
|
|
|
322
|
|
|
$image = $this->Image(); |
|
|
|
|
323
|
|
|
if ($image && $image->exists()) { |
324
|
|
|
$style = $this->Style(); |
|
|
|
|
325
|
|
|
$array = [ |
326
|
|
|
'Styles' => $this->buildStyles(), |
327
|
|
|
'ClassNameForCSS' => $style->ClassNameForCSS, |
328
|
|
|
'ImageTag' => $image->PerfectCMSImageTag($style->ClassNameForCSS), |
329
|
|
|
'Caption' => $this->Description, |
|
|
|
|
330
|
|
|
'ImageObject' => $this->Image() |
|
|
|
|
331
|
|
|
]; |
332
|
|
|
return ArrayData::create($array)->renderWith('ImageWithStyle'); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
return $html; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @return string (HTML) |
340
|
|
|
*/ |
341
|
|
|
public function buildStyles() |
342
|
|
|
{ |
343
|
|
|
$styles = ''; |
344
|
|
|
if ($this->hasRealStyle()) { |
345
|
|
|
$style = $this->Style(); |
|
|
|
|
346
|
|
|
$stylesArray = []; |
347
|
|
|
$varsArray = $this->Config()->get('unique_vars'); |
348
|
|
|
; |
349
|
|
|
//var_dump($this->Style()); |
|
|
|
|
350
|
|
|
foreach ($varsArray as $var) { |
351
|
|
|
if ($this->$var) { |
352
|
|
|
$name = $var . 'Name'; |
353
|
|
|
$type = $var . 'Type'; |
354
|
|
|
$styleString = $style->$name . ': ' . $this->$var . $this->convertVarTypeToCSS($style->$type) . ';'; |
355
|
|
|
array_push($stylesArray, $styleString); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
if (! empty($stylesArray)) { |
359
|
|
|
$styles = ' style="'.implode(" ", $stylesArray).'"'; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
return $styles; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
protected function hasRealStyle() |
|
|
|
|
366
|
|
|
{ |
367
|
|
|
if ($this->StyleID) { |
|
|
|
|
368
|
|
|
$style = $this->Style(); |
|
|
|
|
369
|
|
|
return $style->exists(); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return false; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param string |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
protected function convertVarTypeToCSS($type) |
380
|
|
|
{ |
381
|
|
|
$str = ''; |
382
|
|
|
switch ($type) { |
383
|
|
|
case 'Pixels': |
384
|
|
|
$str = 'px'; |
385
|
|
|
break; |
386
|
|
|
case 'Percentage': |
387
|
|
|
$str = '%'; |
388
|
|
|
break; |
389
|
|
|
case 'Options': |
390
|
|
|
break; |
391
|
|
|
} |
392
|
|
|
return $str; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function BestFolder() |
396
|
|
|
{ |
397
|
|
|
if($this->Selections()->count()) { |
|
|
|
|
398
|
|
|
foreach($this->Selections() as $selection) { |
|
|
|
|
399
|
|
|
if($folder = $selection->PlaceToStoreImages()) { |
400
|
|
|
if($folder->FileName) { |
401
|
|
|
return trim( |
402
|
|
|
str_replace(ASSETS_DIR, '', $folder->Filename), |
403
|
|
|
'/' |
404
|
|
|
); |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
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.