xref: /plugin/struct/types/DateTime.php (revision e2c90eebd879dde19ad396148dba094c80ce7cab)
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     * @return string html
37     */
38    public function valueEditor($name, $rawvalue) {
39        if($this->config['prefilltoday'] && !$rawvalue) {
40            $rawvalue = date('Y-m-d H:i:s');
41        }
42        return parent::valueEditor($name, $rawvalue);
43    }
44
45    /**
46     * Validate a single value
47     *
48     * This function needs to throw a validation exception when validation fails.
49     * The exception message will be prefixed by the appropriate field on output
50     *
51     * @param string|array $rawvalue
52     * @return string
53     * @throws ValidationException
54     */
55    public function validate($rawvalue) {
56        $rawvalue = trim($rawvalue);
57        list($date, $time) = explode(' ', $rawvalue, 2);
58        $date = trim($date);
59        $time = trim($time);
60
61        list($year, $month, $day) = explode('-', $date, 3);
62        if(!checkdate((int) $month, (int) $day, (int) $year)) {
63            throw new ValidationException('invalid datetime format');
64        }
65
66        list($h, $m, $s) = explode(':', $time, 3);
67        $h = (int) $h;
68        $m = (int) $m;
69        $s = (int) $s;
70        if($h < 0 || $h > 23 || $m < 0 || $m > 59 || $s < 0 || $s > 59) {
71            throw new ValidationException('invalid datetime format');
72        }
73
74        return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $h, $m, $s);
75    }
76
77    /**
78     * @param QueryBuilder $QB
79     * @param string $tablealias
80     * @param string $colname
81     * @param string $alias
82     */
83    public function select(QueryBuilder $QB, $tablealias, $colname, $alias) {
84        // when accessing the revision column we need to convert from Unix timestamp
85        $col = "$tablealias.$colname";
86        if($colname == 'rev') {
87            $col = "DATETIME($col, 'unixepoch', 'localtime')";
88        }
89
90        $QB->addSelectStatement($col, $alias);
91    }
92
93    /**
94     * @param QueryBuilder $QB
95     * @param string $tablealias
96     * @param string $colname
97     * @param string $comp
98     * @param string|\string[] $value
99     * @param string $op
100     */
101    public function filter(QueryBuilder $QB, $tablealias, $colname, $comp, $value, $op) {
102        // when accessing the revision column we need to convert from Unix timestamp
103        $col = "$tablealias.$colname";
104        if($colname == 'rev') {
105            $col = "DATETIME($col, 'unixepoch', 'localtime')";
106        }
107
108        /** @var QueryBuilderWhere $add Where additionional queries are added to*/
109        if(is_array($value)) {
110            $add = $QB->filters()->where($op); // sub where group
111            $op = 'OR';
112        } else {
113            $add = $QB->filters(); // main where clause
114        }
115        foreach((array) $value as $item) {
116            $pl = $QB->addValue($item);
117            $add->where($op, "$col $comp $pl");
118        }
119    }
120
121}
122