Move saveImage action in ReactionTypeAction
authorJoshua Rüsweg <josh@bastelstu.be>
Sun, 3 Feb 2019 18:35:57 +0000 (19:35 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Sun, 3 Feb 2019 18:35:57 +0000 (19:35 +0100)
See #2825

wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php
wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php
wcfsetup/install/files/lib/data/reaction/type/ReactionTypeAction.class.php

index d210544ac35b55de3d38c184e140136c911fe059..d5c2c2ff1c3b28462a9fd0c9bd16933a072e7c19 100644 (file)
@@ -2,10 +2,8 @@
 namespace wcf\acp\form;
 use wcf\data\reaction\type\ReactionType;
 use wcf\data\reaction\type\ReactionTypeAction;
-use wcf\data\reaction\type\ReactionTypeEditor;
 use wcf\data\reaction\type\ReactionTypeList;
 use wcf\form\AbstractFormBuilderForm;
-use wcf\system\file\upload\UploadFile;
 use wcf\system\form\builder\container\FormContainer;
 use wcf\system\form\builder\field\IsDisabledFormField;
 use wcf\system\form\builder\field\RadioButtonFormField;
@@ -102,36 +100,4 @@ class ReactionTypeAddForm extends AbstractFormBuilderForm {
                        $iconContainer
                ]);
        }
-       
-       /**
-        * @inheritDoc
-        */
-       public function saved() {
-               $this->saveImage($this->objectAction->getReturnValues()['returnValues']);
-               
-               parent::saved();
-       }
-       
-       /**
-        * Save the image for a reaction type.
-        * 
-        * @param       ReactionType    $reactionType
-        */
-       protected function saveImage(ReactionType $reactionType) {
-               $files = $this->uploadFormField->getValue();
-               
-               /** @var UploadFile $file */
-               $file = array_pop($files);
-               if (!$file->isProcessed()) {
-                       $fileName = $reactionType->reactionTypeID . '-' . $file->getFilename();
-                       
-                       rename($file->getLocation(), WCF_DIR . '/images/reaction/' . $fileName);
-                       $file->setProcessed(WCF_DIR . '/images/reaction/' . $fileName);
-                       
-                       $reactionTypeEditor = new ReactionTypeEditor($reactionType);
-                       $reactionTypeEditor->update([
-                               'iconFile' => $fileName
-                       ]);
-               }
-       }
 }
index 95f42695c9b2de3307980f56d09e9574931fec39..fae735ed65d39d978b410e4ec59283409b19bbaa 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 namespace wcf\acp\form;
 use wcf\data\reaction\type\ReactionType;
-use wcf\form\AbstractFormBuilderForm;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\file\upload\UploadFile;
 
@@ -51,18 +50,4 @@ class ReactionTypeEditForm extends ReactionTypeAddForm {
                        ]);
                }
        }
-       
-       /**
-        * @inheritDoc
-        */
-       public function saved() {
-               // remove removed files
-               foreach ($this->uploadFormField->getRemovedFiles(true) as $file) {
-                       @unlink($file->getLocation());
-               }
-               
-               $this->saveImage($this->formObject);
-               
-               AbstractFormBuilderForm::saved();
-       }
 }
index 43b1245d0bafe5ac29ab76f54d48ac2eb61c54be..76c8b148f376e83c80901e312e918027876159a9 100644 (file)
@@ -4,6 +4,7 @@ use wcf\data\AbstractDatabaseObjectAction;
 use wcf\data\ISortableAction;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\UserInputException;
+use wcf\system\file\upload\UploadFile;
 use wcf\system\language\I18nHandler;
 use wcf\system\WCF;
 
@@ -56,6 +57,24 @@ class ReactionTypeAction extends AbstractDatabaseObjectAction implements ISortab
                        $updateData['title'] = 'wcf.reactionType.title' . $reactionType->reactionTypeID;
                }
                
+               // image
+               if (isset($this->parameters['iconFile']) && is_array($this->parameters['iconFile'])) {
+                       $iconFile = reset($this->parameters['iconFile']);
+                       if (!($iconFile instanceof UploadFile)) {
+                               throw new \InvalidArgumentException("The parameter 'image' is no instance of '". UploadFile::class ."', instance of '". get_class($iconFile) ."' given.");
+                       }
+                       
+                       // save new image
+                       if (!$iconFile->isProcessed()) {
+                               $fileName = $reactionType->reactionTypeID . '-' . $iconFile->getFilename();
+                               
+                               rename($iconFile->getLocation(), WCF_DIR . '/images/reaction/' . $fileName);
+                               $iconFile->setProcessed(WCF_DIR . '/images/reaction/' . $fileName);
+                               
+                               $updateData['iconFile'] = $fileName;
+                       }
+               }
+               
                if (!empty($updateData)) {
                        $reactionTypeEditor->update($updateData);
                }
@@ -69,8 +88,10 @@ class ReactionTypeAction extends AbstractDatabaseObjectAction implements ISortab
        public function update() {
                parent::update();
                
-               // i18n
                foreach ($this->getObjects() as $object) {
+                       $updateData = [];
+                       
+                       // i18n
                        if (isset($this->parameters['title_i18n'])) {
                                I18nHandler::getInstance()->save(
                                        $this->parameters['title_i18n'],
@@ -79,10 +100,38 @@ class ReactionTypeAction extends AbstractDatabaseObjectAction implements ISortab
                                        1
                                );
                                
-                               $object->update([
-                                       'title' => 'wcf.reactionType.title' . $object->reactionTypeID
-                               ]);
+                               $updateData['title'] = 'wcf.reactionType.title' . $object->reactionTypeID;
+                       }
+                       
+                       // delete orphaned images
+                       if (isset($this->parameters['iconFile_removedFiles']) && is_array($this->parameters['iconFile_removedFiles'])) {
+                               /** @var UploadFile $file */
+                               foreach ($this->parameters['iconFile_removedFiles'] as $file) {
+                                       @unlink($file->getLocation());
+                               }
                        }
+                       
+                       // image
+                       if (isset($this->parameters['iconFile']) && is_array($this->parameters['iconFile'])) {
+                               $iconFile = reset($this->parameters['iconFile']);
+                               if (!($iconFile instanceof UploadFile)) {
+                                       throw new \InvalidArgumentException("The parameter 'image' is no instance of '". UploadFile::class ."', instance of '". get_class($iconFile) ."' given.");
+                               }
+                               
+                               // save new image
+                               if (!$iconFile->isProcessed()) {
+                                       $fileName = $object->reactionTypeID . '-' . $iconFile->getFilename();
+                                       
+                                       rename($iconFile->getLocation(), WCF_DIR . '/images/reaction/' . $fileName);
+                                       $iconFile->setProcessed(WCF_DIR . '/images/reaction/' . $fileName);
+                                       
+                                       $updateData['iconFile'] = $fileName;
+                               }
+                       }
+                       
+                       if (!empty($updateData)) {
+                               $object->update($updateData);
+                       } 
                }
        }