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