1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Brend Wanders <b.wanders@utwente.nl>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die('Meh.');
8
9/**
10 * The date type.
11 */
12class plugin_strata_type_date extends plugin_strata_type {
13    function render($mode, &$R, &$triples, $value, $hint) {
14        if(is_numeric($value)) {
15            // use the hint if available
16            $format = $hint ?: 'Y-m-d';
17
18            // construct representation
19            $date = new DateTime();
20            $date->setTimestamp((int)$value);
21
22            // render
23            $R->cdata($date->format($format));
24        } else {
25            $R->cdata($value);
26        }
27        return true;
28    }
29
30    function normalize($value, $hint) {
31        // use hint if available
32        // (prefix with '!' te reset all fields to the unix epoch)
33        $format = '!'. ($hint ?: 'Y-m-d');
34
35        // try and parse the value
36        $date = date_create_from_format($format, $value);
37
38        // handle failure in a non-intrusive way
39        if($date === false) {
40            return $value;
41        } else {
42            return $date->getTimestamp();
43        }
44    }
45
46    function getInfo() {
47        return array(
48            'desc'=>'Stores and displays dates in the YYYY-MM-DD format. The optional hint can give a different format to use.',
49            'tags'=>array('numeric'),
50            'hint'=>'different date format'
51        );
52    }
53}
54