From 2cbf069c50a3d26a8f2c5cf1c65b7c981b29d897 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 11 Aug 2013 14:17:40 +0300 Subject: [PATCH] rework datetime widget. use custom formats. --- ipf/admin/static/admin/js/admin.js | 8 ++++- ipf/form/widget/datetimeinput.php | 51 ++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/ipf/admin/static/admin/js/admin.js b/ipf/admin/static/admin/js/admin.js index 2c070c4..cf0225a 100644 --- a/ipf/admin/static/admin/js/admin.js +++ b/ipf/admin/static/admin/js/admin.js @@ -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), diff --git a/ipf/form/widget/datetimeinput.php b/ipf/form/widget/datetimeinput.php index 3cb7928..73b8509 100644 --- a/ipf/form/widget/datetimeinput.php +++ b/ipf/form/widget/datetimeinput.php @@ -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; + } + } } + -- 2.49.0