From: Joshua Rüsweg Date: Thu, 31 Jan 2019 13:20:25 +0000 (+0100) Subject: Add upload form field to ReactionType(Add|Edit)Form X-Git-Tag: 5.2.0_Alpha_1~296^2~18 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8827fb94155e7c9a84a8398e5bd7494eeb98742e;p=GitHub%2FWoltLab%2FWCF.git Add upload form field to ReactionType(Add|Edit)Form See #2825 See #2508 --- diff --git a/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php b/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php index baafb162e6..84c6be0d7d 100644 --- a/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php @@ -2,16 +2,17 @@ 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; use wcf\system\form\builder\field\ShowOrderFormField; -use wcf\system\form\builder\field\TextFormField; use wcf\system\form\builder\field\TitleFormField; -use wcf\system\form\builder\field\validation\FormFieldValidationError; -use wcf\system\form\builder\field\validation\FormFieldValidator; +use wcf\system\form\builder\field\UploadFormField; +use wcf\util\StringUtil; /** * Represents the reaction type add form. @@ -48,6 +49,11 @@ class ReactionTypeAddForm extends AbstractFormBuilderForm { */ public $neededModules = ['MODULE_LIKE']; + /** + * @var UploadFormField + */ + protected $uploadFormField; + /** * @inheritDoc */ @@ -78,21 +84,18 @@ class ReactionTypeAddForm extends AbstractFormBuilderForm { ->label('wcf.acp.reactionType.isDisabled') ]); + $this->uploadFormField = UploadFormField::create('iconFile') + ->label('wcf.acp.reactionType.image') + ->description('wcf.acp.reactionType.image.description') + ->required() + ->maximum(1) + ->imageOnly(true) + ->allowSvgImage(true); + $iconContainer = FormContainer::create('imageSection') ->label('wcf.acp.reactionType.image') ->appendChildren([ - TextFormField::create('iconFile') - ->label('wcf.acp.reactionType.image') - ->description('wcf.acp.reactionType.image.description') - ->required() - ->addValidator(new FormFieldValidator('invalidPath', function(TextFormField $field) { - if (!file_exists(WCF_DIR.'images/reaction/'.$field->getValue())) { - $field->addValidationError(new FormFieldValidationError( - 'invalidPath', - 'wcf.acp.reactionType.image.invalidPath' - )); - } - })) + $this->uploadFormField ]); $this->form->appendChildren([ @@ -100,4 +103,40 @@ 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(); + + if (file_exists(WCF_DIR . '/images/reaction/' . $fileName)) { + $fileName = $reactionType->reactionTypeID . '-'. substr(0, 5, StringUtil::getRandomID()) . '-' . $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 af5f65af95..95f42695c9 100644 --- a/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php @@ -1,7 +1,9 @@ uploadFormField->value([ + new UploadFile(WCF_DIR . 'images/reaction/' . $this->formObject->iconFile, basename($this->formObject->iconFile), true, true, true) + ]); + } + } + + /** + * @inheritDoc + */ + public function saved() { + // remove removed files + foreach ($this->uploadFormField->getRemovedFiles(true) as $file) { + @unlink($file->getLocation()); + } + + $this->saveImage($this->formObject); + + AbstractFormBuilderForm::saved(); + } }