From: Joshua Rüsweg Date: Tue, 15 Jan 2019 21:16:32 +0000 (+0100) Subject: Add basic UploadFormField X-Git-Tag: 5.2.0_Alpha_1~296^2~37 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=3d111360c921f89c70f6191cfb1b1642d0026b28;p=GitHub%2FWoltLab%2FWCF.git Add basic UploadFormField See #2825 --- diff --git a/com.woltlab.wcf/templates/__uploadFormField.tpl b/com.woltlab.wcf/templates/__uploadFormField.tpl new file mode 100644 index 0000000000..83bcc45a13 --- /dev/null +++ b/com.woltlab.wcf/templates/__uploadFormField.tpl @@ -0,0 +1,5 @@ +{include file='__formFieldHeader'} + +{@$__wcf->getUploadHandler()->renderField($field->getPrefixedId())} + +{include file='__formFieldFooter'} 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 8352ad1aba..3b169813be 100644 --- a/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php +++ b/wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php @@ -310,6 +310,16 @@ class UploadHandler extends SingletonFactory { return count($this->getFilesForInternalId($internalId)); } + /** + * Returns true, iff a field with the given fieldId is already registered. + * + * @param string $fieldId + * @return boolean + */ + public function isRegisteredFieldId($fieldId) { + return isset($this->fields[$fieldId]); + } + /** * Returns the files for an internal identifier. * 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 new file mode 100644 index 0000000000..30911da0ff --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php @@ -0,0 +1,168 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.2 + */ +class UploadFormField extends AbstractFormField { + use TMaximumFormField { + maximum as traitMaximum; + } + use TMinimumFormField; + + /** + * imageOnly flag for the upload field. + * @var boolean + */ + protected $imageOnly = false; + + /** + * @inheritDoc + */ + protected $templateName = '__uploadFormField'; + + /** + * Registers the current field in the upload handler. + */ + private function registerField() { + if (!UploadHandler::getInstance()->isRegisteredFieldId($this->getId())) { + UploadHandler::getInstance()->registerUploadField($this->buildUploadField()); + } + } + + /** + * Builds the UploadField class. + * + * @return UploadField + */ + protected function buildUploadField() { + $uploadField = new UploadField($this->getId()); + $uploadField->maxFiles = $this->getMaximum(); + $uploadField->setImageOnly($this->isImageOnly()); + + return $uploadField; + } + + /** + * Returns true, iff the current field is already registered. + * + * @return boolean + */ + private function isRegistered() { + return UploadHandler::getInstance()->isRegisteredFieldId($this->getId()); + } + + /** + * @inheritDoc + * @return UploadFile[] + */ + public function getValue() { + return UploadHandler::getInstance()->getFilesForFieldId($this->getId()); + } + + /** + * @inheritDoc + */ + public function readValue() { + $this->registerField(); + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() { + parent::validate(); + + if (empty($this->getValue())) { + if ($this->isRequired()) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + } + + if ($this->getMinimum() !== null && count($this->getValue()) < $this->getMinimum()) { + $this->addValidationError(new FormFieldValidationError( + 'minimum', + 'wcf.form.field.upload.error.minimum', + ['minimum' => $this->getMinimum()] + )); + } + else if ($this->getMaximum() !== null && count($this->getValue()) > $this->getMaximum()) { + $this->addValidationError(new FormFieldValidationError( + 'maximum', + 'wcf.form.field.upload.error.maximum', + ['maximum' => $this->getMaximum()] + )); + } + } + + /** + * @inheritDoc + */ + public function getHtml() { + $this->registerField(); + + return parent::getHtml(); + } + + /** + * @inheritDoc + */ + public function value($value) { + // @TODO + throw new NotImplementedException(); + } + + /** + * @inheritDoc + */ + public function hasSaveValue() { + return false; + } + + /** + * @inheritDoc + * + * @throws \RuntimeException if the field has already been initalisated + */ + public function maximum($maximum = null) { + if ($this->isRegistered()) { + throw new \LogicException('The upload field has already been registered. Therefore no modifications are allowed.'); + } + + return $this->traitMaximum($maximum); + } + + /** + * Sets the imageOnly flag for this field. + * + * @param boolean $imageOnly maximum field value + * @return static this field + */ + public function imageOnly($imageOnly = true) { + $this->imageOnly = $imageOnly; + + return $this; + } + + /** + * Returns true, if the field is an image only field (only images can be uploaded). + * + * @return boolean + */ + public function isImageOnly() { + return $this->imageOnly; + } +}