]> git.andy128k.dev Git - ipf.git/commitdiff
rework datetime widget. use custom formats.
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Aug 2013 11:17:40 +0000 (14:17 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Aug 2013 11:17:40 +0000 (14:17 +0300)
ipf/admin/static/admin/js/admin.js
ipf/form/widget/datetimeinput.php

index 2c070c4cebc95e7a72ca28a806cef20754a0e2b1..cf0225aeacbc05039b9dc45b3c92ba2067c1521a 100644 (file)
@@ -6,7 +6,13 @@ $(function(){
         $(this).datepicker(opts);
     });
 
-    $('.datetimeinput').datetimepicker({dateFormat: 'yy-mm-dd'});
+    $('.datetimeinput').each(function(){
+        var opts = {
+            dateFormat: $(this).data('dateformat') || 'yy-mm-dd',
+            timeFormat: $(this).data('timeformat') || 'HH-mm-ss'
+        };
+        $(this).datetimepicker(opts);
+    });
 
     $('.checkgroup').closest('ul').each(function(){
         var master = $(this),
index 3cb7928bc255ca48fe554734ccef3d0f80cfe9b8..73b8509aaadf5614f1a4de182eaa7808b8a62858 100644 (file)
@@ -3,16 +3,55 @@
 class IPF_Form_Widget_DatetimeInput extends IPF_Form_Widget_Input
 {
     public $input_type = 'text';
-    public $format = 'Y-m-d H:i'; 
+    public $format = IPF_Format::DATETIME_DEFAULT;
+
+    public function __construct($attrs=array())
+    {
+        $format = ArrayTools::pop($attrs, 'format');
+        if ($format)
+            $this->format = is_string($format) ? IPF_Format::datetimeFlagsFromString($format) : $format;
+
+        parent::__construct($attrs);
+    }
 
     public function render($name, $value, $extra_attrs=array())
     {
-        // Internally we use GMT, so we convert back to the current
-        // timezone.
-        if (strlen($value) > 0) {
-            $value = date($this->format, strtotime($value.' GMT'));
-        }
         $extra_attrs['class'] = 'datetimeinput';
+        $extra_attrs['data-dateformat'] = IPF_Format::makeDateFormat($this->format, IPF_Format::$pickerControls);
+        $extra_attrs['data-timeformat'] = IPF_Format::makeTimeFormat($this->format, IPF_Format::$pickerControls);
         return parent::render($name, $value, $extra_attrs);
     }
+
+    public function valueFromFormData($name, &$data)
+    {
+        if (!isset($data[$name]) || !$data[$name]) {
+            return null;
+        }
+
+        $date = IPF_Format::parseDate($this->format, $data[$name]);
+        if ($date !== false) {
+            $day    = str_pad($date['tm_mday'], 2, '0', STR_PAD_LEFT);
+            $month  = str_pad($date['tm_mon']+1, 2, '0', STR_PAD_LEFT);
+            $year   = str_pad($date['tm_year']+1900, 4, '0', STR_PAD_LEFT);
+            $h      = str_pad($date['tm_hour'], 2, '0', STR_PAD_LEFT);
+            $m      = str_pad($date['tm_min'], 2, '0', STR_PAD_LEFT);
+            $s      = str_pad(min($date['tm_sec'], 59), 2, '0', STR_PAD_LEFT);
+            $out = $year.'-'.$month.'-'.$day.' '.$h.':'.$m.':'.$s;
+            // Internally we use GMT, so we convert back to the current timezone.
+            return gmdate('Y-m-d H:i:s', strtotime($out.' GMT'));
+        } else {
+            return null;
+        }
+    }
+
+    public function valueToFormData($name, $data)
+    {
+        if (isset($data[$name]) && $data[$name]) {
+            // Internally we use GMT, so we convert back to the current timezone.
+            return IPF_Format::formatDate($this->format, strtotime($data[$name].' GMT'));
+        } else {
+            return null;
+        }
+    }
 }
+