Add upload form field to ReactionType(Add|Edit)Form
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 31 Jan 2019 13:20:25 +0000 (14:20 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 31 Jan 2019 13:20:25 +0000 (14:20 +0100)
See #2825
See #2508

wcfsetup/install/files/lib/acp/form/ReactionTypeAddForm.class.php
wcfsetup/install/files/lib/acp/form/ReactionTypeEditForm.class.php

index baafb162e6f8869c3f9559eb46f77c521defa201..84c6be0d7d039975f3a54da5ebc89d3790d48cbc 100644 (file)
@@ -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
+                       ]);
+               }
+       }
 }
index af5f65af9572796667193badd4fe45e372e80b3c..95f42695c9b2de3307980f56d09e9574931fec39 100644 (file)
@@ -1,7 +1,9 @@
 <?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;
 
 /**
  * Represents the reaction type add form.
@@ -36,4 +38,31 @@ class ReactionTypeEditForm extends ReactionTypeAddForm {
                        }
                }
        }
+       
+       /**
+        * Sets the form data based on the current form object.
+        */
+       protected function setFormObjectData() {
+               parent::setFormObjectData(); 
+               
+               if (empty($_POST)) {
+                       $this->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();
+       }
 }