Passed
Pull Request — master (#11)
by Matthew
14:35
created

ElementCountDown::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\CountDown\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\View\ArrayData;
7
8
/**
9
 * Class ElementCountDown
10
 * @package Dynamic\Elements\Elements
11
 *
12
 * @property string $EndDate
13
 * @property string $EndTime
14
 * @property boolean $ShowMonths
15
 * @property boolean $ShowSeconds
16
 * @property boolean $Elapse
17
 */
18
class ElementCountDown extends BaseElement
19
{
20
    /**
21
     * @var string
22
     */
23
    private static $icon = 'sectionnav-icon';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
24
25
    /**
26
     * @var string
27
     */
28
    private static $singular_name = 'Count Down Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    private static $plural_name = 'Count Down Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var array
37
     */
38
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
39
        'EndDate' => 'Date',
40
        'EndTime' => 'Time',
41
        'ShowMonths' => 'Boolean',
42
        'ShowSeconds' => 'Boolean',
43
        'Elapse' => 'Boolean',
44
    ];
45
46
    /**
47
     * @var string
48
     */
49
    private static $table_name = 'ElementCountDown';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var ArrayData
53
     */
54
    private $client_config;
55
56
    /**
57 1
     * @return string
58
     */
59 1
    public function getType()
60
    {
61
        return _t(__CLASS__ . '.BlockType', 'Count Down');
62
    }
63
64
    /**
65 1
     * @return \SilverStripe\Forms\FieldList
66
     */
67 1
    public function getCMSFields()
68
    {
69 1
        $fields = parent::getCMSFields();
70 1
71
        // so seconds shows in field
72
        $fields->dataFieldByName('EndTime')
73 1
            ->setAttribute('step', 1);
74
75
        $fields->dataFieldByName('Elapse')
76
            ->setDescription('Count up after reaching the end date and time');
77
78
        return $fields;
79 2
    }
80
81
    /**
82 2
     * @return \SilverStripe\ORM\ValidationResult
83 2
     */
84
    public function validate()
85
    {
86 2
        $result = parent::validate();
87
88 2
        if (!$this->EndDate) {
89
            $result->addError('An end date is required');
90
        }
91
92
        if (!$this->EndTime) {
93
            $result->addError('An end time is required');
94 2
        }
95
96 2
        return $result;
97 2
    }
98
99
    /**
100 2
     * @return string
101
     */
102
    public function End()
103
    {
104
        return "{$this->EndDate} {$this->EndTime}";
105
    }
106
107 2
    /**
108
     * @return $this
109 2
     */
110 2
    public function setClientConfig()
111
    {
112
        $clientArray = [
113 2
            'End' => $this->End(),
114
            'Elapse' => $this->Elapse,
115
        ];
116
117
        $this->client_config = ArrayData::create($this->encodeArrayValues($clientArray));
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return ArrayData
124
     */
125
    public function getClientConfig()
126
    {
127
        if (!$this->client_config) {
128
            $this->setClientConfig();
129
        }
130
131
        return $this->client_config;
132
    }
133
134
    /**
135
     * @param $array
136
     * @return mixed
137
     */
138
    protected function encodeArrayValues($array)
139
    {
140
        foreach ($array as $key => $val) {
141
            $array[$key] = json_encode($val);
142
        }
143
144
        return $array;
145
    }
146
}
147