1<?php 2namespace dokuwiki\plugin\struct\types; 3 4use dokuwiki\plugin\struct\meta\ValidationException; 5 6class DateTime extends Date { 7 8 protected $config = array( 9 'format' => "Y/m/d\xC2\xA0H:i:s", //non-breaking space 10 'prefilltoday' => false 11 ); 12 13 /** 14 * Return the editor to edit a single value 15 * 16 * @param string $name the form name where this has to be stored 17 * @param string $value the current value 18 * @return string html 19 */ 20 public function valueEditor($name, $value) { 21 if($this->config['prefilltoday'] && !$value) { 22 $value = date('Y-m-d H:i:s'); 23 } 24 return parent::valueEditor($name, $value); 25 } 26 27 /** 28 * Validate a single value 29 * 30 * This function needs to throw a validation exception when validation fails. 31 * The exception message will be prefixed by the appropriate field on output 32 * 33 * @param string|array $value 34 * @return string 35 * @throws ValidationException 36 */ 37 public function validate($value) { 38 $value = trim($value); 39 list($date, $time) = explode(' ', $value, 2); 40 $date = trim($date); 41 $time = trim($time); 42 43 list($year, $month, $day) = explode('-', $date, 3); 44 if(!checkdate((int) $month, (int) $day, (int) $year)) { 45 throw new ValidationException('invalid datetime format'); 46 } 47 48 list($h, $m, $s) = explode(':', $time, 3); 49 $h = (int) $h; 50 $m = (int) $m; 51 $s = (int) $s; 52 if($h < 0 || $h > 23 || $m < 0 || $m > 59 || $s < 0 || $s > 59) { 53 throw new ValidationException('invalid datetime format'); 54 } 55 56 return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $h, $m, $s); 57 } 58 59} 60