/**
* Create all header scripts
*
* @param \PageModel $objPage
* @param \LayoutModel $objLayout
*/
protected function createHeaderScripts($objPage, $objLayout)
{
$strStyleSheets = '';
$strCcStyleSheets = '';
$arrStyleSheets = deserialize($objLayout->stylesheet);
$arrFramework = deserialize($objLayout->framework);
// Google web fonts
if ($objLayout->webfonts != '') {
$strStyleSheets .= \Template::generateStyleTag('//fonts.googleapis.com/css?family=' . str_replace('|', '%7C', $objLayout->webfonts), 'all') . "\n";
}
// Add the Contao CSS framework style sheets
if (is_array($arrFramework)) {
foreach ($arrFramework as $strFile) {
if ($strFile != 'tinymce.css') {
$GLOBALS['TL_FRAMEWORK_CSS'][] = 'assets/contao/css/' . basename($strFile, '.css') . '.min.css';
}
}
}
// Make sure TL_USER_CSS is set
if (!is_array($GLOBALS['TL_USER_CSS'])) {
$GLOBALS['TL_USER_CSS'] = array();
}
// User style sheets
if (is_array($arrStyleSheets) && strlen($arrStyleSheets[0])) {
$objStylesheets = \StyleSheetModel::findByIds($arrStyleSheets);
if ($objStylesheets !== null) {
while ($objStylesheets->next()) {
$media = implode(',', deserialize($objStylesheets->media));
// Overwrite the media type with a custom media query
if ($objStylesheets->mediaQuery != '') {
$media = $objStylesheets->mediaQuery;
}
// Style sheets with a CC or a combination of font-face and media-type != all cannot be aggregated (see #5216)
if ($objStylesheets->cc || $objStylesheets->hasFontFace && $media != 'all') {
$strStyleSheet = '';
// External style sheet
if ($objStylesheets->type == 'external') {
$objFile = \FilesModel::findByPk($objStylesheets->singleSRC);
if ($objFile !== null) {
$strStyleSheet = \Template::generateStyleTag(TL_ASSETS_URL . $objFile->path, $media);
}
} else {
$strStyleSheet = \Template::generateStyleTag(TL_ASSETS_URL . 'assets/css/' . $objStylesheets->name . '.css', $media);
}
if ($objStylesheets->cc) {
$strStyleSheet = '<!--[' . $objStylesheets->cc . ']>' . $strStyleSheet . '<![endif]-->';
}
$strCcStyleSheets .= $strStyleSheet . "\n";
} else {
// External style sheet
if ($objStylesheets->type == 'external') {
$objFile = \FilesModel::findByPk($objStylesheets->singleSRC);
if ($objFile !== null) {
$GLOBALS['TL_USER_CSS'][] = $objFile->path . '|' . $media . '|static|' . filemtime(TL_ROOT . '/' . $objFile->path);
}
} else {
$GLOBALS['TL_USER_CSS'][] = 'assets/css/' . $objStylesheets->name . '.css|' . $media . '|static|' . max($objStylesheets->tstamp, $objStylesheets->tstamp2, $objStylesheets->tstamp3);
}
}
}
}
}
$arrExternal = deserialize($objLayout->external);
// External style sheets
if (!empty($arrExternal) && is_array($arrExternal)) {
// Consider the sorting order (see #5038)
if ($objLayout->orderExt != '') {
$tmp = deserialize($objLayout->orderExt);
if (!empty($tmp) && is_array($tmp)) {
// Remove all values
$arrOrder = array_map(function () {
}, array_flip($tmp));
// Move the matching elements to their position in $arrOrder
foreach ($arrExternal as $k => $v) {
if (array_key_exists($v, $arrOrder)) {
$arrOrder[$v] = $v;
unset($arrExternal[$k]);
}
}
// Append the left-over style sheets at the end
if (!empty($arrExternal)) {
$arrOrder = array_merge($arrOrder, array_values($arrExternal));
}
// Remove empty (unreplaced) entries
$arrExternal = array_values(array_filter($arrOrder));
unset($arrOrder);
}
}
// Get the file entries from the database
$objFiles = \FilesModel::findMultipleByUuids($arrExternal);
if ($objFiles !== null) {
$arrFiles = array();
while ($objFiles->next()) {
if (file_exists(TL_ROOT . '/' . $objFiles->path)) {
$arrFiles[] = $objFiles->path . '|static';
}
}
// Inject the external style sheets before or after the internal ones (see #6937)
if ($objLayout->loadingOrder == 'external_first') {
array_splice($GLOBALS['TL_USER_CSS'], 0, 0, $arrFiles);
} else {
array_splice($GLOBALS['TL_USER_CSS'], count($GLOBALS['TL_USER_CSS']), 0, $arrFiles);
}
}
}
// Add a placeholder for dynamic style sheets (see #4203)
$strStyleSheets .= '[[TL_CSS]]';
// Always add conditional style sheets at the end
$strStyleSheets .= $strCcStyleSheets;
$newsfeeds = deserialize($objLayout->newsfeeds);
$calendarfeeds = deserialize($objLayout->calendarfeeds);
// Add newsfeeds
if (!empty($newsfeeds) && is_array($newsfeeds)) {
$objFeeds = \NewsFeedModel::findByIds($newsfeeds);
if ($objFeeds !== null) {
while ($objFeeds->next()) {
$strStyleSheets .= \Template::generateFeedTag(($objFeeds->feedBase ?: \Environment::get('base')) . 'share/' . $objFeeds->alias . '.xml', $objFeeds->format, $objFeeds->title) . "\n";
}
}
}
// Add calendarfeeds
if (!empty($calendarfeeds) && is_array($calendarfeeds)) {
$objFeeds = \CalendarFeedModel::findByIds($calendarfeeds);
if ($objFeeds !== null) {
while ($objFeeds->next()) {
$strStyleSheets .= \Template::generateFeedTag(($objFeeds->feedBase ?: \Environment::get('base')) . 'share/' . $objFeeds->alias . '.xml', $objFeeds->format, $objFeeds->title) . "\n";
}
}
}
// Add a placeholder for dynamic <head> tags (see #4203)
$strHeadTags = '[[TL_HEAD]]';
// Add the user <head> tags
if (($strHead = trim($objLayout->head)) != false) {
$strHeadTags .= $strHead . "\n";
}
$this->Template->stylesheets = $strStyleSheets;
$this->Template->head = $strHeadTags;
}