Added support for {icon} tags in templates
authorMarcel Werk <burntime@woltlab.com>
Tue, 25 Oct 2011 19:03:05 +0000 (21:03 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 25 Oct 2011 19:03:05 +0000 (21:03 +0200)
wcfsetup/install/files/lib/data/package/Package.class.php
wcfsetup/install/files/lib/data/style/ActiveStyle.class.php
wcfsetup/install/files/lib/system/cache/builder/IconCacheBuilder.class.php
wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php
wcfsetup/install/files/lib/system/style/StyleHandler.class.php
wcfsetup/install/files/lib/system/template/plugin/IconCompilerTemplatePlugin.class.php

index 48e397e53c7ef946ca39564d7c912fa0df0a8122..2b68950b7f97d9264e9ed60409568b64f6ad95f4 100644 (file)
@@ -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.
         *
index 15b82e630a919d7278d7a893d39eefdc31bd69dc..c04bd1366d901e13789ec1b5e846de8653960e96 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\style;
 use wcf\data\DatabaseObjectDecorator;
+use wcf\system\cache\CacheHandler;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
 use wcf\util\StyleUtil;
@@ -42,12 +43,12 @@ class ActiveStyle extends DatabaseObjectDecorator {
                
                // load icon cache
                $cacheName = 'icon-'.PACKAGE_ID.'-'.$this->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';
        }
 }
index 41c312f0559a22d1aa2a9daebf3859d09d4cddcd..14a89bf0e416790b424c53eafe4fe9e90cb9ee93 100644 (file)
@@ -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());
                                }
                        }
                }
index 7f0dfab6a5b46ef028be61319748ea2e847cb829..98f0f234360313478541f11139c98d9cddf6b19c 100644 (file)
@@ -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());
                        }
index c6178e17c3edc966e05f06629edd8c8581febab5..58f60f18b0dde0a81188aa95863e70fa2bd31c65 100644 (file)
@@ -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])) {
index b016ccb23d1edaeb292dccc5f00b2c36469d7dbc..ed999feffae77bf87dfbaa9e4389b48b9d481cb6 100644 (file)
@@ -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<string>
+        */
+       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 "<?php ob_start(); ?>";
        }
@@ -31,6 +52,6 @@ class IconCompilerTemplatePlugin implements ICompilerTemplatePlugin {
        public function executeEnd(TemplateScriptingCompiler $compiler) {
                $compiler->popTag('icon');
                $hash = StringUtil::getRandomID();
-               return "<?php \$_icon".$hash." = ob_get_contents(); ob_end_clean(); echo wcf\system\style\StyleHandler::getInstance()->getStyle()->getIconPath(\$_icon".$hash."); ?>";
+               return "<?php \$_icon".$hash." = ob_get_contents(); ob_end_clean(); echo wcf\system\style\StyleHandler::getInstance()->getStyle()->getIconPath(\$_icon".$hash.", '".$this->size."'); ?>";
        }
 }