Passed
Push — master ( 563851...e4f57d )
by Jason
03:10
created

ElementCountDown::setClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
ccs 5
cts 5
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\ORM\FieldType\DBField;
7
use SilverStripe\View\ArrayData;
8
9
/**
10
 * Class ElementCountDown
11
 * @package Dynamic\Elements\Elements
12
 *
13
 * @property string $End
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 = 'font-icon-block-content';
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 = 'Countdown 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 = 'Countdown 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 string
37
     */
38
    private static $description = 'Displays a countdown to a specific date and time.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var array
42
     */
43
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
44
        'End' => 'DBDatetime',
45
        'ShowMonths' => 'Boolean',
46
        'ShowSeconds' => 'Boolean',
47
        'Elapse' => 'Boolean',
48
    ];
49
50
    /**
51
     * @var string
52
     */
53
    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...
54
55
    /**
56
     * @var ArrayData
57
     */
58
    private $client_config;
59
60
    /**
61
     * Sets the Date field to the current date.
62
     */
63 1
    public function populateDefaults()
64
    {
65 1
        $this->End = date('Y-m-d', strtotime("+30 days"));
66 1
        parent::populateDefaults();
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getSummary()
73
    {
74
        $end = $this->dbObject('End');
75
        return DBField::create_field(
76
            'HTMLText',
77
            'Count down to ' . $end->Date() . ' ' . $end->Time()
78
        )->Summary(20);
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    protected function provideBlockSchema()
85
    {
86
        $blockSchema = parent::provideBlockSchema();
87
        $blockSchema['content'] = $this->getSummary();
88
        return $blockSchema;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 1
    public function getType()
95
    {
96 1
        return _t(__CLASS__ . '.BlockType', 'Countdown');
97
    }
98
99
    /**
100
     * @return $this
101
     */
102 2
    public function setClientConfig()
103
    {
104
        $clientArray = [
105 2
            'End' => $this->End,
106 2
            'Elapse' => $this->Elapse,
107
        ];
108
109 2
        $this->client_config = ArrayData::create($this->encodeArrayValues($clientArray));
110
111 2
        return $this;
112
    }
113
114
    /**
115
     * @return ArrayData
116
     */
117 2
    public function getClientConfig()
118
    {
119 2
        if (!$this->client_config) {
120 2
            $this->setClientConfig();
121
        }
122
123 2
        return $this->client_config;
124
    }
125
126
    /**
127
     * @param $array
128
     * @return mixed
129
     */
130 2
    protected function encodeArrayValues($array)
131
    {
132 2
        foreach ($array as $key => $val) {
133 2
            $array[$key] = json_encode($val);
134
        }
135
136 2
        return $array;
137
    }
138
}
139