src/Rhyme/WMassArtsHub/Hooks/CompileArticle/AddCss.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2020 Rhyme Digital, LLC.
  4.  *
  5.  * @author        Blair Winans <blair@rhyme.digital>
  6.  * @author        Adam Fisher <adam@rhyme.digital>
  7.  * @link        https://rhyme.digital
  8.  * @license        http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  9.  */
  10. namespace Rhyme\WMassArtsHub\Hooks\CompileArticle;
  11. use Contao\Config;
  12. use Contao\System;
  13. use Contao\Controller;
  14. use Contao\StyleSheets;
  15. use Contao\FilesModel;
  16. use Contao\File;
  17. use Contao\StringUtil;
  18. use Contao\Image\PictureConfiguration;
  19. /**
  20.  * Class AddCss
  21.  */
  22. class AddCss extends StyleSheets
  23. {
  24.     /**
  25.      * @var string
  26.      */
  27.     protected $strCustomClass '';
  28.     /**
  29.      * @var string
  30.      */
  31.     protected $strStyles '';
  32.     /**
  33.      * @param $objTemplate
  34.      * @param $arrData
  35.      * @param $objModule
  36.      */
  37.     public function run(&$objTemplate$arrData, &$objModule)
  38.     {
  39.         if (TL_MODE !== 'FE')
  40.         {
  41.             return;
  42.         }
  43.         // Add the custom class to the article
  44.         $this->strCustomClass 'rhyme_article_'.$arrData['id'];
  45.         $arrCssID $objModule->cssID;
  46.         $arrCssID[1] .= (strlen($arrCssID[1]) > ' ' '') .$this->strCustomClass;
  47.         $objModule->cssID $arrCssID;
  48.         // Gather styles
  49.         $this->addBackgroundImage($objTemplate$arrData$objModule);
  50.         $this->addBackgroundColor($objTemplate$arrData$objModule);
  51.         $this->addPadding($objTemplate$arrData$objModule);
  52.         $this->addBorders($objTemplate$arrData$objModule);
  53.         // Add to <head>
  54.         $GLOBALS['TL_HEAD'][$this->strCustomClass] = '<style>'.$this->strStyles.'</style>';
  55.     }
  56.     /**
  57.      * @param $objTemplate
  58.      * @param $arrData
  59.      * @param $objModule
  60.      */
  61.     protected function addBackgroundImage($objTemplate$arrData$objModule)
  62.     {
  63.         if (!$arrData['bg_singleSRC'])
  64.         {
  65.             return;
  66.         }
  67.         $objFileModel FilesModel::findByUuid($arrData['bg_singleSRC']);
  68.         if ($objFileModel === null)
  69.         {
  70.             return;
  71.         }
  72.         if (!is_file(TL_ROOT '/' $objFileModel->path))
  73.         {
  74.             return;
  75.         }
  76.         try
  77.         {
  78.             $objFile = new File($objFileModel->path);
  79.         }
  80.         catch (\Exception $e)
  81.         {
  82.             $objFile null;
  83.         }
  84.         $size StringUtil::deserialize($arrData['bg_imgSize']);
  85.         if (is_numeric($size))
  86.         {
  87.             $size = array(00, (int) $size);
  88.         }
  89.         elseif (!$size instanceof PictureConfiguration)
  90.         {
  91.             if (!\is_array($size))
  92.             {
  93.                 $size = array();
  94.             }
  95.             $size += array(00'crop');
  96.         }
  97.         $container System::getContainer();
  98.         try
  99.         {
  100.             $rootDir $container->getParameter('kernel.project_dir');
  101.             $staticUrl $container->get('contao.assets.files_context')->getStaticUrl();
  102.             $picture $container->get('contao.image.picture_factory')->create($rootDir '/' $objFileModel->path$size);
  103.             $picture = array
  104.             (
  105.                 'img' => $picture->getImg($rootDir$staticUrl),
  106.                 'sources' => $picture->getSources($rootDir$staticUrl)
  107.             );
  108.             $src $picture['img']['src'];
  109.             if ($src !== $objFileModel->path)
  110.             {
  111.                 $objFile = new File(rawurldecode($src));
  112.             }
  113.         }
  114.         catch (\Exception $e)
  115.         {
  116.             System::log('Image "' $objFileModel->path '" could not be processed: ' $e->getMessage(), __METHOD__TL_ERROR);
  117.             $src '';
  118.             $picture = array('img'=>array('src'=>'''srcset'=>''), 'sources'=>array());
  119.         }
  120.         if ($src == '')
  121.         {
  122.             return;
  123.         }
  124.         // Start adding styles // Todo: make these configurable too?  Also, make settings for different sizes
  125.         $this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { background-image: url("'.$src.'"); background-repeat: no-repeat; background-position: center center; background-size: cover; } ';
  126.         // See if we want to add media queries
  127.         if (!empty($picture['sources']))
  128.         {
  129.             foreach ($picture['sources'] as $source)
  130.             {
  131.                 $this->strStyles .= '@media all and '.$source['media'].' { body .'.$this->strCustomClass.'[class*=mod_article] { background-image: url("'.$source['src'].'"); } } ';
  132.             }
  133.         }
  134.     }
  135.     /**
  136.      * @param $objTemplate
  137.      * @param $arrData
  138.      * @param $objModule
  139.      */
  140.     protected function addBackgroundColor($objTemplate$arrData$objModule)
  141.     {
  142.         if (!$arrData['bg_color'])
  143.         {
  144.             return;
  145.         }
  146.         $bgColor StringUtil::deserialize($arrData['bg_color'], true);
  147.         if ($bgColor[0] != '')
  148.         {
  149.             $this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { background-color:' $this->compileColor($bgColor) . '; } ';
  150.         }
  151.     }
  152.     /**
  153.      * @param $objTemplate
  154.      * @param $arrData
  155.      * @param $objModule
  156.      */
  157.     protected function addPadding($objTemplate$arrData$objModule)
  158.     {
  159.         if (!$arrData['padding'])
  160.         {
  161.             return;
  162.         }
  163.         $arrPadding StringUtil::deserialize($arrData['padding']);
  164.         if (\is_array($arrPadding))
  165.         {
  166.             $top $arrPadding['top'];
  167.             $right $arrPadding['right'];
  168.             $bottom $arrPadding['bottom'];
  169.             $left $arrPadding['left'];
  170.             $unit $arrPadding['unit'];
  171.             $strStyle '';
  172.             $arrDir compact('top''right''bottom''left');
  173.             foreach ($arrDir as $k=>$v)
  174.             {
  175.                 if ($v != '')
  176.                 {
  177.                     $strStyle .= 'padding-' $k ':' $v . (($v === '0') ? '' $unit) . '; ';
  178.                 }
  179.             }
  180.             if ($strStyle)
  181.             {
  182.                 $this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { '.$strStyle.' } ';
  183.             }
  184.         }
  185.     }
  186.     /**
  187.      * @param $objTemplate
  188.      * @param $arrData
  189.      * @param $objModule
  190.      */
  191.     protected function addBorders($objTemplate$arrData$objModule)
  192.     {
  193.         $strStyle '';
  194.         if ($arrData['borderTop'])
  195.         {
  196.             $borderTopWidth StringUtil::deserialize($arrData['borderTopWidth'], true);
  197.             if ($borderTopWidth['value'] != '')
  198.             {
  199.                 $strStyle .= 'border-top:' $borderTopWidth['value'] . ($borderTopWidth['unit'] ?: 'px') . ' solid'// Todo: make style configurable?
  200.                 $borderColor StringUtil::deserialize($arrData['borderTopColor'], true);
  201.                 if ($borderColor[0] != '')
  202.                 {
  203.                     $strStyle .= ' ' $this->compileColor($borderColor);
  204.                 }
  205.                 $strStyle .= '; ';
  206.             }
  207.         }
  208.         if ($arrData['borderBottom'])
  209.         {
  210.             $borderBottomWidth StringUtil::deserialize($arrData['borderBottomWidth'], true);
  211.             if ($borderBottomWidth['value'] != '')
  212.             {
  213.                 $strStyle .= 'border-bottom:' $borderBottomWidth['value'] . ($borderBottomWidth['unit'] ?: 'px') . ' solid'// Todo: make style configurable?
  214.                 $borderColor StringUtil::deserialize($arrData['borderBottomColor'], true);
  215.                 if ($borderColor[0] != '')
  216.                 {
  217.                     $strStyle .= ' ' $this->compileColor($borderColor);
  218.                 }
  219.                 $strStyle .= '; ';
  220.             }
  221.         }
  222.         if ($strStyle)
  223.         {
  224.             $this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { '.$strStyle.' } ';
  225.         }
  226.     }
  227. }