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;
+ }
+ }
}
+