generateStyleTag() public static method

Generate the markup for a style sheet tag
public static generateStyleTag ( string $href, string $media = null ) : string
$href string The script path
$media string The media type string
return string The markup string
Ejemplo n.º 1
0
 /**
  * Replace the dynamic script tags (see #4203)
  *
  * @param string $strBuffer The string with the tags to be replaced
  *
  * @return string The string with the replaced tags
  */
 public static function replaceDynamicScriptTags($strBuffer)
 {
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) {
         foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) {
             $strBuffer = static::importStatic($callback[0])->{$callback}[1]($strBuffer);
         }
     }
     $arrReplace = array();
     $strScripts = '';
     // Add the internal jQuery scripts
     if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) {
         foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_JQUERY]]'] = $strScripts;
     $strScripts = '';
     // Add the internal MooTools scripts
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal <body> tags
     if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) {
         foreach (array_unique($GLOBALS['TL_BODY']) as $script) {
             $strScripts .= trim($script) . "\n";
         }
     }
     // Command scheduler
     if (!\Config::get('disableCron')) {
         $strScripts .= "\n" . \Template::generateInlineScript('setTimeout(function(){var e=function(e,t){try{var n=new XMLHttpRequest}catch(r){return}n.open("GET",e,!0),n.onreadystatechange=function(){this.readyState==4&&this.status==200&&typeof t=="function"&&t(this.responseText)},n.send()},t="system/cron/cron.";e(t+"txt",function(n){parseInt(n||0)<Math.round(+(new Date)/1e3)-' . \Frontend::getCronTimeout() . '&&e(t+"php")})},5e3);') . "\n";
     }
     $arrReplace['[[TL_BODY]]'] = $strScripts;
     $strScripts = '';
     $objCombiner = new \Combiner();
     // Add the CSS framework style sheets
     if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) {
         foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) {
             $objCombiner->add($stylesheet);
         }
     }
     // Add the internal style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Add the user style sheets
     if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) {
         foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Create the aggregated style sheet
     if ($objCombiner->hasEntries()) {
         $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . "\n";
     }
     $arrReplace['[[TL_CSS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal scripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $objCombiner = new \Combiner();
         $objCombinerAsync = new \Combiner();
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             if ($options->static) {
                 if ($options->async) {
                     $objCombinerAsync->add($javascript, $options->mtime);
                 } else {
                     $objCombiner->add($javascript, $options->mtime);
                 }
             } else {
                 $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $options->async) . "\n";
             }
         }
         // Create the aggregated script and add it before the non-static scripts (see #4890)
         if ($objCombiner->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile()) . "\n" . $strScripts;
         }
         if ($objCombinerAsync->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . "\n" . $strScripts;
         }
     }
     // Add the internal <head> tags
     if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) {
         foreach (array_unique($GLOBALS['TL_HEAD']) as $head) {
             $strScripts .= trim($head) . "\n";
         }
     }
     $arrReplace['[[TL_HEAD]]'] = $strScripts;
     return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer);
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * Replace the dynamic script tags (see #4203)
  *
  * @param string $strBuffer The string with the tags to be replaced
  *
  * @return string The string with the replaced tags
  */
 public static function replaceDynamicScriptTags($strBuffer)
 {
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) {
         foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) {
             $strBuffer = static::importStatic($callback[0])->{$callback[1]}($strBuffer);
         }
     }
     $arrReplace = array();
     $strScripts = '';
     // Add the internal jQuery scripts
     if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) {
         foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_JQUERY]]'] = $strScripts;
     $strScripts = '';
     // Add the internal MooTools scripts
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal <body> tags
     if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) {
         foreach (array_unique($GLOBALS['TL_BODY']) as $script) {
             $strScripts .= trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_BODY]]'] = $strScripts;
     $strScripts = '';
     $objCombiner = new \Combiner();
     // Add the CSS framework style sheets
     if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) {
         foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) {
             $objCombiner->add($stylesheet);
         }
     }
     // Add the internal style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 if ($options->mtime === null) {
                     $options->mtime = filemtime(TL_ROOT . '/' . $stylesheet);
                 }
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Add the user style sheets
     if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) {
         foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Create the aggregated style sheet
     if ($objCombiner->hasEntries()) {
         $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . "\n";
     }
     $arrReplace['[[TL_CSS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal scripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $objCombiner = new \Combiner();
         $objCombinerAsync = new \Combiner();
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             if ($options->static) {
                 if ($options->mtime === null) {
                     $options->mtime = filemtime(TL_ROOT . '/' . $javascript);
                 }
                 $options->async ? $objCombinerAsync->add($javascript, $options->mtime) : $objCombiner->add($javascript, $options->mtime);
             } else {
                 $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $options->async) . "\n";
             }
         }
         // Create the aggregated script and add it before the non-static scripts (see #4890)
         if ($objCombiner->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile()) . "\n" . $strScripts;
         }
         if ($objCombinerAsync->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . "\n" . $strScripts;
         }
     }
     // Add the internal <head> tags
     if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) {
         foreach (array_unique($GLOBALS['TL_HEAD']) as $head) {
             $strScripts .= trim($head) . "\n";
         }
     }
     $arrReplace['[[TL_HEAD]]'] = $strScripts;
     return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer);
 }