vendor/madeyourday/contao-rocksolid-slider/src/Module/Slider.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright MADE/YOUR/DAY OG <mail@madeyourday.net>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace MadeYourDay\RockSolidSlider\Module;
  9. use MadeYourDay\RockSolidSlider\Model\SlideModel;
  10. use MadeYourDay\RockSolidSlider\Model\SliderModel;
  11. use MadeYourDay\RockSolidSlider\Model\ContentModel;
  12. /**
  13.  * Slider Frontend Module
  14.  *
  15.  * @author Martin Auswöger <martin@madeyourday.net>
  16.  */
  17. class Slider extends \Module
  18. {
  19.     /**
  20.      * @var string Template
  21.      */
  22.     protected $strTemplate 'rsts_default';
  23.     /**
  24.      * @return string
  25.      */
  26.     public function generate()
  27.     {
  28.         // Display a wildcard in the back end
  29.         if (TL_MODE === 'BE') {
  30.             $template = new \BackendTemplate('be_wildcard');
  31.             $template->wildcard '### ROCKSOLID SLIDER ###';
  32.             $template->title $this->name;
  33.             $template->id $this->id;
  34.             $template->link $this->name;
  35.             $template->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  36.             if ($this->objModel->rsts_id && ($slider SliderModel::findByPk($this->objModel->rsts_id)) !== null) {
  37.                 $template->id $slider->id;
  38.                 $template->link $slider->name;
  39.                 $template->href 'contao/main.php?do=rocksolid_slider&amp;table=tl_rocksolid_slide&amp;id=' $slider->id;
  40.             }
  41.             return $template->parse();
  42.         }
  43.         if (
  44.             $this->rsts_import_settings
  45.             && $this->rsts_import_settings_from
  46.             && ($settingsModule \ModuleModel::findByPk($this->rsts_import_settings_from))
  47.         ) {
  48.             $this->objModel->imgSize $settingsModule->imgSize;
  49.             $this->objModel->fullsize $settingsModule->fullsize;
  50.         }
  51.         if ($this->rsts_content_type === 'rsts_news') {
  52.             $newsModule = new SliderNews($this->objModel$this->strColumn);
  53.             $this->newsArticles $newsModule->getNewsArticles();
  54.             if (!count($this->newsArticles)) {
  55.                 // Return if there are no news articles
  56.                 return '';
  57.             }
  58.         }
  59.         else if ($this->rsts_content_type === 'rsts_events') {
  60.             $eventsModule = new SliderEvents($this->objModel$this->strColumn);
  61.             $this->eventItems $eventsModule->getEventItems();
  62.             if (!count($this->eventItems)) {
  63.                 // Return if there are no events
  64.                 return '';
  65.             }
  66.         }
  67.         else if ($this->rsts_content_type === 'rsts_settings') {
  68.             return '';
  69.         }
  70.         else if ($this->rsts_content_type === 'rsts_images' || !$this->rsts_id) {
  71.             $this->multiSRC deserialize($this->multiSRC);
  72.             if (!is_array($this->multiSRC) || !count($this->multiSRC)) {
  73.                 // Return if there are no images
  74.                 return '';
  75.             }
  76.         }
  77.         else {
  78.             $this->slider SliderModel::findByPk($this->rsts_id);
  79.             // Return if there is no slider
  80.             if (! $this->slider || $this->slider->id !== $this->rsts_id) {
  81.                 return '';
  82.             }
  83.             if ($this->slider->type === 'image') {
  84.                 $this->multiSRC deserialize($this->slider->multiSRC);
  85.                 $this->orderSRC $this->slider->orderSRC;
  86.             }
  87.         }
  88.         $this->files \FilesModel::findMultipleByUuids($this->multiSRC);
  89.         if (
  90.             $this->rsts_import_settings
  91.             && $this->rsts_import_settings_from
  92.             && ($settingsModule \ModuleModel::findByPk($this->rsts_import_settings_from))
  93.         ) {
  94.             $exclude = array('rsts_import_settings''rsts_import_settings_from''rsts_content_type''rsts_id');
  95.             $include = array('imgSize''fullsize');
  96.             foreach ($settingsModule->row() as $key => $value) {
  97.                 if (
  98.                     (substr($key05) === 'rsts_' || in_array($key$include))
  99.                     && !in_array($key$exclude)
  100.                 ) {
  101.                     $this->arrData[$key] = $value;
  102.                 }
  103.             }
  104.             $settingsCssId \StringUtil::deserialize($settingsModule->cssIDtrue);
  105.             if (!empty($settingsCssId[1])) {
  106.                 $this->arrData['cssID'][1] = (
  107.                     empty($this->arrData['cssID'][1]) ? '' $this->arrData['cssID'][1] . ' '
  108.                 ) . $settingsCssId[1];
  109.             }
  110.         }
  111.         if ($this->rsts_template) {
  112.             $this->strTemplate $this->rsts_template;
  113.         }
  114.         return parent::generate();
  115.     }
  116.     /**
  117.      * Generate the module
  118.      */
  119.     protected function compile()
  120.     {
  121.         global $objPage;
  122.         $images = array();
  123.         if ($this->files) {
  124.             $files $this->files;
  125.             $filesExpaned = array();
  126.             // Get all images
  127.             while ($files->next()) {
  128.                 if ($files->type === 'file') {
  129.                     $filesExpaned[] = $files->current();
  130.                 }
  131.                 else {
  132.                     $subFiles \FilesModel::findByPid($files->uuid);
  133.                     while ($subFiles && $subFiles->next()) {
  134.                         if ($subFiles->type === 'file'){
  135.                             $filesExpaned[] = $subFiles->current();
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.             foreach ($filesExpaned as $files) {
  141.                 // Continue if the files has been processed or does not exist
  142.                 if (isset($images[$files->path]) || ! file_exists(TL_ROOT '/' $files->path)) {
  143.                     continue;
  144.                 }
  145.                 $file = new \File($files->pathtrue);
  146.                 if (!$file->isGdImage && !$file->isImage) {
  147.                     continue;
  148.                 }
  149.                 $arrMeta $this->getMetaData($files->meta$objPage->language);
  150.                 // Add the image
  151.                 $images[$files->path] = array
  152.                 (
  153.                     'id'        => $files->id,
  154.                     'uuid'      => isset($files->uuid) ? $files->uuid null,
  155.                     'name'      => $file->basename,
  156.                     'singleSRC' => $files->path,
  157.                     'alt'       => $arrMeta['alt'] ?? null,
  158.                     'title'     => $arrMeta['title'] ?? null,
  159.                     'imageUrl'  => $arrMeta['link'] ?? null,
  160.                     'caption'   => $arrMeta['caption'] ?? null,
  161.                     'fullsize'  => $this->fullsize,
  162.                 );
  163.             }
  164.             if ($this->orderSRC) {
  165.                 // Turn the order string into an array and remove all values
  166.                 $order deserialize($this->orderSRC);
  167.                 if (!$order || !is_array($order)) {
  168.                     $order = array();
  169.                 }
  170.                 $order array_flip($order);
  171.                 $order array_map(function(){}, $order);
  172.                 // Move the matching elements to their position in $order
  173.                 foreach ($images as $k => $v) {
  174.                     if (array_key_exists($v['uuid'], $order)) {
  175.                         $order[$v['uuid']] = $v;
  176.                         unset($images[$k]);
  177.                     }
  178.                 }
  179.                 $order array_merge($orderarray_values($images));
  180.                 // Remove empty (unreplaced) entries
  181.                 $images array_filter($order);
  182.                 unset($order);
  183.             }
  184.             $images array_values($images);
  185.             foreach ($images as $key => $image) {
  186.                 $newImage = new \stdClass();
  187.                 $image['size'] = isset($this->imgSize) ? $this->imgSize $this->size;
  188.                 $this->addImageToTemplate($newImage$imagenullsubstr(md5('mod_rocksolid_slider_' $this->id), 06), \FilesModel::findByPk($image['id']));
  189.                 if ($this->rsts_navType === 'thumbs') {
  190.                     $newImage->thumb = new \stdClass;
  191.                     $image['size'] = $this->rsts_thumbs_imgSize;
  192.                     $this->addImageToTemplate($newImage->thumb$image);
  193.                 }
  194.                 $images[$key] = $newImage;
  195.             }
  196.         }
  197.         // use custom skin if specified
  198.         if (trim($this->arrData['rsts_customSkin'])) {
  199.             $this->arrData['rsts_skin'] = trim($this->arrData['rsts_customSkin']);
  200.         }
  201.         $this->Template->images $images;
  202.         $slides = array();
  203.         if (isset($this->newsArticles)) {
  204.             foreach ($this->newsArticles as $newsArticle) {
  205.                 $slides[] = array(
  206.                     'text' => $newsArticle,
  207.                 );
  208.             }
  209.         }
  210.         else if (isset($this->eventItems)) {
  211.             foreach ($this->eventItems as $eventItem) {
  212.                 $slides[] = array(
  213.                     'text' => $eventItem,
  214.                 );
  215.             }
  216.         }
  217.         else if (isset($this->slider->id) && $this->slider->type === 'content') {
  218.             $slides $this->parseSlides(SlideModel::findPublishedByPid($this->slider->id));
  219.         }
  220.         $this->Template->slides $slides;
  221.         $options = array();
  222.         // strings
  223.         foreach (array(
  224.             'type',
  225.             'direction',
  226.             'cssPrefix',
  227.             'skin',
  228.             'width',
  229.             'height',
  230.             'navType',
  231.             'scaleMode',
  232.             'imagePosition',
  233.             'deepLinkPrefix',
  234.             'thumbs_width',
  235.             'thumbs_height',
  236.             'thumbs_scaleMode',
  237.             'thumbs_imagePosition',
  238.         ) as $key) {
  239.             if (! empty($this->arrData['rsts_' $key])) {
  240.                 $options[$key] = $this->arrData['rsts_' $key];
  241.             }
  242.         }
  243.         // strings / boolean
  244.         foreach (array('centerContent') as $key) {
  245.             if (! empty($this->arrData['rsts_' $key])) {
  246.                 $options[$key] = $this->arrData['rsts_' $key];
  247.                 if ($options[$key] === 'false') {
  248.                     $options[$key] = false;
  249.                 }
  250.                 if ($options[$key] === 'true') {
  251.                     $options[$key] = true;
  252.                 }
  253.             }
  254.         }
  255.         // boolean
  256.         foreach (array(
  257.             'random',
  258.             'loop',
  259.             'videoAutoplay',
  260.             'autoplayProgress',
  261.             'pauseAutoplayOnHover',
  262.             'keyboard',
  263.             'captions',
  264.             'controls',
  265.             'thumbControls',
  266.             'combineNavItems',
  267.             'thumbs_controls',
  268.         ) as $key) {
  269.             $options[$key] = (bool) $this->arrData['rsts_' $key];
  270.         }
  271.         // positive numbers
  272.         foreach (array(
  273.             'preloadSlides',
  274.             'duration',
  275.             'autoplay',
  276.             'autoplayRestart',
  277.             'slideMaxCount',
  278.             'slideMinSize',
  279.             'slideMaxSize',
  280.             'rowMaxCount',
  281.             'rowMinSize',
  282.             'prevNextSteps',
  283.             'visibleAreaMax',
  284.             'thumbs_duration',
  285.             'thumbs_slideMaxCount',
  286.             'thumbs_slideMinSize',
  287.             'thumbs_slideMaxSize',
  288.             'thumbs_rowMaxCount',
  289.             'thumbs_rowMinSize',
  290.             'thumbs_prevNextSteps',
  291.             'thumbs_visibleAreaMax',
  292.         ) as $key) {
  293.             if (! empty($this->arrData['rsts_' $key]) && $this->arrData['rsts_' $key] > 0) {
  294.                 $options[$key] = $this->arrData['rsts_' $key] * 1;
  295.             }
  296.         }
  297.         // percentages
  298.         foreach (array('visibleArea''thumbs_visibleArea') as $key) {
  299.             if (!empty($this->arrData['rsts_' $key])) {
  300.                 $options[$key] = $this->arrData['rsts_' $key] / 100;
  301.             }
  302.         }
  303.         // percentages including zero
  304.         foreach (array('visibleAreaAlign') as $key) {
  305.             if (!empty($this->arrData['rsts_' $key])) {
  306.                 $options[$key] = $this->arrData['rsts_' $key] / 100;
  307.             }
  308.             else {
  309.                 $options[$key] = 0;
  310.             }
  311.         }
  312.         // ratios
  313.         foreach (array('rowSlideRatio''thumbs_rowSlideRatio') as $key) {
  314.             if (!empty($this->arrData['rsts_' $key])) {
  315.                 $ratio explode('x'$this->arrData['rsts_' $key], 2);
  316.                 if (empty($ratio[1])) {
  317.                     $ratio floatval($ratio[0]);
  318.                 }
  319.                 else {
  320.                     $ratio floatval($ratio[1]) / floatval($ratio[0]);
  321.                 }
  322.                 $options[$key] = $ratio;
  323.             }
  324.         }
  325.         // gap size
  326.         foreach (array('gapSize''thumbs_gapSize') as $key) {
  327.             if (isset($this->arrData['rsts_' $key]) && $this->arrData['rsts_' $key] !== '') {
  328.                 if (substr($this->arrData['rsts_' $key], -1) === '%') {
  329.                     $options[$key] = $this->arrData['rsts_' $key];
  330.                 }
  331.                 else {
  332.                     $options[$key] = $this->arrData['rsts_' $key] * 1;
  333.                 }
  334.             }
  335.         }
  336.         foreach ($options as $key => $value) {
  337.             if (substr($key07) === 'thumbs_') {
  338.                 $options['thumbs'][substr($key7)] = $value;
  339.                 unset($options[$key]);
  340.             }
  341.         }
  342.         if (empty($this->arrData['rsts_thumbs']) && isset($options['thumbs'])) {
  343.             unset($options['thumbs']);
  344.         }
  345.         $this->Template->options $options;
  346.         $assetsDir 'bundles/rocksolidslider';
  347.         $GLOBALS['TL_JAVASCRIPT'][] = $assetsDir '/js/rocksolid-slider.min.js|static';
  348.         $GLOBALS['TL_CSS'][] = $assetsDir '/css/rocksolid-slider.min.css||static';
  349.         $skinPath $assetsDir '/css/' . (empty($this->arrData['rsts_skin']) ? 'default' $this->arrData['rsts_skin']) . '-skin.min.css';
  350.         if (file_exists(\System::getContainer()->getParameter('contao.web_dir') . '/' $skinPath)) {
  351.             $GLOBALS['TL_CSS'][] = $skinPath '||static';
  352.         }
  353.     }
  354.     /**
  355.      * Parse slides
  356.      *
  357.      * @param  \Model\Collection $objSlides slides retrieved from the database
  358.      * @return array                        parsed slides
  359.      */
  360.     protected function parseSlides($objSlides)
  361.     {
  362.         global $objPage;
  363.         $slides = array();
  364.         $pids = array();
  365.         $idIndexes = array();
  366.         if (! $objSlides) {
  367.             return $slides;
  368.         }
  369.         while ($objSlides->next()) {
  370.             $slide $objSlides->row();
  371.             $slide['text'] = '';
  372.             if ($slide['type'] === 'content') {
  373.                 $pids[] = $slide['id'];
  374.                 $idIndexes[(int)$slide['id']] = count($slides);
  375.             }
  376.             if (
  377.                 in_array($slide['type'], array('image''video')) &&
  378.                 trim($slide['singleSRC']) &&
  379.                 ($file \FilesModel::findByUuid($slide['singleSRC'])) &&
  380.                 ($fileObject = new \File($file->pathtrue)) &&
  381.                 ($fileObject->isGdImage || $fileObject->isImage)
  382.             ) {
  383.                 $meta $this->getMetaData($file->meta$objPage->language);
  384.                 $slide['image'] = new \stdClass;
  385.                 $this->addImageToTemplate($slide['image'], array(
  386.                     'id' => $file->id,
  387.                     'name' => $fileObject->basename,
  388.                     'singleSRC' => $file->path,
  389.                     'alt' => $meta['alt'] ?? null,
  390.                     'title' => $meta['title'] ?? null,
  391.                     'imageUrl' => $meta['link'] ?? null,
  392.                     'caption' => $meta['caption'] ?? null,
  393.                     'size' => isset($this->imgSize) ? $this->imgSize $this->size,
  394.                     'fullsize' => $this->fullsize,
  395.                 ), nullsubstr(md5('mod_rocksolid_slider_' $this->id), 06), $file);
  396.             }
  397.             if ($slide['type'] === 'video' && $slide['videoURL'] && empty($slide['image'])) {
  398.                 $slide['image'] = new \stdClass;
  399.                 if (preg_match(
  400.                     '(^
  401.                         https?://  # http or https
  402.                         (?:
  403.                             www\\.youtube\\.com/(?:watch\\?v=|v/|embed/)  # Different URL formats
  404.                             | youtu\\.be/  # Short YouTube domain
  405.                         )
  406.                         ([0-9a-z_\\-]{11})  # YouTube ID
  407.                         (?:$|&|/)  # End or separator
  408.                     )ix',
  409.                     html_entity_decode($slide['videoURL']), $matches)
  410.                 ) {
  411.                     $video $matches[1];
  412.                     $slide['image']->src '//img.youtube.com/vi/' $video '/0.jpg';
  413.                 }
  414.                 else {
  415.                     // Grey dummy image
  416.                     $slide['image']->src 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAJCAMAAAAM9FwAAAAAA1BMVEXGxsbd/8BlAAAAFUlEQVR42s3BAQEAAACAkP6vdiO6AgCZAAG/wrlvAAAAAElFTkSuQmCC';
  417.                 }
  418.                 $slide['image']->imgSize '';
  419.                 $slide['image']->alt '';
  420.                 $slide['image']->title '';
  421.                 $slide['image']->picture = array(
  422.                     'img' => array('src' => $slide['image']->src'srcset' => $slide['image']->src),
  423.                     'sources' => array(),
  424.                 );
  425.             }
  426.             if ($slide['type'] !== 'video' && $slide['videoURL']) {
  427.                 $slide['videoURL'] = '';
  428.             }
  429.             if ($slide['type'] === 'video' && $slide['videos']) {
  430.                 $videoFiles deserialize($slide['videos'], true);
  431.                 $videoFiles \FilesModel::findMultipleByUuids($videoFiles);
  432.                 $videos = array();
  433.                 foreach ($videoFiles as $file) {
  434.                     $videos[] = $file;
  435.                 }
  436.                 $slide['videos'] = $videos;
  437.             }
  438.             else {
  439.                 $slide['videos'] = null;
  440.             }
  441.             if (
  442.                 trim($slide['backgroundImage']) &&
  443.                 ($file \FilesModel::findByUuid($slide['backgroundImage'])) &&
  444.                 ($fileObject = new \File($file->pathtrue)) &&
  445.                 ($fileObject->isGdImage || $fileObject->isImage)
  446.             ) {
  447.                 $meta $this->getMetaData($file->meta$objPage->language);
  448.                 $slide['backgroundImage'] = new \stdClass;
  449.                 $this->addImageToTemplate($slide['backgroundImage'], array(
  450.                     'id' => $file->id,
  451.                     'name' => $fileObject->basename,
  452.                     'singleSRC' => $file->path,
  453.                     'alt' => $meta['alt'] ?? null,
  454.                     'title' => $meta['title'] ?? null,
  455.                     'imageUrl' => $meta['link'] ?? null,
  456.                     'caption' => $meta['caption'] ?? null,
  457.                     'size' => $slide['backgroundImageSize'] ?? null,
  458.                 ));
  459.             }
  460.             else {
  461.                 $slide['backgroundImage'] = null;
  462.             }
  463.             if ($slide['backgroundVideos']) {
  464.                 $videoFiles deserialize($slide['backgroundVideos'], true);
  465.                 $videoFiles \FilesModel::findMultipleByUuids($videoFiles);
  466.                 $videos = array();
  467.                 foreach ($videoFiles as $file) {
  468.                     $videos[] = $file;
  469.                 }
  470.                 $slide['backgroundVideos'] = $videos;
  471.             }
  472.             if ($this->rsts_navType === 'thumbs') {
  473.                 $slide['thumb'] = new \stdClass;
  474.                 if (
  475.                     trim($slide['thumbImage']) &&
  476.                     ($file \FilesModel::findByUuid($slide['thumbImage'])) &&
  477.                     ($fileObject = new \File($file->pathtrue)) &&
  478.                     ($fileObject->isGdImage || $fileObject->isImage)
  479.                 ) {
  480.                     $this->addImageToTemplate($slide['thumb'], array(
  481.                         'id' => $file->id,
  482.                         'name' => $fileObject->basename,
  483.                         'singleSRC' => $file->path,
  484.                         'size' => $this->rsts_thumbs_imgSize,
  485.                     ));
  486.                 }
  487.                 elseif (
  488.                     in_array($slide['type'], array('image''video')) &&
  489.                     trim($slide['singleSRC']) &&
  490.                     ($file \FilesModel::findByUuid($slide['singleSRC'])) &&
  491.                     ($fileObject = new \File($file->pathtrue)) &&
  492.                     ($fileObject->isGdImage || $fileObject->isImage)
  493.                 ) {
  494.                     $this->addImageToTemplate($slide['thumb'], array(
  495.                         'id' => $file->id,
  496.                         'name' => $fileObject->basename,
  497.                         'singleSRC' => $file->path,
  498.                         'size' => $this->rsts_thumbs_imgSize,
  499.                     ));
  500.                 }
  501.                 elseif (!empty($slide['image']->src)) {
  502.                     $slide['thumb'] = clone $slide['image'];
  503.                 }
  504.                 elseif (!empty($slide['backgroundImage']->src)) {
  505.                     $slide['thumb'] = clone $slide['backgroundImage'];
  506.                 }
  507.             }
  508.             $slides[] = $slide;
  509.         }
  510.         if (count($pids)) {
  511.             $slideContents ContentModel::findPublishedByPidsAndTable($pidsSlideModel::getTable());
  512.             if ($slideContents) {
  513.                 while ($slideContents->next()) {
  514.                     $slides[$idIndexes[(int)$slideContents->pid]]['text'] .= $this->getContentElement($slideContents->current());
  515.                 }
  516.             }
  517.         }
  518.         return $slides;
  519.     }
  520. }