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 (strpos($this->getWidth(), '%') === false) {
  486.             $width = ((int)$this->getWidth() - 1) . 'px';
  487.         }
  488.         // only display error message in debug mode
  489.         if (!\Pimcore::inDebugMode()) {
  490.             $message '';
  491.         }
  492.         $code '
  493.         <div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">
  494.             <div class="pimcore_editable_video_error" style="text-align:center; width: ' $width '; height: ' . ($this->getHeight() - 1) . 'px; border:1px solid #000; background: url(/bundles/pimcoreadmin/img/filetype-not-supported.svg) no-repeat center center #fff;">
  495.                 ' $message '
  496.             </div>
  497.         </div>';
  498.         return $code;
  499.     }
  500.     /**
  501.      * @return string
  502.      */
  503.     private function parseYoutubeId()
  504.     {
  505.         $youtubeId '';
  506.         if ($this->type === self::TYPE_YOUTUBE) {
  507.             if ($youtubeId $this->id) {
  508.                 if (strpos($youtubeId'//') !== false) {
  509.                     $parts parse_url($this->id);
  510.                     if (array_key_exists('query'$parts)) {
  511.                         parse_str($parts['query'], $vars);
  512.                         if (isset($vars['v']) && $vars['v']) {
  513.                             $youtubeId $vars['v'];
  514.                         }
  515.                     }
  516.                     //get youtube id if form urls like  http://www.youtube.com/embed/youtubeId
  517.                     if (strpos($this->id'embed') !== false) {
  518.                         $explodedPath explode('/'$parts['path']);
  519.                         $youtubeId $explodedPath[array_search('embed'$explodedPath) + 1];
  520.                     }
  521.                     if (isset($parts['host']) && $parts['host'] === 'youtu.be') {
  522.                         $youtubeId trim($parts['path'], ' /');
  523.                     }
  524.                 }
  525.             }
  526.         }
  527.         return $youtubeId;
  528.     }
  529.     private function getYoutubeCode(bool $inAdmin false): string
  530.     {
  531.         if (!$this->id) {
  532.             return $this->getEmptyCode();
  533.         }
  534.         $config $this->getConfig();
  535.         $code '';
  536.         $youtubeId $this->parseYoutubeId();
  537.         if (!$youtubeId) {
  538.             return $this->getEmptyCode();
  539.         }
  540.         if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  541.             return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  542.                 <img src="https://img.youtube.com/vi/' $youtubeId '/0.jpg">
  543.             </div>';
  544.         }
  545.         $width '100%';
  546.         if (array_key_exists('width'$config)) {
  547.             $width $config['width'];
  548.         }
  549.         $height '300';
  550.         if (array_key_exists('height'$config)) {
  551.             $height $config['height'];
  552.         }
  553.         $wmode '?wmode=transparent';
  554.         $seriesPrefix '';
  555.         if (strpos($youtubeId'PL') === 0) {
  556.             $wmode '';
  557.             $seriesPrefix 'videoseries?list=';
  558.         }
  559.         $valid_youtube_prams = [ 'autohide',
  560.             'autoplay',
  561.             'cc_load_policy',
  562.             'color',
  563.             'controls',
  564.             'disablekb',
  565.             'enablejsapi',
  566.             'end',
  567.             'fs',
  568.             'playsinline',
  569.             'hl',
  570.             'iv_load_policy',
  571.             'list',
  572.             'listType',
  573.             'loop',
  574.             'modestbranding',
  575.             'mute',
  576.             'origin',
  577.             'playerapiid',
  578.             'playlist',
  579.             'rel',
  580.             'showinfo',
  581.             'start',
  582.             'theme',
  583.         ];
  584.         $additional_params '';
  585.         $clipConfig = [];
  586.         if (isset($config['config']['clip']) && is_array($config['config']['clip'])) {
  587.             $clipConfig $config['config']['clip'];
  588.         }
  589.         // this is to be backward compatible to <= v 1.4.7
  590.         $configurations $clipConfig;
  591.         if (array_key_exists(self::TYPE_YOUTUBE$config) && is_array($config[self::TYPE_YOUTUBE])) {
  592.             $configurations array_merge($clipConfig$config[self::TYPE_YOUTUBE]);
  593.         }
  594.         if (!empty($configurations)) {
  595.             foreach ($configurations as $key => $value) {
  596.                 if (in_array($key$valid_youtube_prams)) {
  597.                     if (is_bool($value)) {
  598.                         if ($value) {
  599.                             $additional_params .= '&'.$key.'=1';
  600.                         } else {
  601.                             $additional_params .= '&'.$key.'=0';
  602.                         }
  603.                     } else {
  604.                         $additional_params .= '&'.$key.'='.$value;
  605.                     }
  606.                 }
  607.             }
  608.         }
  609.         $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  610.             <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>
  611.         </div>';
  612.         return $code;
  613.     }
  614.     private function getVimeoCode(bool $inAdmin false): string
  615.     {
  616.         if (!$this->id) {
  617.             return $this->getEmptyCode();
  618.         }
  619.         $config $this->getConfig();
  620.         $code '';
  621.         $uid 'video_' uniqid();
  622.         // get vimeo id
  623.         if (preg_match("@vimeo.*/([\d]+)@i"$this->id$matches)) {
  624.             $vimeoId = (int)$matches[1];
  625.         } else {
  626.             // for object-videos
  627.             $vimeoId $this->id;
  628.         }
  629.         if (ctype_digit($vimeoId)) {
  630.             if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  631.                 return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  632.                     <img src="https://vumbnail.com/' $vimeoId '.jpg">
  633.                 </div>';
  634.             }
  635.             $width '100%';
  636.             if (array_key_exists('width'$config)) {
  637.                 $width $config['width'];
  638.             }
  639.             $height '300';
  640.             if (array_key_exists('height'$config)) {
  641.                 $height $config['height'];
  642.             }
  643.             $valid_vimeo_prams = [
  644.                 'autoplay',
  645.                 'background',
  646.                 'loop',
  647.                 'muted',
  648.             ];
  649.             $additional_params '';
  650.             $clipConfig = [];
  651.             if (isset($config['config']['clip']) && is_array($config['config']['clip'])) {
  652.                 $clipConfig $config['config']['clip'];
  653.             }
  654.             // this is to be backward compatible to <= v 1.4.7
  655.             $configurations $clipConfig;
  656.             if (isset($config[self::TYPE_VIMEO]) && is_array($config[self::TYPE_VIMEO])) {
  657.                 $configurations array_merge($clipConfig$config[self::TYPE_VIMEO]);
  658.             }
  659.             if (!empty($configurations)) {
  660.                 foreach ($configurations as $key => $value) {
  661.                     if (in_array($key$valid_vimeo_prams)) {
  662.                         if (is_bool($value)) {
  663.                             if ($value) {
  664.                                 $additional_params .= '&'.$key.'=1';
  665.                             } else {
  666.                                 $additional_params .= '&'.$key.'=0';
  667.                             }
  668.                         } else {
  669.                             $additional_params .= '&'.$key.'='.$value;
  670.                         }
  671.                     }
  672.                 }
  673.             }
  674.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  675.                 <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>
  676.             </div>';
  677.             return $code;
  678.         }
  679.         // default => return the empty code
  680.         return $this->getEmptyCode();
  681.     }
  682.     private function getDailymotionCode(bool $inAdmin false): string
  683.     {
  684.         if (!$this->id) {
  685.             return $this->getEmptyCode();
  686.         }
  687.         $config $this->getConfig();
  688.         $code '';
  689.         $uid 'video_' uniqid();
  690.         // get dailymotion id
  691.         if (preg_match('@dailymotion.*/video/([^_]+)@i'$this->id$matches)) {
  692.             $dailymotionId $matches[1];
  693.         } else {
  694.             // for object-videos
  695.             $dailymotionId $this->id;
  696.         }
  697.         if ($dailymotionId) {
  698.             if ($inAdmin && isset($config['editmodeImagePreview']) && $config['editmodeImagePreview'] === true) {
  699.                 return '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  700.                     <img src="https://www.dailymotion.com/thumbnail/video/' $dailymotionId '">
  701.                 </div>';
  702.             }
  703.             $width $config['width'] ?? '100%';
  704.             $height $config['height'] ?? '300';
  705.             $valid_dailymotion_prams = [
  706.                 'autoplay',
  707.                 'loop',
  708.                 'mute', ];
  709.             $additional_params '';
  710.             $clipConfig = isset($config['config']['clip']) && is_array($config['config']['clip']) ? $config['config']['clip'] : [];
  711.             // this is to be backward compatible to <= v 1.4.7
  712.             $configurations $clipConfig;
  713.             if (isset($config[self::TYPE_DAILYMOTION]) && is_array($config[self::TYPE_DAILYMOTION])) {
  714.                 $configurations array_merge($clipConfig$config[self::TYPE_DAILYMOTION]);
  715.             }
  716.             if (!empty($configurations)) {
  717.                 foreach ($configurations as $key => $value) {
  718.                     if (in_array($key$valid_dailymotion_prams)) {
  719.                         if (is_bool($value)) {
  720.                             if ($value) {
  721.                                 $additional_params .= '&'.$key.'=1';
  722.                             } else {
  723.                                 $additional_params .= '&'.$key.'=0';
  724.                             }
  725.                         } else {
  726.                             $additional_params .= '&'.$key.'='.$value;
  727.                         }
  728.                     }
  729.                 }
  730.             }
  731.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video '. ($config['class'] ?? '') .'">
  732.                 <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>
  733.             </div>';
  734.             return $code;
  735.         }
  736.         // default => return the empty code
  737.         return $this->getEmptyCode();
  738.     }
  739.     /**
  740.      * @param array $urls
  741.      * @param Asset\Image\Thumbnail|Asset\Video\ImageThumbnail|null $thumbnail
  742.      *
  743.      * @return string
  744.      */
  745.     private function getHtml5Code($urls = [], $thumbnail null)
  746.     {
  747.         $code '';
  748.         $video $this->getVideoAsset();
  749.         if ($video) {
  750.             $duration ceil($video->getDuration());
  751.             $durationParts = ['PT'];
  752.             // hours
  753.             if ($duration 3600 >= 1) {
  754.                 $hours floor($duration 3600);
  755.                 $durationParts[] = $hours 'H';
  756.                 $duration $duration $hours 3600;
  757.             }
  758.             // minutes
  759.             if ($duration 60 >= 1) {
  760.                 $minutes floor($duration 60);
  761.                 $durationParts[] = $minutes 'M';
  762.                 $duration $duration $minutes 60;
  763.             }
  764.             $durationParts[] = $duration 'S';
  765.             $durationString implode(''$durationParts);
  766.             $code .= '<div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">' "\n";
  767.             $uploadDate = new \DateTime();
  768.             $uploadDate->setTimestamp($video->getCreationDate());
  769.             $jsonLd = [
  770.                 '@context' => 'https://schema.org',
  771.                 '@type' => 'VideoObject',
  772.                 'name' => $this->getTitle(),
  773.                 'description' => $this->getDescription(),
  774.                 'uploadDate' => $uploadDate->format('Y-m-d\TH:i:sO'),
  775.                 'duration' => $durationString,
  776.                 //'contentUrl' => Tool::getHostUrl() . $urls['mp4'],
  777.                 //"embedUrl" => "http://www.example.com/videoplayer.swf?video=123",
  778.                 //"interactionCount" => "1234",
  779.             ];
  780.             if (!$thumbnail) {
  781.                 $thumbnail $video->getImageThumbnail([]);
  782.             }
  783.             $jsonLd['contentUrl'] = $urls['mp4'];
  784.             if (!preg_match('@https?://@'$urls['mp4'])) {
  785.                 $jsonLd['contentUrl'] = Tool::getHostUrl() . $urls['mp4'];
  786.             }
  787.             $thumbnailUrl = (string)$thumbnail;
  788.             $jsonLd['thumbnailUrl'] = $thumbnailUrl;
  789.             if (!preg_match('@https?://@'$thumbnailUrl)) {
  790.                 $jsonLd['thumbnailUrl'] = Tool::getHostUrl() . $thumbnailUrl;
  791.             }
  792.             $code .= "\n\n<script type=\"application/ld+json\">\n" json_encode($jsonLd) . "\n</script>\n\n";
  793.             // default attributes
  794.             $attributesString '';
  795.             $attributes = [
  796.                 'width' => $this->getWidth(),
  797.                 'height' => $this->getHeight(),
  798.                 'poster' => $thumbnailUrl,
  799.                 'controls' => 'controls',
  800.                 'class' => 'pimcore_video',
  801.             ];
  802.             $config $this->getConfig();
  803.             if (array_key_exists('attributes'$config)) {
  804.                 $attributes array_merge($attributes$config['attributes']);
  805.             }
  806.             if (isset($config['removeAttributes']) && is_array($config['removeAttributes'])) {
  807.                 foreach ($config['removeAttributes'] as $attribute) {
  808.                     unset($attributes[$attribute]);
  809.                 }
  810.             }
  811.             // do not allow an empty controls editable
  812.             if (isset($attributes['controls']) && !$attributes['controls']) {
  813.                 unset($attributes['controls']);
  814.             }
  815.             if (isset($urls['mpd'])) {
  816.                 $attributes['data-dashjs-player'] = null;
  817.             }
  818.             foreach ($attributes as $key => $value) {
  819.                 $attributesString .= ' ' $key;
  820.                 if (!empty($value)) {
  821.                     $quoteChar '"';
  822.                     if (strpos($value'"')) {
  823.                         $quoteChar "'";
  824.                     }
  825.                     $attributesString .= '=' $quoteChar $value $quoteChar;
  826.                 }
  827.             }
  828.             $code .= '<video' $attributesString '>' "\n";
  829.             foreach ($urls as $type => $url) {
  830.                 if ($type == 'medias') {
  831.                     foreach ($url as $format => $medias) {
  832.                         foreach ($medias as $media => $mediaUrl) {
  833.                             $code .= '<source type="video/' $format '" src="' $mediaUrl '" media="' $media '"  />' "\n";
  834.                         }
  835.                     }
  836.                 } else {
  837.                     $code .= '<source type="video/' $type '" src="' $url '" />' "\n";
  838.                 }
  839.             }
  840.             $code .= '</video>' "\n";
  841.             $code .= '</div>' "\n";
  842.         }
  843.         return $code;
  844.     }
  845.     /**
  846.      * @param string|null $thumbnail
  847.      *
  848.      * @return string
  849.      */
  850.     private function getProgressCode($thumbnail null)
  851.     {
  852.         $uid 'video_' uniqid();
  853.         $code '
  854.         <div id="pimcore_video_' $this->getName() . '" class="pimcore_editable_video">
  855.             <style type="text/css">
  856.                 #' $uid ' .pimcore_editable_video_progress_status {
  857.                     box-sizing:content-box;
  858.                     background:#fff url(/bundles/pimcoreadmin/img/video-loading.gif) center center no-repeat;
  859.                     width:66px;
  860.                     height:66px;
  861.                     padding:20px;
  862.                     border:1px solid #555;
  863.                     box-shadow: 2px 2px 5px #333;
  864.                     border-radius:20px;
  865.                     margin: 0 20px 0 20px;
  866.                     top: calc(50% - 66px);
  867.                     left: calc(50% - 66px);
  868.                     position:absolute;
  869.                     opacity: 0.8;
  870.                 }
  871.             </style>
  872.             <div class="pimcore_editable_video_progress" id="' $uid '">
  873.                 <img src="' $thumbnail '" style="width: ' $this->getWidth() . 'px; height: ' $this->getHeight() . 'px;">
  874.                 <div class="pimcore_editable_video_progress_status"></div>
  875.             </div>
  876.         </div>';
  877.         return $code;
  878.     }
  879.     /**
  880.      * @return string
  881.      */
  882.     private function getEmptyCode(): string
  883.     {
  884.         $uid 'video_' uniqid();
  885.         $width $this->getWidth();
  886.         $height $this->getHeight();
  887.         if (is_numeric($width)) {
  888.             $width .= 'px';
  889.         }
  890.         if (is_numeric($height)) {
  891.             $height .= 'px';
  892.         }
  893.         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>';
  894.     }
  895.     private function updateAllowedTypesFromConfig(array $config): void
  896.     {
  897.         $this->allowedTypes self::ALLOWED_TYPES;
  898.         if (
  899.             isset($config['allowedTypes']) === true
  900.             && empty($config['allowedTypes']) === false
  901.             && empty(array_diff($config['allowedTypes'], self::ALLOWED_TYPES))
  902.         ) {
  903.             $this->allowedTypes $config['allowedTypes'];
  904.         }
  905.     }
  906.     /**
  907.      * {@inheritdoc}
  908.      */
  909.     public function isEmpty()
  910.     {
  911.         if ($this->id) {
  912.             return false;
  913.         }
  914.         return true;
  915.     }
  916.     /**
  917.      * @return string
  918.      */
  919.     public function getVideoType()
  920.     {
  921.         if (empty($this->type) === true) {
  922.             $this->type $this->getAllowedTypes()[0];
  923.         }
  924.         return $this->type;
  925.     }
  926.     /**
  927.      * @return Asset\Video|null
  928.      */
  929.     public function getVideoAsset()
  930.     {
  931.         if ($this->getVideoType() === self::TYPE_ASSET) {
  932.             return Asset\Video::getById($this->id);
  933.         }
  934.         return null;
  935.     }
  936.     /**
  937.      * @return Asset\Image|null
  938.      */
  939.     public function getPosterAsset()
  940.     {
  941.         return Asset\Image::getById($this->poster);
  942.     }
  943.     /**
  944.      * @param string|Asset\Video\Thumbnail\Config $config
  945.      *
  946.      * @return Asset\Image\Thumbnail|Asset\Video\ImageThumbnail|string
  947.      *
  948.      * TODO Pimcore 11: Change empty string return to null
  949.      */
  950.     public function getImageThumbnail($config)
  951.     {
  952.         if ($this->poster && ($poster Asset\Image::getById($this->poster))) {
  953.             return $poster->getThumbnail($config);
  954.         }
  955.         if ($this->getVideoAsset()) {
  956.             return $this->getVideoAsset()->getImageThumbnail($config);
  957.         }
  958.         return '';
  959.     }
  960.     /**
  961.      * @param string|Asset\Video\Thumbnail\Config $config
  962.      *
  963.      * @return array
  964.      */
  965.     public function getThumbnail($config)
  966.     {
  967.         if ($this->getVideoAsset()) {
  968.             return $this->getVideoAsset()->getThumbnail($config);
  969.         }
  970.         return [];
  971.     }
  972.     /**
  973.      * { @inheritdoc }
  974.      */
  975.     public function rewriteIds($idMapping/** : void */
  976.     {
  977.         if ($this->type == self::TYPE_ASSET && array_key_exists(self::TYPE_ASSET$idMapping) && array_key_exists($this->getId(), $idMapping[self::TYPE_ASSET])) {
  978.             $this->setId($idMapping[self::TYPE_ASSET][$this->getId()]);
  979.         }
  980.     }
  981. }