<?php
/**
* Copyright (C) 2020 Rhyme Digital, LLC.
*
* @author Blair Winans <blair@rhyme.digital>
* @author Adam Fisher <adam@rhyme.digital>
* @link https://rhyme.digital
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
namespace Rhyme\WMassArtsHub\Hooks\CompileArticle;
use Contao\Config;
use Contao\System;
use Contao\Controller;
use Contao\StyleSheets;
use Contao\FilesModel;
use Contao\File;
use Contao\StringUtil;
use Contao\Image\PictureConfiguration;
/**
* Class AddCss
*/
class AddCss extends StyleSheets
{
/**
* @var string
*/
protected $strCustomClass = '';
/**
* @var string
*/
protected $strStyles = '';
/**
* @param $objTemplate
* @param $arrData
* @param $objModule
*/
public function run(&$objTemplate, $arrData, &$objModule)
{
if (TL_MODE !== 'FE')
{
return;
}
// Add the custom class to the article
$this->strCustomClass = 'rhyme_article_'.$arrData['id'];
$arrCssID = $objModule->cssID;
$arrCssID[1] .= (strlen($arrCssID[1]) > 0 ? ' ' : '') .$this->strCustomClass;
$objModule->cssID = $arrCssID;
// Gather styles
$this->addBackgroundImage($objTemplate, $arrData, $objModule);
$this->addBackgroundColor($objTemplate, $arrData, $objModule);
$this->addPadding($objTemplate, $arrData, $objModule);
$this->addBorders($objTemplate, $arrData, $objModule);
// Add to <head>
$GLOBALS['TL_HEAD'][$this->strCustomClass] = '<style>'.$this->strStyles.'</style>';
}
/**
* @param $objTemplate
* @param $arrData
* @param $objModule
*/
protected function addBackgroundImage($objTemplate, $arrData, $objModule)
{
if (!$arrData['bg_singleSRC'])
{
return;
}
$objFileModel = FilesModel::findByUuid($arrData['bg_singleSRC']);
if ($objFileModel === null)
{
return;
}
if (!is_file(TL_ROOT . '/' . $objFileModel->path))
{
return;
}
try
{
$objFile = new File($objFileModel->path);
}
catch (\Exception $e)
{
$objFile = null;
}
$size = StringUtil::deserialize($arrData['bg_imgSize']);
if (is_numeric($size))
{
$size = array(0, 0, (int) $size);
}
elseif (!$size instanceof PictureConfiguration)
{
if (!\is_array($size))
{
$size = array();
}
$size += array(0, 0, 'crop');
}
$container = System::getContainer();
try
{
$rootDir = $container->getParameter('kernel.project_dir');
$staticUrl = $container->get('contao.assets.files_context')->getStaticUrl();
$picture = $container->get('contao.image.picture_factory')->create($rootDir . '/' . $objFileModel->path, $size);
$picture = array
(
'img' => $picture->getImg($rootDir, $staticUrl),
'sources' => $picture->getSources($rootDir, $staticUrl)
);
$src = $picture['img']['src'];
if ($src !== $objFileModel->path)
{
$objFile = new File(rawurldecode($src));
}
}
catch (\Exception $e)
{
System::log('Image "' . $objFileModel->path . '" could not be processed: ' . $e->getMessage(), __METHOD__, TL_ERROR);
$src = '';
$picture = array('img'=>array('src'=>'', 'srcset'=>''), 'sources'=>array());
}
if ($src == '')
{
return;
}
// Start adding styles // Todo: make these configurable too? Also, make settings for different sizes
$this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { background-image: url("'.$src.'"); background-repeat: no-repeat; background-position: center center; background-size: cover; } ';
// See if we want to add media queries
if (!empty($picture['sources']))
{
foreach ($picture['sources'] as $source)
{
$this->strStyles .= '@media all and '.$source['media'].' { body .'.$this->strCustomClass.'[class*=mod_article] { background-image: url("'.$source['src'].'"); } } ';
}
}
}
/**
* @param $objTemplate
* @param $arrData
* @param $objModule
*/
protected function addBackgroundColor($objTemplate, $arrData, $objModule)
{
if (!$arrData['bg_color'])
{
return;
}
$bgColor = StringUtil::deserialize($arrData['bg_color'], true);
if ($bgColor[0] != '')
{
$this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { background-color:' . $this->compileColor($bgColor) . '; } ';
}
}
/**
* @param $objTemplate
* @param $arrData
* @param $objModule
*/
protected function addPadding($objTemplate, $arrData, $objModule)
{
if (!$arrData['padding'])
{
return;
}
$arrPadding = StringUtil::deserialize($arrData['padding']);
if (\is_array($arrPadding))
{
$top = $arrPadding['top'];
$right = $arrPadding['right'];
$bottom = $arrPadding['bottom'];
$left = $arrPadding['left'];
$unit = $arrPadding['unit'];
$strStyle = '';
$arrDir = compact('top', 'right', 'bottom', 'left');
foreach ($arrDir as $k=>$v)
{
if ($v != '')
{
$strStyle .= 'padding-' . $k . ':' . $v . (($v === '0') ? '' : $unit) . '; ';
}
}
if ($strStyle)
{
$this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { '.$strStyle.' } ';
}
}
}
/**
* @param $objTemplate
* @param $arrData
* @param $objModule
*/
protected function addBorders($objTemplate, $arrData, $objModule)
{
$strStyle = '';
if ($arrData['borderTop'])
{
$borderTopWidth = StringUtil::deserialize($arrData['borderTopWidth'], true);
if ($borderTopWidth['value'] != '')
{
$strStyle .= 'border-top:' . $borderTopWidth['value'] . ($borderTopWidth['unit'] ?: 'px') . ' solid'; // Todo: make style configurable?
$borderColor = StringUtil::deserialize($arrData['borderTopColor'], true);
if ($borderColor[0] != '')
{
$strStyle .= ' ' . $this->compileColor($borderColor);
}
$strStyle .= '; ';
}
}
if ($arrData['borderBottom'])
{
$borderBottomWidth = StringUtil::deserialize($arrData['borderBottomWidth'], true);
if ($borderBottomWidth['value'] != '')
{
$strStyle .= 'border-bottom:' . $borderBottomWidth['value'] . ($borderBottomWidth['unit'] ?: 'px') . ' solid'; // Todo: make style configurable?
$borderColor = StringUtil::deserialize($arrData['borderBottomColor'], true);
if ($borderColor[0] != '')
{
$strStyle .= ' ' . $this->compileColor($borderColor);
}
$strStyle .= '; ';
}
}
if ($strStyle)
{
$this->strStyles .= 'body .'.$this->strCustomClass.'[class*=mod_article] { '.$strStyle.' } ';
}
}
}