Add method to determine UploadFiles by DBO
authorJoshua Rüsweg <josh@bastelstu.be>
Tue, 5 Feb 2019 11:29:10 +0000 (12:29 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Tue, 5 Feb 2019 11:29:10 +0000 (12:29 +0100)
See #2825

wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php
wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php
wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php

index c55f39bdc374300857d6885117b8e54993037ae2..c3e6e9353e7542f04bd6e98032fd4a541697d979 100644 (file)
@@ -97,4 +97,13 @@ class ReactionType extends DatabaseObject implements ITitledObject {
        public function isNeutral() {
                return $this->type == self::REACTION_TYPE_NEUTRAL;
        }
+       
+       /**
+        * Returns the absolute location of the icon file. 
+        * 
+        * @return string
+        */
+       public function getIconFileUploadFileLocations() {
+               return WCF_DIR . 'images/reaction/'. $this->iconFile;
+       }
 }
index 69a07cb51810da2550543c69c3549a80570568c6..7441856aa8f38a60846d4451bd4c3edc63febba8 100644 (file)
@@ -372,7 +372,7 @@ class UploadHandler extends SingletonFactory {
                        /** @var UploadFile $file */
                        foreach ($files as $file) {
                                if (!file_exists($file->getLocation())) {
-                                       $this->removeFile($internalId, $file);
+                                       $this->removeFileByObject($internalId, $file);
                                }
                        }
                        
index ce5ae762a7fe1eb1a71658896f5d569881d2b4fc..c2e26dd159db694bbff6de6da982620bd5c4e61b 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\form\builder\field;
+use wcf\data\IStorableObject;
 use wcf\system\file\upload\UploadField;
 use wcf\system\file\upload\UploadFile;
 use wcf\system\file\upload\UploadHandler;
@@ -104,14 +105,6 @@ class UploadFormField extends AbstractFormField {
                return UploadHandler::getInstance()->getRemovedFiledByFieldId($this->getId(), $processFiles);
        }
        
-       
-       /**
-        * @inheritDoc
-        */
-       public function getObjectProperty() {
-               return null;
-       }
-       
        /**
         * @inheritDoc
         */
@@ -158,6 +151,95 @@ class UploadFormField extends AbstractFormField {
                return parent::getHtml();
        }
        
+       /**
+        * @inheritDoc
+        * 
+        * @throws \InvalidArgumentException    if the getter for the value provides invalid values
+        */
+       public function loadValueFromObject(IStorableObject $object) {
+               // first check, whether an getter for the field exists
+               if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations')) {
+                       $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations']);
+                       
+                       if (is_array($value)) {
+                               foreach ($value as &$v) {
+                                       if (is_string($v) && file_exists($v)) {
+                                               $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                                       }
+                                       else {
+                                               throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()' provides invalid data.");
+                                       }
+                               }
+                               
+                               $this->value($value);
+                       }
+                       else {
+                               if (is_string($value) && file_exists($value)) {
+                                       $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                               }
+                               else {
+                                       throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()' provides invalid data.");
+                               }
+                               
+                               $this->value([$value]);
+                       }
+               }
+               else if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()))) {
+                       $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty())]);
+                       
+                       if (is_array($value)) {
+                               foreach ($value as &$v) {
+                                       if (is_string($v) && file_exists($v)) {
+                                               $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                                       }
+                                       else {
+                                               throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."()' provides invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'.");
+                                       }
+                               }
+                               
+                               $this->value($value);
+                       }
+                       else {
+                               if (is_string($value) && file_exists($value)) {
+                                       $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                               }
+                               else {
+                                       throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."()' provides invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'.");
+                               }
+                               
+                               $this->value([$value]);
+                       }
+               }
+               else {
+                       $value = $object->{$this->getObjectProperty()};
+                       
+                       if (is_array($value)) {
+                               foreach ($value as &$v) {
+                                       if (is_string($v) && file_exists($v)) {
+                                               $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                                       }
+                                       else {
+                                               throw new \InvalidArgumentException("The property '". get_class($object) ."::$". $this->getObjectProperty() ."' contains invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'.");
+                                       }
+                               }
+                               
+                               $this->value($value);
+                       }
+                       else {
+                               if (is_string($value) && file_exists($value)) {
+                                       $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+                               }
+                               else {
+                                       throw new \InvalidArgumentException("The property '". get_class($object) ."::$". $this->getObjectProperty() ."' contains invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'.");
+                               }
+                               
+                               $this->value([$value]);
+                       }
+               }
+               
+               return $this;
+       }
+       
        /**
         * @inheritDoc
         *