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

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.             $arrPositionM = array
  245.             (
  246.                 'x' => $data_position_m[0],
  247.                 'y' => $data_position_m[1],
  248.                 'hoffset' => null,
  249.                 'voffset' => null,
  250.             );
  251.             // h offset
  252.             if(isset($data_position[0]) && strlen($data_position9[0]) > 0)
  253.             {
  254.                 $arrPosition['x'] = $arrPositionM['x'] = $data_position9[0];
  255.                 $arrPosition['hoffset'] = $data_position[0];
  256.                 $arrPositionM['hoffset'] = $data_position_m[0];
  257.             }
  258.             
  259.             // v offset
  260.             if(isset($data_position[1]) && strlen($data_position9[1]) > 0)
  261.             {
  262.                 $arrPosition['y'] = $arrPositionM['y'] = str_replace(array('center'),array('middle'), $data_position9[1]);
  263.                 $arrPosition['voffset'] = $data_position[1];
  264.                 $arrPositionM['voffset'] = $data_position_m[1];
  265.             }
  266.             #$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]']";
  267.             #$arrAttributes['data-y'] = $this->buildDataAttributeValueFromArray( array($pos_y,$pos_y_m) );
  268.             #$arrAttributes['data-hoffset'] = $this->buildDataAttributeValueFromArray( array($hoffset,$hoffset_m) ); #\sprintf("['%s','%s']",$hoffset,$hoffset_m); #"['$data_position[0]','','','$data_position_m[0]']";
  269.             #$arrAttributes['data-voffset'] = $this->buildDataAttributeValueFromArray( array($voffset,$voffset_m) ); #"['$data_position[0]','','','$data_position_m[0]']";
  270.             
  271.             // check if there is atleas one mobile value and mark this slider as mobile first
  272.             $GLOBALS['REVOLUTIONSLIDER_HAS_MOBILE'][ $this->Slider->id ] = false;
  273.             if( empty( array_filter$arrPositionM ) ) === false )
  274.             {
  275.                 $GLOBALS['REVOLUTIONSLIDER_HAS_MOBILE'][ $this->Slider->id ] = true;
  276.             }
  277.             
  278.             // DESKTOP, SMARTPHONE
  279.             $arrAttributes['data-x'] = \sprintf("['%s','%s']",$arrPosition['x'],$arrPositionM['x']);
  280.             $arrAttributes['data-y'] = \sprintf("['%s','%s']",$arrPosition['y'],$arrPositionM['y']);
  281.             if( empty($arrPosition['hoffset']) === false || empty($arrPositionM['hoffset']) === false )
  282.             {
  283.                 $arrAttributes['data-hoffset'] = \sprintf("['%s','%s']",$arrPosition['hoffset'],$arrPositionM['hoffset']);
  284.             }
  285.             if( empty($arrPosition['voffset']) === false || empty($arrPositionM['voffset']) === false )
  286.             {
  287.                 $arrAttributes['data-voffset'] = \sprintf("['%s','%s']",$arrPosition['voffset'],$arrPositionM['voffset']); #"['$data_position[0]','','','$data_position_m[0]']";
  288.             }
  289.             $arrAttributes['data-lineheight'] = "['inherit','inherit']";
  290.             $arrAttributes['data-whitespace'] = "nowrap";
  291.             $arrAttributes['data-responsive_offset'] = "on";    
  292.             
  293.             // visibility
  294.             $arrAttributes['data-visibility'] = "['on','on']";
  295.             if( $objContentModel->revolutionslider_visibility == )
  296.             {
  297.                 $arrAttributes['data-visibility'] = "['on','off']";
  298.             }
  299.             else if( $objContentModel->revolutionslider_visibility == )
  300.             {
  301.                 $arrAttributes['data-visibility'] = "['off','on']";
  302.             }
  303.             // hyperlinks
  304.             if( \in_array($objContentModel->type, array('hyperlink','revolutionslider_hyperlink') )  )
  305.             {
  306.                 $arrAttributes['data-responsive'] = "off";
  307.                 if( $arrPosition['x'] == 'center' )
  308.                 {
  309.                     $arrAttributes['data-responsive_offset'] = "off";
  310.                 }    
  311.             }
  312.             // data type
  313.             $arrAttributes['data-type'] = 'text';
  314.             if( \in_array($objContentModel->type, array('image','revolutionslider_image') )  )
  315.             {
  316.                 $arrAttributes['data-type'] = 'image';
  317.             }
  318.             //! data-fontsize
  319.             $arrFontSize StringUtil::deserialize($objContentModel->revolutionslider_text_fontsize);
  320.             if( !\is_array($arrFontSize) )
  321.             {
  322.                 $arrFontSize \explode(',',$arrFontSize);
  323.             }
  324.             if( empty($arrFontSize[1]) === false )
  325.             {
  326.                 $arrAttributes['data-fontsize'] = \sprintf("['%s','%s']",$arrFontSize[0],$arrFontSize[1]);
  327.             }
  328.             if( \in_array($objContentModel->type, array('hyperlink','revolutionslider_hyperlink') )  )
  329.             {
  330.                 unset($arrAttributes['data-fontsize']);
  331.             }
  332.             //--
  333.             
  334.             // out animation attributes
  335.             if( (boolean)$cte['revolutionslider_OUT'] === true)
  336.             {
  337.                 $arrAttributes['data-endelementdelay']     = ( !empty($objContentModel->revolutionslider_data_start_OUT) ? $cte['revolutionslider_data_start_OUT'] * 1000 'wait');
  338.                 $arrAttributes['data-endspeed']         = $cte['revolutionslider_data_speed_OUT'] * 1000;
  339.                 $arrAttributes['data-endeasing']         = $cte['revolutionslider_data_easing_OUT'] ?: 'wait';
  340.             }
  341.             //! link element to slide
  342.             if($cte['revolutionslider_data_linktoslide'] > 0)
  343.             {
  344.                 $objModels \RevolutionSlider\Models\Slides::findBy( array('pid=?','published=?'), array($this->get('pid'),1), array('order' => 'sorting') );
  345.                 if( $objModels !== null )
  346.                 {
  347.                     foreach($objModels as $i => $objModel)
  348.                     {
  349.                         if($cte['revolutionslider_data_linktoslide'] == $objModel->id)
  350.                         {
  351.                             $arrAttributes['data-linktoslide'] = $i 1;
  352.                         }
  353.                     }
  354.                 }
  355.             }
  356.             
  357.             // disable debug mode
  358.             $blnDebug Config::get('debugMode');
  359.             Config::set('debugMode',0);
  360.             // generate the content element
  361.             $strCte Controller::replaceInsertTags('{{insert_content::'.$cte['id'].'}}');
  362.         
  363.             Config::set('debugMode',$blnDebug);
  364.             //! data-frames
  365.             
  366.             $strFrameJson $GLOBALS['REVOLUTIONSLIDER']['FRAMES'][ $objContentModel->revolutionslider_data_animation_start ];
  367.             $arrFrameJson \explode('},{',$strFrameJson);
  368.             $arrFrameJson[0] .= '}';
  369.             $arrFrameJson[1] = '{'.$arrFrameJson[1];
  370.             
  371.             $arrFrames = array
  372.             (
  373.                 'start' => \json_decode($arrFrameJson[0],true),
  374.                 'end'     => \json_decode($arrFrameJson[1],true),
  375.             );
  376.             // map attributes to frame
  377.             $frameFromAttributes = array
  378.             (
  379.                 'start' => array
  380.                 (
  381.                     'delay' => 'data-start',
  382.                     'speed' => 'data-speed',
  383.                     'ease'    => 'data-easing',
  384.                 ),
  385.                 'end' => array
  386.                 (
  387.                     'delay' => 'data-start',
  388.                     'speed' => 'data-endspeed',
  389.                     'ease'    => 'data-endeasing',
  390.                 ),
  391.             );
  392.             // if no out animation is set, use the default
  393.             if( (boolean)$objContentModel->revolutionslider_OUT === false)
  394.             {
  395.                 unset( $frameFromAttributes['end'] );
  396.             }
  397.             
  398.             foreach( $frameFromAttributes as $k => $arr)
  399.             {
  400.                 foreach( $arr as $frame => $attr )
  401.                 {
  402.                     if( isset($arrAttributes$attr ]) )
  403.                     {
  404.                         $arrFrames[$k][$frame ] = $arrAttributes$attr ];
  405.                         // remove from attributes
  406.                         unset( $arrAttributes$attr ] );
  407.                     }
  408.                 }
  409.             }
  410.             $json_frames_start \json_encode($arrFrames['start']);
  411.             $json_frames_end =  \json_encode($arrFrames['end']);
  412.             
  413.             // inject attributes in content element
  414.             $strAttributes '';
  415.             foreach($arrAttributes as $attr => $value)
  416.             {
  417.                 $strAttributes .= $attr .'="'.$value.'" ';
  418.             }
  419.             trim($strAttributes);
  420.             
  421.             $str_data_frames \sprintf("[%s,%s]"$json_frames_start,$json_frames_end);
  422.             
  423.             //! custom data-frames
  424.             if( (boolean)$objContentModel->revolutionslider_frames === true )
  425.             {
  426.                 $str_data_frames trim($objContentModel->revolutionslider_data_frames);
  427.             }
  428.             // append data-frames
  429.             $strAttributes .= " data-frames='".$str_data_frames."'";
  430.             //--
  431.             
  432.             // since we only work on html elements we can easily just replace everything in the first <...>
  433.             $preg preg_match('/<(.*?)\>/'$strCte,$result);
  434.             if($preg)
  435.             {
  436.                 $search $result[0];
  437.                 $replace sprintf('<%s %s>',$result[1], $strAttributes);
  438.                 $strCte str_replace($search$replace$strCte);
  439.             }
  440.             // inject attributes
  441.             $strCte \str_replace('###attributes###',$strAttributes,$strCte);
  442.             $arrReturn[] = $strCte;
  443.             
  444.             // add must have classes
  445.             $preg preg_match('/class="(.*?)\"/'$strCte,$result);
  446.             if($preg)
  447.             {
  448.                 $arrClass explode(' 'trim($result[1]));
  449.                 
  450.                 $arrClass[] = 'tp-caption';
  451.                 $arrClass[] = 'tp-resizeme';
  452.                 #$arrClass[] = 'rs-parallaxlevel-0';
  453.                 
  454.                 // parallax
  455.                 if( (int)$cte['revolutionslider_parallax'] > && empty($cte['revolutionslider_parallax']) === false )
  456.                 {
  457.                     $arrClass[] = 'rs-parallaxlevel-'.(int)$cte['revolutionslider_parallax'];
  458.                 }
  459.                 // visibility
  460.                 if( (int)$cte['revolutionslider_visibility'] == )
  461.                 {
  462.                     $arrClass[] = '';
  463.                 }
  464.                 else if( (int)$cte['revolutionslider_visibility'] == )
  465.                 {
  466.                     $arrClass[] = '';
  467.                 }
  468.                 
  469.                 // add start animation class (deprecated fallback)
  470.                 $arrClass[] = $cte['revolutionslider_data_animation_start'];
  471.                 
  472.                 // add end animation class (deprecated fallback)
  473.                 $arrClass[] = $cte['revolutionslider_data_animation_end'];
  474.                 
  475.                 $search $result[0];
  476.                 $strCte str_replace($search'class="'.implode(' 'array_unique($arrClass)).'"'$strCte);
  477.             }
  478.         }
  479.         
  480.         return $arrReturn;
  481.     }
  482.     /**
  483.      * Generate the current thumbnail from the main image or from the slide image and return the filepath
  484.      * @param string
  485.      * @param string
  486.      * @param string
  487.      * @param string
  488.      * @return string    path to thumb
  489.      */
  490.     protected function getThumb()
  491.     {
  492.         if($this->get('singleSRC') == '' && $this->get('singleSRC_thumb') == '')
  493.         {
  494.             return '';
  495.         }
  496.         
  497.         $src $this->get('singleSRC_thumb');
  498.         if($src == '')
  499.         {
  500.             $src $this->get('singleSRC');
  501.         }
  502.         
  503.         $objFile FilesModel::findByPk($src);
  504.         if( $objFile === null )
  505.         {
  506.             return '';
  507.         }
  508.         
  509.         $size $GLOBALS['REVOLUTIONSLIDER']['THUMBNAIL_SIZES'] ?? array();
  510.         return Image::get($objFile->path,$size[0] ?? null$size[1] ?? null$size[2] ?? null);
  511.     }
  512. }