From c4b57b566c6809f9f4cc3bc0161c0fb7e1371fd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Mon, 4 Mar 2019 12:41:55 +0100
Subject: [PATCH] :sparkles: feat(simpleplugin) Switch DateAttribute to html5
 input type=date

This replaces our custom datepicker with the standard html5 one for date
 fields in plugins.
This deprecates the defaultDate attribute which was used to set the
 starting point of the datepicker without setting a default value,
 because this is not possible in HTML5.
This does not add support for min and max dates which are supported by
 the HTML5 input type and may be added later.
This also changes the internal date format from DD.MM.YYYY to YYYY-MM-DD
 to match the HTML5 POST format, this should not have serious
 consequences.

issue #5931
---
 include/class_templateHandling.inc            |  2 +-
 .../attributes/class_DateAttribute.inc        | 42 ++++---------------
 2 files changed, 8 insertions(+), 36 deletions(-)

diff --git a/include/class_templateHandling.inc b/include/class_templateHandling.inc
index df0c55ebb..f8754d8b2 100644
--- a/include/class_templateHandling.inc
+++ b/include/class_templateHandling.inc
@@ -414,7 +414,7 @@ class templateHandling
       $args[] = 'now';
     }
     if (count($args) < 2) {
-      $args[] = 'd.m.Y';
+      $args[] = 'Y-m-d';
     }
     $dateObject = new DateTime($args[0], new DateTimeZone('UTC'));
     if ($args[1] == 'epoch') {
diff --git a/include/simpleplugin/attributes/class_DateAttribute.inc b/include/simpleplugin/attributes/class_DateAttribute.inc
index bb84e8075..d02562f23 100644
--- a/include/simpleplugin/attributes/class_DateAttribute.inc
+++ b/include/simpleplugin/attributes/class_DateAttribute.inc
@@ -1,7 +1,8 @@
 <?php
 /*
   This code is part of FusionDirectory (http://www.fusiondirectory.org/)
-  Copyright (C) 2012-2018  FusionDirectory
+
+  Copyright (C) 2012-2019  FusionDirectory
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -25,7 +26,6 @@
 class DateAttribute extends Attribute
 {
   protected $format;
-  protected $defaultDate;
 
   /*! \brief The constructor of DateAttribute
    *
@@ -35,22 +35,13 @@ class DateAttribute extends Attribute
    *  \param boolean $required Is this attribute mandatory or not
    *  \param string $format The date format. It can be any format recognized by DateTime::format. see http://www.php.net/manual/fr/function.date.php
    *  \param mixed $defaultValue The default value for this attribute
-   *  \param mixed $defaultDate The default date for the date picker when attribute is empty
+   *  \param mixed $defaultDate Deprecated
    *  \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
    */
   function __construct ($label, $description, $ldapName, $required, $format, $defaultValue = 'now', $defaultDate = NULL, $acl = '')
   {
     parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
     $this->format = $format;
-    if ($defaultDate !== NULL) {
-      try {
-        $date = new DateTime($defaultDate, new DateTimeZone('UTC'));
-        $defaultDate = $date->format('d.m.Y');
-      } catch (Exception $e) {
-        $defaultDate = NULL;
-      }
-    }
-    $this->defaultDate = $defaultDate;
   }
 
   function inputValue ($value)
@@ -71,7 +62,7 @@ class DateAttribute extends Attribute
       return $this->value;
     } else {
       try {
-        return $this->getDateValue()->format('d.m.Y');
+        return $this->getDateValue()->format('Y-m-d');
       } catch (Exception $e) {
         return $this->value;
       }
@@ -140,32 +131,13 @@ class DateAttribute extends Attribute
 
   function renderFormInput ()
   {
-    $smarty = get_smarty();
-    $smarty->assign('usePrototype', 'true');
-    $id = $this->getHtmlId();
     $display = $this->renderInputField(
-      'text', $id,
+      'date', $this->getHtmlId(),
       [
-        'value' => '{literal}'.$this->getValue().'{/literal}',
-        'class' => 'date'
+        'value'   => '{literal}'.$this->getValue().'{/literal}',
+        'pattern' => '{literal}[0-9]{4}-[0-9]{2}-[0-9]{2}{/literal}',
       ]
     );
-    $display  .= '{if $'.$this->getAcl().'ACL|regex_replace:"/[cdmr]/":"" == "w"}'.
-        '<script type="text/javascript">
-          {literal}
-          var datepicker  = new DatePicker('.
-            '{ '.
-              'relative : \''.$id.'\', '.
-              (($this->defaultDate !== NULL) ? 'defaultDate : \''.$this->defaultDate.'\', ' : '').
-              'language : \'{/literal}{$lang}{literal}\', '.
-              'keepFieldEmpty : true, '.
-              'enableCloseEffect : false, '.
-              'enableShowEffect : false '.
-            '}'.
-          ');
-          {/literal}
-        </script>
-        {/if}';
     return $this->renderAcl($display);
   }
 }
-- 
GitLab