1d2b31c9bSMichael Große<?php 2d6d97f60SAnna Dabrowska 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types; 4d2b31c9bSMichael Große 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ValidationException; 6d2b31c9bSMichael Große 7d6d97f60SAnna Dabrowskaclass Date extends AbstractBaseType 8d6d97f60SAnna Dabrowska{ 9*7fe2cdf2SAndreas Gohr protected $config = [ 10*7fe2cdf2SAndreas Gohr 'format' => 'Y/m/d', 11*7fe2cdf2SAndreas Gohr 'prefilltoday' => false, 12*7fe2cdf2SAndreas Gohr 'pastonly' => false, 13*7fe2cdf2SAndreas Gohr 'futureonly' => false 14*7fe2cdf2SAndreas Gohr ]; 15d2b31c9bSMichael Große 16d2b31c9bSMichael Große /** 17d2b31c9bSMichael Große * Output the stored data 18d2b31c9bSMichael Große * 19d2b31c9bSMichael Große * @param string|int $value the value stored in the database 20d2b31c9bSMichael Große * @param \Doku_Renderer $R the renderer currently used to render the data 21d2b31c9bSMichael Große * @param string $mode The mode the output is rendered in (eg. XHTML) 22d2b31c9bSMichael Große * @return bool true if $mode could be satisfied 23d2b31c9bSMichael Große */ 24d6d97f60SAnna Dabrowska public function renderValue($value, \Doku_Renderer $R, $mode) 25d6d97f60SAnna Dabrowska { 26d2b31c9bSMichael Große $date = date_create($value); 274d19af58SAndreas Gohr if ($date !== false) { 284d19af58SAndreas Gohr $out = date_format($date, $this->config['format']); 294d19af58SAndreas Gohr } else { 304d19af58SAndreas Gohr $out = ''; 314d19af58SAndreas Gohr } 324d19af58SAndreas Gohr 334d19af58SAndreas Gohr $R->cdata($out); 34d2b31c9bSMichael Große return true; 35d2b31c9bSMichael Große } 36d2b31c9bSMichael Große 37d2b31c9bSMichael Große /** 38d2b31c9bSMichael Große * Return the editor to edit a single value 39d2b31c9bSMichael Große * 40d2b31c9bSMichael Große * @param string $name the form name where this has to be stored 41c0230d2cSAndreas Gohr * @param string $rawvalue the current value 42ee983135SMichael Große * @param string $htmlID 43ee983135SMichael Große * 44d2b31c9bSMichael Große * @return string html 45d2b31c9bSMichael Große */ 46d6d97f60SAnna Dabrowska public function valueEditor($name, $rawvalue, $htmlID) 47d6d97f60SAnna Dabrowska { 48c0230d2cSAndreas Gohr if ($this->config['prefilltoday'] && !$rawvalue) { 49c0230d2cSAndreas Gohr $rawvalue = date('Y-m-d'); 509aad279dSAndreas Gohr } 519aad279dSAndreas Gohr 527234bfb1Ssplitbrain $params = [ 533e7a5b3cSMichael Große 'name' => $name, 543e7a5b3cSMichael Große 'value' => $rawvalue, 553e7a5b3cSMichael Große 'class' => 'struct_date', 56*7fe2cdf2SAndreas Gohr 'type' => 'date', // HTML5 date picker 573e7a5b3cSMichael Große 'id' => $htmlID, 587234bfb1Ssplitbrain ]; 593e7a5b3cSMichael Große $attributes = buildAttributes($params, true); 603e7a5b3cSMichael Große return "<input $attributes />"; 61d2b31c9bSMichael Große } 62d2b31c9bSMichael Große 6322dd50e7SMichael Große /** 6422dd50e7SMichael Große * Validate a single value 6522dd50e7SMichael Große * 6622dd50e7SMichael Große * This function needs to throw a validation exception when validation fails. 6722dd50e7SMichael Große * The exception message will be prefixed by the appropriate field on output 6822dd50e7SMichael Große * 6923169abeSAndreas Gohr * @param string|int $rawvalue 70806eec82SAndreas Gohr * @return int|string 7122dd50e7SMichael Große * @throws ValidationException 7222dd50e7SMichael Große */ 73d6d97f60SAnna Dabrowska public function validate($rawvalue) 74d6d97f60SAnna Dabrowska { 7523169abeSAndreas Gohr $rawvalue = parent::validate($rawvalue); 767234bfb1Ssplitbrain [$rawvalue] = explode(' ', $rawvalue, 2); // strip off time if there is any 77806eec82SAndreas Gohr 787234bfb1Ssplitbrain [$year, $month, $day] = explode('-', $rawvalue, 3); 794978faf1SAndreas Gohr if (!checkdate((int)$month, (int)$day, (int)$year)) { 80d2b31c9bSMichael Große throw new ValidationException('invalid date format'); 81d2b31c9bSMichael Große } 82733fd33bSAnna Dabrowska if ($this->config['pastonly'] && strtotime($rawvalue) > time()) { 83d8bfcd03SAnna Dabrowska throw new ValidationException('pastonly'); 84218490cfSAnna Dabrowska } 85733fd33bSAnna Dabrowska if ($this->config['futureonly'] && strtotime($rawvalue) < time()) { 86d8bfcd03SAnna Dabrowska throw new ValidationException('futureonly'); 87218490cfSAnna Dabrowska } 8864454728SAndreas Gohr return sprintf('%d-%02d-%02d', $year, $month, $day); 89d2b31c9bSMichael Große } 90d2b31c9bSMichael Große} 91