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-clock'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $singular_name = 'Countdown Element'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $plural_name = 'Countdown Elements'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private static $description = 'Displays a countdown to a specific date and time.'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $db = [ |
|
|
|
|
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'; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ArrayData |
57
|
|
|
*/ |
58
|
|
|
private $client_config; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getSummary() |
64
|
|
|
{ |
65
|
|
|
$end = $this->dbObject('End'); |
66
|
|
|
return DBField::create_field( |
67
|
|
|
'HTMLText', |
68
|
|
|
'Count down to ' . $end->Date() . ' ' . $end->Time() |
69
|
|
|
)->Summary(20); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function provideBlockSchema() |
76
|
|
|
{ |
77
|
|
|
$blockSchema = parent::provideBlockSchema(); |
78
|
|
|
$blockSchema['content'] = $this->getSummary(); |
79
|
|
|
return $blockSchema; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getType() |
86
|
|
|
{ |
87
|
1 |
|
return _t(__CLASS__ . '.BlockType', 'Countdown'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
2 |
|
public function setClientConfig() |
94
|
|
|
{ |
95
|
|
|
$clientArray = [ |
96
|
2 |
|
'End' => $this->End, |
97
|
2 |
|
'Elapse' => $this->Elapse, |
98
|
|
|
]; |
99
|
|
|
|
100
|
2 |
|
$this->client_config = ArrayData::create($this->encodeArrayValues($clientArray)); |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return ArrayData |
107
|
|
|
*/ |
108
|
2 |
|
public function getClientConfig() |
109
|
|
|
{ |
110
|
2 |
|
if (!$this->client_config) { |
111
|
2 |
|
$this->setClientConfig(); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
return $this->client_config; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $array |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
2 |
|
protected function encodeArrayValues($array) |
122
|
|
|
{ |
123
|
2 |
|
foreach ($array as $key => $val) { |
124
|
2 |
|
$array[$key] = json_encode($val); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
return $array; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return \SilverStripe\ORM\ValidationResult |
132
|
|
|
*/ |
133
|
1 |
|
public function validate() |
134
|
|
|
{ |
135
|
1 |
|
$result = parent::validate(); |
136
|
|
|
|
137
|
|
|
// skip if not written |
138
|
1 |
|
if (!$this->isInDB()) { |
139
|
1 |
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// skip if only sort changed |
143
|
1 |
|
$changed = $this->getChangedFields(true); |
144
|
1 |
|
if (count($changed) == 1 && array_key_exists('Sort', $changed)) { |
145
|
1 |
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
if (!$this->End) { |
149
|
1 |
|
$result->addError('An end date and time is required before saving the Countdown Element record'); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $result; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|