|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Model\File; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
|
6
|
|
|
use SilverStripe\Assets\Folder; |
|
7
|
|
|
use SilverStripe\Control\Director; |
|
8
|
|
|
use SilverStripe\Core\Convert; |
|
9
|
|
|
use SilverStripe\ORM\DataExtension; |
|
10
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
11
|
|
|
use SilverStripe\View\ArrayData; |
|
12
|
|
|
use Sunnysideup\PerfectCmsImages\Api\ImageManipulations; |
|
13
|
|
|
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* defines the image sizes |
|
17
|
|
|
* and default upload folder. |
|
18
|
|
|
*/ |
|
19
|
|
|
class PerfectCmsImageDataExtension extends DataExtension |
|
20
|
|
|
{ |
|
21
|
|
|
private static $perfect_cms_images_background_padding_color = '#ffffff'; |
|
22
|
|
|
|
|
23
|
|
|
private static $perfect_cms_images_image_definitions = []; |
|
24
|
|
|
|
|
25
|
|
|
private static $casting = [ |
|
26
|
|
|
'PerfectCMSImageTag' => 'HTMLText', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $name PerfectCMSImages name |
|
31
|
|
|
* @param bool $inline for use within existing image tag - optional |
|
32
|
|
|
* @param string $alt alt tag for image -optional |
|
33
|
|
|
* @param string $attributes additional attributes |
|
34
|
|
|
* |
|
35
|
|
|
* @return string (HTML) |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getPerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->PerfectCMSImageTag($name, $inline, $alt, $attributes); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $name PerfectCMSImages name |
|
44
|
|
|
* @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
|
45
|
|
|
* @param string $alt alt tag for image -optional |
|
46
|
|
|
* @param string $attributes additional attributes |
|
47
|
|
|
* |
|
48
|
|
|
* @return string (HTML) |
|
49
|
|
|
*/ |
|
50
|
|
|
public function PerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
|
51
|
|
|
{ |
|
52
|
|
|
$arrayData = $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); |
|
53
|
|
|
$template = 'Includes/PerfectCMSImageTag'; |
|
54
|
|
|
if (true === $inline || 1 === (int) $inline || 'true' === strtolower($inline)) { |
|
|
|
|
|
|
55
|
|
|
$template .= 'Inline'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return DBField::create_field('HTMLText', $arrayData->renderWith($template)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $name PerfectCMSImages name |
|
63
|
|
|
* @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
|
64
|
|
|
* @param string $alt alt tag for image -optional |
|
65
|
|
|
* @param string $attributes additional attributes |
|
66
|
|
|
* |
|
67
|
|
|
* @return ArrayData |
|
68
|
|
|
*/ |
|
69
|
|
|
public function PerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $name PerfectCMSImages name |
|
76
|
|
|
* @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
|
77
|
|
|
* @param string $alt alt tag for image -optional |
|
78
|
|
|
* @param string $attributes additional attributes |
|
79
|
|
|
* |
|
80
|
|
|
* @return ArrayData |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getPerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$retinaLink = $this->PerfectCMSImageLinkRetina($name); |
|
85
|
|
|
$nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name); |
|
86
|
|
|
|
|
87
|
|
|
$retinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebP($name); |
|
88
|
|
|
$nonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebP($name); |
|
89
|
|
|
|
|
90
|
|
|
$mobileRetinaLink = $this->PerfectCMSImageLinkRetinaForMobile($name); |
|
91
|
|
|
$mobileNonRetinaLink = $this->PerfectCMSImageLinkNonRetinaForMobile($name); |
|
92
|
|
|
|
|
93
|
|
|
$mobileRetinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebPForMobile($name); |
|
94
|
|
|
$mobileNonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebPForMobile($name); |
|
95
|
|
|
|
|
96
|
|
|
$width = PerfectCMSImages::get_width($name, true); |
|
97
|
|
|
$height = PerfectCMSImages::get_height($name, true); |
|
98
|
|
|
$loadingStyle = PerfectCMSImages::loading_style($name); |
|
99
|
|
|
$mobileMediaWidth = PerfectCMSImages::get_mobile_media_width($name); |
|
100
|
|
|
|
|
101
|
|
|
if (! $alt) { |
|
102
|
|
|
$alt = $this->getOwner()->Title; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return ArrayData::create( |
|
106
|
|
|
[ |
|
107
|
|
|
'MobileMediaWidth' => $mobileMediaWidth, |
|
108
|
|
|
'Width' => $width, |
|
109
|
|
|
'Height' => $height, |
|
110
|
|
|
'Alt' => Convert::raw2att($alt), |
|
111
|
|
|
'MobileRetinaLink' => $mobileRetinaLink, |
|
112
|
|
|
'MobileNonRetinaLink' => $mobileNonRetinaLink, |
|
113
|
|
|
'MobileRetinaLinkWebP' => $mobileRetinaLinkWebP, |
|
114
|
|
|
'MobileNonRetinaLinkWebP' => $mobileNonRetinaLinkWebP, |
|
115
|
|
|
'RetinaLink' => $retinaLink, |
|
116
|
|
|
'NonRetinaLink' => $nonRetinaLink, |
|
117
|
|
|
'RetinaLinkWebP' => $retinaLinkWebP, |
|
118
|
|
|
'NonRetinaLinkWebP' => $nonRetinaLinkWebP, |
|
119
|
|
|
'LoadingStyle' => $loadingStyle, |
|
120
|
|
|
'Attributes' => DBField::create_field('HTMLText', $attributes), |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name of Image Field template |
|
127
|
|
|
* |
|
128
|
|
|
* @return string (link) |
|
129
|
|
|
*/ |
|
130
|
|
|
public function PerfectCMSImageLinkNonRetina(string $name): string |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->PerfectCMSImageLink($name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $name of Image Field template |
|
137
|
|
|
* |
|
138
|
|
|
* @return string (link) |
|
139
|
|
|
*/ |
|
140
|
|
|
public function PerfectCMSImageLinkRetina(string $name): string |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->PerfectCMSImageLink($name, true); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $name of Image Field template |
|
147
|
|
|
* |
|
148
|
|
|
* @return string (link) |
|
149
|
|
|
*/ |
|
150
|
|
|
public function PerfectCMSImageLinkNonRetinaWebP(string $name): string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->PerfectCMSImageLink($name, false, true); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $name of Image Field template |
|
157
|
|
|
* |
|
158
|
|
|
* @return string (link) |
|
159
|
|
|
*/ |
|
160
|
|
|
public function PerfectCMSImageLinkRetinaWebP(string $name): string |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->PerfectCMSImageLink($name, true, true); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $name of Image Field template |
|
167
|
|
|
* |
|
168
|
|
|
* @return string (link) |
|
169
|
|
|
*/ |
|
170
|
|
|
public function PerfectCMSImageLinkNonRetinaForMobile(string $name): string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->PerfectCMSImageLink($name, false, false, true); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $name of Image Field template |
|
177
|
|
|
* |
|
178
|
|
|
* @return string (link) |
|
179
|
|
|
*/ |
|
180
|
|
|
public function PerfectCMSImageLinkRetinaForMobile(string $name): string |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->PerfectCMSImageLink($name, true, false, true); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $name of Image Field template |
|
187
|
|
|
* |
|
188
|
|
|
* @return string (link) |
|
189
|
|
|
*/ |
|
190
|
|
|
public function PerfectCMSImageLinkNonRetinaWebPForMobile(string $name): string |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->PerfectCMSImageLink($name, false, true, true); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $name of Image Field template |
|
197
|
|
|
* |
|
198
|
|
|
* @return string (link) |
|
199
|
|
|
*/ |
|
200
|
|
|
public function PerfectCMSImageLinkRetinaWebPForMobile(string $name): string |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->PerfectCMSImageLink($name, true, true, true); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param string $link |
|
207
|
|
|
* |
|
208
|
|
|
* @return string (link) |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getPerfectCMSImageAbsoluteLink(string $link): string |
|
211
|
|
|
{ |
|
212
|
|
|
return Director::absoluteURL($link); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* returns image link (if any). |
|
217
|
|
|
*/ |
|
218
|
|
|
public function PerfectCMSImageLink(string $name, ?bool $useRetina = false, ?bool $isWebP = false, ?bool $forMobile = false): string |
|
219
|
|
|
{ |
|
220
|
|
|
/** @var null|Image $image */ |
|
221
|
|
|
$image = $this->owner; |
|
222
|
|
|
if ($image && $image->exists() && $image instanceof Image) { |
|
223
|
|
|
//we are all good ... |
|
224
|
|
|
} else { |
|
225
|
|
|
$image = ImageManipulations::get_backup_image($name); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if ($image && $image->exists() && $image instanceof Image) { |
|
229
|
|
|
// $backEndString = Image::get_backend(); |
|
230
|
|
|
// $backend = Injector::inst()->get($backEndString); |
|
231
|
|
|
$link = ImageManipulations::get_image_link($image, $name, $useRetina, $forMobile); |
|
232
|
|
|
|
|
233
|
|
|
if ($isWebP) { |
|
234
|
|
|
$link = ImageManipulations::web_p_link($link); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return '' !== $link ? ImageManipulations::add_fake_parts($image, $link) : ''; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// no image -> provide placeholder if in DEV MODE only!!! |
|
241
|
|
|
if (Director::isDev()) { |
|
242
|
|
|
return ImageManipulations::get_placeholder_image_tag($name); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return ''; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
protected function PerfectCMSImageFixFolder($name, ?string $folderName): ?Folder |
|
249
|
|
|
{ |
|
250
|
|
|
$folder = null; |
|
251
|
|
|
if(PerfectCMSImages::move_to_right_folder($name) || $folderName) { |
|
252
|
|
|
$image = $this->getOwner(); |
|
253
|
|
|
if(! $folderName) { |
|
254
|
|
|
$folderName = PerfectCMSImages::get_folder($name); |
|
255
|
|
|
} |
|
256
|
|
|
$folder = Folder::find_or_make($folderName); |
|
257
|
|
|
if(!$folder->exists()) { |
|
258
|
|
|
$folder->write(); |
|
259
|
|
|
} |
|
260
|
|
|
if ($image && $image->exists() && $image->ParentID !== $folder->ID) { |
|
261
|
|
|
$isPublished = $image->isPublished(); |
|
262
|
|
|
$image->ParentID = $folder->ID; |
|
263
|
|
|
$image->write(); |
|
264
|
|
|
if($isPublished) { |
|
265
|
|
|
$image->publishRecursive(); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
return $folder; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|