xref: /plugin/struct/types/Date.php (revision 308cc83fd5391df29d21d2bc1306c8da49fdb335)
1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6
7class Date extends AbstractBaseType
8{
9
10    protected $config = array(
11        'format' => 'Y/m/d',
12        'prefilltoday' => false,
13        'pastonly' => false,
14        'futureonly' => false
15    );
16
17    /**
18     * Output the stored data
19     *
20     * @param string|int $value the value stored in the database
21     * @param \Doku_Renderer $R the renderer currently used to render the data
22     * @param string $mode The mode the output is rendered in (eg. XHTML)
23     * @return bool true if $mode could be satisfied
24     */
25    public function renderValue($value, \Doku_Renderer $R, $mode)
26    {
27        $date = date_create($value);
28        if ($date !== false) {
29            $out = date_format($date, $this->config['format']);
30        } else {
31            $out = '';
32        }
33
34        $R->cdata($out);
35        return true;
36    }
37
38    /**
39     * Return the editor to edit a single value
40     *
41     * @param string $name     the form name where this has to be stored
42     * @param string $rawvalue the current value
43     * @param string $htmlID
44     *
45     * @return string html
46     */
47    public function valueEditor($name, $rawvalue, $htmlID)
48    {
49        if ($this->config['prefilltoday'] && !$rawvalue) {
50            $rawvalue = date('Y-m-d');
51        }
52
53        $params = array(
54            'name' => $name,
55            'value' => $rawvalue,
56            'class' => 'struct_date',
57            'type' => 'date',  // HTML5 date picker
58            'id' => $htmlID,
59        );
60        $attributes = buildAttributes($params, true);
61        return "<input $attributes />";
62    }
63
64    /**
65     * Validate a single value
66     *
67     * This function needs to throw a validation exception when validation fails.
68     * The exception message will be prefixed by the appropriate field on output
69     *
70     * @param string|int $rawvalue
71     * @return int|string
72     * @throws ValidationException
73     */
74    public function validate($rawvalue)
75    {
76        $rawvalue = parent::validate($rawvalue);
77        list($rawvalue) = explode(' ', $rawvalue, 2); // strip off time if there is any
78
79        list($year, $month, $day) = explode('-', $rawvalue, 3);
80        if (!checkdate((int) $month, (int) $day, (int) $year)) {
81            throw new ValidationException('invalid date format');
82        }
83        if ($this->config['pastonly'] && strtotime($rawvalue) > time()) {
84            throw new ValidationException('pastonly');
85        }
86        if ($this->config['futureonly'] && strtotime($rawvalue) < time()) {
87            throw new ValidationException('futureonly');
88        }
89        return sprintf('%d-%02d-%02d', $year, $month, $day);
90    }
91}
92