Improved style API
authorAlexander Ebert <ebert@woltlab.com>
Thu, 11 Oct 2012 22:02:55 +0000 (00:02 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 11 Oct 2012 22:02:55 +0000 (00:02 +0200)
wcfsetup/install/files/lib/data/style/Style.class.php
wcfsetup/install/files/lib/data/style/StyleAction.class.php
wcfsetup/install/files/lib/data/style/StyleEditor.class.php
wcfsetup/install/files/lib/system/cache/builder/StyleCacheBuilder.class.php

index 3c834483de172165260308095a765e9d199cddf9..f8cfaf40bc039c930b1b5e41189d9a6b55fd5bc3 100644 (file)
@@ -24,6 +24,12 @@ class Style extends DatabaseObject {
         */
        protected static $databaseTableIndexName = 'styleID';
        
+       /**
+        * list of style variables
+        * @var array<string>
+        */
+       protected $variables = array();
+       
        const PREVIEW_IMAGE_MAX_HEIGHT = 140;
        const PREVIEW_IMAGE_MAX_WIDTH = 185;
        
@@ -42,7 +48,33 @@ class Style extends DatabaseObject {
         * @return      array<string>
         */
        public function getVariables() {
-               $variables = array();
+               $this->loadVariables();
+               
+               return $this->variables;
+       }
+       
+       /**
+        * Returns a specific style variable or null if not found.
+        * 
+        * @param       string          $variableName
+        * @return      string
+        */
+       public function getVariable($variableName) {
+               if (isset($this->variables[$variableName])) {
+                       return $this->variables[$variableName];
+               }
+               
+               return null;
+       }
+       
+       /**
+        * Loads style-specific variables.
+        */
+       public function loadVariables() {
+               if (!empty($this->variables)) {
+                       return;
+               }
+               
                $sql = "SELECT          variable.variableName, variable.defaultValue, value.variableValue
                        FROM            wcf".WCF_N."_style_variable variable
                        LEFT JOIN       wcf".WCF_N."_style_variable_value value
@@ -51,10 +83,8 @@ class Style extends DatabaseObject {
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute(array($this->styleID));
                while ($row = $statement->fetchArray()) {
-                       $variables[$row['variableName']] = (isset($row['variableValue'])) ? $row['variableValue'] : $row['defaultValue'];
+                       $this->variables[$row['variableName']] = (isset($row['variableValue'])) ? $row['variableValue'] : $row['defaultValue'];
                }
-               
-               return $variables;
        }
        
        /**
index 839a5a7051a43bda51f397fae33df71e1da022b5..b0aeaf2d23b77a7551e05a27408eaf2d95dcade6 100644 (file)
@@ -27,6 +27,11 @@ class StyleAction extends AbstractDatabaseObjectAction {
         */
        protected $className = 'wcf\data\style\StyleEditor';
        
+       /**
+        * @see wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
+        */
+       protected $permissionsDelete = array('admin.style.canDeleteStyle');
+       
        /**
         * @see wcf\data\AbstractDatabaseObjectAction::create()
         */
@@ -282,4 +287,32 @@ class StyleAction extends AbstractDatabaseObjectAction {
                
                return array('errorType' => $file->getValidationErrorType());
        }
+       
+       /**
+        * Validates parameters to assign a new default style.
+        */
+       public function validateSetAsDefault() {
+               if (WCF::getSession()->getPermission('admin.style.canEditStyle')) {
+                       throw new PermissionDeniedException();
+               }
+               
+               if (empty($this->objects)) {
+                       $this->readObjects();
+                       if (empty($this->objects)) {
+                               throw new UserInputException('objectIDs');
+                       }
+               }
+               
+               if (count($this->objects) > 1) {
+                       throw new UserInputException('objectIDs');
+               }
+       }
+       
+       /**
+        * Sets a style as new default style.
+        */
+       public function setAsDefault() {
+               $styleEditor = current($this->objects);
+               $styleEditor->setAsDefault();
+       }
 }
index ef54f61894b46ae8988c5518d2dac9d9b3642750..cb4c386517d0d3baebf731bafc18afb6d7757f19 100644 (file)
@@ -110,6 +110,8 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
                        'isDefault' => 1,
                        'disabled' => 0
                ));
+               
+               self::resetCache();
        }
        
        /**
index 33e529a97d697c477cce9a3c74be6bb9b0cc82f7..078dedd2dd81d4a3351c7a89e32e53806647d5e8 100644 (file)
@@ -32,9 +32,14 @@ class StyleCacheBuilder implements ICacheBuilder {
                $statement->execute();
                while ($row = $statement->fetchArray()) {
                        if ($row['isDefault']) $data['default'] = $row['styleID'];
-                       $data['styles'][$row['styleID']] = new Style(null, $row);
+                       $style = new Style(null, $row);
+                       $style->loadVariables();
+                       
+                       $data['styles'][$row['styleID']] = $style;
                }
                
+               // load style-specific variables
+               
                // get style to packages
                $sql = "SELECT          *
                        FROM            wcf".WCF_N."_style_to_package