1<?php 2namespace plugin\struct\types; 3 4use plugin\struct\meta\ValidationException; 5 6class Date extends AbstractBaseType { 7 8 protected $config = array( 9 'format' => 'Y/m/d' 10 ); 11 12 /** 13 * Output the stored data 14 * 15 * @param string|int $value the value stored in the database 16 * @param \Doku_Renderer $R the renderer currently used to render the data 17 * @param string $mode The mode the output is rendered in (eg. XHTML) 18 * @return bool true if $mode could be satisfied 19 */ 20 public function renderValue($value, \Doku_Renderer $R, $mode) { 21 $date = date_create($value); 22 if($date !== false) { 23 $out = date_format($date, $this->config['format']); 24 } else { 25 $out = ''; 26 } 27 28 $R->cdata($out); 29 return true; 30 } 31 32 /** 33 * Return the editor to edit a single value 34 * 35 * @param string $name the form name where this has to be stored 36 * @param string $value the current value 37 * @return string html 38 */ 39 public function valueEditor($name, $value) { 40 $name = hsc($name); 41 $value = hsc($value); 42 $html = "<input class=\"struct_date\" name=\"$name\" value=\"$value\" />"; 43 return "$html"; 44 } 45 46 /** 47 * Validate a single value 48 * 49 * This function needs to throw a validation exception when validation fails. 50 * The exception message will be prefixed by the appropriate field on output 51 * 52 * @param string|int $value 53 * @throws ValidationException 54 */ 55 public function validate($value) { 56 list($year, $month, $day) = explode('-',$value, 3); 57 if (!checkdate($month, $day, $year)) { 58 throw new ValidationException('invalid date format'); 59 } 60 } 61 62} 63