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 if($this->config['prefilltoday'] && !$rawvalue) { 42 $rawvalue = date('Y-m-d H:i:s'); 43 } 44 45 $params = array( 46 'name' => $name, 47 'value' => $rawvalue, 48 'class' => 'struct_datetime', 49 'id' => $htmlID, 50 ); 51 $attributes = buildAttributes($params, true); 52 return "<input $attributes />"; 53 } 54 55 /** 56 * Validate a single value 57 * 58 * This function needs to throw a validation exception when validation fails. 59 * The exception message will be prefixed by the appropriate field on output 60 * 61 * @param string|array $rawvalue 62 * @return string 63 * @throws ValidationException 64 */ 65 public function validate($rawvalue) { 66 $rawvalue = trim($rawvalue); 67 list($date, $time) = explode(' ', $rawvalue, 2); 68 $date = trim($date); 69 $time = trim($time); 70 71 list($year, $month, $day) = explode('-', $date, 3); 72 if(!checkdate((int) $month, (int) $day, (int) $year)) { 73 throw new ValidationException('invalid datetime format'); 74 } 75 76 list($h, $m, $s) = explode(':', $time, 3); 77 $h = (int) $h; 78 $m = (int) $m; 79 $s = (int) $s; 80 if($h < 0 || $h > 23 || $m < 0 || $m > 59 || $s < 0 || $s > 59) { 81 throw new ValidationException('invalid datetime format'); 82 } 83 84 return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $h, $m, $s); 85 } 86 87 /** 88 * @param QueryBuilder $QB 89 * @param string $tablealias 90 * @param string $colname 91 * @param string $alias 92 */ 93 public function select(QueryBuilder $QB, $tablealias, $colname, $alias) { 94 $col = "$tablealias.$colname"; 95 96 // when accessing the revision column we need to convert from Unix timestamp 97 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 98 $rightalias = $QB->generateTableAlias(); 99 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 100 $col = "DATETIME($rightalias.lastrev, 'unixepoch', 'localtime')"; 101 } 102 103 $QB->addSelectStatement($col, $alias); 104 } 105 106 /** 107 * @param QueryBuilderWhere $add 108 * @param string $tablealias 109 * @param string $colname 110 * @param string $comp 111 * @param string|\string[] $value 112 * @param string $op 113 */ 114 public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op) { 115 $col = "$tablealias.$colname"; 116 117 // when accessing the revision column we need to convert from Unix timestamp 118 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 119 $QB = $add->getQB(); 120 $rightalias = $QB->generateTableAlias(); 121 $col = "DATETIME($rightalias.lastrev, 'unixepoch', 'localtime')"; 122 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 123 } 124 125 /** @var QueryBuilderWhere $add Where additionional queries are added to*/ 126 if(is_array($value)) { 127 $add = $add->where($op); // sub where group 128 $op = 'OR'; 129 } 130 foreach((array) $value as $item) { 131 $pl = $QB->addValue($item); 132 $add->where($op, "$col $comp $pl"); 133 } 134 } 135 136 /** 137 * When sorting `%lastupdated%`, then sort the data from the `titles` table instead the `data_` table. 138 * 139 * @param QueryBuilder $QB 140 * @param string $tablealias 141 * @param string $colname 142 * @param string $order 143 */ 144 public function sort(QueryBuilder $QB, $tablealias, $colname, $order) { 145 $col = "$tablealias.$colname"; 146 147 if(is_a($this->context,'dokuwiki\plugin\struct\meta\RevisionColumn')) { 148 $rightalias = $QB->generateTableAlias(); 149 $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid"); 150 $col = "$rightalias.lastrev"; 151 } 152 153 $QB->addOrderBy("$col $order"); 154 } 155 156} 157