1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Donatas Navidonskis <[email protected]> |
5
|
|
|
* @since 2017 |
6
|
|
|
* @class VideoBlock |
7
|
|
|
* |
8
|
|
|
* @property int CoverID |
9
|
|
|
* @property int Mp4ID |
10
|
|
|
* @property int WebMID |
11
|
|
|
* @property int OggID |
12
|
|
|
* @property string Type |
13
|
|
|
* @property string URL |
14
|
|
|
* @property boolean AutoPlay |
15
|
|
|
* |
16
|
|
|
* @method File Mp4 |
17
|
|
|
* @method File WebM |
18
|
|
|
* @method File Ogg |
19
|
|
|
* @method Image Cover |
20
|
|
|
*/ |
21
|
|
|
class VideoBlock extends BaseBlock { |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
* @config |
26
|
|
|
*/ |
27
|
|
|
private static $db = [ |
|
|
|
|
28
|
|
|
'Type' => 'Enum(array("Youtube", "Vimeo", "File"), "File")', |
29
|
|
|
'URL' => 'Varchar(1024)', |
30
|
|
|
'AutoPlay' => 'Boolean(true)', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
* @config |
36
|
|
|
*/ |
37
|
|
|
private static $has_one = [ |
|
|
|
|
38
|
|
|
'Mp4' => 'File', |
39
|
|
|
'WebM' => 'File', |
40
|
|
|
'Ogg' => 'File', |
41
|
|
|
'Cover' => 'Image', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Allow to call those functions. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
* @config |
49
|
|
|
*/ |
50
|
|
|
private static $better_buttons_actions = [ |
51
|
|
|
'fetchVideosPicture' |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Load javascript plugin to load all block features. Set to false |
56
|
|
|
* and add yours. This will load /assets/javascript/video-block.js file. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
* @config |
60
|
|
|
*/ |
61
|
|
|
private static $load_javascript_plugin = true; |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* If the singular name is set in a private static $singular_name, it cannot be changed using the translation files |
65
|
|
|
* for some reason. Fix it by defining a method that handles the translation. |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function singular_name() { |
69
|
|
|
return _t('VideoBlock.SINGULARNAME', 'Video Block'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* If the plural name is set in a private static $plural_name, it cannot be changed using the translation files |
74
|
|
|
* for some reason. Fix it by defining a method that handles the translation. |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function plural_name() { |
78
|
|
|
return _t('VideoBlock.PLURALNAME', 'Video Blocks'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getVideoType() { |
85
|
|
|
return strtolower($this->Type); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function getVideoTypes() { |
|
|
|
|
92
|
|
|
$types = []; |
93
|
|
|
|
94
|
|
|
foreach ($this->dbObject('Type')->enumValues() as $type) { |
95
|
|
|
$types[$type] = $this->fieldLabel($type); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $types; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return FieldList |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function getCMSFields() { |
|
|
|
|
105
|
|
|
$fields = parent::getCMSFields(); |
106
|
|
|
$fields->removeByName(['Type', 'Mp4', 'WebM', 'Ogg', 'URL', 'AutoPlay', 'Cover', 'Content']); |
107
|
|
|
$fields->findOrMakeTab('Root.Media', $this->fieldLabel('Media')); |
108
|
|
|
|
109
|
|
|
$fields->addFieldsToTab('Root.Media', [ |
110
|
|
|
$coverField = UploadField::create('Cover', $this->fieldLabel('Cover')), |
111
|
|
|
DropdownField::create('AutoPlay', $this->fieldLabel('TurnOnAutoPlayMode'), BlocksUtility::localized_answers()), |
112
|
|
|
$videoType = OptionsetField::create('Type', $this->fieldLabel('Type'), $this->getVideoTypes(), 'File'), |
113
|
|
|
$uploadFieldContainer = DisplayLogicWrapper::create( |
114
|
|
|
$mp4UploadField = UploadField::create('Mp4', $this->fieldLabel('VideoMp4')), |
115
|
|
|
$webMUploadField = UploadField::create('WebM', $this->fieldLabel('VideoWebM')), |
116
|
|
|
$oggUploadField = UploadField::create('Ogg', $this->fieldLabel('VideoOgg')) |
117
|
|
|
), |
118
|
|
|
$urlAddressField = TextField::create('URL', $this->fieldLabel('URLAddress'))->setRightTitle( |
119
|
|
|
$this->fieldLabel('SetVideoURLAddress') |
120
|
|
|
), |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$coverField |
124
|
|
|
->setAllowedMaxFileNumber(1) |
125
|
|
|
->setAllowedFileCategories('image') |
126
|
|
|
->setRightTitle( |
127
|
|
|
_t('VideoSliderItem.SET_VIDEO_COVER_IMAGE', 'Set video cover image') |
128
|
|
|
)->setFolderName(sprintf('%s/covers', BaseBlock::config()->upload_directory)); |
129
|
|
|
|
130
|
|
|
$mp4UploadField |
131
|
|
|
->setAllowedMaxFileNumber(1) |
132
|
|
|
->setAllowedExtensions('mp4') |
|
|
|
|
133
|
|
|
->setRightTitle( |
134
|
|
|
_t('VideoSliderItem.ALLOWED_FILE_EXTENSIONS', 'Allowed file extensions: {extensions}', [ |
|
|
|
|
135
|
|
|
'extensions' => '.mp4', |
136
|
|
|
]) |
137
|
|
|
)->setFolderName(sprintf('%s/videos', BaseBlock::config()->upload_directory)); |
138
|
|
|
|
139
|
|
|
$webMUploadField |
140
|
|
|
->setAllowedMaxFileNumber(1) |
141
|
|
|
->setAllowedExtensions('webm') |
|
|
|
|
142
|
|
|
->setRightTitle( |
143
|
|
|
_t('VideoSliderItem.ALLOWED_FILE_EXTENSIONS', 'Allowed file extensions: {extensions}', [ |
|
|
|
|
144
|
|
|
'extensions' => '.webm', |
145
|
|
|
]) |
146
|
|
|
)->setFolderName(sprintf('%s/videos', BaseBlock::config()->upload_directory)); |
147
|
|
|
|
148
|
|
|
$oggUploadField |
149
|
|
|
->setAllowedMaxFileNumber(1) |
150
|
|
|
->setAllowedExtensions('ogg') |
|
|
|
|
151
|
|
|
->setRightTitle( |
152
|
|
|
_t('VideoSliderItem.ALLOWED_FILE_EXTENSIONS', 'Allowed file extensions: {extensions}', [ |
|
|
|
|
153
|
|
|
'extensions' => '.ogg', |
154
|
|
|
]) |
155
|
|
|
)->setFolderName(sprintf('%s/videos', BaseBlock::config()->upload_directory)); |
156
|
|
|
|
157
|
|
|
$uploadFieldContainer->displayIf('Type')->isEqualTo('File'); |
158
|
|
|
$urlAddressField->displayIf('Type')->isNotEqualTo('File'); |
159
|
|
|
|
160
|
|
|
$this->extend('updateCMSFields', $fields); |
161
|
|
|
|
162
|
|
|
return $fields; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param bool $includeRelations |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
public function fieldLabels($includeRelations = true) { |
171
|
|
|
return array_merge(parent::fieldLabels($includeRelations), VideoSliderItem::labels()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* This will get an id of the URL address or false |
176
|
|
|
* if can't parsed, object type not one of supported |
177
|
|
|
* providers or just empty url address field. |
178
|
|
|
* |
179
|
|
|
* @return string|false |
180
|
|
|
* @throws ProviderNotFound |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function getVideoId() { |
|
|
|
|
183
|
|
|
if (! empty($this->URL) && $this->Type != 'File') { |
184
|
|
|
$videoId = BlocksUtility::parse_video_id($this->URL, $this->Type); |
185
|
|
|
|
186
|
|
|
return $videoId; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get embed link by the set of Type field. Method depends by |
194
|
|
|
* VideoSliderItem::$embed_links property. |
195
|
|
|
* |
196
|
|
|
* @return bool|string |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function getEmbedLink() { |
|
|
|
|
199
|
|
|
if (! empty($this->URL) && $this->Type != 'File') { |
200
|
|
|
try { |
201
|
|
|
$videoId = BlocksUtility::parse_video_id($this->URL, $this->Type); |
202
|
|
|
} catch (ProviderNotFound $ex) { |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($videoId && array_key_exists($this->Type, ($options = VideoSliderItem::config()->embed_links))) { |
207
|
|
|
$options = $options[$this->Type]; |
208
|
|
|
$autoPlay = array_key_exists("AutoPlay", $options) && ! empty($options["AutoPlay"]) ? $options["AutoPlay"] : ''; |
209
|
|
|
|
210
|
|
|
return str_replace( |
211
|
|
|
['{VideoId}', '{AutoPlay}'], |
212
|
|
|
[$videoId, $autoPlay], |
213
|
|
|
$options["Link"] |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return \ValidationResult |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
protected function validate() { |
|
|
|
|
225
|
|
|
$validation = parent::validate(); |
226
|
|
|
|
227
|
|
|
if (! empty($this->URL) && $this->Type != 'File') { |
228
|
|
|
try { |
229
|
|
|
$result = $this->getVideoId(); |
230
|
|
|
} catch (ProviderNotFound $ex) { |
231
|
|
|
$validation->error($ex->getMessage()); |
232
|
|
|
|
233
|
|
|
return $validation; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// if we can't parse url address, return an error with bad url address or |
237
|
|
|
// the type is not of the url address providers. |
238
|
|
|
if (! $result) { |
|
|
|
|
239
|
|
|
$validation->error(_t('VideoSliderItem.INVALID_URL_ADDRESS_OR_THE_TYPE', 'Invalid URL address or the type')); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $validation; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return Image|false |
248
|
|
|
*/ |
249
|
|
|
public function getCoverImage() { |
250
|
|
|
if ($this->Cover()->exists()) { |
|
|
|
|
251
|
|
|
return $this->Cover(); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Creating a button to fetch videos picture if cover image not exists. |
259
|
|
|
* |
260
|
|
|
* @return FieldList |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function getBetterButtonsActions() { |
|
|
|
|
263
|
|
|
$fields = parent::getBetterButtonsActions(); |
|
|
|
|
264
|
|
|
|
265
|
|
|
if ($this->Type != 'File' && ! $this->Cover()->exists() && ! empty($this->URL)) { |
|
|
|
|
266
|
|
|
$fields->push(BetterButtonCustomAction::create('fetchVideosPicture', _t('VideoSliderItem.FETCH_VIDEOS_PICTURE', 'Fetch videos picture'))); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $fields; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return bool|string |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function getMp4VideoUrl() { |
|
|
|
|
276
|
|
|
$file = $this->Mp4(); |
|
|
|
|
277
|
|
|
|
278
|
|
|
if (! ($file instanceof File) || ! $file->exists()) { |
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $file->getAbsoluteURL(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return bool|string |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function getWebMVideoUrl() { |
|
|
|
|
289
|
|
|
$file = $this->WebM(); |
|
|
|
|
290
|
|
|
|
291
|
|
|
if (! ($file instanceof File) || ! $file->exists()) { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $file->getAbsoluteURL(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return bool|string |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function getOggVideoUrl() { |
|
|
|
|
302
|
|
|
$file = $this->Ogg(); |
|
|
|
|
303
|
|
|
|
304
|
|
|
if (! ($file instanceof File) || ! $file->exists()) { |
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $file->getAbsoluteURL(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Fetching/downloading picture from the providers url address and |
313
|
|
|
* saving as Image object. |
314
|
|
|
* |
315
|
|
|
* @return false |
316
|
|
|
*/ |
317
|
|
|
public function fetchVideosPicture() { |
318
|
|
|
try { |
319
|
|
|
$videoId = $this->getVideoId(); |
320
|
|
|
} catch (ProviderNotFound $ex) { |
321
|
|
|
return false; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$directoryPath = sprintf("%s/Sliders", BaseBlock::config()->upload_directory); |
325
|
|
|
$folder = Folder::find_or_make($directoryPath); |
326
|
|
|
|
327
|
|
|
if (empty($this->URL)) { |
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$title = ! empty($this->Title) ? FileNameFilter::create()->filter($this->Title)."-{$this->ID}" : "video-{$this->ID}"; |
332
|
|
|
$fileName = strtolower(sprintf("%s.jpg", $title)); |
333
|
|
|
$baseFolder = Director::baseFolder()."/".$folder->getFilename(); |
334
|
|
|
$type = strtolower($this->Type); |
335
|
|
|
$fileContent = file_get_contents(str_replace('{VideoId}', $videoId, VideoSliderItem::config()->thumbnail_links[$type])); |
336
|
|
|
|
337
|
|
|
switch ($type) { |
338
|
|
|
case 'vimeo': |
339
|
|
|
$fileContent = unserialize($fileContent); |
340
|
|
|
$fileContent = file_get_contents($fileContent[0]['thumbnail_large']); |
341
|
|
|
break; |
342
|
|
|
case 'file': |
343
|
|
|
// get picture from video (via ffmpeg) |
344
|
|
|
if ($this->Mp4()->exists() && ($fileUrl = $this->getMp4VideoUrl())) { |
|
|
|
|
345
|
|
|
|
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
break; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
View Code Duplication |
if ($fileContent) { |
|
|
|
|
352
|
|
|
if (file_put_contents($absoluteFileName = ($baseFolder.$fileName), $fileContent)) { |
353
|
|
|
$image = Image::create([ |
354
|
|
|
"Filename" => $folder->getFilename().$fileName, |
355
|
|
|
"Title" => $this->Title, |
356
|
|
|
"Name" => $fileName, |
357
|
|
|
"ParentID" => $folder->ID, |
358
|
|
|
"OwnerID" => Member::currentUserID(), |
359
|
|
|
]); |
360
|
|
|
|
361
|
|
|
if ($image->write()) { |
362
|
|
|
$this->CoverID = $image->ID; |
363
|
|
|
$this->write(); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function getVideoOptions() { |
370
|
|
|
$options = []; |
371
|
|
|
|
372
|
|
|
if ($this->Type == 'File') { |
373
|
|
|
$options = [ |
374
|
|
|
'videoTypes' => [ |
375
|
|
|
'mp4' => $this->getMp4VideoUrl(), |
376
|
|
|
'webm' => $this->getWebMVideoUrl(), |
377
|
|
|
'ogg' => $this->getOggVideoUrl(), |
378
|
|
|
], |
379
|
|
|
]; |
380
|
|
|
} else { |
381
|
|
|
$options['embed'] = $this->getEmbedLink(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$options = array_merge([ |
385
|
|
|
'autoPlay' => (boolean) $this->AutoPlay, |
386
|
|
|
'type' => $this->getVideoType(), |
387
|
|
|
'coverImage' => (($cover = $this->getCoverImage()) ? $cover->getAbsoluteURL() : false), |
388
|
|
|
], $options); |
389
|
|
|
|
390
|
|
|
return Convert::raw2att(Convert::array2json($options)); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
class VideoBlock_Controller extends Block_Controller { |
|
|
|
|
396
|
|
|
|
397
|
|
|
public function init() { |
398
|
|
|
if (VideoBlock::config()->load_javascript_plugin) { |
399
|
|
|
Requirements::javascript(CONTENT_BLOCKS_DIR."/assets/javascript/video-block.js"); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
parent::init(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
} |
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.