xref: /plugin/struct/types/Date.php (revision d2b31c9b2b42ab6635b261d1f4bcd72551c4ffbf)
1<?php
2namespace plugin\struct\types;
3
4use plugin\struct\meta\ValidationException;
5
6class Date extends AbstractBaseType {
7
8    protected $config = array(
9        'prefix' => '',
10        'postfix' => '',
11    );
12
13    /**
14     * Output the stored data
15     *
16     * @param string|int $value the value stored in the database
17     * @param \Doku_Renderer $R the renderer currently used to render the data
18     * @param string $mode The mode the output is rendered in (eg. XHTML)
19     * @return bool true if $mode could be satisfied
20     */
21    public function renderValue($value, \Doku_Renderer $R, $mode) {
22        $config_helper = plugin_load('helper','struct_config');
23        $format = $config_helper->getConf('dformat');
24        $date = date_create($value);
25        $R->cdata($this->config['prefix'] . date_format($date, $format) . $this->config['postfix']);
26        return true;
27    }
28
29    /**
30     * Return the editor to edit a single value
31     *
32     * @param string $name the form name where this has to be stored
33     * @param string $value the current value
34     * @return string html
35     */
36    public function valueEditor($name, $value) {
37        $name = hsc($name);
38        $value = hsc($value);
39        $html = "<input class=\"struct_date\" name=\"$name\" value=\"$value\" />";
40        return "$html";
41    }
42
43    public function validate($value) {
44        list($year, $month, $day) = explode('-',$value);
45        if (!checkdate($month, $day, $year)) {
46            throw new ValidationException('invalid date format');
47        }
48    }
49
50}
51