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