protected function buildOptions(\Model\Collection $collection) { while ($collection->next()) { $theme = \ThemeModel::findByPk($collection->pid); switch ($collection->type) { case 'code': $label = $collection->code_snippet_title; break; case 'url': $label = preg_replace('#/([^/]+)$#', '/<strong>$1</strong>', $collection->url); break; case 'file': if ($collection->filesource == $GLOBALS['TL_CONFIG']['uploadPath'] && version_compare(VERSION, '3', '>=')) { $file = version_compare(VERSION, '3.2', '>=') ? \FilesModel::findByUuid($collection->file) : \FilesModel::findByPk($collection->file); if ($file) { $label = preg_replace('#/([^/]+)$#', '/<strong>$1</strong>', $file->path); break; } } else { $label = preg_replace('#/([^/]+)$#', '/<strong>$1</strong>', $collection->file); break; } // no break // no break default: $label = '?'; } if (strlen($collection->cc)) { $label .= ' <span style="padding-left: 3px; color: #B3B3B3;">[' . $collection->cc . ']</span>'; } $filterRules = File::renderFilterRules($collection->row()); if ($filterRules) { $label .= ' <span style="padding-left: 3px; color: #B3B3B3;">' . $filterRules . '</span>'; } $image = 'assets/theme-plus/images/' . $collection->type . '.png'; $options[$theme->name][$collection->id] = ($image ? $this->generateImage($image, $label, 'style="vertical-align:-3px"') . ' ' : '') . $label; } return $options; }
/** * Return all template files of a particular group as array * * @param string $strPrefix The template name prefix (e.g. "ce_") * @param integer $intTheme The ID of the theme * * @return array An array of template names */ public static function getTemplateGroup($strPrefix, $intTheme = 0) { $strTplFolder = 'templates'; $arrTemplates = \TemplateLoader::getPrefixedFiles($strPrefix); // Check for a theme templates folder if ($intTheme > 0) { $objTheme = \ThemeModel::findByPk($intTheme); if ($objTheme !== null && $objTheme->templates != '') { $strTplFolder = $objTheme->templates; } } // Scan the templates directory $arrFiles = array_values(preg_grep('/^' . $strPrefix . '/', scan(TL_ROOT . '/' . $strTplFolder))); if (!empty($arrFiles)) { foreach ($arrFiles as $strFile) { $arrTemplates[] = basename($strFile, strrchr($strFile, '.')); } } natcasesort($arrTemplates); $arrTemplates = array_values(array_unique($arrTemplates)); return $arrTemplates; }
public function generateCSS(\PageModel $objPage, \LayoutModel $objLayout, \PageRegular $objPageRegular) { $db = \Database::getInstance(); $lessFolder = 'assets/css'; $options = array('compress' => true, 'cache_dir' => TL_ROOT . '/' . $lessFolder); $objFiles = unserialize($objLayout->external_css); if (!$objFiles || !is_array($objFiles)) { return; } $objFiles = $db->query("SELECT * FROM tl_external_css WHERE id IN(" . implode(',', $objFiles) . ") ORDER BY sorting")->fetchAllAssoc(); $arrFiles = array(); if ($objFiles) { foreach ($objFiles as $file) { if ($file['type'] == 'url' && $file['url']) { $GLOBALS['TL_HEAD'][] = \Template::generateStyleTag($file['url'], '', false); } if ($file['type'] == 'file') { $obj = \FilesModel::findByUuid($file['file']); if ($obj) { $arrFiles[] = $obj->path; } else { if (is_file($file['file'])) { $arrFiles[] = $file['file']; } } } } } if (isset($GLOBALS['TL_HOOKS']['addExternalCssFiles']) && is_array($GLOBALS['TL_HOOKS']['addExternalCssFiles'])) { foreach ($GLOBALS['TL_HOOKS']['addExternalCssFiles'] as $callback) { $this->import($callback[0]); $arrFiles = $this->{$callback}[0]->{$callback}[1]($arrFiles); } } if ($arrFiles) { $tmpFiles = array(); foreach ($arrFiles as $file) { if (is_readable($file)) { $tmpFiles[TL_ROOT . '/' . $file] = '/' . dirname($file); } } } else { return; } $variables = array(); $arrVars = array(); $objTheme = \ThemeModel::findByPk($objLayout->pid); if ($objTheme->vars) { $arrVars = deserialize($objTheme->vars); foreach ($arrVars as $var) { $k = preg_replace('/\\$/', '@', $var['key'], 1); if ($k[0] != '@') { $k = '@' . $k; } $variables[$k] = $var['value']; } } if (isset($GLOBALS['TL_HOOKS']['addExternalCssVariables']) && is_array($GLOBALS['TL_HOOKS']['addExternalCssVariables'])) { foreach ($GLOBALS['TL_HOOKS']['addExternalCssVariables'] as $callback) { $this->import($callback[0]); $variables = $this->{$callback}[0]->{$callback}[1]($variables); } } $arrFiles = $tmpFiles; if ($_COOKIE['BE_USER_AUTH']) { $DB = \Database::getInstance(); $Session = $DB->prepare('SELECT pid FROM tl_session WHERE name="BE_USER_AUTH" AND hash=?')->limit(1)->execute($_COOKIE['BE_USER_AUTH']); $User = \Database::getInstance()->prepare('SELECT external_css_livereload FROM tl_user WHERE id=?')->execute($Session->pid)->fetchAssoc(); if ($User['external_css_livereload']) { $arrParsed = array(); $strFiles = ''; $strAjaxFiles = array(); foreach ($arrFiles as $file => $path) { $reloadFile = false; $parser = new \Less_Parser(); $parser->parseFile($file, $path); $css = $parser->getCss(); $filename = str_replace('.less', '.css', basename($file)); $path = $lessFolder . '/' . $filename; $oldCss = ''; if (is_file($path)) { $oldCss = file_get_contents($path); if ($oldCss != $css) { file_put_contents($path, $css); $reloadFile = true; } } else { file_put_contents($path, $css); } $filetime = filemtime($path); $fileClass = 'external_css_' . standardize($filename); $fileSRC = '<link class="' . $fileClass . '" rel="stylesheet" href="' . $path . '?v=' . $filetime . '" />'; $strFiles .= $fileSRC; if ($reloadFile) { $strAjaxFiles[] = array('class' => $fileClass, 'src' => $fileSRC, 'path' => $path . '?v=' . $filetime); } } if (\Input::get('action') == 'getLiveCSS') { echo json_encode(array('files' => $strAjaxFiles)); die; } $GLOBALS['TL_HEAD'][] = $strFiles; $GLOBALS['TL_JQUERY'][] = '<script src="system/modules/external_css/assets/j/livereload.js"></script>'; return; } } $file = \Less_Cache::Get($arrFiles, $options, $variables); if (!$file) { return; } $filePath = $lessFolder . '/' . $file; $imgs = array(); $strCss = file_get_contents($filePath); $re = '/url\\(\\s*[\'"]?(\\S*\\.(?:jpe?g|gif|png))[\'"]?\\s*\\)[^;}]*?/i'; if (preg_match_all($re, $strCss, $matches)) { $imgs = $matches[1]; } $embedFile = str_replace('.css', '_embed.css', $filePath); if (!is_file($embedFile)) { $arrParsed = array(); $strCss = file_get_contents($filePath); foreach ($imgs as $img) { $imgPath = TL_ROOT . $img; if (in_array($img, $arrParsed)) { continue; } if (is_file($imgPath)) { $size = filesize($imgPath); $mb = $size / 1048576; if ($mb < 0.2) { $ext = pathinfo($imgPath, PATHINFO_EXTENSION); $b64 = file_get_contents($imgPath); $b64 = base64_encode($b64); $base64 = 'data:image/' . $ext . ';base64,' . $b64; $strCss = str_replace($img, $base64, $strCss); $arrParsed[] = $img; } } } file_put_contents($embedFile, $strCss); } $filePath = $embedFile; $GLOBALS['TL_HEAD'][] = \Template::generateStyleTag(\Controller::addStaticUrlTo($filePath), '', false); }