Completed
Pull Request — master (#11)
by Matthew
05:04
created

ElementCountDown::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 1
rs 10
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
     * @return string
58
     */
59 1
    public function getType()
60
    {
61 1
        return _t(__CLASS__ . '.BlockType', 'Count Down');
62
    }
63
64
    /**
65
     * @return \SilverStripe\Forms\FieldList
66
     */
67 1
    public function getCMSFields()
68
    {
69 1
        $fields = parent::getCMSFields();
70
71
        // so seconds shows in field
72 1
        $fields->dataFieldByName('EndTime')
73 1
            ->setAttribute('step', 1);
74
75 1
        $fields->dataFieldByName('Elapse')
76 1
            ->setDescription('Count up after reaching the end date and time');
77
78 1
        return $fields;
79
    }
80
81
    /**
82
     * @return \SilverStripe\ORM\ValidationResult
83
     */
84 1
    public function validate()
85
    {
86 1
        $result = parent::validate();
87
88 1
        if (!$this->EndDate) {
89 1
            $result->addError('An end date is required');
90
        }
91
92 1
        if (!$this->EndTime) {
93 1
            $result->addError('An end time is required');
94
        }
95
96 1
        return $result;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 3
    public function End()
103
    {
104 3
        return "{$this->EndDate} {$this->EndTime}";
105
    }
106
107
    /**
108
     * @return $this
109
     */
110 2
    public function setClientConfig()
111
    {
112
        $clientArray = [
113 2
            'End' => $this->End(),
114 2
            'Elapse' => $this->Elapse,
115
        ];
116
117 2
        $this->client_config = ArrayData::create($this->encodeArrayValues($clientArray));
118
119 2
        return $this;
120
    }
121
122
    /**
123
     * @return ArrayData
124
     */
125 2
    public function getClientConfig()
126
    {
127 2
        if (!$this->client_config) {
128 2
            $this->setClientConfig();
129
        }
130
131 2
        return $this->client_config;
132
    }
133
134
    /**
135
     * @param $array
136
     * @return mixed
137
     */
138 2
    protected function encodeArrayValues($array)
139
    {
140 2
        foreach ($array as $key => $val) {
141 2
            $array[$key] = json_encode($val);
142
        }
143
144 2
        return $array;
145
    }
146
}
147