Added read*() methods to DBOActions
authorAlexander Ebert <ebert@woltlab.com>
Mon, 15 Oct 2012 01:28:37 +0000 (03:28 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 15 Oct 2012 01:28:37 +0000 (03:28 +0200)
wcfsetup/install/files/lib/data/AbstractDatabaseObjectAction.class.php
wcfsetup/install/files/lib/data/style/StyleAction.class.php

index 21682082e7f13a283f643a388c123b1ebba24487..6cc8253581b8b761b4a9038ff3a627ccc5bbb0ab 100644 (file)
@@ -3,6 +3,7 @@ namespace wcf\data;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\SystemException;
+use wcf\system\exception\UserInputException;
 use wcf\system\exception\ValidateActionException;
 use wcf\system\WCF;
 use wcf\util\ClassUtil;
@@ -80,6 +81,9 @@ abstract class AbstractDatabaseObjectAction implements IDatabaseObjectAction, ID
         */
        protected $allowGuestAccess = array();
        
+       const TYPE_INTEGER = 1;
+       const TYPE_STRING = 2;
+       
        /**
         * Initialize a new DatabaseObject-related action.
         *
@@ -352,6 +356,106 @@ abstract class AbstractDatabaseObjectAction implements IDatabaseObjectAction, ID
                }
        }
        
+       /**
+        * Returns a single object and throws and exception if no or more than one object is given.
+        * 
+        * @return      wcf\data\DatabaseObject
+        */
+       protected function getSingleObject() {
+               if (empty($this->objects)) {
+                       $this->readObjects();
+               }
+               
+               if (count($this->objects) != 1) {
+                       throw new UserInputException('objectIDs');
+               }
+               
+               reset($this->objects);
+               return current($this->objects);
+       }
+       
+       /**
+        * Reads an integer value and validates it.
+        * 
+        * @param       string          $variableName
+        * @param       boolean         $allowEmpty
+        * @param       string          $arrayIndex
+        */
+       protected function readInteger($variableName, $allowEmpty = false, $arrayIndex = '', $allowUnset = false) {
+               $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_INTEGER);
+       }
+       
+       /**
+        * Reads a string value and validates it.
+        * 
+        * @param       string          $variableName
+        * @param       boolean         $allowEmpty
+        * @param       string          $arrayIndex
+        */
+       protected function readString($variableName, $allowEmpty = false, $arrayIndex = '') {
+               $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_STRING);
+       }
+       
+       /**
+        * Reads a value and validates it. If you set $allowEmpty to true, no exception will
+        * be thrown if the variable evaluates to 0 (integer) or '' (string). Furthermore the
+        * variable will be always created with a sane value if it does not exist.
+        * 
+        * @param       string          $variableName
+        * @param       boolean         $allowEmpty
+        * @param       string          $arrayIndex
+        * @param       integer         $type
+        */
+       protected function readValue($variableName, $allowEmpty, $arrayIndex, $type) {
+               if ($arrayIndex) {
+                       if (!isset($this->parameters[$arrayIndex])) {
+                               throw new SystemException("Corrupt parameters, index '".$arrayIndex."' is missing");
+                       }
+                       
+                       $target =& $this->parameters[$variableName];
+               }
+               else {
+                       $target =& $this->parameters;
+               }
+               
+               switch ($type) {
+                       case self::TYPE_INTEGER:
+                               if (!isset($target[$variableName])) {
+                                       if ($allowEmpty) {
+                                               $target[$variableName] = 0;
+                                       }
+                                       else {
+                                               throw new UserInputException($variableName);
+                                       }
+                               }
+                               else {
+                                       $target[$variableName] = intval($target[$variableName]);
+                                       if (!$allowEmpty && !$target[$variableName]) {
+                                               throw new UserInputException($variableName);
+                                       }
+                               }
+                       break;
+                       
+                       case self::TYPE_STRING:
+                               if (!isset($target[$variableName])) {
+                                       if ($allowEmpty) {
+                                               $target[$variableName] = '';
+                                       }
+                                       else {
+                                               throw new UserInputException($variableName);
+                                       }
+                               }
+                               else {
+                                       $target[$variableName] = StringUtil::trim($target[$variableName]);
+                                       if (!$allowEmpty && empty($target[$variableName])) {
+                                               throw new UserInputException($variableName);
+                                       }
+                               }
+                       break;
+               }
+               
+       }
+       
        /**
         * Returns object class name.
         * 
index c3b9baff01db00ae8e72ed5d1541e12972ead400..79c0af7bc828775d39d319a1c8455e430dd847b3 100644 (file)
@@ -40,6 +40,12 @@ class StyleAction extends AbstractDatabaseObjectAction {
         */
        protected $permissionsUpdate = array('admin.style.canEditStyle');
        
+       /**
+        * style editor object
+        * @var wcf\data\style\StyleEditor
+        */
+       public $styleEditor = null;
+       
        /**
         * @see wcf\data\AbstractDatabaseObjectAction::create()
         */
@@ -332,16 +338,7 @@ class StyleAction extends AbstractDatabaseObjectAction {
                        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');
-               }
+               $this->styleEditor = $this->getSingleObject();
        }
        
        /**
@@ -350,8 +347,6 @@ class StyleAction extends AbstractDatabaseObjectAction {
         * @return      array<string>
         */
        public function copy() {
-               $style = current($this->objects);
-               
                // get unique style name
                $sql = "SELECT  styleName
                        FROM    wcf".WCF_N."_style
@@ -359,8 +354,8 @@ class StyleAction extends AbstractDatabaseObjectAction {
                                AND styleID <> ?";
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute(array(
-                       $style->styleName.'%',
-                       $style->styleID
+                       $this->styleEditor->styleName.'%',
+                       $this->styleEditor->styleID
                ));
                $numbers = array();
                $regEx = new Regex('\((\d+)\)$');
@@ -371,30 +366,30 @@ class StyleAction extends AbstractDatabaseObjectAction {
                                $matches = $regEx->getMatches();
                                
                                // check if name matches the pattern 'styleName (x)'
-                               if ($styleName == $style->styleName . ' ('.$matches[1].')') {
+                               if ($styleName == $this->styleEditor->styleName . ' ('.$matches[1].')') {
                                        $numbers[] = $matches[1];
                                }
                        }
                }
                
                $number = (count($numbers)) ? max($numbers) + 1 : 2;
-               $styleName = $style->styleName . ' ('.$number.')';
+               $styleName = $this->styleEditor->styleName . ' ('.$number.')';
                
                // create the new style
                $newStyle = StyleEditor::create(array(
                        'packageID' => PACKAGE_ID,
                        'styleName' => $styleName,
-                       'templateGroupID' => $style->templateGroupID,
+                       'templateGroupID' => $this->styleEditor->templateGroupID,
                        'disabled' => 1, // newly created styles are disabled by default
-                       'styleDescription' => $style->styleDescription,
-                       'styleVersion' => $style->styleVersion,
-                       'styleDate' => $style->styleDate,
-                       'copyright' => $style->copyright,
-                       'license' => $style->license,
-                       'authorName' => $style->authorName,
-                       'authorURL' => $style->authorURL,
-                       'iconPath' => $style->iconPath,
-                       'imagePath' => $style->imagePath
+                       'styleDescription' => $this->styleEditor->styleDescription,
+                       'styleVersion' => $this->styleEditor->styleVersion,
+                       'styleDate' => $this->styleEditor->styleDate,
+                       'copyright' => $this->styleEditor->copyright,
+                       'license' => $this->styleEditor->license,
+                       'authorName' => $this->styleEditor->authorName,
+                       'authorURL' => $this->styleEditor->authorURL,
+                       'iconPath' => $this->styleEditor->iconPath,
+                       'imagePath' => $this->styleEditor->imagePath
                ));
                
                // copy style variables
@@ -404,15 +399,15 @@ class StyleAction extends AbstractDatabaseObjectAction {
                        FROM            wcf".WCF_N."_style_variable_value value
                        WHERE           value.styleID = ?";
                $statement = WCF::getDB()->prepareStatement($sql);
-               $statement->execute(array($style->styleID));
+               $statement->execute(array($this->styleEditor->styleID));
                
                // copy preview image
-               if ($style->image) {
+               if ($this->styleEditor->image) {
                        // get extension
-                       $fileExtension = StringUtil::substring($style->image, StringUtil::lastIndexOf($style->image, '.'));
+                       $fileExtension = StringUtil::substring($this->styleEditor->image, StringUtil::lastIndexOf($this->styleEditor->image, '.'));
                        
                        // copy existing preview image
-                       if (@copy(WCF_DIR.'images/'.$style->image, WCF_DIR.'images/stylePreview-'.$newStyle->styleID.$fileExtension)) {
+                       if (@copy(WCF_DIR.'images/'.$this->styleEditor->image, WCF_DIR.'images/stylePreview-'.$newStyle->styleID.$fileExtension)) {
                                // bypass StyleEditor::update() to avoid scaling of already fitting image
                                $sql = "UPDATE  wcf".WCF_N."_style
                                        SET     image = ?