system/modules/pct_revolutionslider/RevolutionSlider/Core/Slide.php line 390

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  * 
  5.  * Copyright (C) 2005-2013 Leo Feyer
  6.  * 
  7.  * @copyright    Tim Gatzky 2013
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        revolutionslider
  10.  * @link        http://contao.org
  11.  * @license        http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  12.  */
  13. /**
  14.  * Namespace
  15.  */
  16. namespace RevolutionSlider\Core;
  17. /**
  18.  * Imports
  19.  */
  20. use Contao\Config;
  21. use Contao\FilesModel;
  22. use Contao\Controller;
  23. use Contao\Image;
  24. use Contao\StringUtil;
  25. /**
  26.  * Class files
  27.  * RevolutionSliderSlide
  28.  */
  29. class Slide
  30. {
  31.     /**
  32.      * Content elements array
  33.      * @var array
  34.      */
  35.     protected $arrContent = array();
  36.     
  37.     /**
  38.      * Attributes array
  39.      * @var array
  40.      */
  41.     protected $arrAttributes = array();
  42.     
  43.     /**
  44.      * Data Array
  45.      * @var array
  46.      */
  47.     protected $arrData = array();
  48.     /**
  49.      * Create new instance
  50.      * @param array
  51.      */ 
  52.     public function __construct($arrData)
  53.     {
  54.         $this->arrData $arrData;
  55.         
  56.         // fetch the slider model
  57.         // @var object
  58.         $this->Slider \RevolutionSlider\Models\Slider::findByPk($arrData['pid']);
  59.         
  60.         $this->arrContent $this->prepareContent($arrData['id']);
  61.         $this->arrAttributes = array
  62.         (
  63.             'data-transition'    => $arrData['transition'] ?? '',
  64.             'data-slotamount'    => $arrData['slotamount'] ?? 4,
  65.         );
  66.         
  67.         // fallback transition
  68.         if(!$this->arrAttributes['data-transition'])
  69.         {
  70.             $this->arrAttributes['data-transition'] = $this->Slider->transition;
  71.         }
  72.         
  73.         // delay
  74.         if($arrData['delay'] > 0)
  75.         {
  76.             $this->arrAttributes['data-delay'] = $arrData['delay'] * 1000;
  77.         }
  78.         
  79.         // masterspeed
  80.         if($arrData['masterspeed'] > 0)
  81.         {
  82.             $this->arrAttributes['data-masterspeed'] = $arrData['masterspeed'] * 1000;
  83.         }
  84.         
  85.         // link slide
  86.         if(strlen($arrData['slideUrl']) > 0)
  87.         {
  88.             $this->arrAttributes['data-link'] = $arrData['slideUrl'];
  89.             $this->arrAttributes['data-target'] = ($arrData['newWindow'] ? '_blank' '');
  90.         }
  91.         // generate thumbs
  92.         $this->arrAttributes['data-thumb'] = $this->getThumb();
  93.         
  94.         // slide title
  95.         $this->arrAttributes['data-title'] = $arrData['title'];
  96.         // param1
  97.         if( empty($arrData['subtitle']) === false )
  98.         {
  99.             $this->arrAttributes['data-param1'] = $arrData['subtitle'];
  100.         }
  101.         if( $arrData['data_bgcolor'] )
  102.         {
  103.             // bg_color
  104.             $this->arrAttributes['data-bgcolor'] = '#'.$arrData['data_bgcolor'];
  105.         }
  106.     }
  107.     
  108.     /**
  109.      * Set Data
  110.      * @param array
  111.      */
  112.     public function setData($arrData)
  113.     {
  114.         $this->arrData $arrData;
  115.     }
  116.     /**
  117.      * Getters
  118.      * @param string
  119.      */
  120.     public function get($strKey)
  121.     {
  122.         if(isset($this->$strKey))
  123.         {
  124.             return $this->$strKey;
  125.         }
  126.         else if(array_key_exists($strKey$this->arrData))
  127.         {
  128.             return $this->arrData[$strKey];
  129.         }
  130.         else
  131.         {
  132.             return null;
  133.         }
  134.     }
  135.     
  136.     /**
  137.      * Setters
  138.      * @param string
  139.      * @param mixed
  140.      */
  141.     public function set($strKey$varValue)
  142.     {
  143.         if(isset($this->$strKey))
  144.         {
  145.             $this->$strKey $varValue;
  146.         }
  147.         else if(array_key_exists($strKey$this->arrData))
  148.         {
  149.             $this->arrData[$strKey] = $varValue;
  150.         }
  151.         else
  152.         {
  153.             return null;
  154.         }
  155.     }
  156.     /** 
  157.      * Prepare the data- attributes logic from an array
  158.      * @param array
  159.      * @return string 
  160.      */
  161.     public static function buildDataAttributeValueFromArray($arrInput)
  162.     {
  163.         if( empty($arrInput) )
  164.         {
  165.             return '';
  166.         }
  167.         $tmp = array();
  168.         foreach($arrInput as $v)
  169.         {
  170.             if( empty($v) )
  171.             {
  172.                 continue;
  173.             }
  174.             $tmp[] = "'".$v."'";
  175.         }
  176.         return '['.\implode(',',$tmp).']';
  177.     }
  178.         
  179.     /**
  180.      * Prepare the slide
  181.      * Fetch all content elements
  182.      * @param array
  183.      * @return array
  184.      */
  185.     public function prepareContent($intSlide)
  186.     {
  187.         $objContentModels \Contao\ContentModel::findPublishedByPidAndTable($intSlide,'tl_revolutionslider_slides');
  188.         
  189.         if($objContentModels === null)
  190.         {
  191.             return array();
  192.         }
  193.         
  194.         // generate content
  195.         $arrReturn = array();
  196.         foreach($objContentModels as $objContentModel)
  197.         {
  198.             $cte $objContentModel->row();
  199.             
  200.             if( !isset($cte['revolutionslider_data_speed']) )
  201.             {
  202.                 $cte['revolutionslider_data_speed'] = 0;
  203.             }
  204.             if( !isset($cte['revolutionslider_data_start']) )
  205.             {
  206.                 $cte['revolutionslider_data_start'] = 0;
  207.             }
  208.             if( !isset($cte['revolutionslider_data_delay']) )
  209.             {
  210.                 $cte['revolutionslider_data_delay'] = 0;
  211.             }
  212.             
  213.                         
  214.             // attributes
  215.             $data_position StringUtil::deserialize($cte['revolutionslider_data_position']);
  216.             $data_position9 explode(' ',$cte['revolutionslider_data_pos9grid']);
  217.             $data_position_m StringUtil::deserialize($cte['revolutionslider_data_position_m']);
  218.             $arrAttributes = array
  219.             (
  220.                 'data-easing'    => $cte['revolutionslider_data_easing'],
  221.                 'data-speed'    => ($cte['revolutionslider_data_speed'] > $cte['revolutionslider_data_speed'] * 1000 0), // sec to ms
  222.                 'data-y'        => (strlen($data_position[1]) ? $data_position[1]: $data_position9[1]),
  223.                 'data-start'    => ($cte['revolutionslider_data_start'] > $cte['revolutionslider_data_start'] * 1000 100),
  224.                 'data-elementdelay' => ($cte['revolutionslider_data_delay'] > $cte['revolutionslider_data_delay'] / 100 0),
  225.             );
  226.             // DESKTOP, SMARTPHONE
  227.             #$pos_x = $data_position[0];
  228.             #$pos_y = $data_position[1];
  229.             #$pos_x_m = $data_position_m[0];
  230.             #$pos_y_m = $data_position_m[1];
  231.             #$hoffset = 0;
  232.             #$hoffset_m = null;
  233.             #$voffset = 0;
  234.             #$voffset_m = null;
  235.             // positions
  236.             $arrPosition = array
  237.             (
  238.                 'x' => $data_position[0],
  239.                 'y' => $data_position[1],
  240.                 'hoffset' => null,
  241.                 'voffset' => null,
  242.             );
  243.             // positions mobile
  244.             if(is_string($data_position_m) && strlen($data_position_m) > 0) {
  245.                 $arrPositionM = array
  246.                 (
  247.                     'x' => $data_position_m[0],
  248.                     'y' => $data_position_m[1],
  249.                     'hoffset' => null,
  250.                     'voffset' => null,
  251.                 );
  252.             }
  253.             // h offset
  254.             if(isset($data_position[0]) && strlen($data_position9[0]) > 0)
  255.             {
  256.                 $arrPosition['x'] = $arrPositionM['x'] = $data_position9[0];
  257.                 $arrPosition['hoffset'] = $data_position[0];
  258.                 $arrPositionM['hoffset'] = $data_position_m[0];
  259.             }
  260.             
  261.             // v offset
  262.             if(isset($data_position[1]) && isset($data_position9[1]))
  263.             {
  264.                 $arrPosition['y'] = $arrPositionM['y'] = str_replace(array('center'),array('middle'), $data_position9[1]);
  265.                 $arrPosition['voffset'] = $data_position[1];
  266.                 $arrPositionM['voffset'] = $data_position_m[1];
  267.             }
  268.             #$arrAttributes['data-x'] = $this->buildDataAttributeValueFromArray( array($pos_x,$pos_x_m) ); #\sprintf("['%s','%s']",$pos_x,$pos_x_m); #"['$data_position[0]','','','$data_position_m[0]']";
  269.             #$arrAttributes['data-y'] = $this->buildDataAttributeValueFromArray( array($pos_y,$pos_y_m) );
  270.             #$arrAttributes['data-hoffset'] = $this->buildDataAttributeValueFromArray( array($hoffset,$hoffset_m) ); #\sprintf("['%s','%s']",$hoffset,$hoffset_m); #"['$data_position[0]','','','$data_position_m[0]']";
  271.             #$arrAttributes['data-voffset'] = $this->buildDataAttributeValueFromArray( array($voffset,$voffset_m) ); #"['$data_position[0]','','','$data_position_m[0]']";
  272.             
  273.             // check if there is atleas one mobile value and mark this slider as mobile first
  274.             $GLOBALS['REVOLUTIONSLIDER_HAS_MOBILE'][ $this->Slider->id ] = false;
  275.             if(!isset($arrPositionM) || empty( array_filter$arrPositionM ) ) === false )
  276.             {
  277.                 $GLOBALS['REVOLUTIONSLIDER_HAS_MOBILE'][ $this->Slider->id ] = true;
  278.             }
  279.             
  280.             // DESKTOP, SMARTPHONE
  281.             if(isset($arrPosition) && isset($arrPositionM)) {
  282.                 $arrAttributes['data-x'] = \sprintf("['%s','%s']",$arrPosition['x'],$arrPositionM['x']);
  283.                 $arrAttributes['data-y'] = \sprintf("['%s','%s']",$arrPosition['y'],$arrPositionM['y']);
  284.                 if( empty($arrPosition['hoffset']) === false || empty($arrPositionM['hoffset']) === false )
  285.                 {
  286.                     $arrAttributes['data-hoffset'] = \sprintf("['%s','%s']",$arrPosition['hoffset'],$arrPositionM['hoffset']);
  287.                 }
  288.                 if( empty($arrPosition['voffset']) === false || empty($arrPositionM['voffset']) === false )
  289.                 {
  290.                     $arrAttributes['data-voffset'] = \sprintf("['%s','%s']",$arrPosition['voffset'],$arrPositionM['voffset']); #"['$data_position[0]','','','$data_position_m[0]']";
  291.                 }
  292.             }
  293.             $arrAttributes['data-lineheight'] = "['inherit','inherit']";
  294.             $arrAttributes['data-whitespace'] = "nowrap";
  295.             $arrAttributes['data-responsive_offset'] = "on";    
  296.             
  297.             // visibility
  298.             $arrAttributes['data-visibility'] = "['on','on']";
  299.             if( $objContentModel->revolutionslider_visibility == )
  300.             {
  301.                 $arrAttributes['data-visibility'] = "['on','off']";
  302.             }
  303.             else if( $objContentModel->revolutionslider_visibility == )
  304.             {
  305.                 $arrAttributes['data-visibility'] = "['off','on']";
  306.             }
  307.             // hyperlinks
  308.             if( \in_array($objContentModel->type, array('hyperlink','revolutionslider_hyperlink') )  )
  309.             {
  310.                 $arrAttributes['data-responsive'] = "off";
  311.                 if( $arrPosition['x'] == 'center' )
  312.                 {
  313.                     $arrAttributes['data-responsive_offset'] = "off";
  314.                 }    
  315.             }
  316.             // data type
  317.             $arrAttributes['data-type'] = 'text';
  318.             if( \in_array($objContentModel->type, array('image','revolutionslider_image') )  )
  319.             {
  320.                 $arrAttributes['data-type'] = 'image';
  321.             }
  322.             //! data-fontsize
  323.             $arrFontSize StringUtil::deserialize($objContentModel->revolutionslider_text_fontsize);
  324.             if( !\is_array($arrFontSize) )
  325.             {
  326.                 $arrFontSize \explode(',',$arrFontSize);
  327.             }
  328.             if( empty($arrFontSize[1]) === false )
  329.             {
  330.                 $arrAttributes['data-fontsize'] = \sprintf("['%s','%s']",$arrFontSize[0],$arrFontSize[1]);
  331.             }
  332.             if( \in_array($objContentModel->type, array('hyperlink','revolutionslider_hyperlink') )  )
  333.             {
  334.                 unset($arrAttributes['data-fontsize']);
  335.             }
  336.             //--
  337.             
  338.             // out animation attributes
  339.             if( (boolean)$cte['revolutionslider_OUT'] === true)
  340.             {
  341.                 $arrAttributes['data-endelementdelay']     = ( !empty($objContentModel->revolutionslider_data_start_OUT) ? $cte['revolutionslider_data_start_OUT'] * 1000 'wait');
  342.                 $arrAttributes['data-endspeed']         = $cte['revolutionslider_data_speed_OUT'] * 1000;
  343.                 $arrAttributes['data-endeasing']         = $cte['revolutionslider_data_easing_OUT'] ?: 'wait';
  344.             }
  345.             //! link element to slide
  346.             if($cte['revolutionslider_data_linktoslide'] > 0)
  347.             {
  348.                 $objModels \RevolutionSlider\Models\Slides::findBy( array('pid=?','published=?'), array($this->get('pid'),1), array('order' => 'sorting') );
  349.                 if( $objModels !== null )
  350.                 {
  351.                     foreach($objModels as $i => $objModel)
  352.                     {
  353.                         if($cte['revolutionslider_data_linktoslide'] == $objModel->id)
  354.                         {
  355.                             $arrAttributes['data-linktoslide'] = $i 1;
  356.                         }
  357.                     }
  358.                 }
  359.             }
  360.             
  361.             // disable debug mode
  362.             $blnDebug Config::get('debugMode');
  363.             Config::set('debugMode',0);
  364.             // generate the content element
  365.             $strCte Controller::replaceInsertTags('{{insert_content::'.$cte['id'].'}}');
  366.         
  367.             Config::set('debugMode',$blnDebug);
  368.             //! data-frames
  369.             
  370.             $strFrameJson $GLOBALS['REVOLUTIONSLIDER']['FRAMES'][ $objContentModel->revolutionslider_data_animation_start ];
  371.             $arrFrameJson \explode('},{',$strFrameJson);
  372.             $arrFrameJson[0] .= '}';
  373.             $arrFrameJson[1] = '{'.$arrFrameJson[1];
  374.             
  375.             $arrFrames = array
  376.             (
  377.                 'start' => \json_decode($arrFrameJson[0],true),
  378.                 'end'     => \json_decode($arrFrameJson[1],true),
  379.             );
  380.             // map attributes to frame
  381.             $frameFromAttributes = array
  382.             (
  383.                 'start' => array
  384.                 (
  385.                     'delay' => 'data-start',
  386.                     'speed' => 'data-speed',
  387.                     'ease'    => 'data-easing',
  388.                 ),
  389.                 'end' => array
  390.                 (
  391.                     'delay' => 'data-start',
  392.                     'speed' => 'data-endspeed',
  393.                     'ease'    => 'data-endeasing',
  394.                 ),
  395.             );
  396.             // if no out animation is set, use the default
  397.             if( (boolean)$objContentModel->revolutionslider_OUT === false)
  398.             {
  399.                 unset( $frameFromAttributes['end'] );
  400.             }
  401.             
  402.             foreach( $frameFromAttributes as $k => $arr)
  403.             {
  404.                 foreach( $arr as $frame => $attr )
  405.                 {
  406.                     if( isset($arrAttributes$attr ]) )
  407.                     {
  408.                         $arrFrames[$k][$frame ] = $arrAttributes$attr ];
  409.                         // remove from attributes
  410.                         unset( $arrAttributes$attr ] );
  411.                     }
  412.                 }
  413.             }
  414.             $json_frames_start \json_encode($arrFrames['start']);
  415.             $json_frames_end =  \json_encode($arrFrames['end']);
  416.             
  417.             // inject attributes in content element
  418.             $strAttributes '';
  419.             foreach($arrAttributes as $attr => $value)
  420.             {
  421.                 $strAttributes .= $attr .'="'.$value.'" ';
  422.             }
  423.             trim($strAttributes);
  424.             
  425.             $str_data_frames \sprintf("[%s,%s]"$json_frames_start,$json_frames_end);
  426.             
  427.             //! custom data-frames
  428.             if( (boolean)$objContentModel->revolutionslider_frames === true )
  429.             {
  430.                 $str_data_frames trim($objContentModel->revolutionslider_data_frames);
  431.             }
  432.             // append data-frames
  433.             $strAttributes .= " data-frames='".$str_data_frames."'";
  434.             //--
  435.             
  436.             // since we only work on html elements we can easily just replace everything in the first <...>
  437.             $preg preg_match('/<(.*?)\>/'$strCte,$result);
  438.             if($preg)
  439.             {
  440.                 $search $result[0];
  441.                 $replace sprintf('<%s %s>',$result[1], $strAttributes);
  442.                 $strCte str_replace($search$replace$strCte);
  443.             }
  444.             // inject attributes
  445.             $strCte \str_replace('###attributes###',$strAttributes,$strCte);
  446.             $arrReturn[] = $strCte;
  447.             
  448.             // add must have classes
  449.             $preg preg_match('/class="(.*?)\"/'$strCte,$result);
  450.             if($preg)
  451.             {
  452.                 $arrClass explode(' 'trim($result[1]));
  453.                 
  454.                 $arrClass[] = 'tp-caption';
  455.                 $arrClass[] = 'tp-resizeme';
  456.                 #$arrClass[] = 'rs-parallaxlevel-0';
  457.                 
  458.                 // parallax
  459.                 if( (int)$cte['revolutionslider_parallax'] > && empty($cte['revolutionslider_parallax']) === false )
  460.                 {
  461.                     $arrClass[] = 'rs-parallaxlevel-'.(int)$cte['revolutionslider_parallax'];
  462.                 }
  463.                 // visibility
  464.                 if( (int)$cte['revolutionslider_visibility'] == )
  465.                 {
  466.                     $arrClass[] = '';
  467.                 }
  468.                 else if( (int)$cte['revolutionslider_visibility'] == )
  469.                 {
  470.                     $arrClass[] = '';
  471.                 }
  472.                 
  473.                 // add start animation class (deprecated fallback)
  474.                 $arrClass[] = $cte['revolutionslider_data_animation_start'];
  475.                 
  476.                 // add end animation class (deprecated fallback)
  477.                 $arrClass[] = $cte['revolutionslider_data_animation_end'];
  478.                 
  479.                 $search $result[0];
  480.                 $strCte str_replace($search'class="'.implode(' 'array_unique($arrClass)).'"'$strCte);
  481.             }
  482.         }
  483.         
  484.         return $arrReturn;
  485.     }
  486.     /**
  487.      * Generate the current thumbnail from the main image or from the slide image and return the filepath
  488.      * @param string
  489.      * @param string
  490.      * @param string
  491.      * @param string
  492.      * @return string    path to thumb
  493.      */
  494.     protected function getThumb()
  495.     {
  496.         if($this->get('singleSRC') == '' && $this->get('singleSRC_thumb') == '')
  497.         {
  498.             return '';
  499.         }
  500.         
  501.         $src $this->get('singleSRC_thumb');
  502.         if($src == '')
  503.         {
  504.             $src $this->get('singleSRC');
  505.         }
  506.         
  507.         $objFile FilesModel::findByPk($src);
  508.         if( $objFile === null )
  509.         {
  510.             return '';
  511.         }
  512.         
  513.         $size $GLOBALS['REVOLUTIONSLIDER']['THUMBNAIL_SIZES'] ?? array();
  514.         return Image::get($objFile->path,$size[0] ?? null$size[1] ?? null$size[2] ?? null);
  515.     }
  516. }