vendor/pimcore/pimcore/models/Document/Editable/Video.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Document\Editable;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Frontend\FullPageCacheListener;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Tool;
  20. /**
  21.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  22.  */
  23. class Video extends Model\Document\Editable implements IdRewriterInterface
  24. {
  25.     public const TYPE_ASSET 'asset';
  26.     public const TYPE_YOUTUBE 'youtube';
  27.     public const TYPE_VIMEO 'vimeo';
  28.     public const TYPE_DAILYMOTION 'dailymotion';
  29.     public const ALLOWED_TYPES = [
  30.         self::TYPE_ASSET,
  31.         self::TYPE_YOUTUBE,
  32.         self::TYPE_VIMEO,
  33.         self::TYPE_DAILYMOTION,
  34.     ];
  35.     /**
  36.      * contains depending on the type of the video the unique identifier eg. "http://www.youtube.com", "789", ...
  37.      *
  38.      * @internal
  39.      *
  40.      * @var int|string|null
  41.      */
  42.     protected $id;
  43.     /**
  44.      * one of self::ALLOWED_TYPES
  45.      *
  46.      * @internal
  47.      *
  48.      * @var string|null
  49.      */
  50.     protected $type;
  51.     /**
  52.      * asset ID of poster image
  53.      *
  54.      * @internal
  55.      *
  56.      * @var int|null
  57.      */
  58.     protected $poster;
  59.     /**
  60.      * @internal
  61.      *
  62.      * @var string
  63.      */
  64.     protected $title '';
  65.     /**
  66.      * @internal
  67.      *
  68.      * @var string
  69.      */
  70.     protected $description '';
  71.     /**
  72.      * @internal
  73.      *
  74.      * @var array|null
  75.      */
  76.     protected $allowedTypes;
  77.     /**
  78.      * @param int|string|null $id
  79.      *
  80.      * @return Video
  81.      */
  82.     public function setId($id)
  83.     {
  84.         $this->id $id;
  85.         return $this;
  86.     }
  87.     /**
  88.      * @return int|string|null
  89.      */
  90.     public function getId()
  91.     {
  92.         return $this->id;
  93.     }
  94.     /**
  95.      * @param string $title
  96.      *
  97.      * @return $this
  98.      */
  99.     public function setTitle($title)
  100.     {
  101.         $this->title $title;
  102.         return $this;
  103.     }
  104.     /**
  105.      * @return string
  106.      */
  107.     public function getTitle()
  108.     {
  109.         if (!$this->title && $this->getVideoAsset()) {
  110.             // default title for microformats
  111.             return $this->getVideoAsset()->getFilename();
  112.         }
  113.         return $this->title;
  114.     }
  115.     /**
  116.      * @param string $description
  117.      *
  118.      * @return $this
  119.      */
  120.     public function setDescription($description)
  121.     {
  122.         $this->description $description;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return string
  127.      */
  128.     public function getDescription()
  129.     {
  130.         if (!$this->description) {
  131.             // default description for microformats
  132.             return $this->getTitle();
  133.         }
  134.         return $this->description;
  135.     }
  136.     /**
  137.      * @param int|null $id
  138.      *
  139.      * @return $this
  140.      */
  141.     public function setPoster($id)
  142.     {
  143.         $this->poster $id;
  144.         return $this;
  145.     }
  146.     /**
  147.      * @return int|null
  148.      */
  149.     public function getPoster()
  150.     {
  151.         return $this->poster;
  152.     }
  153.     /**
  154.      * @param string $type
  155.      *
  156.      * @return $this
  157.      */
  158.     public function setType($type)
  159.     {
  160.         $this->type $type;
  161.         return $this;
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public function getType()
  167.     {
  168.         return 'video';
  169.     }
  170.     /**
  171.      * @param array $allowedTypes
  172.      *
  173.      * @return $this
  174.      */
  175.     public function setAllowedTypes($allowedTypes)
  176.     {
  177.         $this->allowedTypes $allowedTypes;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return array
  182.      */
  183.     public function getAllowedTypes()
  184.     {
  185.         if ($this->allowedTypes === null) {
  186.             $this->updateAllowedTypesFromConfig($this->getConfig());
  187.         }
  188.         return $this->allowedTypes;
  189.     }
  190.     /**
  191.      * {@inheritdoc}
  192.      */
  193.     public function getData()
  194.     {
  195.         $path $this->id;
  196.         if ($this->type === self::TYPE_ASSET && ($video Asset::getById($this->id))) {
  197.             $path $video->getFullPath();
  198.         }
  199.         $allowedTypes $this->getAllowedTypes();
  200.         if (
  201.             empty($this->type) === true
  202.             || in_array($this->type$allowedTypestrue) === false
  203.         ) {
  204.             // Set the first type in array as default selection for dropdown
  205.             $this->type $allowedTypes[0];
  206.             // Reset "id" and "path" to prevent invalid references
  207.             $this->id   '';
  208.             $path       '';
  209.         }
  210.         $poster Asset::getById($this->poster);
  211.         return [
  212.             'id'           => $this->id,
  213.             'type'         => $this->type,
  214.             'allowedTypes' => $allowedTypes,
  215.             'title'        => $this->title,
  216.             'description'  => $this->description,
  217.             'path'         => $path,
  218.             'poster'       => $poster $poster->getFullPath() : '',
  219.         ];
  220.     }
  221.     /**
  222.      * {@inheritdoc}
  223.      */
  224.     protected function getDataEditmode()
  225.     {
  226.         $data $this->getData();
  227.         $poster Asset::getById($this->poster);
  228.         if ($poster) {
  229.             $data['poster'] = $poster->getRealFullPath();
  230.         }
  231.         if ($this->type === self::TYPE_ASSET && ($video Asset::getById($this->id))) {
  232.             $data['path'] = $video->getRealFullPath();
  233.         }
  234.         return $data;
  235.     }
  236.     /**
  237.      * {@inheritdoc}
  238.      */
  239.     public function getDataForResource()
  240.     {
  241.         return [
  242.             'id'           => $this->id,
  243.             'type'         => $this->type,
  244.             'allowedTypes' => $this->getAllowedTypes(),
  245.             'title'        => $this->title,
  246.             'description'  => $this->description,
  247.             'poster'       => $this->poster,
  248.         ];
  249.     }
  250.     /**
  251.      * {@inheritdoc}
  252.      */
  253.     public function frontend()
  254.     {
  255.         $inAdmin false;
  256.         $args    func_get_args();
  257.         if (array_key_exists(0$args)) {
  258.             $inAdmin $args[0];
  259.         }
  260.         if (
  261.             empty($this->id) === true
  262.             || empty($this->type) === true
  263.             || in_array($this->type$this->getAllowedTypes(), true) === false
  264.         ) {
  265.             return $this->getEmptyCode();
  266.         } elseif ($this->type === self::TYPE_ASSET) {
  267.             return $this->getAssetCode($inAdmin);
  268.         } elseif ($this->type === self::TYPE_YOUTUBE) {
  269.             return $this->getYoutubeCode($inAdmin);
  270.         } elseif ($this->type === self::TYPE_VIMEO) {
  271.             return $this->getVimeoCode($inAdmin);
  272.         } elseif ($this->type === self::TYPE_DAILYMOTION) {
  273.             return $this->getDailymotionCode($inAdmin);
  274.         } elseif ($this->type === 'url') {
  275.             return $this->getUrlCode();
  276.         }
  277.         return $this->getEmptyCode();
  278.     }
  279.     /**
  280.      * {@inheritdoc}
  281.      */
  282.     public function resolveDependencies()
  283.     {
  284.         $dependencies = [];
  285.         if ($this->type === self::TYPE_ASSET) {
  286.             $asset Asset::getById($this->id);
  287.             if ($asset instanceof Asset) {
  288.                 $key 'asset_' $asset->getId();
  289.                 $dependencies[$key] = [
  290.                     'id' => $asset->getId(),
  291.                     'type' => self::TYPE_ASSET,
  292.                 ];
  293.             }
  294.         }
  295.         if ($poster Asset::getById($this->poster)) {
  296.             $key 'asset_' $poster->getId();
  297.             $dependencies[$key] = [
  298.                 'id' => $poster->getId(),
  299.                 'type' => self::TYPE_ASSET,
  300.             ];
  301.         }
  302.         return $dependencies;
  303.     }
  304.     /**
  305.      * {@inheritdoc}
  306.      */
  307.     public function checkValidity()
  308.     {
  309.         $valid true;
  310.         if ($this->type === self::TYPE_ASSET && !empty($this->id)) {
  311.             $el Asset::getById($this->id);
  312.             if (!$el instanceof Asset) {
  313.                 $valid false;
  314.                 Logger::notice('Detected invalid relation, removing reference to non existent asset with id ['.$this->id.']');
  315.                 $this->id   null;
  316.                 $this->type null;
  317.             }
  318.         }
  319.         if (!($poster Asset::getById($this->poster))) {
  320.             $valid false;
  321.             Logger::notice('Detected invalid relation, removing reference to non existent asset with id ['.$this->id.']');
  322.             $this->poster null;
  323.         }
  324.         return $valid;
  325.     }
  326.     /**
  327.      * {@inheritdoc}
  328.      */
  329.     public function admin()
  330.     {
  331.         $html parent::admin();
  332.         // get frontendcode for preview
  333.         // put the video code inside the generic code
  334.         $html str_replace('</div>'$this->frontend(true) . '</div>'$html);
  335.         return $html;
  336.     }
  337.     /**
  338.      * {@inheritdoc}
  339.      */
  340.     public function setDataFromResource($data)
  341.     {
  342.         if (!empty($data)) {
  343.             $data \Pimcore\Tool\Serialize::unserialize($data);
  344.         }
  345.         $this->id $data['id'];
  346.         $this->type $data['type'];
  347.         $this->poster $data['poster'];
  348.         $this->title $data['title'];
  349.         $this->description $data['description'];
  350.         return $this;
  351.     }
  352.     /**
  353.      * {@inheritdoc}
  354.      */
  355.     public function setDataFromEditmode($data)
  356.     {
  357.         if (isset($data['type'])
  358.             && in_array($data['type'], self::ALLOWED_TYPEStrue) === true
  359.         ) {
  360.             $this->type $data['type'];
  361.         }
  362.         if (isset($data['title'])) {
  363.             $this->title $data['title'];
  364.         }
  365.         if (isset($data['description'])) {
  366.             $this->description $data['description'];
  367.         }
  368.         // this is to be backward compatible to <= v 1.4.7
  369.         if (isset($data['id']) && $data['id']) {
  370.             $data['path'] = $data['id'];
  371.         }
  372.         $video Asset::getByPath($data['path']);
  373.         if ($video instanceof Asset\Video) {
  374.             $this->id $video->getId();
  375.         } else {
  376.             $this->id $data['path'];
  377.         }
  378.         $this->poster null;
  379.         $poster Asset::getByPath($data['poster']);
  380.         if ($poster instanceof Asset\Image) {
  381.             $this->poster $poster->getId();
  382.         }
  383.         return $this;
  384.     }
  385.     /**
  386.      * @return int|string
  387.      */
  388.     public function getWidth()
  389.     {
  390.         return $this->getConfig()['width'] ?? '100%';
  391.     }
  392.     /**
  393.      * @return int|string
  394.      */
  395.     public function getHeight()
  396.     {
  397.         return $this->getConfig()['height'] ?? 300;
  398.     }
  399.     private function getAssetCode(bool $inAdmin false): string
  400.     {
  401.         $asset Asset::getById($this->id);
  402.         $config $this->getConfig();
  403.         $thumbnailConfig $config['thumbnail'] ?? null;
  404.         // compatibility mode when FFMPEG is not present or no thumbnail config is given
  405.         if (!\Pimcore\Video::isAvailable() || !$thumbnailConfig) {
  406.             if ($asset instanceof Asset\Video && preg_match("/\.(f4v|flv|mp4)/i"$asset->getFullPath())) {
  407.                 $image $this->getPosterThumbnailImage($asset);
  408.                 return $this->getHtml5Code(['mp4' => (string) $asset], $image);
  409.             }
  410.             return $this->getErrorCode('Asset is not a video, or missing thumbnail configuration');
  411.         }
  412.         if ($asset instanceof Asset\Video) {
  413.             $thumbnail $asset->getThumbnail($thumbnailConfig);
  414.             if ($thumbnail) {
  415.                 $image $this->getPosterThumbnailImage($asset);
  416.                 if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview']) {
  417.                     $code '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">';
  418.                     $code .= '<img width="' $this->getWidth() . '" src="' $image '" />';
  419.                     $code .= '</div>';
  420.                     return $code;
  421.                 }
  422.                 if ($thumbnail['status'] === 'finished') {
  423.                     return $this->getHtml5Code($thumbnail['formats'], $image);
  424.                 }
  425.                 if ($thumbnail['status'] === 'inprogress') {
  426.                     // disable the output-cache if enabled
  427.                     $cacheService \Pimcore::getContainer()->get(FullPageCacheListener::class);
  428.                     $cacheService->disable('Video rendering in progress');
  429.                     return $this->getProgressCode($image);
  430.                 }
  431.                 return $this->getErrorCode('The video conversion failed, please see the log files in /var/log for more details.');
  432.             }
  433.             return $this->getErrorCode("The given thumbnail doesn't exist: '" $thumbnailConfig "'");
  434.         }
  435.         return $this->getEmptyCode();
  436.     }
  437.     /**
  438.      * @param Asset\Video $asset
  439.      *
  440.      * @return Asset\Image\Thumbnail|Asset\Video\ImageThumbnail
  441.      */
  442.     private function getPosterThumbnailImage(Asset\Video $asset)
  443.     {
  444.         $config $this->getConfig();
  445.         if (!array_key_exists('imagethumbnail'$config) || empty($config['imagethumbnail'])) {
  446.             $thumbnailConfig $asset->getThumbnailConfig($config['thumbnail'] ?? null);
  447.             if ($thumbnailConfig instanceof Asset\Video\Thumbnail\Config) {
  448.                 // try to get the dimensions out ouf the video thumbnail
  449.                 $imageThumbnailConf $thumbnailConfig->getEstimatedDimensions();
  450.                 $imageThumbnailConf['format'] = 'JPEG';
  451.             }
  452.         } else {
  453.             $imageThumbnailConf $config['imagethumbnail'];
  454.         }
  455.         if (empty($imageThumbnailConf)) {
  456.             $imageThumbnailConf['width'] = 800;
  457.             $imageThumbnailConf['format'] = 'JPEG';
  458.         }
  459.         if ($this->poster && ($poster Asset\Image::getById($this->poster))) {
  460.             return $poster->getThumbnail($imageThumbnailConf);
  461.         }
  462.         if (
  463.             $asset->getCustomSetting('image_thumbnail_asset') &&
  464.             ($customPreviewAsset Asset\Image::getById($asset->getCustomSetting('image_thumbnail_asset')))
  465.         ) {
  466.             return $customPreviewAsset->getThumbnail($imageThumbnailConf);
  467.         }
  468.         return $asset->getImageThumbnail($imageThumbnailConf);
  469.     }
  470.     /**
  471.      * @return string
  472.      */
  473.     private function getUrlCode()
  474.     {
  475.         return $this->getHtml5Code(['mp4' => (string) $this->id]);
  476.     }
  477.     /**
  478.      * @param string $message
  479.      *
  480.      * @return string
  481.      */
  482.     private function getErrorCode($message '')
  483.     {
  484.         $width $this->getWidth();
  485.         // If contains at least one digit (0-9), then assume it is a value that can be calculated,
  486.         // otherwise it is likely be `auto`,`inherit`,etc..
  487.         if (preg_match('/[\d]/'$width)) {
  488.             // when is numeric, assume there are no length units nor %, and considering the value as pixels
  489.             if (is_numeric($width)) {
  490.                 $width .= 'px';
  491.             }
  492.             $width 'calc(' $width ' - 1px)';
  493.         }
  494.         $height $this->getHeight();
  495.         if (preg_match('/[\d]/'$height)) {
  496.             if (is_numeric($height)) {
  497.                 $height .= 'px';
  498.             }
  499.             $height 'calc(' $height ' - 1px)';
  500.         }
  501.         // only display error message in debug mode
  502.         if (!\Pimcore::inDebugMode()) {
  503.             $message '';
  504.         }
  505.         $code '
  506.         <div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">
  507.             <div class="pimcore_editable_video_error" style="text-align:center; width: ' $width '; height: ' $height '; border:1px solid #000; background: url(/bundles/pimcoreadmin/img/filetype-not-supported.svg) no-repeat center center #fff;">
  508.                 ' $message '
  509.             </div>
  510.         </div>';
  511.         return $code;
  512.     }
  513.     /**
  514.      * @return string
  515.      */
  516.     private function parseYoutubeId()
  517.     {
  518.         $youtubeId '';
  519.         if ($this->type === self::TYPE_YOUTUBE) {
  520.             if ($youtubeId $this->id) {
  521.                 if (strpos($youtubeId'//') !== false) {
  522.                     $parts parse_url($this->id);
  523.                     if (array_key_exists('query'$parts)) {
  524.                         parse_str($parts['query'], $vars);
  525.                         if (isset($vars['v']) && $vars['v']) {
  526.                             $youtubeId $vars['v'];
  527.                         }
  528.                     }
  529.                     //get youtube id if form urls like  http://www.youtube.com/embed/youtubeId
  530.                     if (strpos($this->id'embed') !== false) {
  531.                         $explodedPath explode('/'$parts['path']);
  532.                         $youtubeId $explodedPath[array_search('embed'$explodedPath) + 1];
  533.                     }
  534.                     if (isset($parts['host']) && $parts['host'] === 'youtu.be') {
  535.                         $youtubeId trim($parts['path'], ' /');
  536.                     }
  537.                 }
  538.             }
  539.         }
  540.         return $youtubeId;
  541.     }
  542.     private function getYoutubeCode(bool $inAdmin false): string
  543.     {
  544.         if (!$this->id) {
  545.             return $this->getEmptyCode();
  546.         }
  547.         $config $this->getConfig();
  548.         $code '';
  549.         $youtubeId $this->parseYoutubeId();
  550.         if (!$youtubeId) {
  551.             return $this->getEmptyCode();
  552.         }
  553.         if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  554.             return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  555.                 <img src="https://img.youtube.com/vi/' $youtubeId '/0.jpg">
  556.             </div>';
  557.         }
  558.         $width '100%';
  559.         if (array_key_exists('width'$config)) {
  560.             $width $config['width'];
  561.         }
  562.         $height '300';
  563.         if (array_key_exists('height'$config)) {
  564.             $height $config['height'];
  565.         }
  566.         $wmode '?wmode=transparent';
  567.         $seriesPrefix '';
  568.         if (strpos($youtubeId'PL') === 0) {
  569.             $wmode '';
  570.             $seriesPrefix 'videoseries?list=';
  571.         }
  572.         $valid_youtube_prams = [ 'autohide',
  573.             'autoplay',
  574.             'cc_load_policy',
  575.             'color',
  576.             'controls',
  577.             'disablekb',
  578.             'enablejsapi',
  579.             'end',
  580.             'fs',
  581.             'playsinline',
  582.             'hl',
  583.             'iv_load_policy',
  584.             'list',
  585.             'listType',
  586.             'loop',
  587.             'modestbranding',
  588.             'mute',
  589.             'origin',
  590.             'playerapiid',
  591.             'playlist',
  592.             'rel',
  593.             'showinfo',
  594.             'start',
  595.             'theme',
  596.         ];
  597.         $additional_params '';
  598.         $clipConfig = [];
  599.         if (isset($config['config']['clip']) && is_array($config['config']['clip'])) {
  600.             $clipConfig $config['config']['clip'];
  601.         }
  602.         // this is to be backward compatible to <= v 1.4.7
  603.         $configurations $clipConfig;
  604.         if (array_key_exists(self::TYPE_YOUTUBE$config) && is_array($config[self::TYPE_YOUTUBE])) {
  605.             $configurations array_merge($clipConfig$config[self::TYPE_YOUTUBE]);
  606.         }
  607.         if (!empty($configurations)) {
  608.             foreach ($configurations as $key => $value) {
  609.                 if (in_array($key$valid_youtube_prams)) {
  610.                     if (is_bool($value)) {
  611.                         if ($value) {
  612.                             $additional_params .= '&'.$key.'=1';
  613.                         } else {
  614.                             $additional_params .= '&'.$key.'=0';
  615.                         }
  616.                     } else {
  617.                         $additional_params .= '&'.$key.'='.$value;
  618.                     }
  619.                 }
  620.             }
  621.         }
  622.         $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  623.             <iframe width="' $width '" height="' $height '" src="https://www.youtube-nocookie.com/embed/' $seriesPrefix $youtubeId $wmode $additional_params .'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow="fullscreen" data-type="pimcore_video_editable"></iframe>
  624.         </div>';
  625.         return $code;
  626.     }
  627.     private function getVimeoCode(bool $inAdmin false): string
  628.     {
  629.         if (!$this->id) {
  630.             return $this->getEmptyCode();
  631.         }
  632.         $config $this->getConfig();
  633.         $code '';
  634.         $uid 'video_' uniqid();
  635.         // get vimeo id
  636.         if (preg_match("@vimeo.*/([\d]+)@i"$this->id$matches)) {
  637.             $vimeoId = (int)$matches[1];
  638.         } else {
  639.             // for object-videos
  640.             $vimeoId $this->id;
  641.         }
  642.         if (ctype_digit($vimeoId)) {
  643.             if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  644.                 return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  645.                     <img src="https://vumbnail.com/' $vimeoId '.jpg">
  646.                 </div>';
  647.             }
  648.             $width '100%';
  649.             if (array_key_exists('width'$config)) {
  650.                 $width $config['width'];
  651.             }
  652.             $height '300';
  653.             if (array_key_exists('height'$config)) {
  654.                 $height $config['height'];
  655.             }
  656.             $valid_vimeo_prams = [
  657.                 'autoplay',
  658.                 'background',
  659.                 'loop',
  660.                 'muted',
  661.             ];
  662.             $additional_params '';
  663.             $clipConfig = [];
  664.             if (isset($config['config']['clip']) && is_array($config['config']['clip'])) {
  665.                 $clipConfig $config['config']['clip'];
  666.             }
  667.             // this is to be backward compatible to <= v 1.4.7
  668.             $configurations $clipConfig;
  669.             if (isset($config[self::TYPE_VIMEO]) && is_array($config[self::TYPE_VIMEO])) {
  670.                 $configurations array_merge($clipConfig$config[self::TYPE_VIMEO]);
  671.             }
  672.             if (!empty($configurations)) {
  673.                 foreach ($configurations as $key => $value) {
  674.                     if (in_array($key$valid_vimeo_prams)) {
  675.                         if (is_bool($value)) {
  676.                             if ($value) {
  677.                                 $additional_params .= '&'.$key.'=1';
  678.                             } else {
  679.                                 $additional_params .= '&'.$key.'=0';
  680.                             }
  681.                         } else {
  682.                             $additional_params .= '&'.$key.'='.$value;
  683.                         }
  684.                     }
  685.                 }
  686.             }
  687.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  688.                 <iframe src="https://player.vimeo.com/video/' $vimeoId '?dnt=1&title=0&amp;byline=0&amp;portrait=0'$additional_params .'" width="' $width '" height="' $height '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow="fullscreen" data-type="pimcore_video_editable"></iframe>
  689.             </div>';
  690.             return $code;
  691.         }
  692.         // default => return the empty code
  693.         return $this->getEmptyCode();
  694.     }
  695.     private function getDailymotionCode(bool $inAdmin false): string
  696.     {
  697.         if (!$this->id) {
  698.             return $this->getEmptyCode();
  699.         }
  700.         $config $this->getConfig();
  701.         $code '';
  702.         $uid 'video_' uniqid();
  703.         // get dailymotion id
  704.         if (preg_match('@dailymotion.*/video/([^_]+)@i'$this->id$matches)) {
  705.             $dailymotionId $matches[1];
  706.         } else {
  707.             // for object-videos
  708.             $dailymotionId $this->id;
  709.         }
  710.         if ($dailymotionId) {
  711.             if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  712.                 return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  713.                     <img src="https://www.dailymotion.com/thumbnail/video/' $dailymotionId '">
  714.                 </div>';
  715.             }
  716.             $width $config['width'] ?? '100%';
  717.             $height $config['height'] ?? '300';
  718.             $valid_dailymotion_prams = [
  719.                 'autoplay',
  720.                 'loop',
  721.                 'mute', ];
  722.             $additional_params '';
  723.             $clipConfig = isset($config['config']['clip']) && is_array($config['config']['clip']) ? $config['config']['clip'] : [];
  724.             // this is to be backward compatible to <= v 1.4.7
  725.             $configurations $clipConfig;
  726.             if (isset($config[self::TYPE_DAILYMOTION]) && is_array($config[self::TYPE_DAILYMOTION])) {
  727.                 $configurations array_merge($clipConfig$config[self::TYPE_DAILYMOTION]);
  728.             }
  729.             if (!empty($configurations)) {
  730.                 foreach ($configurations as $key => $value) {
  731.                     if (in_array($key$valid_dailymotion_prams)) {
  732.                         if (is_bool($value)) {
  733.                             if ($value) {
  734.                                 $additional_params .= '&'.$key.'=1';
  735.                             } else {
  736.                                 $additional_params .= '&'.$key.'=0';
  737.                             }
  738.                         } else {
  739.                             $additional_params .= '&'.$key.'='.$value;
  740.                         }
  741.                     }
  742.                 }
  743.             }
  744.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  745.                 <iframe src="https://www.dailymotion.com/embed/video/' $dailymotionId '?' $additional_params .'" width="' $width '" height="' $height '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow="fullscreen" data-type="pimcore_video_editable"></iframe>
  746.             </div>';
  747.             return $code;
  748.         }
  749.         // default => return the empty code
  750.         return $this->getEmptyCode();
  751.     }
  752.     /**
  753.      * @param array $urls
  754.      * @param Asset\Image\Thumbnail|Asset\Video\ImageThumbnail|null $thumbnail
  755.      *
  756.      * @return string
  757.      */
  758.     private function getHtml5Code($urls = [], $thumbnail null)
  759.     {
  760.         $code '';
  761.         $video $this->getVideoAsset();
  762.         if ($video) {
  763.             $duration ceil($video->getDuration());
  764.             $durationParts = ['PT'];
  765.             // hours
  766.             if ($duration 3600 >= 1) {
  767.                 $hours floor($duration 3600);
  768.                 $durationParts[] = $hours 'H';
  769.                 $duration $duration $hours 3600;
  770.             }
  771.             // minutes
  772.             if ($duration 60 >= 1) {
  773.                 $minutes floor($duration 60);
  774.                 $durationParts[] = $minutes 'M';
  775.                 $duration $duration $minutes 60;
  776.             }
  777.             $durationParts[] = $duration 'S';
  778.             $durationString implode(''$durationParts);
  779.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">' "\n";
  780.             $uploadDate = new \DateTime();
  781.             $uploadDate->setTimestamp($video->getCreationDate());
  782.             $jsonLd = [
  783.                 '@context' => 'https://schema.org',
  784.                 '@type' => 'VideoObject',
  785.                 'name' => $this->getTitle(),
  786.                 'description' => $this->getDescription(),
  787.                 'uploadDate' => $uploadDate->format('Y-m-d\TH:i:sO'),
  788.                 'duration' => $durationString,
  789.                 //'contentUrl' => Tool::getHostUrl() . $urls['mp4'],
  790.                 //"embedUrl" => "http://www.example.com/videoplayer.swf?video=123",
  791.                 //"interactionCount" => "1234",
  792.             ];
  793.             if (!$thumbnail) {
  794.                 $thumbnail $video->getImageThumbnail([]);
  795.             }
  796.             $jsonLd['contentUrl'] = $urls['mp4'];
  797.             if (!preg_match('@https?://@'$urls['mp4'])) {
  798.                 $jsonLd['contentUrl'] = Tool::getHostUrl() . $urls['mp4'];
  799.             }
  800.             $thumbnailUrl = (string)$thumbnail;
  801.             $jsonLd['thumbnailUrl'] = $thumbnailUrl;
  802.             if (!preg_match('@https?://@'$thumbnailUrl)) {
  803.                 $jsonLd['thumbnailUrl'] = Tool::getHostUrl() . $thumbnailUrl;
  804.             }
  805.             $code .= "\n\n<script type=\"application/ld+json\">\n" json_encode($jsonLd) . "\n</script>\n\n";
  806.             // default attributes
  807.             $attributesString '';
  808.             $attributes = [
  809.                 'width' => $this->getWidth(),
  810.                 'height' => $this->getHeight(),
  811.                 'poster' => $thumbnailUrl,
  812.                 'controls' => 'controls',
  813.                 'class' => 'pimcore_video',
  814.             ];
  815.             $config $this->getConfig();
  816.             if (array_key_exists('attributes'$config)) {
  817.                 $attributes array_merge($attributes$config['attributes']);
  818.             }
  819.             if (isset($config['removeAttributes']) && is_array($config['removeAttributes'])) {
  820.                 foreach ($config['removeAttributes'] as $attribute) {
  821.                     unset($attributes[$attribute]);
  822.                 }
  823.             }
  824.             // do not allow an empty controls editable
  825.             if (isset($attributes['controls']) && !$attributes['controls']) {
  826.                 unset($attributes['controls']);
  827.             }
  828.             if (isset($urls['mpd'])) {
  829.                 $attributes['data-dashjs-player'] = null;
  830.             }
  831.             foreach ($attributes as $key => $value) {
  832.                 $attributesString .= ' ' $key;
  833.                 if (!empty($value)) {
  834.                     $quoteChar '"';
  835.                     if (strpos($value'"')) {
  836.                         $quoteChar "'";
  837.                     }
  838.                     $attributesString .= '=' $quoteChar $value $quoteChar;
  839.                 }
  840.             }
  841.             $code .= '<video' $attributesString '>' "\n";
  842.             foreach ($urls as $type => $url) {
  843.                 if ($type == 'medias') {
  844.                     foreach ($url as $format => $medias) {
  845.                         foreach ($medias as $media => $mediaUrl) {
  846.                             $code .= '<source type="video/' $format '" src="' $mediaUrl '" media="' $media '"  />' "\n";
  847.                         }
  848.                     }
  849.                 } else {
  850.                     $code .= '<source type="video/' $type '" src="' $url '" />' "\n";
  851.                 }
  852.             }
  853.             $code .= '</video>' "\n";
  854.             $code .= '</div>' "\n";
  855.         }
  856.         return $code;
  857.     }
  858.     /**
  859.      * @param string|null $thumbnail
  860.      *
  861.      * @return string
  862.      */
  863.     private function getProgressCode($thumbnail null)
  864.     {
  865.         $uid 'video_' uniqid();
  866.         $code '
  867.         <div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">
  868.             <style type="text/css">
  869.                 #' $uid ' .pimcore_editable_video_progress_status {
  870.                     box-sizing:content-box;
  871.                     background:#fff url(/bundles/pimcoreadmin/img/video-loading.gif) center center no-repeat;
  872.                     width:66px;
  873.                     height:66px;
  874.                     padding:20px;
  875.                     border:1px solid #555;
  876.                     box-shadow: 2px 2px 5px #333;
  877.                     border-radius:20px;
  878.                     margin: 0 20px 0 20px;
  879.                     top: calc(50% - 66px);
  880.                     left: calc(50% - 66px);
  881.                     position:absolute;
  882.                     opacity: 0.8;
  883.                 }
  884.             </style>
  885.             <div class="pimcore_editable_video_progress" id="' $uid '">
  886.                 <img src="' $thumbnail '" style="width: ' $this->getWidth() . 'px; height: ' $this->getHeight() . 'px;">
  887.                 <div class="pimcore_editable_video_progress_status"></div>
  888.             </div>
  889.         </div>';
  890.         return $code;
  891.     }
  892.     /**
  893.      * @return string
  894.      */
  895.     private function getEmptyCode(): string
  896.     {
  897.         $uid 'video_' uniqid();
  898.         $width $this->getWidth();
  899.         $height $this->getHeight();
  900.         if (is_numeric($width)) {
  901.             $width .= 'px';
  902.         }
  903.         if (is_numeric($height)) {
  904.             $height .= 'px';
  905.         }
  906.         return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video"><div class="pimcore_editable_video_empty" id="' $uid '" style="width: ' $width '; height: ' $height ';"></div></div>';
  907.     }
  908.     private function updateAllowedTypesFromConfig(array $config): void
  909.     {
  910.         $this->allowedTypes self::ALLOWED_TYPES;
  911.         if (
  912.             isset($config['allowedTypes']) === true
  913.             && empty($config['allowedTypes']) === false
  914.             && empty(array_diff($config['allowedTypes'], self::ALLOWED_TYPES))
  915.         ) {
  916.             $this->allowedTypes $config['allowedTypes'];
  917.         }
  918.     }
  919.     /**
  920.      * {@inheritdoc}
  921.      */
  922.     public function isEmpty()
  923.     {
  924.         if ($this->id) {
  925.             return false;
  926.         }
  927.         return true;
  928.     }
  929.     /**
  930.      * @return string
  931.      */
  932.     public function getVideoType()
  933.     {
  934.         if (empty($this->type) === true) {
  935.             $this->type $this->getAllowedTypes()[0];
  936.         }
  937.         return $this->type;
  938.     }
  939.     /**
  940.      * @return Asset\Video|null
  941.      */
  942.     public function getVideoAsset()
  943.     {
  944.         if ($this->getVideoType() === self::TYPE_ASSET) {
  945.             return Asset\Video::getById($this->id);
  946.         }
  947.         return null;
  948.     }
  949.     /**
  950.      * @return Asset\Image|null
  951.      */
  952.     public function getPosterAsset()
  953.     {
  954.         return Asset\Image::getById($this->poster);
  955.     }
  956.     /**
  957.      * @param string|Asset\Video\Thumbnail\Config $config
  958.      *
  959.      * @return Asset\Image\Thumbnail|Asset\Video\ImageThumbnail|string
  960.      *
  961.      * TODO Pimcore 11: Change empty string return to null
  962.      */
  963.     public function getImageThumbnail($config)
  964.     {
  965.         if ($this->poster && ($poster Asset\Image::getById($this->poster))) {
  966.             return $poster->getThumbnail($config);
  967.         }
  968.         if ($this->getVideoAsset()) {
  969.             return $this->getVideoAsset()->getImageThumbnail($config);
  970.         }
  971.         return '';
  972.     }
  973.     /**
  974.      * @param string|Asset\Video\Thumbnail\Config $config
  975.      *
  976.      * @return array
  977.      */
  978.     public function getThumbnail($config)
  979.     {
  980.         if ($this->getVideoAsset()) {
  981.             return $this->getVideoAsset()->getThumbnail($config);
  982.         }
  983.         return [];
  984.     }
  985.     /**
  986.      * { @inheritdoc }
  987.      */
  988.     public function rewriteIds($idMapping/** : void */
  989.     {
  990.         if ($this->type == self::TYPE_ASSET && array_key_exists(self::TYPE_ASSET$idMapping) && array_key_exists($this->getId(), $idMapping[self::TYPE_ASSET])) {
  991.             $this->setId($idMapping[self::TYPE_ASSET][$this->getId()]);
  992.         }
  993.     }
  994. }