aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/DataFieldDateEntry.php
blob: 4b8c04951895037250b3c877726fd11399183391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
# Lifter002: DONE
# Lifter007: TEST

/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author  Marcus Lunzenauer <mlunzena@uos.de>
 * @author  Martin Gieseking  <mgieseki@uos.de>
 * @license GPL2 or any later version
 */
class DataFieldDateEntry extends DataFieldEntry
{
    protected $template = 'date.php';

    /**
     * Sets the value from a post request
     *
     * @param mixed $submitted_value The value from request
     */
    public function setValueFromSubmit($submitted_value)
    {
        if ($submitted_value) {
            $submitted_value = trim($submitted_value);
            $items = explode('.', $submitted_value);
            $submitted_value = array_reverse($items);
            $submitted_value = array_filter($submitted_value);
            $submitted_value = implode('-', $submitted_value);
        }
        parent::setValueFromSubmit($submitted_value);
    }

    /**
     * Returns the display/rendered value of this datafield
     *
     * @param bool $entities Should html entities be encoded (defaults to true)
     * @return String containg the rendered value
     */
    public function getDisplayValue($entities = true)
    {
        if ($this->isValid()) {
            $value = trim($this->value);
            $value = explode('-', $value);
            $value = array_reverse($value);
            $value = implode('.', $value);
            return $value;
        }

        return '';
    }

    /**
     * Returns the according input elements as html for this datafield
     *
     * @param String $name      Name prefix of the associated input
     * @param Array  $variables Additional variables
     * @return String containing the required html
     */
    public function getHTML($name = '', $variables = [])
    {
        return parent::getHTML($name, $variables + [
            'timestamp' => strtotime(trim($this->value)),
        ]);
    }

    /**
     * Returns whether the datafield contents are valid
     *
     * @return boolean indicating whether the datafield contents are valid
     */
    public function isValid()
    {
        $value = trim($this->value);

        if (!$value) {
            return parent::isValid();
        }

        return parent::isValid()
            && strtotime($value) !== false;
    }
}