1<?php 2namespace dokuwiki\plugin\struct\types; 3 4use dokuwiki\plugin\struct\meta\DateFormatConverter; 5use dokuwiki\plugin\struct\meta\QueryBuilder; 6use dokuwiki\plugin\struct\meta\QueryBuilderWhere; 7use dokuwiki\plugin\struct\meta\ValidationException; 8 9class DateTime extends Date { 10 11 protected $config = array( 12 'format' => '', // filled by constructor 13 'prefilltoday' => false 14 ); 15 16 /** 17 * DateTime constructor. 18 * 19 * @param array|null $config 20 * @param string $label 21 * @param bool $ismulti 22 * @param int $tid 23 */ 24 public function __construct($config = null, $label = '', $ismulti = false, $tid = 0) { 25 global $conf; 26 $this->config['format'] = DateFormatConverter::toDate($conf['dformat']); 27 28 parent::__construct($config, $label, $ismulti, $tid); 29 } 30 31 /** 32 * Return the editor to edit a single value 33 * 34 * @param string $name the form name where this has to be stored 35 * @param string $rawvalue the current value 36 * @param string $htmlID 37 * 38 * @return string html 39 */ 40 public function valueEditor($name, $rawvalue, $htmlID) { 41 $name = hsc($name); 42 $rawvalue = hsc($rawvalue); 43 44 if($this->config['prefilltoday'] && !$rawvalue) { 45 $rawvalue = date('Y-m-d H:i:s'); 46 } 47 48 $id = !empty($htmlID) ? "id=\"$htmlID\"" : ''; 49 50 $html = "<input class=\"struct_datetime\" name=\"$name\" value=\"$rawvalue\" $id />"; 51 return "$html"; 52 } 53 54 /** 55 * Validate a single value 56 * 57 * This function needs to throw a validation exception when validation fails. 58 * The exception message will be prefixed by the appropriate field on output 59 * 60 * @param string|array $rawvalue 61 * @return string 62 * @throws ValidationException 63 */ 64 public function validate($rawvalue) { 65 $rawvalue = trim($rawvalue); 66 list($date, $time) = explode(' ', $rawvalue, 2); 67 $date = trim($date); 68 $time = trim($time); 69 70 list($year, $month, $day) = explode('-', $date, 3); 71 if(!checkdate((int) $month, (int) $day, (int) $year)) { 72 throw new ValidationException('invalid datetime format'); 73 } 74 75 list($h, $m, $s) = explode(':', $time, 3); 76 $h = (int) $h; 77 $m = (int) $m; 78 $s = (int) $s; 79 if($h < 0 || $h > 23 || $m < 0 || $m > 59 || $s < 0 || $s > 59) { 80 throw new ValidationException('invalid datetime format'); 81 } 82 83 return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $h, $m, $s); 84 } 85 86 /** 87 * @param QueryBuilder $QB 88 * @param string $tablealias 89 * @param string $colname 90 * @param string $alias 91 */ 92 public function select(QueryBuilder $QB, $tablealias, $colname, $alias) { 93 $col = "$tablealias.$colname"; 94 95 // when accessing the revision column we need to convert from Unix timestamp 96 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 97 $rightalias = $QB->generateTableAlias(); 98 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 99 $col = "DATETIME($rightalias.lastrev, 'unixepoch', 'localtime')"; 100 } 101 102 $QB->addSelectStatement($col, $alias); 103 } 104 105 /** 106 * @param QueryBuilderWhere $add 107 * @param string $tablealias 108 * @param string $colname 109 * @param string $comp 110 * @param string|\string[] $value 111 * @param string $op 112 */ 113 public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op) { 114 $col = "$tablealias.$colname"; 115 116 // when accessing the revision column we need to convert from Unix timestamp 117 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 118 $QB = $add->getQB(); 119 $rightalias = $QB->generateTableAlias(); 120 $col = "DATETIME($rightalias.lastrev, 'unixepoch', 'localtime')"; 121 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 122 } 123 124 /** @var QueryBuilderWhere $add Where additionional queries are added to*/ 125 if(is_array($value)) { 126 $add = $add->where($op); // sub where group 127 $op = 'OR'; 128 } 129 foreach((array) $value as $item) { 130 $pl = $QB->addValue($item); 131 $add->where($op, "$col $comp $pl"); 132 } 133 } 134 135 /** 136 * When sorting `%lastupdated%`, then sort the data from the `titles` table instead the `data_` table. 137 * 138 * @param QueryBuilder $QB 139 * @param string $tablealias 140 * @param string $colname 141 * @param string $order 142 */ 143 public function sort(QueryBuilder $QB, $tablealias, $colname, $order) { 144 $col = "$tablealias.$colname"; 145 146 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 147 $rightalias = $QB->generateTableAlias(); 148 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 149 $col = "$rightalias.lastrev"; 150 } 151 152 $QB->addOrderBy("$col $order"); 153 } 154 155} 156