From 3bb6e607bb4104339d9e6df34c19fd0fda04d931 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 25 Oct 2011 21:03:05 +0200 Subject: [PATCH] Added support for {icon} tags in templates --- .../files/lib/data/package/Package.class.php | 18 ------- .../lib/data/style/ActiveStyle.class.php | 11 +++-- .../cache/builder/IconCacheBuilder.class.php | 49 ++++++++++++++----- .../cache/builder/StyleCacheBuilder.class.php | 4 +- .../lib/system/style/StyleHandler.class.php | 7 ++- .../IconCompilerTemplatePlugin.class.php | 25 +++++++++- 6 files changed, 72 insertions(+), 42 deletions(-) diff --git a/wcfsetup/install/files/lib/data/package/Package.class.php b/wcfsetup/install/files/lib/data/package/Package.class.php index 48e397e53c..2b68950b7f 100644 --- a/wcfsetup/install/files/lib/data/package/Package.class.php +++ b/wcfsetup/install/files/lib/data/package/Package.class.php @@ -72,24 +72,6 @@ class Package extends DatabaseObject { return ($this->instanceName ? $this->instanceName : $this->packageName); } - /** - * Returns the installation dir of this package. - * - * @return string - */ - public function getDir() { - return $this->dir; - } - - /** - * Sets the installation dir of this package. - * - * @param string $dir - */ - public function setDir($dir) { - $this->dir = $dir; - } - /** * Returns the abbreviation of the package name. * diff --git a/wcfsetup/install/files/lib/data/style/ActiveStyle.class.php b/wcfsetup/install/files/lib/data/style/ActiveStyle.class.php index 15b82e630a..c04bd1366d 100644 --- a/wcfsetup/install/files/lib/data/style/ActiveStyle.class.php +++ b/wcfsetup/install/files/lib/data/style/ActiveStyle.class.php @@ -1,6 +1,7 @@ styleID; - WCF::getCache()->addResource( + CacheHandler::getInstance()->addResource( $cacheName, WCF_DIR.'cache/cache.'.$cacheName.'.php', 'wcf\system\cache\builder\IconCacheBuilder' ); - $this->iconCache = WCF::getCache()->get($cacheName); + $this->iconCache = CacheHandler::getInstance()->get($cacheName); } /** @@ -67,8 +68,8 @@ class ActiveStyle extends DatabaseObjectDecorator { * @param string $iconName * @return string */ - public function getIconPath($iconName) { - if (isset($this->iconCache[$iconName])) return $this->iconCache[$iconName]; - return RELATIVE_WCF_DIR.'icon/'.$iconName; + public function getIconPath($iconName, $size = 'L') { + if (isset($this->iconCache[$iconName][$size])) return $this->iconCache[$iconName][$size]; + return RELATIVE_WCF_DIR.'icon/'.$iconName.'.svg'; } } diff --git a/wcfsetup/install/files/lib/system/cache/builder/IconCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/IconCacheBuilder.class.php index 41c312f055..14a89bf0e4 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/IconCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/IconCacheBuilder.class.php @@ -26,22 +26,24 @@ class IconCacheBuilder implements ICacheBuilder { // get active package $activePackage = new Package($packageID); - $activePackageDir = FileUtil::getRealPath(WCF_DIR.$activePackage->getDir()); + $activePackageDir = FileUtil::getRealPath(WCF_DIR.$activePackage->packageDir); // get package dirs $packageDirs = array(); $conditionBuilder = new PreparedStatementConditionBuilder(); - $conditionBuilder->add("packageID IN (?) AND packageDir <> ''", array(PackageDependencyHandler::getDependenciesString())); - $sql = "SELECT DISTINCT packageDir - FROM wcf".WCF_N."_package package + $conditionBuilder->add("dependency.packageID IN (?) AND package.packageDir <> ''", array(PackageDependencyHandler::getDependencies())); + $sql = "SELECT DISTINCT package.packageDir + FROM wcf".WCF_N."_package_dependency dependency + LEFT JOIN wcf".WCF_N."_package package + ON (package.packageID = dependency.dependency) ".$conditionBuilder->__toString()." - ORDER BY priority DESC"; + ORDER BY dependency.priority DESC"; $statement = WCF::getDB()->prepareStatement($sql); $statement->execute($conditionBuilder->getParameters()); while ($row = $statement->fetchArray()) { $packageDirs[] = FileUtil::getRealPath(WCF_DIR.$row['packageDir']); } - $packageDirs[] = WCF_DIR; + $packageDirs[] = FileUtil::unifyDirSeperator(WCF_DIR); // get style icon path $iconDirs = array(); @@ -58,14 +60,35 @@ class IconCacheBuilder implements ICacheBuilder { // get icons foreach ($packageDirs as $packageDir) { $relativePackageDir = ($activePackageDir != $packageDir ? FileUtil::getRelativePath($activePackageDir, $packageDir) : ''); - + echo $relativePackageDir."\n"; foreach ($iconDirs as $iconDir) { $path = FileUtil::addTrailingSlash($packageDir.$iconDir); - $icons = self::getIconFiles($path); + + // get png icons + $icons = self::getIconFiles($path, 'png'); foreach ($icons as $icon) { $icon = str_replace($path, '', $icon); - if (!isset($data[$icon])) { - $data[$icon] = $relativePackageDir.$iconDir.$icon; + if (preg_match('/^(.*)(S|M|L)\.png$/', $icon, $match)) { + if (!isset($data[$match[1]][$match[2]])) { + $data[$match[1]][$match[2]] = $relativePackageDir.$iconDir.$icon; + } + } + } + + // get svg icons + $icons = self::getIconFiles($path, 'svg'); + foreach ($icons as $icon) { + $icon = str_replace($path, '', $icon); + if (preg_match('/^(.*)\.svg$/', $icon, $match)) { + if (!isset($data[$match[1]]['S'])) { + $data[$match[1]]['S'] = $relativePackageDir.$iconDir.$icon; + } + if (!isset($data[$match[1]]['M'])) { + $data[$match[1]]['M'] = $relativePackageDir.$iconDir.$icon; + } + if (!isset($data[$match[1]]['L'])) { + $data[$match[1]]['L'] = $relativePackageDir.$iconDir.$icon; + } } } } @@ -74,13 +97,13 @@ class IconCacheBuilder implements ICacheBuilder { return $data; } - protected static function getIconFiles($path) { + protected static function getIconFiles($path, $extension = 'svg') { $files = array(); if (is_dir($path)) { $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); foreach ($iterator as $file) { - if (preg_match('/\.png$/', $file->getFilename())) { - $files[] = $file->getPathname(); + if (preg_match('/\.'.$extension.'$/', $file->getFilename())) { + $files[] = FileUtil::unifyDirSeperator($file->getPathname()); } } } diff --git a/wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php index 7f0dfab6a5..98f0f23436 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php @@ -40,8 +40,7 @@ class StyleCacheBuilder implements ICacheBuilder { WHERE styleID = ?"; $statement2 = WCF::getDB()->prepareStatement($sql); $statement2->execute(array($row['styleID'])); - while ($row = $statement2->fetchArray()) { - + while ($row2 = $statement2->fetchArray()) { $row['variables'][$row2['variableName']] = $row2['variableValue']; } @@ -55,7 +54,6 @@ class StyleCacheBuilder implements ICacheBuilder { $statement = WCF::getDB()->prepareStatement($sql); $statement->execute(); while ($row = $statement->fetchArray()) { - if (!isset($data['packages'][$row['packageID']])) { $data['packages'][$row['packageID']] = array('default' => 0, 'disabled' => array()); } diff --git a/wcfsetup/install/files/lib/system/style/StyleHandler.class.php b/wcfsetup/install/files/lib/system/style/StyleHandler.class.php index c6178e17c3..58f60f18b0 100644 --- a/wcfsetup/install/files/lib/system/style/StyleHandler.class.php +++ b/wcfsetup/install/files/lib/system/style/StyleHandler.class.php @@ -65,6 +65,10 @@ class StyleHandler extends SingletonFactory { * @return wcf\data\style\ActiveStyle */ public function getStyle() { + if ($this->style === null) { + $this->changeStyle(); + } + return $this->style; } @@ -72,8 +76,9 @@ class StyleHandler extends SingletonFactory { * Changes the active style. * * @param integer $styleID + * @param boolean $ignorePermissions */ - public function changeStyle($styleID, $ignorePermissions = false) { + public function changeStyle($styleID = 0, $ignorePermissions = false) { // check permission if (!$ignorePermissions) { if (isset($this->cache['styles'][$styleID])) { diff --git a/wcfsetup/install/files/lib/system/template/plugin/IconCompilerTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/IconCompilerTemplatePlugin.class.php index b016ccb23d..ed999feffa 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/IconCompilerTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/IconCompilerTemplatePlugin.class.php @@ -7,7 +7,7 @@ use wcf\util\StringUtil; * The 'icon' compiler function compiles dynamic icon paths. * * Usage: - * {icon}{$foo}{/icon} + * {icon size='L'}{$foo}{/icon} * * @author Marcel Werk * @copyright 2001-2011 WoltLab GmbH @@ -17,10 +17,31 @@ use wcf\util\StringUtil; * @category Community Framework */ class IconCompilerTemplatePlugin implements ICompilerTemplatePlugin { + /** + * icon size + * @var string + */ + protected $size = ''; + + /** + * valid icon sizes + * @var array + */ + protected static $validSizes = array('S', 'M', 'L'); + /** * @see wcf\system\template\ICompilerTemplatePlugin::executeStart() */ public function executeStart($tagArgs, TemplateScriptingCompiler $compiler) { + // set default size + $this->size = 'L'; + + // get size + if (isset($tagArgs['size'])) { + if (strlen($tagArgs['size']) > 1) $tagArgs['size'] = substr($tagArgs['size'], 1, 1); + if (in_array($tagArgs['size'], self::$validSizes)) $this->size = $tagArgs['size']; + } + $compiler->pushTag('icon'); return ""; } @@ -31,6 +52,6 @@ class IconCompilerTemplatePlugin implements ICompilerTemplatePlugin { public function executeEnd(TemplateScriptingCompiler $compiler) { $compiler->popTag('icon'); $hash = StringUtil::getRandomID(); - return "getStyle()->getIconPath(\$_icon".$hash."); ?>"; + return "getStyle()->getIconPath(\$_icon".$hash.", '".$this->size."'); ?>"; } } -- 2.20.1