Passed
Push — 4.0 ( cfe93b...546c6c )
by Damian
06:06
created

DateField_Disabled::Field()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\ORM\FieldType\DBDate;
7
8
/**
9
 * Disabled version of {@link DateField}.
10
 * Allows dates to be represented in a form, by showing in a user friendly format, eg, dd/mm/yyyy.
11
 */
12
class DateField_Disabled extends DateField
13
{
14
15
    protected $disabled = true;
16
17
    public function Field($properties = [])
18
    {
19
        // Default display value
20
        $displayValue = '<i>(' . _t(DateField::class . '.NOTSET', 'not set') . ')</i>';
21
22
        $value = $this->dataValue();
23
24
        if ($value) {
25
            $value = $this->tidyInternal($value);
26
            $df = new DBDate($this->name);
27
            $df->setValue($value);
28
29
            if ($df->IsToday()) {
30
                // e.g. 2018-06-01 (today)
31
                $format = '%s (%s)';
32
                $infoComplement = _t(DateField::class . '.TODAY', 'today');
33
            } else {
34
                // e.g. 2018-06-01, 5 days ago
35
                $format = '%s, %s';
36
                $infoComplement = $df->Ago();
37
            }
38
39
            // Render the display value with some complement of info
40
            $displayValue = Convert::raw2xml(sprintf(
41
                $format,
42
                $this->Value(),
43
                $infoComplement
44
            ));
45
        }
46
47
        return sprintf(
48
            "<span class=\"readonly\" id=\"%s\">%s</span>",
49
            $this->ID(),
50
            $displayValue
51
        );
52
    }
53
54
    public function Type()
55
    {
56
        return "date_disabled readonly " . parent::Type();
57
    }
58
59
    public function getHTML5()
60
    {
61
        // Always disable HTML5 feature when using the readonly field.
62
        return false;
63
    }
64
}
65