Add basic UploadFormField
authorJoshua Rüsweg <josh@bastelstu.be>
Tue, 15 Jan 2019 21:16:32 +0000 (22:16 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Tue, 15 Jan 2019 21:24:12 +0000 (22:24 +0100)
See #2825

com.woltlab.wcf/templates/__uploadFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/file/upload/UploadHandler.class.php
wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/__uploadFormField.tpl b/com.woltlab.wcf/templates/__uploadFormField.tpl
new file mode 100644 (file)
index 0000000..83bcc45
--- /dev/null
@@ -0,0 +1,5 @@
+{include file='__formFieldHeader'}
+
+{@$__wcf->getUploadHandler()->renderField($field->getPrefixedId())}
+
+{include file='__formFieldFooter'}
index 8352ad1abafc605df1157873aee4c5810368f2c5..3b169813bee6fa2f1802ddc5b5b311e9f9c96235 100644 (file)
@@ -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 (file)
index 0000000..30911da
--- /dev/null
@@ -0,0 +1,168 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\system\exception\NotImplementedException;
+use wcf\system\file\upload\UploadField;
+use wcf\system\file\upload\UploadFile;
+use wcf\system\file\upload\UploadHandler;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+
+/**
+ * Implementation of a form field for to uploads.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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;
+       }
+}