vendor/contao/core-bundle/src/Resources/contao/elements/ContentMedia.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\CoreBundle\Util\LocaleUtil;
  11. use Contao\Model\Collection;
  12. /**
  13.  * Content element "mediaelement".
  14.  *
  15.  * @todo Rename to ContentPlayer in Contao 5.0
  16.  */
  17. class ContentMedia extends ContentElement
  18. {
  19.     /**
  20.      * Template
  21.      * @var string
  22.      */
  23.     protected $strTemplate 'ce_player';
  24.     /**
  25.      * Files object
  26.      * @var Collection|FilesModel
  27.      */
  28.     protected $objFiles;
  29.     /**
  30.      * Return if there are no files
  31.      *
  32.      * @return string
  33.      */
  34.     public function generate()
  35.     {
  36.         if (!$this->playerSRC)
  37.         {
  38.             return '';
  39.         }
  40.         $source StringUtil::deserialize($this->playerSRC);
  41.         if (empty($source) || !\is_array($source))
  42.         {
  43.             return '';
  44.         }
  45.         $objFiles FilesModel::findMultipleByUuidsAndExtensions($source, array('mp4''m4v''mov''wmv''webm''ogv''m4a''mp3''wma''mpeg''wav''ogg'));
  46.         if ($objFiles === null)
  47.         {
  48.             return '';
  49.         }
  50.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  51.         // Display a list of files in the back end
  52.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  53.         {
  54.             $return '<ul>';
  55.             while ($objFiles->next())
  56.             {
  57.                 $objFile = new File($objFiles->path);
  58.                 $return .= '<li>' Image::getHtml($objFile->icon'''class="mime_icon"') . ' <span>' $objFile->name '</span> <span class="size">(' $this->getReadableSize($objFile->size) . ')</span></li>';
  59.             }
  60.             $return .= '</ul>';
  61.             if ($this->headline)
  62.             {
  63.                 $return '<' $this->hl '>' $this->headline '</' $this->hl '>' $return;
  64.             }
  65.             return $return;
  66.         }
  67.         $this->objFiles $objFiles;
  68.         return parent::generate();
  69.     }
  70.     /**
  71.      * Generate the module
  72.      */
  73.     protected function compile()
  74.     {
  75.         /** @var PageModel $objPage */
  76.         global $objPage;
  77.         $this->Template->poster false;
  78.         // Optional poster
  79.         if ($this->posterSRC && ($objFile FilesModel::findByUuid($this->posterSRC)) !== null)
  80.         {
  81.             $this->Template->poster $objFile->path;
  82.         }
  83.         $objFiles $this->objFiles;
  84.         /** @var FilesModel $objFirst */
  85.         $objFirst $objFiles->current();
  86.         // Pre-sort the array by preference
  87.         if (\in_array($objFirst->extension, array('mp4''m4v''mov''wmv''webm''ogv')))
  88.         {
  89.             $this->Template->isVideo true;
  90.             $this->Template->containerClass 'video_container';
  91.             $arrFiles = array('webm'=>null'mp4'=>null'm4v'=>null'mov'=>null'wmv'=>null'ogv'=>null);
  92.         }
  93.         else
  94.         {
  95.             $this->Template->isVideo false;
  96.             $this->Template->containerClass 'audio_container';
  97.             $arrFiles = array('m4a'=>null'mp3'=>null'wma'=>null'mpeg'=>null'wav'=>null'ogg'=>null);
  98.         }
  99.         // Convert the language to a locale (see #5678)
  100.         $strLanguage LocaleUtil::formatAsLocale($objPage->language);
  101.         $strCaption $this->playerCaption;
  102.         // Pass File objects to the template
  103.         foreach ($objFiles as $objFileModel)
  104.         {
  105.             /** @var FilesModel $objFileModel */
  106.             $objMeta $objFileModel->getMetadata($strLanguage);
  107.             $strTitle null;
  108.             if (null !== $objMeta)
  109.             {
  110.                 $strTitle $objMeta->getTitle();
  111.                 if (empty($strCaption))
  112.                 {
  113.                     $strCaption $objMeta->getCaption();
  114.                 }
  115.             }
  116.             $objFile = new File($objFileModel->path);
  117.             $objFile->title StringUtil::specialchars($strTitle ?: $objFile->name);
  118.             $arrFiles[$objFile->extension] = $objFile;
  119.         }
  120.         $size StringUtil::deserialize($this->playerSize);
  121.         if (\is_array($size) && !empty($size[0]) && !empty($size[1]))
  122.         {
  123.             $this->Template->size ' width="' $size[0] . '" height="' $size[1] . '"';
  124.         }
  125.         else
  126.         {
  127.             // $this->size might contain image size data, therefore unset it (see #2351)
  128.             $this->Template->size '';
  129.         }
  130.         $this->Template->files array_values(array_filter($arrFiles));
  131.         $attributes = array('controls' => 'controls');
  132.         $options StringUtil::deserialize($this->playerOptions);
  133.         if (\is_array($options))
  134.         {
  135.             foreach ($options as $option)
  136.             {
  137.                 if ($option == 'player_nocontrols')
  138.                 {
  139.                     unset($attributes['controls']);
  140.                 }
  141.                 else
  142.                 {
  143.                     $attributes[substr($option7)] = substr($option7);
  144.                 }
  145.             }
  146.         }
  147.         $this->Template->attributes $attributes;
  148.         $this->Template->preload $this->playerPreload;
  149.         $this->Template->caption $strCaption;
  150.         if ($this->playerStart || $this->playerStop)
  151.         {
  152.             $range '#t=';
  153.             if ($this->playerStart)
  154.             {
  155.                 $range .= $this->playerStart;
  156.             }
  157.             if ($this->playerStop)
  158.             {
  159.                 $range .= ',' $this->playerStop;
  160.             }
  161.             $this->Template->range $range;
  162.         }
  163.     }
  164. }
  165. class_alias(ContentMedia::class, 'ContentMedia');