Add date form field
authorMatthias Schmidt <gravatronics@live.com>
Thu, 2 Aug 2018 15:48:39 +0000 (17:48 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 2 Aug 2018 15:48:39 +0000 (17:48 +0200)
See #2509

wcfsetup/install/files/acp/templates/__dateFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/DateFormField.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/acp/templates/__dateFormField.tpl b/wcfsetup/install/files/acp/templates/__dateFormField.tpl
new file mode 100644 (file)
index 0000000..5d19a58
--- /dev/null
@@ -0,0 +1,14 @@
+{include file='__formFieldHeader'}
+
+<input {*
+       *}type="{if $field->supportsTime()}datetime{else}date{/if}"{*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{$field->getValue()}" {*
+       *}class="medium"{*
+       *}{if $field->isAutofocused()} autofocus{/if}{*
+       *}{if $field->isRequired()} required{/if}{*
+       *}{if $field->isImmutable()} disabled{/if}{*
+*}>
+
+{include file='__formFieldFooter'}
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/DateFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/DateFormField.class.php
new file mode 100644 (file)
index 0000000..858230e
--- /dev/null
@@ -0,0 +1,157 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+
+/**
+ * Implementation of a form field for to select a FontAwesome icon.
+ * 
+ * @author     Matthias Schmidt
+ * @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      3.2
+ */
+class DateFormField extends AbstractFormField {
+       /**
+        * date time format of the save value
+        * @var string
+        */
+       protected $__saveValueFormat = null;
+       
+       /**
+        * is `true` if not only the date, but also the time can be set
+        * @var bool
+        */
+       protected $__supportsTime = false;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__dateFormField';
+       
+       /**
+        * Returns the type of the returned save value.
+        * 
+        * If no save value format has been set, `U` (unix timestamp) will be set and returned.
+        * 
+        * @return      string
+        */
+       public function getSaveValueFormat() {
+               if ($this->__saveValueFormat === null) {
+                       $this->__saveValueFormat = 'U';
+               }
+               
+               return $this->__saveValueFormat;
+       }
+       
+       /**
+        * Returns a date time object for the current value or `null` if no date time
+        * object could be created.
+        * 
+        * @return      \DateTime|null
+        */
+       protected function getValueDateTimeObject() {
+               if ($this->supportsTime()) {
+                       $dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:sP', $this->getValue());
+               }
+               else {
+                       $dateTime = \DateTime::createFromFormat('Y-m-d', $this->getValue());
+               }
+               
+               if ($dateTime === false) {
+                       return null;
+               }
+               
+               return $dateTime;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getSaveValue() {
+               if ($this->getValue() === null) {
+                       return null;
+               }
+               
+               return $this->getValueDateTimeObject()->format($this->getSaveValueFormat());
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function readValue() {
+               if ($this->getDocument()->hasRequestData($this->getPrefixedId()) && is_string($this->getDocument()->getRequestData($this->getPrefixedId()))) {
+                       $this->__value = $this->getDocument()->getRequestData($this->getPrefixedId());
+                       
+                       if ($this->__value === '') {
+                               $this->__value = null;
+                       }
+               }
+               
+               return $this;
+       }
+       
+       /**
+        * Sets the date time format of the save value.
+        * 
+        * @param       string          $saveValueFormat
+        * @return      static
+        */
+       public function saveValueFormat($saveValueFormat) {
+               if ($this->__saveValueFormat !== null) {
+                       throw new \BadMethodCallException("Save value type has already been set.");
+               }
+               
+               try {
+                       \DateTime::createFromFormat($saveValueFormat, TIME_NOW);
+               }
+               catch (\Exception $e) {
+                       throw new \InvalidArgumentException("Invalid date time format '{$saveValueFormat}'.");
+               }
+               
+               $this->__saveValueFormat = $saveValueFormat;
+               
+               return $this;
+       }
+       
+       /**
+        * Sets if not only the date, but also the time can be set.
+        *
+        * @param       bool            $supportsTime
+        * @return      static          thsi field
+        */
+       public function supportTime($supportsTime = true) {
+               $this->__supportsTime = $supportsTime;
+               
+               return $this;
+       }
+       
+       /**
+        * Returns `true` if not only the date, but also the time can be set, and
+        * returns `false` otherwise.
+        * 
+        * @return      bool
+        */
+       public function supportsTime() {
+               return $this->__supportsTime;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               if ($this->getValue() === null) {
+                       if ($this->isRequired()) {
+                               $this->addValidationError(new FormFieldValidationError('empty'));
+                       }
+               }
+               else {
+                       if ($this->getValueDateTimeObject() === null) {
+                               $this->addValidationError(new FormFieldValidationError(
+                                       'format',
+                                       'wcf.form.field.date.error.format'
+                               ));
+                       }
+               }
+       }
+}