From: Joshua Rüsweg Date: Tue, 5 Feb 2019 11:29:10 +0000 (+0100) Subject: Add method to determine UploadFiles by DBO X-Git-Tag: 5.2.0_Alpha_1~296^2~3 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=d5bee06952b0134a13ad78e4af8467c8a887cc59;p=GitHub%2FWoltLab%2FWCF.git Add method to determine UploadFiles by DBO See #2825 --- diff --git a/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php b/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php index c55f39bdc3..c3e6e9353e 100644 --- a/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php +++ b/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php @@ -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; + } } diff --git a/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php b/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php index 69a07cb518..7441856aa8 100644 --- a/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php +++ b/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php @@ -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); } } diff --git a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php index ce5ae762a7..c2e26dd159 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php @@ -1,5 +1,6 @@ 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 *