From aa8ed8fb48c2fc8b6aa135585d27550f9bdc7006 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Sun, 3 Feb 2019 19:35:57 +0100 Subject: [PATCH] Move saveImage action in ReactionTypeAction See #2825 --- .../acp/form/ReactionTypeAddForm.class.php | 34 ----------- .../acp/form/ReactionTypeEditForm.class.php | 15 ----- .../type/ReactionTypeAction.class.php | 57 +++++++++++++++++-- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php b/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php index d210544ac3..d5c2c2ff1c 100644 --- a/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php @@ -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 - ]); - } - } } diff --git a/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php b/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php index 95f42695c9..fae735ed65 100644 --- a/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php @@ -1,7 +1,6 @@ uploadFormField->getRemovedFiles(true) as $file) { - @unlink($file->getLocation()); - } - - $this->saveImage($this->formObject); - - AbstractFormBuilderForm::saved(); - } } diff --git a/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeAction.class.php b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeAction.class.php index 43b1245d0b..76c8b148f3 100644 --- a/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeAction.class.php +++ b/wcfsetup/install/files/lib/data/reaction/type/ReactionTypeAction.class.php @@ -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); + } } } -- 2.20.1