138 lines
4.5 KiB
PHP
138 lines
4.5 KiB
PHP
<?php
|
|
namespace YTKNS\Effects;
|
|
|
|
use Exception;
|
|
use YTKNS\HtmlTag;
|
|
use YTKNS\PageBuilder;
|
|
use YTKNS\PageEffectInterface;
|
|
use YTKNS\PageEffectException;
|
|
use YTKNS\Upload;
|
|
use YTKNS\UploadNotFoundException;
|
|
|
|
class BackgroundAudioEffect implements PageEffectInterface {
|
|
private $oggUpload = null;
|
|
private $mp3Upload = null;
|
|
private bool $autoPlay = true;
|
|
private bool $loop = true;
|
|
|
|
private const OGG_MIME = [
|
|
'audio/ogg',
|
|
'application/ogg',
|
|
];
|
|
|
|
private const MP3_MIME = [
|
|
'audio/mpeg',
|
|
'application/x-font-gdos',
|
|
];
|
|
|
|
public function getEffectName(): string {
|
|
return 'Background Audio';
|
|
}
|
|
|
|
public function getEffectProperties(): array {
|
|
return [
|
|
[
|
|
'name' => 'ogg',
|
|
'title' => 'Ogg Audio Source',
|
|
'type' => [
|
|
'name' => 'upload',
|
|
'allowed' => self::OGG_MIME,
|
|
],
|
|
],
|
|
[
|
|
'name' => 'mp3',
|
|
'title' => 'MP3 Audio Source',
|
|
'type' => [
|
|
'name' => 'upload',
|
|
'allowed' => self::MP3_MIME,
|
|
],
|
|
],
|
|
[
|
|
'name' => 'autoplay',
|
|
'title' => 'Auto Play',
|
|
'default' => true,
|
|
'type' => [
|
|
'name' => 'bool',
|
|
],
|
|
],
|
|
[
|
|
'name' => 'loop',
|
|
'title' => 'Loop',
|
|
'default' => true,
|
|
'type' => [
|
|
'name' => 'bool',
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function setEffectParams(array $vars, bool $quiet = false): void {
|
|
try {
|
|
if(isset($vars['ogg']) && is_string($vars['ogg'])) {
|
|
try {
|
|
$this->oggUpload = Upload::byId($vars['ogg']);
|
|
} catch(Exception $ex) {
|
|
if(!$quiet)
|
|
throw $ex;
|
|
}
|
|
|
|
if(!$quiet && !in_array($this->oggUpload->getType(), self::OGG_MIME))
|
|
throw new PageEffectException('Ogg source upload was of invalid type.');
|
|
}
|
|
} catch(UploadNotFoundException $ex) {
|
|
$noOGG = true;
|
|
}
|
|
|
|
try {
|
|
if(isset($vars['mp3']) && is_string($vars['mp3'])) {
|
|
try {
|
|
$this->mp3Upload = Upload::byId($vars['mp3']);
|
|
} catch(Exception $ex) {
|
|
if(!$quiet)
|
|
throw $ex;
|
|
}
|
|
|
|
if(!$quiet && !in_array($this->mp3Upload->getType(), self::MP3_MIME))
|
|
throw new PageEffectException('MP3 source upload was of invalid type.');
|
|
}
|
|
} catch(UploadNotFoundException $ex) {
|
|
$noMP3 = true;
|
|
}
|
|
|
|
if(!empty($noMP3) && !empty($noOGG))
|
|
throw new PageEffectException('No audio source uploaded.');
|
|
|
|
if(isset($vars['autoplay']))
|
|
$this->autoPlay = is_bool($vars['autoplay']) ? $vars['autoplay'] : (is_string($vars['autoplay']) ? boolval($vars['autoplay']) : false);
|
|
|
|
if(isset($vars['loop']))
|
|
$this->loop = is_bool($vars['loop']) ? $vars['loop'] : (is_string($vars['loop']) ? boolval($vars['loop']) : false);
|
|
}
|
|
|
|
public function getEffectParams(): array {
|
|
return [
|
|
'ogg' => empty($this->oggUpload) ? null : $this->oggUpload->getId(),
|
|
'mp3' => empty($this->mp3Upload) ? null : $this->mp3Upload->getId(),
|
|
'autoplay' => $this->autoPlay,
|
|
'loop' => $this->loop,
|
|
];
|
|
}
|
|
|
|
public function applyEffect(PageBuilder $builder): void {
|
|
$audioTag = new HtmlTag('audio');
|
|
$audioTag->setAttribute('id', 'BackgroundAudio');
|
|
|
|
if($this->autoPlay)
|
|
$audioTag->setAttribute('autoplay', 'autoplay');
|
|
if($this->loop)
|
|
$audioTag->setAttribute('loop', 'loop');
|
|
|
|
if(!empty($this->oggUpload))
|
|
$audioTag->appendChild(new HtmlTag('source', ['type' => 'audio/ogg', 'src' => $this->oggUpload->getUrl()], true));
|
|
if(!empty($this->mp3Upload))
|
|
$audioTag->appendChild(new HtmlTag('source', ['type' => 'audio/mpeg', 'src' => $this->mp3Upload->getUrl()], true));
|
|
|
|
$builder->getBody()->appendChild($audioTag);
|
|
}
|
|
}
|